Lanczos Approximation
   HOME

TheInfoList



OR:

In
mathematics Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
, the Lanczos approximation is a method for computing the
gamma function In mathematics, the gamma function (represented by Γ, capital Greek alphabet, Greek letter gamma) is the most common extension of the factorial function to complex numbers. Derived by Daniel Bernoulli, the gamma function \Gamma(z) is defined ...
numerically, published by
Cornelius Lanczos __NOTOC__ Cornelius (Cornel) Lanczos (, ; born as Kornél Lőwy, until 1906: ''Löwy (Lőwy) Kornél''; February 2, 1893 – June 25, 1974) was a Hungarian-Jewish, Hungarian-American and later Hungarian-Irish mathematician and physicist. Accordi ...
in 1964. It is a practical alternative to the more popular
Stirling's approximation In mathematics, Stirling's approximation (or Stirling's formula) is an asymptotic approximation for factorials. It is a good approximation, leading to accurate results even for small values of n. It is named after James Stirling, though a related ...
for calculating the gamma function with fixed precision.


Introduction

The Lanczos approximation consists of the formula :\Gamma(z+1) = \sqrt ^ e^ A_g(z) for the gamma function, with :A_g(z) = \frac12p_0(g) + p_1(g) \frac + p_2(g) \frac + \cdots. Here ''g'' is a real constant that may be chosen arbitrarily subject to the restriction that Re(''z''+''g''+) > 0. 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 In mathematics, the upper half-plane, is the set of points in the Cartesian plane with The lower half-plane is the set of points with instead. Arbitrary oriented half-planes can be obtained via a planar rotation. Half-planes are an example ...
, it can be extended to the entire
complex plane In mathematics, the complex plane is the plane (geometry), plane formed by the complex numbers, with a Cartesian coordinate system such that the horizontal -axis, called the real axis, is formed by the real numbers, and the vertical -axis, call ...
by the reflection formula, :\Gamma(1-z) \; \Gamma(z) = . 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 Double, The Double or Dubble may refer to: Mathematics and computing * Multiplication by 2 * Double precision, a floating-point representation of numbers that is typically 64 bits in length * A double number of the form x+yj, where j^2=+1 * A ...
floating-point In computing, floating-point arithmetic (FP) is arithmetic on subsets of real numbers formed by a ''significand'' (a Sign (mathematics), signed sequence of a fixed number of digits in some Radix, base) multiplied by an integer power of that ba ...
precision. If a fixed ''g'' is chosen, the coefficients can be calculated in advance and, thanks to
partial fraction decomposition In algebra, the partial fraction decomposition or partial fraction expansion of a rational fraction (that is, a fraction such that the numerator and the denominator are both polynomials) is an operation that consists of expressing the fraction as ...
, the sum is recast into the following form: :A_g(z) = c_0 + \sum_^ \frac Thus computing the gamma function becomes a matter of evaluating only a small number of
elementary function In mathematics, an elementary function is a function of a single variable (typically real or complex) that is defined as taking sums, products, roots and compositions of finitely many polynomial, rational, trigonometric, hyperbolic, a ...
s and multiplying by stored constants. The Lanczos approximation was popularized by ''
Numerical Recipes ''Numerical Recipes'' is the generic title of a series of books on algorithm In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a cla ...
'', 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 ''e''''x''." The method is also implemented in the
GNU Scientific Library The GNU Scientific Library (or GSL) is a software library for numerical computations in applied mathematics and science. The GSL is written in C (programming language), C; wrappers are available for other programming languages. The GSL is part of ...
, Boost,
CPython CPython is the reference implementation of the Python programming language. Written in C and Python, CPython is the default and most widely used implementation of the Python language. CPython can be defined as both an interpreter and a comp ...
and
musl musl is a C standard library intended for operating systems based on the Linux kernel, released under the MIT License. It was developed by Rich Felker to write a clean, efficient, and standards-conformant libc implementation. Overview musl wa ...
.


Coefficients

The coefficients are given by :p_k(g) = \frac \sum_^k C_ \left(\ell - \tfrac \right)! ^ e^ where C_ represents the (''n'', ''m'')th element of the
matrix Matrix (: matrices or matrixes) or MATRIX may refer to: Science and mathematics * Matrix (mathematics), a rectangular array of numbers, symbols or expressions * Matrix (logic), part of a formula in prenex normal form * Matrix (biology), the m ...
of coefficients for the
Chebyshev polynomials The Chebyshev polynomials are two sequences of orthogonal polynomials related to the cosine and sine functions, notated as T_n(x) and U_n(x). They can be defined in several equivalent ways, one of which starts with trigonometric functions: ...
, which can be calculated recursively from these identities: :\begin C_ &= 1 \\ px C_ &= 1 \\ px C_ &= -\,C_ & \text n &= 2, 3, 4\, \dots \\ pxC_ &= 2\,C_ & \text n &= 2, 3, 4\, \dots \\ pxC_ &= 2\,C_ - C_ & \text n & > m = 1, 2, 3\, \dots \end Godfrey (2001) describes how to obtain the coefficients and also the value of the truncated series ''A'' as a
matrix product In mathematics, specifically in linear algebra, matrix multiplication is a binary operation that produces a matrix from two matrices. For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the s ...
.


Derivation

Lanczos derived the formula from
Leonhard Euler Leonhard Euler ( ; ; ; 15 April 170718 September 1783) was a Swiss polymath who was active as a mathematician, physicist, astronomer, logician, geographer, and engineer. He founded the studies of graph theory and topology and made influential ...
's
integral In mathematics, an integral is the continuous analog of a Summation, sum, which is used to calculate area, areas, volume, volumes, and their generalizations. Integration, the process of computing an integral, is one of the two fundamental oper ...
:\Gamma(z+1) = \int_0^\infty t^\,e^\,dt, performing a sequence of basic manipulations to obtain :\Gamma(z+1) = (z+g+1)^ e^ \int_0^e \Big(v(1-\log v)\Big)^ v^g\,dv, and deriving a series for the integral.


Simple implementation

The following implementation in the
Python programming language Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically type-checked and garbage-collected. It supports multiple prog ...
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 for i in range(1, len(p)): x += p / (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

*
Stirling's approximation In mathematics, Stirling's approximation (or Stirling's formula) is an asymptotic approximation for factorials. It is a good approximation, leading to accurate results even for small values of n. It is named after James Stirling, though a related ...
*
Spouge's approximation In mathematics, Spouge's approximation is a formula for computing an approximation of the gamma function. It was named after John L. Spouge, who defined the formula in a 1994 paper. The formula is a modification of Stirling's approximation, and ha ...


References

* * * * * * {{MathWorld, urlname=LanczosApproximation, title=Lanczos Approximation Gamma and related functions Numerical analysis Articles with example Python (programming language) code