0

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''

main file

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])

function file

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)

Watermelon
  • 72
  • 1
  • 6
  • What are your values for a, b, c? – newkid Jan 23 '19 at 10:49
  • They will be random variables with a certain probability distribution, but for now I've set them equal to 1. – Watermelon Jan 24 '19 at 02:22
  • You need to provide a minimum example which we can run to help you. The only way to reproduce your error is if I pass a function to `gamma`. I think in your case, a,b, c are random number generator functions as opposed to `int`, `float`, `complex`. – newkid Jan 24 '19 at 02:34
  • Probably even `n` needs to be checked for its type. – newkid Jan 24 '19 at 02:40

1 Answers1

0

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.

ev-br
  • 24,968
  • 9
  • 65
  • 78