Python Problem with a gamma function/error message

  • Thread starter Thread starter ChrisVer
  • Start date Start date
  • Tags Tags
    Gamma
Click For Summary
The discussion revolves around a Python script designed to analyze the behavior of the series involving the function f(n) defined as f(n) = Γ(n + 1/2) / (n² Γ(n)). The script encounters a RuntimeWarning for n values of 172 and above due to invalid values in double precision calculations, likely stemming from the large size of Γ(172), which exceeds the floating point limits of the system. Users suggest that the built-in gamma function may not be suitable for large arguments due to inaccuracies when dividing large numbers. An alternative approach is proposed, where f(n) is computed recursively to avoid the pitfalls of large number calculations. This method utilizes the property Γ(x + 1) = x Γ(x) to express f(n+1) in terms of f(n), potentially improving numerical stability. Additionally, the use of arbitrary precision libraries like SymPy is recommended for better handling of large numbers.
ChrisVer
Science Advisor
Messages
3,372
Reaction score
465
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 \Gamma(172) 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
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
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

f(n) = \dfrac{\Gamma(n+\frac{1}{2})}{n^2 \Gamma(n)}

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

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

So 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)
 
  • Like
Likes FactChecker and ChrisVer
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K