Python 中的数值积分与向量化函数的自适应求积
我正在寻找一个超级数字求积函数。它应该具有以下三个属性:
- 自适应 - 它自动调整采样点的密度以适应被积函数。这是绝对必要的,因为我的被积函数非常不均匀且计算成本昂贵。
- 矢量化 - 为了提高效率,它在样本点列表上调用被积函数,而不是一次调用一个点。
- 能够处理向量值函数 - 向量值被积函数的所有分量同时计算,无需额外成本,因此单独积分所有分量是没有意义的。
此外,它应该是:
- 2D - 我想要计算的积分是平面区域上的二重积分,并且我希望能够为整个积分指定总体(相对)容差,并让它适当地管理误差预算。
有人知道有这样的功能的库吗?即使四种属性中的两种或三种也比没有好。
我正在使用 Python 和 SciPy,所以如果它已经可以与 Python 配合使用,那就太好了。 (但我也可以编写粘合代码,让它在必要时调用我的被积函数。)
I'm looking for a super duper numerical quadrature function. It should have the following three properties:
- Adaptive - it automatically adjusts the density of sampling points to fit the integrand. This is absolutely necessary because my integrand is very nonuniform and expensive to compute.
- Vectorized - it calls the integrand on lists of sample points rather than one point at a time, for efficiency.
- Able to handle vector-valued functions - all components of the vector-valued integrand are computed at the same time for no additional cost, so it makes no sense to integrate all the components separately.
In addition, it should be:
- 2D - the integral I want to compute is a double integral over a planar region, and I want to be able to specify an overall (relative) tolerance for the whole integral and have it manage the error budget appropriately.
Does anybody know of a library that has such a function? Even two or three of the four properties would be better than nothing.
I'm using Python and SciPy, so if it already works with Python that's a bonus. (But I'm also able to write glue code to let it call my integrand if necessary.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚在 quadpy 中实现了 1D 和 2D 域的矢量化自适应正交。您需要提供的只是您的领域和您想要集成的功能的三角测量。它可以是向量值的。
安装quadpy
并运行
这给出了
I just implemented vectorized adaptive quadrature for 1D and 2D domains in quadpy. All you need to provide is a triangulation of your domain and the function you want to integrate. It may be vector-valued.
Install quadpy with
and run
This gives
我使用了这个库,它可以做你想要的一切,除了它是用 C 编写的。但它还有一个 R 接口,所以也许你可以从 Python 调用 R(这是可能的)。
http://ab-initio.mit.edu/wiki/ index.php/Cubature_(Multi-Dimension_integration)
或者,您可以使用 ctypes 调用该库(这不是直接的,但它是可行的)。
I used this library, it does everything you want, except it is written in C. But it also has an R interface, so maybe you can call R from Python (that is possible).
http://ab-initio.mit.edu/wiki/index.php/Cubature_(Multi-dimensional_integration)
Or, you can call the library using ctypes (it is not straight forward, but it is doable).
quadrature
函数位于scipy.integrate
满足前两个要求的你在寻找什么。类似的romberg
函数使用不同的方法。其他函数仅满足其中一个要求:
quad
函数执行自适应求积,但仅支持带有标量参数的函数。您可以向其传递 ctypes 函数以提高性能,但普通的 Python 函数会非常慢。simps
函数和相关采样方法可以传递一个(通常是均匀间隔的)样本向量,但不具有自适应性。您列出的第三个要求(向量值函数的联立积分)有点深奥,并且与首先接受向量化函数的能力相冲突(函数参数必须采用矩阵!)类似地,能够计算二重积分会使函数的指定变得非常复杂。
在大多数情况下,
quadrature
函数是最佳选择。The
quadrature
function inscipy.integrate
satisfies the first two requirements of what you are looking for. The similarromberg
function uses a different method.Other functions only satisfy one of the requirements:
quad
function does adaptive quadrature, but only supports a function with a scalar argument. You can pass it actypes
function for increased performance, but normal Python functions will be very slow.simps
function and related sampling methods can be passed a vector of (typically evenly-spaced) samples, but aren't adaptive.The third requirement you listed (simultaneous integral of a vector-valued function) is a bit esoteric and conflicts with the ability to accept a vectorized function in the first place (the function argument would have to take a matrix!) Similarly, the ability to compute a double integral would complicate the specification of the function significantly.
In most cases, the
quadrature
function would be the way to go.