使用 mpmath.nsum() 和 scipy.special.gamma() 时如何修复 TypeError?

How to fix TypeError when using mpmath.nsum() and scipy.special.gamma()?

我在 Python 中实现一些数学公式,当我使用 mpmath.nsum()

时出现以下错误

TypeError: ufunc 'gamma' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

主文件

import math
from mpmath import nsum
H_n = - nsum(lambda n: P_n(a,b,c,n)*math.log(P_n(a,b,c,n)), [0, math.inf])

函数文件

import math
from scipy.special import gamma, hyp1f1
def P_n(a,b,c,n):
    P = ((gamma(b)*gamma(a+n))/(gamma(a)*gamma(b+n)))*(hyp1f1(a+n,b+n,-c))
    return P

当我使用下面的 for 循环时,我得到了我想要的答案。

sum = 0.0
for n in range(100):
    sum += rna.P_n(a,b,c,n)*math.log(rna.P_n(a,b,c,n))

有什么帮助吗? (注意:我是 Python 的绝对初学者,所以我不知道如何解决这个问题)

Scipy.special.gamma 仅适用于整数或浮点参数,不适用于 mpmath 的无限精度值。使用 mpmath.gamma 或避免使用 mpmath 并手动进行求和直到一个大的有限截止点。