简体   繁体   English

scipy集成具有可变边界的数组

[英]scipy integrate over array with variable bounds

I am trying to integrate a function over a list of point and pass the whole array to an integration function in order ot vectorize the thing. 我试图在一个点列表上集成一个函数,然后将整个数组传递给一个集成函数,以便对事物进行矢量化处理。 For starters, calling scipy.integrate.quad is way too slow since I have something like 10 000 000 points to integrate. 对于初学者来说,调用scipy.integrate.quad太慢了,因为我有大约1 000 000点需要积分。 Using scipy.integrate.romberg does the trick much faster, almost instantaneous while quad is slow since you must loop over it or vectorize it. 使用scipy.integrate.romberg可以更快地完成技巧,几乎是瞬间完成的,而Quad则很慢,因为您必须对其进行循环或矢量化处理。

My function is quite complicated, but for demonstation purpose, let's say I want to integrate x^2 from a to b, but x is an array of scalar to evaluate x. 我的函数很复杂,但是出于演示的目的,我想将x ^ 2从a集成到b,但是x是标量数组,用于求x的值。 For example 例如

import numpy as np 将numpy导入为np

from scipy.integrate import quad, romberg

def integrand(x, y):   

    return x**2 + y**2


quad(integrand, 0, 10, args=(10) # this fails since y is not a scalar

romberg(integrand, 0, 10)  # y works here, giving the integral over
                           # the entire range 

But this only work for fixed bounds. 但这仅适用于固定范围。 Is there a way to do something like 有没有办法做类似的事情

z = np.arange(20,30)
romberg(integrand, 0, z)  # Fails since the function doesn't seem to
                          # support variable bounds

Only way I see it is to re-implement the algorithm itself in numpy and use that instead so I can have variable bounds. 我看到的唯一方法是在numpy中重新实现算法本身,而改用numpy,这样我就可以拥有可变范围。 Any function that supports something like this? 任何支持这种功能的功能? There is also romb, where you must supply the values of integrand directly and a dx interval, but that will be too imprecise for my complicated function (the marcum Q function, couldn't find any implementation, that could be another way to dot it). 还有一个问题,您必须直接提供integrand的值和一个dx间隔,但这对于我复杂的函数来说太不精确了(marcum Q函数找不到任何实现,这可能是对其加点的另一种方式)。

The best approach when trying to evaluate a special function is to write a function that uses the properties of the function to quickly and accurately evaluate it in all parameter regimes. 尝试评估特殊功能时,最好的方法是编写一个使用该功能的属性在所有参数范围内快速,准确地评估它的函数。 It is quite unlikely that a single approach will give accurate (or even stable) results for all ranges of parameters. 对于所有参数范围,单一方法不太可能给出准确(甚至稳定)的结果。 Direct evaluation of an integral, as in this case, will almost certainly break down in many cases. 在这种情况下,对积分的直接求值在许多情况下几乎肯定会崩溃。

That being said, the general problem of evaluating an integral over many ranges can be solved by turning the integral into a differential equation and solving that. 就是说,可以通过将积分转化为微分方程并将其求解来解决评估许多范围内的积分的一般问题。 Roughly, the steps would be 大致来说,步骤是

  1. Given an integral I(t) which I will assume is an integral of a function f(x) from 0 to t [this can be generalized to an arbitrary lower limit], write it as the differential equation dI/dt = f(x). 给定一个积分I(t),我将假定它是函数f(x)从0到t的积分[可以将其推广到任意下限],将其写为微分方程dI / dt = f(x )。
  2. Solve this differential equation using scipy.integrate.odeint() for some initial conditions (here I(0)) over some range of times from 0 to t. 使用scipy.integrate.odeint()求解从0到t的某些时间范围内的某些初始条件(此处为I(0))的微分方程。 This range should contain all limits of interest. 此范围应包含所有感兴趣的限制。 How finely this is sampled depends on the function and how accurately it needs to be evaluated. 采样的精细程度取决于功能以及需要对其评估的准确性。
  3. The result will be the value of the integral from 0 to t for the set of t we input. 对于我们输入的t集合,结果将是从0到t的积分值。 We can turn this into a "continuous" function using interpolation. 我们可以使用插值将其转换为“连续”功能。 For example, using a spline we can define i = scipy.interpolate.InterpolatedUnivariateSpline(t,I) . 例如,使用样条曲线,我们可以定义i = scipy.interpolate.InterpolatedUnivariateSpline(t,I)
  4. Given a set of upper and lower limits in arrays b and a , respectively, then we can evaluate them all at once as res=i(b)-i(a) . 分别给定数组ba一组上限和下限,那么我们可以立即将它们全部评估为res=i(b)-i(a)

Whether this approach will work in your case will require you to carefully study it over your range of parameters. 这种方法是否适合您的情况,需要您在参数范围内仔细研究它。 Also note that the Marcum Q function involves a semi-infinite integral. 还要注意,Marcum Q函数涉及一个半无限积分。 In principle this is not a problem, just transform the integral to one over a finite range. 原则上这不是问题,只需在有限范围内将积分转换为一个即可。 For example, consider the transformation x->1/x. 例如,考虑转换x-> 1 / x。 There is no guarantee this approach will be numerically stable for your problem. 无法保证此方法在数值上对您的问题稳定。

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

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