# integral including a vector

I need to calculate the integral of this function

def f(z):
return ((1-2*z)*np.exp(-d/z))/(((1-z)**(2+d))*(z**(2-d)))


Here d is a constant. I am using this function:

 import scipy.integrate as integrate


However, I need to calculate this for many different d values. I tried putting all d values in a numpy array but it didn't work. Any idea how to vectorize this?

The function integrate.quad is a python wrapper to the DQAGSE function from QUADPACK. This function uses adaptive quadrature, i.e. it will apply a fixed rule (in this case Gauss-Kronrod) on intervals that it will adaptively refine trying to reach the absolute and/or relative tolerance you requested.

Since the parameter d alters the behaviour of the function, it will also change the iterative interval refinement. Hence, it doesn't make sense to "vectorize".

What you could do is use the Pool class from the multiprocessing module in the standard Python library. Distribute your values of d over multiple cores. Wrap your integral in a function:

import numpy as np
import scipy.integrate as integrate
from multiprocessing import Pool

def f(z,d):
return ((1-2*z)*np.exp(-d/z))/(((1-z)**(2+d))*(z**(2-d)))

def F_int(d):

• I just compared a serial version to the parallel version using 4 processes for a vector d with length 1.000.000 (generated randomly but with a fixed seed so the serial and parallel run get the same values): approx 345s vs 101s (on a quad core machine). Oct 31, 2016 at 20:52