Problem with a gamma function/error message

  • Python
  • Thread starter ChrisVer
  • Start date
  • Tags
    Gamma
In summary, the python script appears to have a problem calculating the expression \Gamma(n+\frac{1}{2})/n^2 for large values of n.
  • #1
ChrisVer
Gold Member
3,378
464
I wrote the following python script in order to try and look how $$\sum_{i=1}^\infty f(n) = \sum_{i=1}^\infty \frac{\Gamma(n+1/2)}{n^2 \Gamma(n)}$$ behaves. However it appears to have some problem with my function f(n) for n=172 and above throwing this error:
RuntimeWarning: invalid value encountered in double_scalars
return gamma(n+0.5)/(n**2 * gamma(n))
I thought that there was a problem with the function [as division with zero] and so I tried wolframalpha which returns me a non zero value for [itex]\Gamma(172)[/itex] and also a non-infinite value for f(172).
I even tried a user-defined gamma function I had built in the past, and I got the same result for n=172... [it's highly unlike that my method's the same as scipy's]

Python:
from scipy.special import gamma

def f(n):
    return gamma(n+0.5)/(n**2 * gamma(n))

def sume(f,N):
    result=0.0
    for i in range(1,N+1): result+= f(i)
    return result

print f(172)
'''
for i in range(100,200):
    print i," : ",sume(f,i)
'''
 
Technology news on Phys.org
  • #2
gamma(172) = 171! is a huge number. It is probably just too large for the floating point double precision number format of the computer. The limit is about 1.8x10308. gamma(172) is about 1.2x10309. So that is where the number gets too large for the computer to handle.
 
  • Like
Likes ChrisVer
  • #3
I wouldn't use the built-in gamma function for large arguments. Dividing two huge numbers is usually very inaccurate.

I would instead just calculate the expression inside the summation recursively

[itex]f(n) = \dfrac{\Gamma(n+\frac{1}{2})}{n^2 \Gamma(n)}[/itex]

So [itex]f(n+1) = \dfrac{\Gamma(n+\frac{1}{2}+1)}{(n+1)^2 \Gamma(n+1)}[/itex]

Then you use that [itex]\Gamma(x+1) = x \Gamma(x)[/itex] to write this as follows:

So [itex]f(n+1) = \dfrac{(n+\frac{1}{2})\Gamma(n+\frac{1}{2})}{(n+1)^2 n \Gamma(n)} = \dfrac{n(n+\frac{1}{2})}{(n+1)^2} f(n)[/itex]
 
  • Like
Likes FactChecker and ChrisVer
  • #4

1. What is a gamma function?

The gamma function is a mathematical function that is commonly used in statistics, physics, and other fields. It is defined as the integral of the exponential function over the real numbers.

2. How is the gamma function used?

The gamma function is used in many different areas of mathematics and science. It is often used to solve problems involving continuous probability distributions, calculate areas under curves, and evaluate complex integrals.

3. What causes an error message when using the gamma function?

There are a few different reasons why an error message may occur when using the gamma function. One common reason is when the input value is negative or zero, as the gamma function is undefined for these values. Another possible cause is when the argument of the function is too large, resulting in an overflow error.

4. How can I fix an error with the gamma function?

If the error is due to an invalid input value, you can try using a different value or adjusting your calculation to avoid using the gamma function for that particular value. If the error is due to an overflow, you may need to use a different method or approximation to calculate the desired result.

5. Are there any alternatives to using the gamma function?

Yes, there are several alternatives to using the gamma function, such as the factorial function or the incomplete gamma function. It may also be possible to reframe the problem or equation in a way that does not require the use of the gamma function. Consulting with a colleague or mentor may also help to find alternative solutions.

Similar threads

  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
11
Views
812
  • Programming and Computer Science
Replies
8
Views
795
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top