简体   繁体   English

dblquad的性能增益是否超过两倍?

[英]Is there a performance gain of dblquad over twice quad?

From scipy reference manual, dblquad is mathematically equivalent to repeated quad twice. 从scipy参考手册中,dblquad在数学上等效于重复四次方的两次。 Initially, I thought dblquad must have performance advantage over twice quad (besides the convenience of the method). 最初,我认为dblquad必须具有超过两倍四倍的性能优势(除了方法的便利性)。 To my surprise, it seems dblquad performance is even worse. 令我惊讶的是,似乎dblquad的性能更差。 I took examples from "SciPy Reference Guide, Release 0.14.0" pages 12-13 with some modifications: 我从“SciPy参考指南,版本0.14.0”第12-13页进行了一些修改:

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). 我们看到两种方法产生相同的结果(确切的结果应该是1,1 / 2,1 / 3,1 / 4)。 But the dblquad performs worse. 但dblquad表现更差。

Does someone have some insight what is going on with dblquad? 有人对dblquad的情况有所了解吗? I also have the same question for tplquad and nquad. 我对tplquad和nquad也有同样的问题。

Have a look at the source code . 看看源代码 It's clear that dblquad is just a repeated integration, just like what you're doing here. 很明显,dblquad只是一个重复的集成,就像你在这里做的一样。

Re efficiency: scipy versions >0.14 might be better for multivariate functions, see https://github.com/scipy/scipy/pull/3262 效率:对于多变量函数,scipy版本> 0.14可能更好,请参阅https://github.com/scipy/scipy/pull/3262

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM