微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将几个附加参数传递给作为 LowLevelCallable 传递给 scipy.integrate.quad

如何解决如何将几个附加参数传递给作为 LowLevelCallable 传递给 scipy.integrate.quad

我正在尝试复制在另一个问题的所选答案的第 1. 段中描述的技术:How to pass additional parameters to numba cfunc passed as LowLevelCallable to scipy.integrate.quad

但是,我不知道如何修改实现,使 xx[1] 是一个浮点数组而不是唯一的浮点数。

解决方法

我通过将 Jacques Gaudin 中的 https://stackoverflow.com/a/49732825/3925704 代码修改为:

import numpy as np
import scipy.integrate as si
import numba
from numba import cfunc
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):
    t = args[0]
    a = args[1]
    return np.exp(-t/a) / t**2

def do_integrate(func,a):
    """
    Integrate the given function from 1.0 to +inf with additional argument a.
    """
    return si.quad(func,1,np.inf,args=(a,))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。