| 1 |
<html> |
| 2 |
<head> |
| 3 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
| 4 |
<title>Log Gamma</title> |
| 5 |
<link rel="stylesheet" href="../../math.css" type="text/css"> |
| 6 |
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> |
| 7 |
<link rel="home" href="../../index.html" title="Math Toolkit 4.2.1"> |
| 8 |
<link rel="up" href="../sf_gamma.html" title="Gamma Functions"> |
| 9 |
<link rel="prev" href="tgamma.html" title="Gamma"> |
| 10 |
<link rel="next" href="digamma.html" title="Digamma"> |
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 12 |
</head> |
| 13 |
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
| 14 |
<table cellpadding="2" width="100%"><tr> |
| 15 |
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> |
| 16 |
<td align="center"><a href="../../../../../../index.html">Home</a></td> |
| 17 |
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> |
| 18 |
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> |
| 19 |
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> |
| 20 |
<td align="center"><a href="../../../../../../more/index.htm">More</a></td> |
| 21 |
</tr></table> |
| 22 |
<hr> |
| 23 |
<div class="spirit-nav"> |
| 24 |
<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> |
| 25 |
</div> |
| 26 |
<div class="section"> |
| 27 |
<div class="titlepage"><div><div><h3 class="title"> |
| 28 |
<a name="math_toolkit.sf_gamma.lgamma"></a><a class="link" href="lgamma.html" title="Log Gamma">Log Gamma</a> |
| 29 |
</h3></div></div></div> |
| 30 |
<h5> |
| 31 |
<a name="math_toolkit.sf_gamma.lgamma.h0"></a> |
| 32 |
<span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.synopsis"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.synopsis">Synopsis</a> |
| 33 |
</h5> |
| 34 |
<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> |
| 35 |
</pre> |
| 36 |
<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> |
| 37 |
|
| 38 |
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> |
| 39 |
<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">lgamma</span><span class="special">(</span><span class="identifier">T</span> <span class="identifier">z</span><span class="special">);</span> |
| 40 |
|
| 41 |
<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="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span> |
| 42 |
<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">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="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span> |
| 43 |
|
| 44 |
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">></span> |
| 45 |
<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">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> |
| 46 |
|
| 47 |
<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="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">></span> |
| 48 |
<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">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="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a><span class="special">&);</span> |
| 49 |
|
| 50 |
<span class="special">}}</span> <span class="comment">// namespaces</span> |
| 51 |
</pre> |
| 52 |
<h5> |
| 53 |
<a name="math_toolkit.sf_gamma.lgamma.h1"></a> |
| 54 |
<span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.description"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.description">Description</a> |
| 55 |
</h5> |
| 56 |
<p> |
| 57 |
The <a href="http://en.wikipedia.org/wiki/Gamma_function" target="_top">lgamma function</a> |
| 58 |
is defined by: |
| 59 |
</p> |
| 60 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 61 |
<span class="inlinemediaobject"><img src="../../../equations/lgamm1.svg"></span> |
| 62 |
|
| 63 |
</p></blockquote></div> |
| 64 |
<p> |
| 65 |
The second form of the function takes a pointer to an integer, which if non-null |
| 66 |
is set on output to the sign of tgamma(z). |
| 67 |
</p> |
| 68 |
<p> |
| 69 |
The final <a class="link" href="../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">Policy</a> argument is optional and can |
| 70 |
be used to control the behaviour of the function: how it handles errors, |
| 71 |
what level of precision to use etc. Refer to the <a class="link" href="../../policy.html" title="Chapter 22. Policies: Controlling Precision, Error Handling etc">policy |
| 72 |
documentation for more details</a>. |
| 73 |
</p> |
| 74 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 75 |
<span class="inlinemediaobject"><img src="../../../graphs/lgamma.svg" align="middle"></span> |
| 76 |
|
| 77 |
</p></blockquote></div> |
| 78 |
<p> |
| 79 |
The return type of these functions is computed using the <a class="link" href="../result_type.html" title="Calculation of the Type of the Result"><span class="emphasis"><em>result |
| 80 |
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 |
| 81 |
otherwise. |
| 82 |
</p> |
| 83 |
<h5> |
| 84 |
<a name="math_toolkit.sf_gamma.lgamma.h2"></a> |
| 85 |
<span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.accuracy"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.accuracy">Accuracy</a> |
| 86 |
</h5> |
| 87 |
<p> |
| 88 |
The following table shows the peak errors (in units of epsilon) found on |
| 89 |
various platforms with various floating point types, along with comparisons |
| 90 |
to various other libraries. Unless otherwise specified any floating point |
| 91 |
type that is narrower than the one shown will have <a class="link" href="../relative_error.html#math_toolkit.relative_error.zero_error">effectively |
| 92 |
zero error</a>. |
| 93 |
</p> |
| 94 |
<p> |
| 95 |
Note that while the relative errors near the positive roots of lgamma are |
| 96 |
very low, the lgamma function has an infinite number of irrational roots |
| 97 |
for negative arguments: very close to these negative roots only a low absolute |
| 98 |
error can be guaranteed. |
| 99 |
</p> |
| 100 |
<div class="table"> |
| 101 |
<a name="math_toolkit.sf_gamma.lgamma.table_lgamma"></a><p class="title"><b>Table 8.3. Error rates for lgamma</b></p> |
| 102 |
<div class="table-contents"><table class="table" summary="Error rates for lgamma"> |
| 103 |
<colgroup> |
| 104 |
<col> |
| 105 |
<col> |
| 106 |
<col> |
| 107 |
<col> |
| 108 |
<col> |
| 109 |
</colgroup> |
| 110 |
<thead><tr> |
| 111 |
<th> |
| 112 |
</th> |
| 113 |
<th> |
| 114 |
<p> |
| 115 |
GNU C++ version 7.1.0<br> linux<br> double |
| 116 |
</p> |
| 117 |
</th> |
| 118 |
<th> |
| 119 |
<p> |
| 120 |
GNU C++ version 7.1.0<br> linux<br> long double |
| 121 |
</p> |
| 122 |
</th> |
| 123 |
<th> |
| 124 |
<p> |
| 125 |
Sun compiler version 0x5150<br> Sun Solaris<br> long double |
| 126 |
</p> |
| 127 |
</th> |
| 128 |
<th> |
| 129 |
<p> |
| 130 |
Microsoft Visual C++ version 14.1<br> Win32<br> double |
| 131 |
</p> |
| 132 |
</th> |
| 133 |
</tr></thead> |
| 134 |
<tbody> |
| 135 |
<tr> |
| 136 |
<td> |
| 137 |
<p> |
| 138 |
factorials |
| 139 |
</p> |
| 140 |
</td> |
| 141 |
<td> |
| 142 |
<p> |
| 143 |
<span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL |
| 144 |
2.1:</em></span> Max = 33.6ε (Mean = 2.78ε))<br> (<span class="emphasis"><em>Rmath |
| 145 |
3.2.3:</em></span> Max = 1.55ε (Mean = 0.592ε)) |
| 146 |
</p> |
| 147 |
</td> |
| 148 |
<td> |
| 149 |
<p> |
| 150 |
<span class="blue">Max = 0.991ε (Mean = 0.308ε)</span><br> <br> |
| 151 |
(<span class="emphasis"><em><cmath>:</em></span> Max = 1.67ε (Mean = 0.487ε))<br> |
| 152 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 1.67ε (Mean = 0.487ε)) |
| 153 |
</p> |
| 154 |
</td> |
| 155 |
<td> |
| 156 |
<p> |
| 157 |
<span class="blue">Max = 0.991ε (Mean = 0.383ε)</span><br> <br> |
| 158 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 1.36ε (Mean = 0.476ε)) |
| 159 |
</p> |
| 160 |
</td> |
| 161 |
<td> |
| 162 |
<p> |
| 163 |
<span class="blue">Max = 0.914ε (Mean = 0.175ε)</span><br> <br> |
| 164 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.958ε (Mean = 0.38ε)) |
| 165 |
</p> |
| 166 |
</td> |
| 167 |
</tr> |
| 168 |
<tr> |
| 169 |
<td> |
| 170 |
<p> |
| 171 |
near 0 |
| 172 |
</p> |
| 173 |
</td> |
| 174 |
<td> |
| 175 |
<p> |
| 176 |
<span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL |
| 177 |
2.1:</em></span> Max = 5.21ε (Mean = 1.57ε))<br> (<span class="emphasis"><em>Rmath |
| 178 |
3.2.3:</em></span> Max = 0ε (Mean = 0ε)) |
| 179 |
</p> |
| 180 |
</td> |
| 181 |
<td> |
| 182 |
<p> |
| 183 |
<span class="blue">Max = 1.42ε (Mean = 0.566ε)</span><br> <br> |
| 184 |
(<span class="emphasis"><em><cmath>:</em></span> Max = 0.964ε (Mean = 0.543ε))<br> |
| 185 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.964ε (Mean = 0.543ε)) |
| 186 |
</p> |
| 187 |
</td> |
| 188 |
<td> |
| 189 |
<p> |
| 190 |
<span class="blue">Max = 1.42ε (Mean = 0.566ε)</span><br> <br> |
| 191 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.964ε (Mean = 0.543ε)) |
| 192 |
</p> |
| 193 |
</td> |
| 194 |
<td> |
| 195 |
<p> |
| 196 |
<span class="blue">Max = 0.964ε (Mean = 0.462ε)</span><br> <br> |
| 197 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.962ε (Mean = 0.372ε)) |
| 198 |
</p> |
| 199 |
</td> |
| 200 |
</tr> |
| 201 |
<tr> |
| 202 |
<td> |
| 203 |
<p> |
| 204 |
near 1 |
| 205 |
</p> |
| 206 |
</td> |
| 207 |
<td> |
| 208 |
<p> |
| 209 |
<span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL |
| 210 |
2.1:</em></span> Max = 442ε (Mean = 88.8ε))<br> (<span class="emphasis"><em>Rmath |
| 211 |
3.2.3:</em></span> Max = 7.99e+04ε (Mean = 1.68e+04ε)) |
| 212 |
</p> |
| 213 |
</td> |
| 214 |
<td> |
| 215 |
<p> |
| 216 |
<span class="blue">Max = 0.948ε (Mean = 0.36ε)</span><br> <br> |
| 217 |
(<span class="emphasis"><em><cmath>:</em></span> Max = 0.615ε (Mean = 0.096ε))<br> |
| 218 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.615ε (Mean = 0.096ε)) |
| 219 |
</p> |
| 220 |
</td> |
| 221 |
<td> |
| 222 |
<p> |
| 223 |
<span class="blue">Max = 0.948ε (Mean = 0.36ε)</span><br> <br> |
| 224 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 1.71ε (Mean = 0.581ε)) |
| 225 |
</p> |
| 226 |
</td> |
| 227 |
<td> |
| 228 |
<p> |
| 229 |
<span class="blue">Max = 0.867ε (Mean = 0.468ε)</span><br> <br> |
| 230 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.906ε (Mean = 0.565ε)) |
| 231 |
</p> |
| 232 |
</td> |
| 233 |
</tr> |
| 234 |
<tr> |
| 235 |
<td> |
| 236 |
<p> |
| 237 |
near 2 |
| 238 |
</p> |
| 239 |
</td> |
| 240 |
<td> |
| 241 |
<p> |
| 242 |
<span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL |
| 243 |
2.1:</em></span> Max = 1.17e+03ε (Mean = 274ε))<br> (<span class="emphasis"><em>Rmath |
| 244 |
3.2.3:</em></span> Max = 2.63e+05ε (Mean = 5.84e+04ε)) |
| 245 |
</p> |
| 246 |
</td> |
| 247 |
<td> |
| 248 |
<p> |
| 249 |
<span class="blue">Max = 0.878ε (Mean = 0.242ε)</span><br> <br> |
| 250 |
(<span class="emphasis"><em><cmath>:</em></span> Max = 0.741ε (Mean = 0.263ε))<br> |
| 251 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.741ε (Mean = 0.263ε)) |
| 252 |
</p> |
| 253 |
</td> |
| 254 |
<td> |
| 255 |
<p> |
| 256 |
<span class="blue">Max = 0.878ε (Mean = 0.242ε)</span><br> <br> |
| 257 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.598ε (Mean = 0.235ε)) |
| 258 |
</p> |
| 259 |
</td> |
| 260 |
<td> |
| 261 |
<p> |
| 262 |
<span class="blue">Max = 0.591ε (Mean = 0.159ε)</span><br> <br> |
| 263 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.741ε (Mean = 0.473ε)) |
| 264 |
</p> |
| 265 |
</td> |
| 266 |
</tr> |
| 267 |
<tr> |
| 268 |
<td> |
| 269 |
<p> |
| 270 |
near -10 |
| 271 |
</p> |
| 272 |
</td> |
| 273 |
<td> |
| 274 |
<p> |
| 275 |
<span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL |
| 276 |
2.1:</em></span> Max = 24.9ε (Mean = 4.6ε))<br> (<span class="emphasis"><em>Rmath |
| 277 |
3.2.3:</em></span> Max = 4.22ε (Mean = 1.26ε)) |
| 278 |
</p> |
| 279 |
</td> |
| 280 |
<td> |
| 281 |
<p> |
| 282 |
<span class="blue">Max = 3.81ε (Mean = 1.01ε)</span><br> <br> |
| 283 |
(<span class="emphasis"><em><cmath>:</em></span> Max = 0.997ε (Mean = 0.412ε))<br> |
| 284 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.997ε (Mean = 0.412ε)) |
| 285 |
</p> |
| 286 |
</td> |
| 287 |
<td> |
| 288 |
<p> |
| 289 |
<span class="blue">Max = 3.81ε (Mean = 1.01ε)</span><br> <br> |
| 290 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 3.04ε (Mean = 1.01ε)) |
| 291 |
</p> |
| 292 |
</td> |
| 293 |
<td> |
| 294 |
<p> |
| 295 |
<span class="blue">Max = 4.22ε (Mean = 1.33ε)</span><br> <br> |
| 296 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.997ε (Mean = 0.444ε)) |
| 297 |
</p> |
| 298 |
</td> |
| 299 |
</tr> |
| 300 |
<tr> |
| 301 |
<td> |
| 302 |
<p> |
| 303 |
near -55 |
| 304 |
</p> |
| 305 |
</td> |
| 306 |
<td> |
| 307 |
<p> |
| 308 |
<span class="blue">Max = 0ε (Mean = 0ε)</span><br> <br> (<span class="emphasis"><em>GSL |
| 309 |
2.1:</em></span> Max = 7.02ε (Mean = 1.47ε))<br> (<span class="emphasis"><em>Rmath |
| 310 |
3.2.3:</em></span> Max = 250ε (Mean = 60.9ε)) |
| 311 |
</p> |
| 312 |
</td> |
| 313 |
<td> |
| 314 |
<p> |
| 315 |
<span class="blue">Max = 0.821ε (Mean = 0.513ε)</span><br> <br> |
| 316 |
(<span class="emphasis"><em><cmath>:</em></span> Max = 1.58ε (Mean = 0.672ε))<br> |
| 317 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 1.58ε (Mean = 0.672ε)) |
| 318 |
</p> |
| 319 |
</td> |
| 320 |
<td> |
| 321 |
<p> |
| 322 |
<span class="blue">Max = 1.59ε (Mean = 0.587ε)</span><br> <br> |
| 323 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 0.821ε (Mean = 0.674ε)) |
| 324 |
</p> |
| 325 |
</td> |
| 326 |
<td> |
| 327 |
<p> |
| 328 |
<span class="blue">Max = 0.821ε (Mean = 0.419ε)</span><br> <br> |
| 329 |
(<span class="emphasis"><em><math.h>:</em></span> Max = 249ε (Mean = 43.1ε)) |
| 330 |
</p> |
| 331 |
</td> |
| 332 |
</tr> |
| 333 |
</tbody> |
| 334 |
</table></div> |
| 335 |
</div> |
| 336 |
<br class="table-break"><p> |
| 337 |
The following error plot are based on an exhaustive search of the functions |
| 338 |
domain, MSVC-15.5 at <code class="computeroutput"><span class="keyword">double</span></code> |
| 339 |
precision, and GCC-7.1/Ubuntu for <code class="computeroutput"><span class="keyword">long</span> |
| 340 |
<span class="keyword">double</span></code> and <code class="computeroutput"><span class="identifier">__float128</span></code>. |
| 341 |
</p> |
| 342 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 343 |
<span class="inlinemediaobject"><img src="../../../graphs/lgamma__double.svg" align="middle"></span> |
| 344 |
|
| 345 |
</p></blockquote></div> |
| 346 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 347 |
<span class="inlinemediaobject"><img src="../../../graphs/lgamma__80_bit_long_double.svg" align="middle"></span> |
| 348 |
|
| 349 |
</p></blockquote></div> |
| 350 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 351 |
<span class="inlinemediaobject"><img src="../../../graphs/lgamma____float128.svg" align="middle"></span> |
| 352 |
|
| 353 |
</p></blockquote></div> |
| 354 |
<h5> |
| 355 |
<a name="math_toolkit.sf_gamma.lgamma.h3"></a> |
| 356 |
<span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.testing"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.testing">Testing</a> |
| 357 |
</h5> |
| 358 |
<p> |
| 359 |
The main tests for this function involve comparisons against the logs of |
| 360 |
the factorials which can be independently calculated to very high accuracy. |
| 361 |
</p> |
| 362 |
<p> |
| 363 |
Random tests in key problem areas are also used. |
| 364 |
</p> |
| 365 |
<h5> |
| 366 |
<a name="math_toolkit.sf_gamma.lgamma.h4"></a> |
| 367 |
<span class="phrase"><a name="math_toolkit.sf_gamma.lgamma.implementation"></a></span><a class="link" href="lgamma.html#math_toolkit.sf_gamma.lgamma.implementation">Implementation</a> |
| 368 |
</h5> |
| 369 |
<p> |
| 370 |
The generic version of this function is implemented using Sterling's approximation |
| 371 |
for large arguments: |
| 372 |
</p> |
| 373 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 374 |
<span class="inlinemediaobject"><img src="../../../equations/gamma6.svg"></span> |
| 375 |
|
| 376 |
</p></blockquote></div> |
| 377 |
<p> |
| 378 |
For small arguments, the logarithm of tgamma is used. |
| 379 |
</p> |
| 380 |
<p> |
| 381 |
For negative <span class="emphasis"><em>z</em></span> the logarithm version of the reflection |
| 382 |
formula is used: |
| 383 |
</p> |
| 384 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 385 |
<span class="inlinemediaobject"><img src="../../../equations/lgamm3.svg"></span> |
| 386 |
|
| 387 |
</p></blockquote></div> |
| 388 |
<p> |
| 389 |
For types of known precision, the <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos |
| 390 |
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> |
| 391 |
maps type T to an appropriate approximation. The logarithmic version of the |
| 392 |
<a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos approximation</a> is: |
| 393 |
</p> |
| 394 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 395 |
<span class="inlinemediaobject"><img src="../../../equations/lgamm4.svg"></span> |
| 396 |
|
| 397 |
</p></blockquote></div> |
| 398 |
<p> |
| 399 |
Where L<sub>e,g</sub> is the Lanczos sum, scaled by e<sup>g</sup>. |
| 400 |
</p> |
| 401 |
<p> |
| 402 |
As before the reflection formula is used for <span class="emphasis"><em>z < 0</em></span>. |
| 403 |
</p> |
| 404 |
<p> |
| 405 |
When z is very near 1 or 2, then the logarithmic version of the <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos |
| 406 |
approximation</a> suffers very badly from cancellation error: indeed for |
| 407 |
values sufficiently close to 1 or 2, arbitrarily large relative errors can |
| 408 |
be obtained (even though the absolute error is tiny). |
| 409 |
</p> |
| 410 |
<p> |
| 411 |
For types with up to 113 bits of precision (up to and including 128-bit long |
| 412 |
doubles), root-preserving rational approximations <a class="link" href="../sf_implementation.html#math_toolkit.sf_implementation.rational_approximations_used">devised |
| 413 |
by JM</a> are used over the intervals [1,2] and [2,3]. Over the interval |
| 414 |
[2,3] the approximation form used is: |
| 415 |
</p> |
| 416 |
<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> |
| 417 |
</pre> |
| 418 |
<p> |
| 419 |
Where Y is a constant, and R(z-2) is the rational approximation: optimised |
| 420 |
so that its absolute error is tiny compared to Y. In addition, small values |
| 421 |
of z greater than 3 can handled by argument reduction using the recurrence |
| 422 |
relation: |
| 423 |
</p> |
| 424 |
<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> |
| 425 |
</pre> |
| 426 |
<p> |
| 427 |
Over the interval [1,2] two approximations have to be used, one for small |
| 428 |
z uses: |
| 429 |
</p> |
| 430 |
<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> |
| 431 |
</pre> |
| 432 |
<p> |
| 433 |
Once again Y is a constant, and R(z-1) is optimised for low absolute error |
| 434 |
compared to Y. For z > 1.5 the above form wouldn't converge to a minimax |
| 435 |
solution but this similar form does: |
| 436 |
</p> |
| 437 |
<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> |
| 438 |
</pre> |
| 439 |
<p> |
| 440 |
Finally for z < 1 the recurrence relation can be used to move to z > |
| 441 |
1: |
| 442 |
</p> |
| 443 |
<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> |
| 444 |
</pre> |
| 445 |
<p> |
| 446 |
Note that while this involves a subtraction, it appears not to suffer from |
| 447 |
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 |
| 448 |
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 |
| 449 |
case, significant digits are preserved, rather than cancelled. |
| 450 |
</p> |
| 451 |
<p> |
| 452 |
For other types which do have a <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos |
| 453 |
approximation</a> defined for them the current solution is as follows: |
| 454 |
imagine we balance the two terms in the <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos |
| 455 |
approximation</a> by dividing the power term by its value at <span class="emphasis"><em>z |
| 456 |
= 1</em></span>, and then multiplying the Lanczos coefficients by the same |
| 457 |
value. Now each term will take the value 1 at <span class="emphasis"><em>z = 1</em></span> |
| 458 |
and we can rearrange the power terms in terms of log1p. Likewise if we subtract |
| 459 |
1 from the Lanczos sum part (algebraically, by subtracting the value of each |
| 460 |
term at <span class="emphasis"><em>z = 1</em></span>), we obtain a new summation that can be |
| 461 |
also be fed into log1p. Crucially, all of the terms tend to zero, as <span class="emphasis"><em>z |
| 462 |
-> 1</em></span>: |
| 463 |
</p> |
| 464 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 465 |
<span class="inlinemediaobject"><img src="../../../equations/lgamm5.svg"></span> |
| 466 |
|
| 467 |
</p></blockquote></div> |
| 468 |
<p> |
| 469 |
The C<sub>k</sub> terms in the above are the same as in the <a class="link" href="../lanczos.html" title="The Lanczos Approximation">Lanczos |
| 470 |
approximation</a>. |
| 471 |
</p> |
| 472 |
<p> |
| 473 |
A similar rearrangement can be performed at <span class="emphasis"><em>z = 2</em></span>: |
| 474 |
</p> |
| 475 |
<div class="blockquote"><blockquote class="blockquote"><p> |
| 476 |
<span class="inlinemediaobject"><img src="../../../equations/lgamm6.svg"></span> |
| 477 |
|
| 478 |
</p></blockquote></div> |
| 479 |
</div> |
| 480 |
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland, |
| 481 |
Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno |
| 482 |
Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde, |
| 483 |
Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle |
| 484 |
Walker and Xiaogang Zhang<p> |
| 485 |
Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 486 |
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>) |
| 487 |
</p> |
| 488 |
</div> |
| 489 |
<hr> |
| 490 |
<div class="spirit-nav"> |
| 491 |
<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> |
| 492 |
</div> |
| 493 |
</body> |
| 494 |
</html> |