From scipy reference manual, dblquad is mathematically equivalent to repeated quad twice. Initially, I thought dblquad must have performance advantage over twice quad (besides the convenience of the method). To my surprise, it seems dblquad performance is even worse. I took examples from "SciPy Reference Guide, Release 0.14.0" pages 12-13 with some modifications:
import scipy
import math
import timeit
def integrand(t, n, x):
return math.exp(-x*t) / t**n
def expint(n, x):
return scipy.integrate.quad(integrand, 1, scipy.Inf, args=(n, x))[0]
def I11():
res = []
for n in range(1,5):
res.append(scipy.integrate.quad(lambda x: expint(n, x), 0, scipy.Inf)[0])
return res
def I2():
res = []
for n in range(1,5):
res.append(scipy.integrate.dblquad(lambda t, x: integrand(t, n, x), 0, scipy.Inf, lambda x: 1, lambda x: scipy.Inf)[0])
return res
print('twice of quad:')
print(I11())
print(timeit.timeit('I11()', setup='from __main__ import I11', number=100))
print('dblquad:')
print(I2())
print(timeit.timeit('I2()', setup='from __main__ import I2', number=100))
My outputs look like this:
twice of quad:
[1.0000000000048965, 0.4999999999985751, 0.33333333325010883, 0.2500000000043577]
5.42371296883
dblquad:
[1.0000000000048965, 0.4999999999985751, 0.33333333325010883, 0.2500000000043577]
6.31611323357
We see the two methods produce the same results (exact results should be 1, 1/2, 1/3, 1/4). But the dblquad performs worse.
Does someone have some insight what is going on with dblquad? I also have the same question for tplquad and nquad.