<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Log Gamma</title> <link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.74.0"> <link rel="home" href="../../../index.html" title="Math Toolkit"> <link rel="up" href="../sf_gamma.html" title="Gamma Functions"> <link rel="prev" href="tgamma.html" title="Gamma"> <link rel="next" href="digamma.html" title="Digamma"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td> <td align="center"><a href="../../../../../../../../index.html">Home</a></td> <td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="tgamma.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sf_gamma.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="digamma.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section" lang="en"> <div class="titlepage"><div><div><h4 class="title"> <a name="math_toolkit.special.sf_gamma.lgamma"></a><a class="link" href="lgamma.html" title="Log Gamma"> Log Gamma</a> </h4></div></div></div> <a name="math_toolkit.special.sf_gamma.lgamma.synopsis"></a><h5> <a name="id1073809"></a> <a class="link" href="lgamma.html#math_toolkit.special.sf_gamma.lgamma.synopsis">Synopsis</a> </h5> <p> </p> <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">special_functions</span><span class="special">/</span><span class="identifier">gamma</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> </pre> <p> </p> <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> <a class="link" href="../../main_overview/result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">);</span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Policies">Policy</a><span class="special">></span> <a class="link" href="../../main_overview/result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Policies">Policy</a><span class="special">&);</span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> <a class="link" href="../../main_overview/result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">,</span> <span class="keyword">int</span><span class="special">*</span> <span class="identifier">sign</span><span class="special">);</span> <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <a class="link" href="../../policy.html" title="Policies">Policy</a><span class="special">></span> <a class="link" href="../../main_overview/result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">,</span> <span class="keyword">int</span><span class="special">*</span> <span class="identifier">sign</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../../policy.html" title="Policies">Policy</a><span class="special">&);</span> <span class="special">}}</span> <span class="comment">// namespaces </span></pre> <a name="math_toolkit.special.sf_gamma.lgamma.description"></a><h5> <a name="id1074192"></a> <a class="link" href="lgamma.html#math_toolkit.special.sf_gamma.lgamma.description">Description</a> </h5> <p> The <a href="http://en.wikipedia.org/wiki/Gamma_function" target="_top">lgamma function</a> is defined by: </p> <p> <span class="inlinemediaobject"><img src="../../../../equations/lgamm1.png"></span> </p> <p> The second form of the function takes a pointer to an integer, which if non-null is set on output to the sign of tgamma(z). </p> <p> </p> <p> The final <a class="link" href="../../policy.html" title="Policies">Policy</a> argument is optional and can be used to control the behaviour of the function: how it handles errors, what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Policies">policy documentation for more details</a>. </p> <p> </p> <p> <span class="inlinemediaobject"><img src="../../../../graphs/lgamma.png" align="middle"></span> </p> <p> There are effectively two versions of this function internally: a fully generic version that is slow, but reasonably accurate, and a much more efficient approximation that is used where the number of digits in the significand of T correspond to a certain <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a>. In practice, any built-in floating-point type you will encounter has an appropriate <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a> defined for it. It is also possible, given enough machine time, to generate further <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a>'s using the program libs/math/tools/lanczos_generator.cpp. </p> <p> The return type of these functions is computed using the <a class="link" href="../../main_overview/result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>result type calculation rules</em></span></a>: the result is of type <code class="computeroutput"><span class="keyword">double</span></code> if T is an integer type, or type T otherwise. </p> <a name="math_toolkit.special.sf_gamma.lgamma.accuracy"></a><h5> <a name="id1075445"></a> <a class="link" href="lgamma.html#math_toolkit.special.sf_gamma.lgamma.accuracy">Accuracy</a> </h5> <p> The following table shows the peak errors (in units of epsilon) found on various platforms with various floating point types, along with comparisons to the <a href="http://www.gnu.org/software/gsl/" target="_top">GSL-1.9</a>, <a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a>, <a href="http://docs.hp.com/en/B9106-90010/index.html" target="_top">HP-UX C Library</a> and <a href="http://www.netlib.org/cephes/" target="_top">Cephes</a> libraries. Unless otherwise specified any floating point type that is narrower than the one shown will have <a class="link" href="../../backgrounders/relative_error.html#zero_error">effectively zero error</a>. </p> <p> Note that while the relative errors near the positive roots of lgamma are very low, the lgamma function has an infinite number of irrational roots for negative arguments: very close to these negative roots only a low absolute error can be guaranteed. </p> <div class="informaltable"><table class="table"> <colgroup> <col> <col> <col> <col> <col> <col> </colgroup> <thead><tr> <th> <p> Significand Size </p> </th> <th> <p> Platform and Compiler </p> </th> <th> <p> Factorials and Half factorials </p> </th> <th> <p> Values Near Zero </p> </th> <th> <p> Values Near 1 or 2 </p> </th> <th> <p> Values Near a Negative Pole </p> </th> </tr></thead> <tbody> <tr> <td> <p> 53 </p> </td> <td> <p> Win32 Visual C++ 8 </p> </td> <td> <p> Peak=0.88 Mean=0.14 </p> <p> (GSL=33) (<a href="http://www.netlib.org/cephes/" target="_top">Cephes</a>=1.5) </p> </td> <td> <p> Peak=0.96 Mean=0.46 </p> <p> (GSL=5.2) (<a href="http://www.netlib.org/cephes/" target="_top">Cephes</a>=1.1) </p> </td> <td> <p> Peak=0.86 Mean=0.46 </p> <p> (GSL=1168) (<a href="http://www.netlib.org/cephes/" target="_top">Cephes</a>~500000) </p> </td> <td> <p> Peak=4.2 Mean=1.3 </p> <p> (GSL=25) (<a href="http://www.netlib.org/cephes/" target="_top">Cephes</a>=1.6) </p> </td> </tr> <tr> <td> <p> 64 </p> </td> <td> <p> Linux IA32 / GCC </p> </td> <td> <p> Peak=1.9 Mean=0.43 </p> <p> (<a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a> Peak=1.7 Mean=0.49) </p> </td> <td> <p> Peak=1.4 Mean=0.57 </p> <p> (<a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a> Peak= 0.96 Mean=0.54) </p> </td> <td> <p> Peak=0.86 Mean=0.35 </p> <p> (<a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a> Peak=0.74 Mean=0.26) </p> </td> <td> <p> Peak=6.0 Mean=1.8 </p> <p> (<a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a> Peak=3.0 Mean=0.86) </p> </td> </tr> <tr> <td> <p> 64 </p> </td> <td> <p> Linux IA64 / GCC </p> </td> <td> <p> Peak=0.99 Mean=0.12 </p> <p> (<a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a> Peak 0) </p> </td> <td> <p> Pek=1.2 Mean=0.6 </p> <p> (<a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a> Peak 0) </p> </td> <td> <p> Peak=0.86 Mean=0.16 </p> <p> (<a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a> Peak 0) </p> </td> <td> <p> Peak=2.3 Mean=0.69 </p> <p> (<a href="http://www.gnu.org/software/libc/" target="_top">GNU C Lib</a> Peak 0) </p> </td> </tr> <tr> <td> <p> 113 </p> </td> <td> <p> HPUX IA64, aCC A.06.06 </p> </td> <td> <p> Peak=0.96 Mean=0.13 </p> <p> (<a href="http://docs.hp.com/en/B9106-90010/index.html" target="_top">HP-UX C Library</a> Peak 0) </p> </td> <td> <p> Peak=0.99 Mean=0.53 </p> <p> (<a href="http://docs.hp.com/en/B9106-90010/index.html" target="_top">HP-UX C Library</a> Peak 0) </p> </td> <td> <p> Peak=0.9 Mean=0.4 </p> <p> (<a href="http://docs.hp.com/en/B9106-90010/index.html" target="_top">HP-UX C Library</a> Peak 0) </p> </td> <td> <p> Peak=3.0 Mean=0.9 </p> <p> (<a href="http://docs.hp.com/en/B9106-90010/index.html" target="_top">HP-UX C Library</a> Peak 0) </p> </td> </tr> </tbody> </table></div> <a name="math_toolkit.special.sf_gamma.lgamma.testing"></a><h5> <a name="id1075908"></a> <a class="link" href="lgamma.html#math_toolkit.special.sf_gamma.lgamma.testing">Testing</a> </h5> <p> The main tests for this function involve comparisons against the logs of the factorials which can be independently calculated to very high accuracy. </p> <p> Random tests in key problem areas are also used. </p> <a name="math_toolkit.special.sf_gamma.lgamma.implementation"></a><h5> <a name="id1075928"></a> <a class="link" href="lgamma.html#math_toolkit.special.sf_gamma.lgamma.implementation">Implementation</a> </h5> <p> The generic version of this function is implemented by combining the series and continued fraction representations for the incomplete gamma function: </p> <p> <span class="inlinemediaobject"><img src="../../../../equations/lgamm2.png"></span> </p> <p> where <span class="emphasis"><em>l</em></span> is an arbitrary integration limit: choosing <code class="literal">l = max(10, a)</code> seems to work fairly well. For negative <span class="emphasis"><em>z</em></span> the logarithm version of the reflection formula is used: </p> <p> <span class="inlinemediaobject"><img src="../../../../equations/lgamm3.png"></span> </p> <p> For types of known precision, the <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a> is used, a traits class <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">lanczos</span><span class="special">::</span><span class="identifier">lanczos_traits</span></code> maps type T to an appropriate approximation. The logarithmic version of the <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a> is: </p> <p> <span class="inlinemediaobject"><img src="../../../../equations/lgamm4.png"></span> </p> <p> Where L<sub>e,g</sub> is the Lanczos sum, scaled by e<sup>g</sup>. </p> <p> As before the reflection formula is used for <span class="emphasis"><em>z < 0</em></span>. </p> <p> When z is very near 1 or 2, then the logarithmic version of the <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a> suffers very badly from cancellation error: indeed for values sufficiently close to 1 or 2, arbitrarily large relative errors can be obtained (even though the absolute error is tiny). </p> <p> For types with up to 113 bits of precision (up to and including 128-bit long doubles), root-preserving rational approximations <a class="link" href="../../backgrounders/implementation.html#math_toolkit.backgrounders.implementation.rational_approximations_used">devised by JM</a> are used over the intervals [1,2] and [2,3]. Over the interval [2,3] the approximation form used is: </p> <pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">z</span><span class="special">-</span><span class="number">2</span><span class="special">)(</span><span class="identifier">z</span><span class="special">+</span><span class="number">1</span><span class="special">)(</span><span class="identifier">Y</span> <span class="special">+</span> <span class="identifier">R</span><span class="special">(</span><span class="identifier">z</span><span class="special">-</span><span class="number">2</span><span class="special">));</span> </pre> <p> Where Y is a constant, and R(z-2) is the rational approximation: optimised so that it's absolute error is tiny compared to Y. In addition small values of z greater than 3 can handled by argument reduction using the recurrence relation: </p> <pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">+</span><span class="number">1</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">log</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">+</span> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> </pre> <p> Over the interval [1,2] two approximations have to be used, one for small z uses: </p> <pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">=</span> <span class="special">(</span><span class="identifier">z</span><span class="special">-</span><span class="number">1</span><span class="special">)(</span><span class="identifier">z</span><span class="special">-</span><span class="number">2</span><span class="special">)(</span><span class="identifier">Y</span> <span class="special">+</span> <span class="identifier">R</span><span class="special">(</span><span class="identifier">z</span><span class="special">-</span><span class="number">1</span><span class="special">));</span> </pre> <p> Once again Y is a constant, and R(z-1) is optimised for low absolute error compared to Y. For z > 1.5 the above form wouldn't converge to a minimax solution but this similar form does: </p> <pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">=</span> <span class="special">(</span><span class="number">2</span><span class="special">-</span><span class="identifier">z</span><span class="special">)(</span><span class="number">1</span><span class="special">-</span><span class="identifier">z</span><span class="special">)(</span><span class="identifier">Y</span> <span class="special">+</span> <span class="identifier">R</span><span class="special">(</span><span class="number">2</span><span class="special">-</span><span class="identifier">z</span><span class="special">));</span> </pre> <p> Finally for z < 1 the recurrence relation can be used to move to z > 1: </p> <pre class="programlisting"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">+</span><span class="number">1</span><span class="special">)</span> <span class="special">-</span> <span class="identifier">log</span><span class="special">(</span><span class="identifier">z</span><span class="special">);</span> </pre> <p> Note that while this involves a subtraction, it appears not to suffer from cancellation error: as z decreases from 1 the <code class="computeroutput"><span class="special">-</span><span class="identifier">log</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span></code> term grows positive much more rapidly than the <code class="computeroutput"><span class="identifier">lgamma</span><span class="special">(</span><span class="identifier">z</span><span class="special">+</span><span class="number">1</span><span class="special">)</span></code> term becomes negative. So in this specific case, significant digits are preserved, rather than cancelled. </p> <p> For other types which do have a <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a> defined for them the current solution is as follows: imagine we balance the two terms in the <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a> by dividing the power term by its value at <span class="emphasis"><em>z = 1</em></span>, and then multiplying the Lanczos coefficients by the same value. Now each term will take the value 1 at <span class="emphasis"><em>z = 1</em></span> and we can rearrange the power terms in terms of log1p. Likewise if we subtract 1 from the Lanczos sum part (algebraically, by subtracting the value of each term at <span class="emphasis"><em>z = 1</em></span>), we obtain a new summation that can be also be fed into log1p. Crucially, all of the terms tend to zero, as <span class="emphasis"><em>z -> 1</em></span>: </p> <p> <span class="inlinemediaobject"><img src="../../../../equations/lgamm5.png"></span> </p> <p> The C<sub>k</sub> terms in the above are the same as in the <a class="link" href="../../backgrounders/lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a>. </p> <p> A similar rearrangement can be performed at <span class="emphasis"><em>z = 2</em></span>: </p> <p> <span class="inlinemediaobject"><img src="../../../../equations/lgamm6.png"></span> </p> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright © 2006 , 2007, 2008, 2009 John Maddock, Paul A. Bristow, Hubert Holin, Xiaogang Zhang, Bruno Lalande, Johan Råde, Gautam Sewani and Thijs van den Berg<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="tgamma.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../sf_gamma.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="digamma.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>