math/doc/html/math_toolkit/sf_gamma/polygamma.html
2019-10-31 17:55:35 +00:00

422 lines
20 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Polygamma</title>
<link rel="stylesheet" href="../../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Math Toolkit 2.11.0">
<link rel="up" href="../sf_gamma.html" title="Gamma Functions">
<link rel="prev" href="trigamma.html" title="Trigamma">
<link rel="next" href="gamma_ratios.html" title="Ratios of Gamma Functions">
</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="trigamma.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="gamma_ratios.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="math_toolkit.sf_gamma.polygamma"></a><a class="link" href="polygamma.html" title="Polygamma">Polygamma</a>
</h3></div></div></div>
<h5>
<a name="math_toolkit.sf_gamma.polygamma.h0"></a>
<span class="phrase"><a name="math_toolkit.sf_gamma.polygamma.synopsis"></a></span><a class="link" href="polygamma.html#math_toolkit.sf_gamma.polygamma.synopsis">Synopsis</a>
</h5>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</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">polygamma</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<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">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">polygamma</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</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">&lt;</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="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&gt;</span>
<a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>calculated-result-type</em></span></a> <span class="identifier">polygamma</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">n</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="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&amp;);</span>
<span class="special">}}</span> <span class="comment">// namespaces</span>
</pre>
<h5>
<a name="math_toolkit.sf_gamma.polygamma.h1"></a>
<span class="phrase"><a name="math_toolkit.sf_gamma.polygamma.description"></a></span><a class="link" href="polygamma.html#math_toolkit.sf_gamma.polygamma.description">Description</a>
</h5>
<p>
Returns the polygamma function of <span class="emphasis"><em>x</em></span>. Polygamma is defined
as the n'th derivative of the digamma function:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma1.svg"></span>
</p></blockquote></div>
<p>
The following graphs illustrate the behaviour of the function for odd and
even order:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../graphs/polygamma2.svg" align="middle"></span>
</p></blockquote></div>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../graphs/polygamma3.svg" align="middle"></span>
</p></blockquote></div>
<p>
The final <a class="link" href="../../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">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="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">policy
documentation for more details</a>.
</p>
<p>
The return type of this function is computed using the <a class="link" href="../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> when T is an integer type, and type
T otherwise.
</p>
<h5>
<a name="math_toolkit.sf_gamma.polygamma.h2"></a>
<span class="phrase"><a name="math_toolkit.sf_gamma.polygamma.accuracy"></a></span><a class="link" href="polygamma.html#math_toolkit.sf_gamma.polygamma.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. Unless otherwise specified
any floating point type that is narrower than the one shown will have <a class="link" href="../relative_error.html#math_toolkit.relative_error.zero_error">effectively zero error</a>.
</p>
<div class="table">
<a name="math_toolkit.sf_gamma.polygamma.table_polygamma"></a><p class="title"><b>Table&#160;8.6.&#160;Error rates for polygamma</b></p>
<div class="table-contents"><table class="table" summary="Error rates for polygamma">
<colgroup>
<col>
<col>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>
</th>
<th>
<p>
GNU C++ version 7.1.0<br> linux<br> double
</p>
</th>
<th>
<p>
GNU C++ version 7.1.0<br> linux<br> long double
</p>
</th>
<th>
<p>
Sun compiler version 0x5150<br> Sun Solaris<br> long double
</p>
</th>
<th>
<p>
Microsoft Visual C++ version 14.1<br> Win32<br> double
</p>
</th>
</tr></thead>
<tbody>
<tr>
<td>
<p>
Mathematica Data
</p>
</td>
<td>
<p>
<span class="blue">Max = 0.824&#949; (Mean = 0.0574&#949;)</span><br>
<br> (<span class="emphasis"><em>GSL 2.1:</em></span> Max = 62.9&#949; (Mean = 12.8&#949;))<br>
(<span class="emphasis"><em>Rmath 3.2.3:</em></span> Max = 108&#949; (Mean = 15.2&#949;))
</p>
</td>
<td>
<p>
<span class="blue">Max = 7.38&#949; (Mean = 1.84&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 34.3&#949; (Mean = 7.65&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 9.32&#949; (Mean = 1.95&#949;)</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
Mathematica Data - large arguments
</p>
</td>
<td>
<p>
<span class="blue">Max = 0.998&#949; (Mean = 0.0592&#949;)</span><br>
<br> (<span class="emphasis"><em>GSL 2.1:</em></span> Max = 244&#949; (Mean = 32.8&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_polygamma_GSL_2_1_Mathematica_Data_large_arguments">And
other failures.</a>)<br> (<span class="emphasis"><em>Rmath 3.2.3:</em></span>
<span class="red">Max = 1.71e+56&#949; (Mean = 1.01e+55&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_polygamma_Rmath_3_2_3_Mathematica_Data_large_arguments">And
other failures.</a>)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 2.23&#949; (Mean = 0.323&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 11.1&#949; (Mean = 0.848&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 150&#949; (Mean = 13.9&#949;)</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
Mathematica Data - negative arguments
</p>
</td>
<td>
<p>
<span class="blue">Max = 0.516&#949; (Mean = 0.022&#949;)</span><br> <br>
(<span class="emphasis"><em>GSL 2.1:</em></span> Max = 36.6&#949; (Mean = 3.04&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_polygamma_GSL_2_1_Mathematica_Data_negative_arguments">And
other failures.</a>)<br> (<span class="emphasis"><em>Rmath 3.2.3:</em></span>
Max = 0&#949; (Mean = 0&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_polygamma_Rmath_3_2_3_Mathematica_Data_negative_arguments">And
other failures.</a>)
</p>
</td>
<td>
<p>
<span class="blue">Max = 269&#949; (Mean = 87.7&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 269&#949; (Mean = 88.4&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 497&#949; (Mean = 129&#949;)</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
Mathematica Data - large negative arguments
</p>
</td>
<td>
<p>
<span class="blue">Max = 0&#949; (Mean = 0&#949;)</span><br> <br> (<span class="emphasis"><em>GSL
2.1:</em></span> Max = 1.79&#949; (Mean = 0.197&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_polygamma_GSL_2_1_Mathematica_Data_large_negative_arguments">And
other failures.</a>)<br> (<span class="emphasis"><em>Rmath 3.2.3:</em></span>
Max = 0&#949; (Mean = 0&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_polygamma_Rmath_3_2_3_Mathematica_Data_large_negative_arguments">And
other failures.</a>)
</p>
</td>
<td>
<p>
<span class="blue">Max = 155&#949; (Mean = 96.4&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 155&#949; (Mean = 96.4&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 162&#949; (Mean = 101&#949;)</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
Mathematica Data - small arguments
</p>
</td>
<td>
<p>
<span class="blue">Max = 0&#949; (Mean = 0&#949;)</span><br> <br> (<span class="emphasis"><em>GSL
2.1:</em></span> Max = 15.2&#949; (Mean = 5.03&#949;))<br> (<span class="emphasis"><em>Rmath
3.2.3:</em></span> Max = 106&#949; (Mean = 20&#949;))
</p>
</td>
<td>
<p>
<span class="blue">Max = 3.33&#949; (Mean = 0.75&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 3.33&#949; (Mean = 0.75&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 3&#949; (Mean = 0.496&#949;)</span>
</p>
</td>
</tr>
<tr>
<td>
<p>
Mathematica Data - Large orders and other bug cases
</p>
</td>
<td>
<p>
<span class="blue">Max = 0&#949; (Mean = 0&#949;)</span><br> <br> (<span class="emphasis"><em>GSL
2.1:</em></span> Max = 151&#949; (Mean = 39.3&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_polygamma_GSL_2_1_Mathematica_Data_Large_orders_and_other_bug_cases">And
other failures.</a>)<br> (<span class="emphasis"><em>Rmath 3.2.3:</em></span>
<span class="red">Max = +INF&#949; (Mean = +INF&#949;) <a class="link" href="../logs_and_tables/logs.html#errors_GNU_C_version_7_1_0_linux_double_polygamma_Rmath_3_2_3_Mathematica_Data_Large_orders_and_other_bug_cases">And
other failures.</a>)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 54.5&#949; (Mean = 13.3&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 145&#949; (Mean = 55.9&#949;)</span>
</p>
</td>
<td>
<p>
<span class="blue">Max = 200&#949; (Mean = 57.2&#949;)</span>
</p>
</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><p>
As shown above, error rates are generally very acceptable for moderately
sized arguments. Error rates should stay low for exact inputs, however, please
note that the function becomes exceptionally sensitive to small changes in
input for large n and negative x, indeed for cases where <span class="emphasis"><em>n!</em></span>
would overflow, the function changes directly from -&#8734; to +&#8734; somewhere between
each negative integer - <span class="emphasis"><em>these cases are not handled correctly</em></span>.
</p>
<p>
<span class="bold"><strong>For these reasons results should be treated with extreme
caution when <span class="emphasis"><em>n</em></span> is large and x negative</strong></span>.
</p>
<h5>
<a name="math_toolkit.sf_gamma.polygamma.h3"></a>
<span class="phrase"><a name="math_toolkit.sf_gamma.polygamma.testing"></a></span><a class="link" href="polygamma.html#math_toolkit.sf_gamma.polygamma.testing">Testing</a>
</h5>
<p>
Testing is against Mathematica generated spot values to 35 digit precision.
</p>
<h5>
<a name="math_toolkit.sf_gamma.polygamma.h4"></a>
<span class="phrase"><a name="math_toolkit.sf_gamma.polygamma.implementation"></a></span><a class="link" href="polygamma.html#math_toolkit.sf_gamma.polygamma.implementation">Implementation</a>
</h5>
<p>
For x &lt; 0 the following reflection formula is used:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma2.svg"></span>
</p></blockquote></div>
<p>
The n'th derivative of <span class="emphasis"><em>cot(x)</em></span> is tabulated for small
<span class="emphasis"><em>n</em></span>, and for larger n has the general form:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma3.svg"></span>
</p></blockquote></div>
<p>
The coefficients of the cosine terms can be calculated iteratively starting
from <span class="emphasis"><em>C<sub>1,0</sub> = -1</em></span> and then using
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma7.svg"></span>
</p></blockquote></div>
<p>
to generate coefficients for n+1.
</p>
<p>
Note that every other coefficient is zero, and therefore what we have are
even or odd polynomials depending on whether n is even or odd.
</p>
<p>
Once x is positive then we have two methods available to us, for small x
we use the series expansion:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma4.svg"></span>
</p></blockquote></div>
<p>
Note that the evaluation of zeta functions at integer values is essentially
a table lookup as <a class="link" href="../zetas/zeta.html" title="Riemann Zeta Function">zeta</a> is
optimized for those cases.
</p>
<p>
For large x we use the asymptotic expansion:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma5.svg"></span>
</p></blockquote></div>
<p>
For x in-between the two extremes we use the relation:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma6.svg"></span>
</p></blockquote></div>
<p>
to make x large enough for the asymptotic expansion to be used.
</p>
<p>
There are also two special cases:
</p>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma8.svg"></span>
</p></blockquote></div>
<div class="blockquote"><blockquote class="blockquote"><p>
<span class="inlinemediaobject"><img src="../../../equations/polygamma9.svg"></span>
</p></blockquote></div>
</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 &#169; 2006-2019 Nikhar
Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
Daryle Walker and Xiaogang Zhang<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="trigamma.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="gamma_ratios.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>