Lanczos 알고리즘은 Stirlng 공식에 의한 알고리즘 보다 정밀하며, 십진수로 유효숫자 약 15자리 까지는 정확하게 계산해 준다. 단지 exp 함수를 이용하는 부분에서는 exp 함수의 구현에 따라 오차가 더 있을 수 있다.
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
# Filename: testLanczos-01.py
#
# An approximation for the gamma function by using the Lanczos algorithm
#
# Execute: python testLanczos-01.py
# or
# Execute: ./testLanczos-01.py
#
# See: http://en.wikipedia.org/wiki/Lanczos_approximation
# See:http://www-history.mcs.st-and.ac.uk/Biographies/Lanczos.html
# See: http://www.thinbasic.com/community/showthread.php?11279-Gamma-function
from cmath import *
# 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]
def gamma(z):
z = complex(z)
# Reflection formula
if z.real < 0.5:
return pi / (sin(pi*z)*gamma(1-z))
else:
z -= 1
x = p[0]
for i in range(1, g+2):
x += p[i]/(z+i)
t = z + g + 0.5
return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
def factorial(n):
if n < 2:
return 1
else:
k = 1
if n % 2 == 0:
for i in xrange(n/2):
k *= (i + 1) * (n - i)
else:
for i in xrange(n/2):
k *= (i + 1) * (n - 1 - i)
k *= n
return k
def facto(n):
if n < 2:
return 1
else:
k = 1
for i in xrange(2, n + 1):
k *= i
return k
if __name__ == "__main__":
print "gamma(10) = 9! = %s asymptotically" % gamma(10)
# print "gamma(101) = 100! = %16s asymptotically" % gamma(101)
print "gamma(101) = 100! = %16s asymptotically" % "{0.real:.15f}{0.imag:+.5f}j".format(gamma(101))
for i in range(11):
print "%d! = %d" % (i, factorial(i))
i = 100
print "factorial(%d) = %d! = %d" % (i, i, factorial(i))
print "facto(%d) = %d! = %d" % (i, i, facto(i))
"""
Output:
gamma(10) = 9! = (362880+0j) asymptotically
gamma(101) = 100! = 9.33262154439379e+157+0.00000j asymptotically
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
factorial(100) = 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
facto(100) = 100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
"""
'프로그래밍 > Python' 카테고리의 다른 글
Python 언어로 scipy, numpy, mpmath 모듈을 이용하여 역삼각함수, 역쌍곡선함수 값을 구하는 예제 (0) | 2013.01.09 |
---|---|
Python 언어로 역삼각함수, 역쌍곡선함수 값을 구하는 예제 (0) | 2013.01.01 |
Python 2.6, 2.6 및 3.2 로 복소수의 포맷(format) 출력 (0) | 2012.12.07 |
pyglet 에 한글 사용하는 예제 (0) | 2011.08.26 |
Python 3.2 를 이용한 간단한 수학식 계산하기 (0) | 2011.08.18 |