0% found this document useful (0 votes)
96 views3 pages

Lanczos Approximation - Wikipedia

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views3 pages

Lanczos Approximation - Wikipedia

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

13.5.2018.

Lanczos approximation - Wikipedia

Lanczos approximation
In mathematics, the Lanczos approximation is a method for computing the gamma function numerically,
published by Cornelius Lanczos in 1964. It is a practical alternative to the more popular Stirling's approximation for
calculating the gamma function with fixed precision.

Contents
Introduction
Coefficients
Derivation
Simple implementation
See also
References

Introduction
The Lanczos approximation consists of the formula

for the gamma function, with

Here g is a constant that may be chosen arbitrarily subject to the restriction that Re(z) > 21 .[1] The coefficients p, which
depend on g, are slightly more difficult to calculate (see below). Although the formula as stated here is only valid for
arguments in the right complex half-plane, it can be extended to the entire complex plane by the reflection formula,

The series A is convergent, and may be truncated to obtain an approximation with the desired precision. By choosing
an appropriate g (typically a small integer), only some 5–10 terms of the series are needed to compute the Gamma
function with typical single or double floating-point precision. If a fixed g is chosen, the coefficients can be calculated
in advance and the sum is recast into the following form:

Thus computing the gamma function becomes a matter of evaluating only a small number of elementary functions and
multiplying by stored constants. The Lanczos approximation was popularized by Numerical Recipes, according to
which computing the Gamma function becomes "not much more difficult than other built-in functions that we take for

https://en.wikipedia.org/wiki/Lanczos_approximation 1/3
13.5.2018. Lanczos approximation - Wikipedia

granted, such as sin x or ex". The method is also implemented in the GNU Scientific Library.

Coefficients
The coefficients are given by

with C(i, j) denoting the (i, j)th element of the Chebyshev polynomial coefficient matrix which can be calculated
recursively from the identities

Paul Godfrey describes how to obtain the coefficients and also the value of the truncated series A as a matrix product.

Derivation
Lanczos derived the formula from Leonhard Euler's integral

performing a sequence of basic manipulations to obtain

and deriving a series for the integral.

Simple implementation
The following implementation in the Python programming language works for complex arguments and typically gives
15 correct decimal places:

from cmath import sin, sqrt, pi, exp

p = [676.5203681218851
,-1259.1392167224028
,771.32342877765313
,-176.61502916214059
,12.507343278686905
,-0.13857109526572012
,9.9843695780195716e-6
,1.5056327351493116e-7
]

EPSILON = 1e-07
def drop_imag(z):
if abs(z.imag) <= EPSILON:

https://en.wikipedia.org/wiki/Lanczos_approximation 2/3
13.5.2018. Lanczos approximation - Wikipedia
z = z.real
return z

def gamma(z):
z = complex(z)
if z.real < 0.5:
y = pi / (sin(pi*z) * gamma(1-z)) ### Reflection formula
else:
z -= 1
x = 0.99999999999980993
for (i, pval) in enumerate(p):
x += pval / (z+i+1)
t = z + len(p) - 0.5
y = sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
return drop_imag(y)
"""
The above use of the reflection (thus the if-else structure) seems unnecessary
and just adds more code to execute. It calls itself again, so it still needs
to execute the same "for" loop yet has an extra calculation at the end)
"""

print gamma(1)
print gamma(5)
print gamma(0.5)

See also
Stirling's approximation
Spouge's approximation

References
1. Pugh thesis (https://web.viu.ca/pughg/phdThesis/phdThesis.pdf#110)

Godfrey, Paul (2001). "Lanczos Implementation of the Gamma Function" (http://www.numericana.com/answer/inf


o/godfrey.htm).
Lanczos, Cornelius (1964). "A Precision Approximation of the Gamma Function". SIAM Journal on Numerical
Analysis series B (http://www.siam.org/journals/sinum.php). Society for Industrial and Applied Mathematics. 1:
86–96. doi:10.2307/2949767 (https://doi.org/10.2307%2F2949767). ISSN 0887-459X (https://www.worldcat.org/is
sn/0887-459X). JSTOR 2949767 (https://www.jstor.org/stable/2949767). External link in |journal= (help)
Press, W. H.; Teukolsky, S. A.; Vetterling, W. T.; Flannery, B. P. (2007), "Section 6.1. Gamma Function" (http://app
s.nrbook.com/empanel/index.html?pg=256), Numerical Recipes: The Art of Scientific Computing (3rd ed.), New
York: Cambridge University Press, ISBN 978-0-521-88068-8
Pugh, Glendon (2004). An analysis of the Lanczos Gamma approximation (http://web.mala.bc.ca/pughg/phdThesi
s/phdThesis.pdf) (PDF) (PhD thesis).
Toth, Viktor (2005). "Programmable Calculators: The Lanczos Approximation" (http://www.rskey.org/lanczos.htm).
Weisstein, Eric W. "Lanczos Approximation" (http://mathworld.wolfram.com/LanczosApproximation.html).
MathWorld.

Retrieved from "https://en.wikipedia.org/w/index.php?title=Lanczos_approximation&oldid=773853855"

This page was last edited on 4 April 2017, at 20:17.

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using
this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia
Foundation, Inc., a non-profit organization.

https://en.wikipedia.org/wiki/Lanczos_approximation 3/3

You might also like