numpy的广义超几何函数

问题描述

我希望将一些数据拟合到超几何函数中。我正在使用 mpmath,hyper 中给出的广义超几何函数。我正在尝试使用 curve_fit 将其转换为与 np.frompyfunc 一起使用。当我这样做

np_hyp = np.frompyfunc(hyper,3,1)
np_hyp([-1/3],[-2/3,2/3],x**2/4)

其中 x 是一些 numpy 数组。我得到的错误len(a_s): 'float' object has no length 或类似的东西(当我回到我的 PC 来复制错误时,我会更准确)。我怀疑这与当 numpy 尝试转换函数时输入是列表和转换奇怪有关。

有人知道解决这个错误方法吗?任何帮助将不胜感激。

解决方法

事实证明我上面的评论是正确的,即第一个和第二个列表也被分解并作为单个元素传递。这不应该发生。因此,解决方案是

from mpmath import hyper
import numpy as np

print( hyper( [ -1 / 3 ],[ -2 / 3,2 / 3 ],0.255 ) )

nphyper = np.vectorize( hyper )
nphyper.excluded.add(0)
nphyper.excluded.add(1)

print( 
    nphyper(
        [ -1 / 3 ],np.array( [ 0.255,0.257 ] )
    )
)

文档中不清楚,所以感谢 this 帖子,我想出了如何排除位置参数。