I need to perform 5 numerical integrations over the surface of a unit sphere.
I have tried with dblquad
but this is taking quite some time.
Here is a MRE:
import numpy as np
from scipy import integrate
def foo():
f1 = lambda theta, phi: np.cos(theta)**2
f2 = lambda theta, phi: f1(theta, phi)*np.cos(theta)**2*np.sin(theta)
ret = np.zeros(5)
ret[0] = integrate.dblquad(f2, 0, 2*np.pi, 0, np.pi)[0]
ret[1] = integrate.dblquad(f2, 0, 2*np.pi, 0, np.pi)[0]
ret[2] = integrate.dblquad(f2, 0, 2*np.pi, 0, np.pi)[0]
ret[3] = integrate.dblquad(f2, 0, 2*np.pi, 0, np.pi)[0]
ret[4] = integrate.dblquad(f2, 0, 2*np.pi, 0, np.pi)[0]
return ret
def main():
tst = np.zeros((1000, 5))
for i in range(1000):
tst[i] = foo()
main()
In my machine when I do %timeit main()
I get 21.4 s ± 126 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
This code structure is taking a long like to perform the computations, any way to improve this?