Lanczos approximation: Difference between revisions

Content deleted Content added
Yobot (talk | contribs)
m Introduction: WP:CHECKWIKI error fixes using AWB (10093)
Simple implementation: cleaned up implementation
Line 55:
 
<source lang="python">
def gamma(z): # great function from Wiki, but maybe could use memoization?
from cmath import *
epsilon = 0.0000001
def withinepsilon(x):
return abs(x - abs(x)) <= epsilon
 
from cmath import *sin,sqrt,pi,exp
# Coefficients used by the GNU Scientific Library
g = 7
p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
 
p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313,
def gamma(z):
771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012,
z = complex(z)
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
# Reflection formula
if z.real <= 0.5:complex(z)
 
return pi / (sin(pi*z) * gamma(1-z))
# Reflection formula
else:
if z.real -=< 10.5:
x result = p[0]pi / (sin(pi*z) * gamma(1-z))
for i in range(1, g+2)else:
xz +-= p[i]/(z+i)1
t = z + gx += 0.599999999999980993
 
return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
for i,_ in enumerate(p):
x += p[i]/(z+i+1)
 
t = z + len(p) - 0.5
return result = sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
 
if withinepsilon(result.imag):
return result.real
return result
</source>