fbpx
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.

Introduction edit

The Lanczos approximation consists of the formula

 

for the gamma function, with

 

Here g is a real constant that may be chosen arbitrarily subject to the restriction that Re(z+g+1/2) > 0.[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, thanks to partial fraction decomposition, 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 granted, such as sin x or ex." The method is also implemented in the GNU Scientific Library, Boost, CPython and musl.

Coefficients edit

The coefficients are given by

 

where   represents the (n, m)th element of the matrix of coefficients for the Chebyshev polynomials, which can be calculated recursively from these identities:

 

Godfrey (2001) describes how to obtain the coefficients and also the value of the truncated series A as a matrix product.[2]

Derivation edit

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 edit

The following implementation in the Python programming language works for complex arguments and typically gives 13 correct decimal places. Note that omitting the smallest coefficients (in pursuit of speed, for example) gives totally inaccurate results; the coefficients must be recomputed from scratch for an expansion with fewer terms.

from cmath import sin, sqrt, pi, exp """ The coefficients used in the code are for when g = 7 and n = 9 Here are some other samples g = 5 n = 5 p = [  1.0000018972739440364,  76.180082222642137322,  -86.505092037054859197,  24.012898581922685900,  -1.2296028490285820771 ] g = 5 n = 7 p = [  1.0000000001900148240,  76.180091729471463483,  -86.505320329416767652,  24.014098240830910490,  -1.2317395724501553875,  0.0012086509738661785061,  -5.3952393849531283785e-6 ] g = 8 n = 12 p = [  0.9999999999999999298,  1975.3739023578852322,  -4397.3823927922428918,  3462.6328459862717019,  -1156.9851431631167820,  154.53815050252775060,  -6.2536716123689161798,  0.034642762454736807441,  -7.4776171974442977377e-7,  6.3041253821852264261e-8,  -2.7405717035683877489e-8,  4.0486948817567609101e-9 ] """ g = 7 n = 9 p = [ 0.99999999999980993, 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: 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 = p[0] for i in range(1, len(p)): x += p[i] / (z + i) t = z + g + 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) is necessary, even though  it may look strange, as it allows to extend the approximation to values of z where  Re(z) < 0.5, where the Lanczos method is not valid. """ print(gamma(1)) print(gamma(5)) print(gamma(0.5)) 

See also edit

References edit

  1. ^ Pugh, Glendon (2004). An analysis of the Lanczos Gamma approximation (PDF) (Ph.D.).
  2. ^ Godfrey, Paul (2001). "Lanczos implementation of the gamma function". Numericana.
  • Godfrey, Paul (2001). "Lanczos Implementation of the Gamma Function".
  • Lanczos, Cornelius (1964). "A Precision Approximation of the Gamma Function". Journal of the Society for Industrial and Applied Mathematics, Series B: Numerical Analysis. 1: 86–96. Bibcode:1964SJNA....1...86L. doi:10.1137/0701008. ISSN 0887-459X. JSTOR 2949767.
  • Press, W. H.; Teukolsky, S. A.; Vetterling, W. T.; Flannery, B. P. (2007), "Section 6.1. Gamma Function", 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 (PDF) (PhD thesis).
  • Toth, Viktor (2005). "Programmable Calculators: The Lanczos Approximation".
  • Weisstein, Eric W. "Lanczos Approximation". MathWorld.

lanczos, approximation, mathematics, method, computing, gamma, function, numerically, published, cornelius, lanczos, 1964, practical, alternative, more, popular, stirling, approximation, calculating, gamma, function, with, fixed, precision, contents, introduct. 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 1 Introduction 2 Coefficients 3 Derivation 4 Simple implementation 5 See also 6 ReferencesIntroduction editThe Lanczos approximation consists of the formula G z 1 2 p z g 1 2 z 1 2 e z g 1 2 A g z displaystyle Gamma z 1 sqrt 2 pi left z g tfrac 1 2 right z 1 2 e z g 1 2 A g z nbsp for the gamma function with A g z 1 2 p 0 g p 1 g z z 1 p 2 g z z 1 z 1 z 2 displaystyle A g z frac 1 2 p 0 g p 1 g frac z z 1 p 2 g frac z z 1 z 1 z 2 cdots nbsp Here g is a real constant that may be chosen arbitrarily subject to the restriction that Re z g 1 2 gt 0 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 G 1 z G z p sin p z displaystyle Gamma 1 z Gamma z pi over sin pi z nbsp 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 thanks to partial fraction decomposition the sum is recast into the following form A g z c 0 k 1 N c k z k displaystyle A g z c 0 sum k 1 N frac c k z k nbsp 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 granted such as sin x or ex The method is also implemented in the GNU Scientific Library Boost CPython and musl Coefficients editThe coefficients are given by p k g 2 p ℓ 0 k C 2 k 1 2 ℓ 1 ℓ 1 2 ℓ g 1 2 ℓ 1 2 e ℓ g 1 2 displaystyle p k g frac sqrt 2 pi sum ell 0 k C 2k 1 2 ell 1 left ell tfrac 1 2 right left ell g tfrac 1 2 right ell 1 2 e ell g 1 2 nbsp where C n m displaystyle C n m nbsp represents the n m th element of the matrix of coefficients for the Chebyshev polynomials which can be calculated recursively from these identities C 1 1 1 C 2 2 1 C n 1 1 C n 1 1 for n 2 3 4 C n 1 n 1 2 C n n for n 2 3 4 C n 1 m 1 2 C n m C n 1 m 1 for n gt m 1 2 3 displaystyle begin aligned C 1 1 amp 1 5px C 2 2 amp 1 5px C n 1 1 amp C n 1 1 amp text for n amp 2 3 4 dots 5px C n 1 n 1 amp 2 C n n amp text for n amp 2 3 4 dots 5px C n 1 m 1 amp 2 C n m C n 1 m 1 amp text for n amp gt m 1 2 3 dots end aligned nbsp Godfrey 2001 describes how to obtain the coefficients and also the value of the truncated series A as a matrix product 2 Derivation editLanczos derived the formula from Leonhard Euler s integral G z 1 0 t z e t d t displaystyle Gamma z 1 int 0 infty t z e t dt nbsp performing a sequence of basic manipulations to obtain G z 1 z g 1 z 1 e z g 1 0 e v 1 log v z v g d v displaystyle Gamma z 1 z g 1 z 1 e z g 1 int 0 e Big v 1 log v Big z v g dv nbsp and deriving a series for the integral Simple implementation editThe following implementation in the Python programming language works for complex arguments and typically gives 13 correct decimal places Note that omitting the smallest coefficients in pursuit of speed for example gives totally inaccurate results the coefficients must be recomputed from scratch for an expansion with fewer terms from cmath import sin sqrt pi exp The coefficients used in the code are for when g 7 and n 9 Here are some other samples g 5 n 5 p 1 0000018972739440364 76 180082222642137322 86 505092037054859197 24 012898581922685900 1 2296028490285820771 g 5 n 7 p 1 0000000001900148240 76 180091729471463483 86 505320329416767652 24 014098240830910490 1 2317395724501553875 0 0012086509738661785061 5 3952393849531283785e 6 g 8 n 12 p 0 9999999999999999298 1975 3739023578852322 4397 3823927922428918 3462 6328459862717019 1156 9851431631167820 154 53815050252775060 6 2536716123689161798 0 034642762454736807441 7 4776171974442977377e 7 6 3041253821852264261e 8 2 7405717035683877489e 8 4 0486948817567609101e 9 g 7 n 9 p 0 99999999999980993 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 lt EPSILON z z real return z def gamma z z complex z if z real lt 0 5 y pi sin pi z gamma 1 z Reflection formula else z 1 x p 0 for i in range 1 len p x p i z i t z g 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 is necessary even though it may look strange as it allows to extend the approximation to values of z where Re z lt 0 5 where the Lanczos method is not valid print gamma 1 print gamma 5 print gamma 0 5 See also editStirling s approximation Spouge s approximationReferences edit Pugh Glendon 2004 An analysis of the Lanczos Gamma approximation PDF Ph D Godfrey Paul 2001 Lanczos implementation of the gamma function Numericana Godfrey Paul 2001 Lanczos Implementation of the Gamma Function Lanczos Cornelius 1964 A Precision Approximation of the Gamma Function Journal of the Society for Industrial and Applied Mathematics Series B Numerical Analysis 1 86 96 Bibcode 1964SJNA 1 86L doi 10 1137 0701008 ISSN 0887 459X JSTOR 2949767 Press W H Teukolsky S A Vetterling W T Flannery B P 2007 Section 6 1 Gamma Function 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 PDF PhD thesis Toth Viktor 2005 Programmable Calculators The Lanczos Approximation Weisstein Eric W Lanczos Approximation MathWorld Retrieved from https en wikipedia org w index php title Lanczos approximation amp oldid 1128700454, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.