# Script to illustrate the use of the Simpson rule of SciPy # on the approximation of Pi/4 via the area under sqrt(1 - x^2) # above the x-axis, using two processes # If the script is called simpson4py2, # run it typing at the command prompt $ # $ time python simpson4py2.py from multiprocessing import Process, Queue def call_simpson(f,a,b,n,q): """ Calls Simpson rule to integrate f over [a,b] using n intervals. Adds the result to the queue q. """ x = linspace(a,b,n) y = f(x) I = simps(y,x) q.put(I) from scipy.integrate import simps from scipy import sqrt, linspace, pi f = lambda x: sqrt(1-x**2) n = 10000000 qA = Queue() pA = Process(target=call_simpson, args = (f,0,0.5,n/2,qA,)) qB = Queue() pB = Process(target=call_simpson, args = (f,0.5,1,n/2,qB,)) pA.start(); pB.start() pA.join(); a = qA.get() pB.join(); b = qB.get() I = 4*(a+b) print I, abs(I - pi)