lgamma.html 29 KB

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