I am implementing some mathematical formulas in Python and I get the following error when I use 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
When I use the for loop below I get the answers I want.
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))
Any help please? (Note: I am an absolute beginner in Python so I have no idea how to tackle this issue)
Scipy.special.gamma only works for integer or floating-point arguments, not mpmath's infinite-precision values. Either use mpmath.gamma
or avoid mpmath and do the summation manually up to a large finite cutoff.