数值积分python

时间:2016-09-24 16:15:52

标签: python-2.7 numerical-integration quad

我需要减少python中quad()的运行时间(我整合了几千个积分)。我在这里找到了一个类似的问题,他们建议做几个集成并添加部分值。但是,这并没有提高性能。有什么想法吗?这是一个简单的例子:

import numpy as np                      
from scipy.integrate import quad
from scipy.stats import norm
import time

funcB = lambda x: norm.pdf(x,0,1)

start = time.time()
good_missclasified,_ = quad(funcB, 0,3.3333)
stop = time.time()
time_elapsed = stop - start 
print ('quad : ' + str(time_elapsed))

start = time.time()
num = np.linspace(0,3.3333,10)
Lv = []
last, lastG = 0, 0
for g in num:
  Lval,x = quad(funcB, lastG, g)
  last, lastG = last + Lval, g
  Lv.append(last)
Lv = np.array(Lv)
stop = time.time()

time_elapsed = stop - start 
print ('10 int : ' + str(time_elapsed))
print(good_missclasified,Lv[9])

1 个答案:

答案 0 :(得分:2)

quadpy(我的一个项目)是矢量化的,可以同时在多个域(例如,间隔)上集成一个函数。您必须选择自己的集成方法。

XPath xpath = XPathFactory.newInstance().newXPath();
Element itemElement;  //This element has already been populated by prior code
String value = xpath.compile("attrQualOptMany[@name='someName']/value/text()")
                    .evaluate(itemElement, XPathConstants.STRING);
if(value  == null || value .isEmpty()) {
    value = xpath.compile("attr[@name='someName']/text()")
                 .evaluate(itemElement, XPathConstants.STRING);
}