如何使用Numba在SciPy中使用任意数量的变量和参数执行多重集成? [英] How to use Numba to perform multiple integration in SciPy with an arbitrary number of variables and parameters?

查看:199
本文介绍了如何使用Numba在SciPy中使用任意数量的变量和参数执行多重集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Numba 装饰多重积分的被积,以便 SciPy的Nquad 函数可以将其作为 LowLevelCallable 调用

I'd like to use Numba to decorate the integrand of a multiple integral so that it can be called by SciPy's Nquad function as a LowLevelCallable. Ideally, the decorator should allow for an arbitrary number of variables, and an arbitrary number of additional parameters from the Nquad's args argument. This is built off an excellent Q&A from earlier this year, but extended to the case of multiple variables and parameters.

作为一个例子,假设以下具有N个变量和K个参数的多重积分:

As an example, suppose the following multiple integral with N variables and K parameters:

以下代码有效,但仅适用于两个变量和两个参数(N = 2,K = 2).它不适用于更一般的情况.这是因为装饰器中的某些参数是手动枚举的( wrapped 函数中的xx [0],xx [1],xx [2],xx [3]).装饰器将必须针对每个不同数量的变量或参数进行编辑.如果可能的话,我想避免这种情况.请注意, integrand 函数本身利用了Numpy对象和方法,因此不会出现此问题.

The following code works, but only for two variables and two parameters (N=2,K=2). It does not work for the more general case. This is because some of the arguments in the decorator are manually enumerated (xx[0],xx[1],xx[2],xx[3] inside the wrapped function). The decorator would have to be edited for every different number of variables or parameters. I'd like to avoid that, if possible. Note that the integrand function itself takes advantage of Numpy objects and methods and so does not have this problem.

import numpy as np
import scipy.integrate as si
import numba
from numba import cfunc,carray
from numba.types import intc, CPointer, float64
from scipy import LowLevelCallable

def jit_integrand_function(integrand_function):
    jitted_function = numba.jit(integrand_function, nopython=True)

    @cfunc(float64(intc, CPointer(float64)))
    def wrapped(n, xx):
        return jitted_function(xx[0], xx[1], xx[2], xx[3])
        #xx = carray(xx,len(xx))
        #return jitted_function(xx)
    return LowLevelCallable(wrapped.ctypes)

@jit_integrand_function
def integrand(*args):
    d = np.array([args])
    return -np.exp(d.prod())

#Two variable, two parameter example
parms = np.array([2,3])
print si.nquad(integrand,[[0,1],[0,1]],parms)

理想的代码在 integrand 函数上仅使用一个装饰器即可运行:

The ideal code would use just one decorator on the integrand function to also run:

#Three variable, three parameter example
parms2 = np.array([1,2,3])
print si.nquad(integrand,[[0,1],[0,1],[0,1]],parms2)

Numba 文档指的是 carray 函数,该函数应在给定回调中的低级指针和数组大小时返回Numpy数组.可能的是,这可以用于推广超出二变量二参数情况的代码.我(未成功)尝试实现这一点的是在两行注释掉的代码行中.

The Numba documents refer to a carray function that ought to return a Numpy array when given the low-level pointer and size of an array in the callback. Possibly, this could be used to generalize the code beyond the two-variable-two-parameter case. My (unsuccessful) attempt to implement this is in the two commented-out lines of code.

我们将不胜感激.实际上,Numba开发人员之一指出了 SciPy集成是编写Numba的原因之一,但缺少该领域的文档和示例.

Help would be appreciated. Indeed, one of the Numba developers pointed out that SciPy integration was one of the reasons Numba was written, but that documentation and examples in this area are lacking.

推荐答案

以下代码有效:

import numpy as np
import scipy.integrate as si
import numba
from numba import cfunc,carray
from numba.types import intc, CPointer, float64
from scipy import LowLevelCallable

def jit_integrand_function(integrand_function):
    jitted_function = numba.jit(integrand_function, nopython=True)
    @cfunc(float64(intc, CPointer(float64)))
    def wrapped(n, xx):
        values = carray(xx,n)
        return jitted_function(values)
    return LowLevelCallable(wrapped.ctypes)

@jit_integrand_function
def integrand(args):
    return -np.exp(args.prod())

#Two variable, two parameter example
parms = np.array([2,3])
print si.nquad(integrand,[[0,1],[0,1]],parms)

#Three variable, three parameter example
parms2 = np.array([1,2,3])
print si.nquad(integrand,[[0,1],[0,1],[0,1]],parms2)

这篇关于如何使用Numba在SciPy中使用任意数量的变量和参数执行多重集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆