Is there any increase in dblquad performance over two ATVs?

From the scipy reference manual, dblquad is mathematically equivalent to repeating a square twice. I originally thought that dblquad should have a performance advantage of twice (apart from the convenience of the method). To my surprise, dblquad's performance seems to be even worse. I have provided examples from the SciPy Reference, Release 0.14.0 on 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 that the two methods give the same results (the exact results should be 1, 1/2, 1/3, 1/4). But dblquad works worse.

Does anyone have any idea what is going on with dblquad? I also have the same question for tplquad and nquad.

+3


source to share


1 answer


Check out the source code . Clearly dblquad is just a re-integration, just like what you are doing here.



Efficiency: scipy versions> 0.14 may be better for multidimensional functions, see https://github.com/scipy/scipy/pull/3262

+2


source







All Articles