tgamma.qbk 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. [section:tgamma Gamma]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/special_functions/gamma.hpp>
  5. ``
  6. namespace boost{ namespace math{
  7. template <class T>
  8. ``__sf_result`` tgamma(T z);
  9. template <class T, class ``__Policy``>
  10. ``__sf_result`` tgamma(T z, const ``__Policy``&);
  11. template <class T>
  12. ``__sf_result`` tgamma1pm1(T dz);
  13. template <class T, class ``__Policy``>
  14. ``__sf_result`` tgamma1pm1(T dz, const ``__Policy``&);
  15. }} // namespaces
  16. [h4 Description]
  17. template <class T>
  18. ``__sf_result`` tgamma(T z);
  19. template <class T, class ``__Policy``>
  20. ``__sf_result`` tgamma(T z, const ``__Policy``&);
  21. Returns the "true gamma" (hence name tgamma) of value z:
  22. [equation gamm1]
  23. [graph tgamma]
  24. [optional_policy]
  25. The return type of this function is computed using the __arg_promotion_rules:
  26. the result is `double` when T is an integer type, and T otherwise.
  27. template <class T>
  28. ``__sf_result`` tgamma1pm1(T dz);
  29. template <class T, class ``__Policy``>
  30. ``__sf_result`` tgamma1pm1(T dz, const ``__Policy``&);
  31. Returns `tgamma(dz + 1) - 1`. Internally the implementation does not make
  32. use of the addition and subtraction implied by the definition, leading to
  33. accurate results even for very small `dz`.
  34. The return type of this function is computed using the __arg_promotion_rules:
  35. the result is `double` when T is an integer type, and T otherwise.
  36. [optional_policy]
  37. [h4 Accuracy]
  38. The following table shows the peak errors (in units of epsilon)
  39. found on various platforms with various floating point types,
  40. along with comparisons to other common libraries.
  41. Unless otherwise specified any floating point type that is narrower
  42. than the one shown will have __zero_error.
  43. [table_tgamma]
  44. [table_tgamma1pm1]
  45. The following error plot are based on an exhaustive search of the functions domain, MSVC-15.5 at `double` precision,
  46. and GCC-7.1/Ubuntu for `long double` and `__float128`.
  47. [graph tgamma__double]
  48. [graph tgamma__80_bit_long_double]
  49. [graph tgamma____float128]
  50. [h4 Testing]
  51. The gamma is relatively easy to test: factorials and half-integer factorials
  52. can be calculated exactly by other means and compared with the gamma function.
  53. In addition, some accuracy tests in known tricky areas were computed at high precision
  54. using the generic version of this function.
  55. The function `tgamma1pm1` is tested against values calculated very naively
  56. using the formula `tgamma(1+dz)-1` with a lanczos approximation accurate
  57. to around 100 decimal digits.
  58. [h4 Implementation]
  59. The generic version of the `tgamma` function is implemented Sterling's approximation
  60. for `lgamma` for large z:
  61. [equation gamma6]
  62. Following exponentiation, downward recursion is then used for small values of z.
  63. For types of known precision the __lanczos is used, a traits class
  64. `boost::math::lanczos::lanczos_traits` maps type T to an appropriate
  65. approximation.
  66. For z in the range -20 < z < 1 then recursion is used to shift to z > 1 via:
  67. [equation gamm3]
  68. For very small z, this helps to preserve the identity:
  69. [equation gamm4]
  70. For z < -20 the reflection formula:
  71. [equation gamm5]
  72. is used. Particular care has to be taken to evaluate the [^ z * sin([pi] * z)] part:
  73. a special routine is used to reduce z prior to multiplying by [pi] to ensure that the
  74. result in is the range [0, [pi]/2]. Without this an excessive amount of error occurs
  75. in this region (which is hard enough already, as the rate of change near a negative pole
  76. is /exceptionally/ high).
  77. Finally if the argument is a small integer then table lookup of the factorial
  78. is used.
  79. The function `tgamma1pm1` is implemented using rational approximations [jm_rationals] in the
  80. region `-0.5 < dz < 2`. These are the same approximations (and internal routines)
  81. that are used for __lgamma, and so aren't detailed further here. The result of
  82. the approximation is `log(tgamma(dz+1))` which can fed into __expm1 to give
  83. the desired result. Outside the range `-0.5 < dz < 2` then the naive formula
  84. `tgamma1pm1(dz) = tgamma(dz+1)-1` can be used directly.
  85. [endsect] [/section:tgamma The Gamma Function]
  86. [/
  87. Copyright 2006 John Maddock and Paul A. Bristow.
  88. Distributed under the Boost Software License, Version 1.0.
  89. (See accompanying file LICENSE_1_0.txt or copy at
  90. http://www.boost.org/LICENSE_1_0.txt).
  91. ]