gamma.hpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  1. // Copyright John Maddock 2006-7, 2013-14.
  2. // Copyright Paul A. Bristow 2007, 2013-14.
  3. // Copyright Nikhar Agrawal 2013-14
  4. // Copyright Christopher Kormanyos 2013-14
  5. // Use, modification and distribution are subject to the
  6. // Boost Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_MATH_SF_GAMMA_HPP
  9. #define BOOST_MATH_SF_GAMMA_HPP
  10. #ifdef _MSC_VER
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp>
  14. #include <boost/math/tools/series.hpp>
  15. #include <boost/math/tools/fraction.hpp>
  16. #include <boost/math/tools/precision.hpp>
  17. #include <boost/math/tools/promotion.hpp>
  18. #include <boost/math/policies/error_handling.hpp>
  19. #include <boost/math/constants/constants.hpp>
  20. #include <boost/math/special_functions/math_fwd.hpp>
  21. #include <boost/math/special_functions/log1p.hpp>
  22. #include <boost/math/special_functions/trunc.hpp>
  23. #include <boost/math/special_functions/powm1.hpp>
  24. #include <boost/math/special_functions/sqrt1pm1.hpp>
  25. #include <boost/math/special_functions/lanczos.hpp>
  26. #include <boost/math/special_functions/fpclassify.hpp>
  27. #include <boost/math/special_functions/detail/igamma_large.hpp>
  28. #include <boost/math/special_functions/detail/unchecked_factorial.hpp>
  29. #include <boost/math/special_functions/detail/lgamma_small.hpp>
  30. #include <boost/math/special_functions/bernoulli.hpp>
  31. #include <boost/type_traits/is_convertible.hpp>
  32. #include <boost/assert.hpp>
  33. #include <boost/mpl/greater.hpp>
  34. #include <boost/mpl/equal_to.hpp>
  35. #include <boost/mpl/greater.hpp>
  36. #include <boost/config/no_tr1/cmath.hpp>
  37. #include <algorithm>
  38. #ifdef BOOST_MSVC
  39. # pragma warning(push)
  40. # pragma warning(disable: 4702) // unreachable code (return after domain_error throw).
  41. # pragma warning(disable: 4127) // conditional expression is constant.
  42. # pragma warning(disable: 4100) // unreferenced formal parameter.
  43. // Several variables made comments,
  44. // but some difficulty as whether referenced on not may depend on macro values.
  45. // So to be safe, 4100 warnings suppressed.
  46. // TODO - revisit this?
  47. #endif
  48. namespace boost{ namespace math{
  49. namespace detail{
  50. template <class T>
  51. inline bool is_odd(T v, const boost::true_type&)
  52. {
  53. int i = static_cast<int>(v);
  54. return i&1;
  55. }
  56. template <class T>
  57. inline bool is_odd(T v, const boost::false_type&)
  58. {
  59. // Oh dear can't cast T to int!
  60. BOOST_MATH_STD_USING
  61. T modulus = v - 2 * floor(v/2);
  62. return static_cast<bool>(modulus != 0);
  63. }
  64. template <class T>
  65. inline bool is_odd(T v)
  66. {
  67. return is_odd(v, ::boost::is_convertible<T, int>());
  68. }
  69. template <class T>
  70. T sinpx(T z)
  71. {
  72. // Ad hoc function calculates x * sin(pi * x),
  73. // taking extra care near when x is near a whole number.
  74. BOOST_MATH_STD_USING
  75. int sign = 1;
  76. if(z < 0)
  77. {
  78. z = -z;
  79. }
  80. T fl = floor(z);
  81. T dist;
  82. if(is_odd(fl))
  83. {
  84. fl += 1;
  85. dist = fl - z;
  86. sign = -sign;
  87. }
  88. else
  89. {
  90. dist = z - fl;
  91. }
  92. BOOST_ASSERT(fl >= 0);
  93. if(dist > 0.5)
  94. dist = 1 - dist;
  95. T result = sin(dist*boost::math::constants::pi<T>());
  96. return sign*z*result;
  97. } // template <class T> T sinpx(T z)
  98. //
  99. // tgamma(z), with Lanczos support:
  100. //
  101. template <class T, class Policy, class Lanczos>
  102. T gamma_imp(T z, const Policy& pol, const Lanczos& l)
  103. {
  104. BOOST_MATH_STD_USING
  105. T result = 1;
  106. #ifdef BOOST_MATH_INSTRUMENT
  107. static bool b = false;
  108. if(!b)
  109. {
  110. std::cout << "tgamma_imp called with " << typeid(z).name() << " " << typeid(l).name() << std::endl;
  111. b = true;
  112. }
  113. #endif
  114. static const char* function = "boost::math::tgamma<%1%>(%1%)";
  115. if(z <= 0)
  116. {
  117. if(floor(z) == z)
  118. return policies::raise_pole_error<T>(function, "Evaluation of tgamma at a negative integer %1%.", z, pol);
  119. if(z <= -20)
  120. {
  121. result = gamma_imp(T(-z), pol, l) * sinpx(z);
  122. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  123. if((fabs(result) < 1) && (tools::max_value<T>() * fabs(result) < boost::math::constants::pi<T>()))
  124. return -boost::math::sign(result) * policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
  125. result = -boost::math::constants::pi<T>() / result;
  126. if(result == 0)
  127. return policies::raise_underflow_error<T>(function, "Result of tgamma is too small to represent.", pol);
  128. if((boost::math::fpclassify)(result) == (int)FP_SUBNORMAL)
  129. return policies::raise_denorm_error<T>(function, "Result of tgamma is denormalized.", result, pol);
  130. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  131. return result;
  132. }
  133. // shift z to > 1:
  134. while(z < 0)
  135. {
  136. result /= z;
  137. z += 1;
  138. }
  139. }
  140. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  141. if((floor(z) == z) && (z < max_factorial<T>::value))
  142. {
  143. result *= unchecked_factorial<T>(itrunc(z, pol) - 1);
  144. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  145. }
  146. else if (z < tools::root_epsilon<T>())
  147. {
  148. if (z < 1 / tools::max_value<T>())
  149. result = policies::raise_overflow_error<T>(function, 0, pol);
  150. result *= 1 / z - constants::euler<T>();
  151. }
  152. else
  153. {
  154. result *= Lanczos::lanczos_sum(z);
  155. T zgh = (z + static_cast<T>(Lanczos::g()) - boost::math::constants::half<T>());
  156. T lzgh = log(zgh);
  157. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  158. BOOST_MATH_INSTRUMENT_VARIABLE(tools::log_max_value<T>());
  159. if(z * lzgh > tools::log_max_value<T>())
  160. {
  161. // we're going to overflow unless this is done with care:
  162. BOOST_MATH_INSTRUMENT_VARIABLE(zgh);
  163. if(lzgh * z / 2 > tools::log_max_value<T>())
  164. return boost::math::sign(result) * policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
  165. T hp = pow(zgh, (z / 2) - T(0.25));
  166. BOOST_MATH_INSTRUMENT_VARIABLE(hp);
  167. result *= hp / exp(zgh);
  168. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  169. if(tools::max_value<T>() / hp < result)
  170. return boost::math::sign(result) * policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
  171. result *= hp;
  172. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  173. }
  174. else
  175. {
  176. BOOST_MATH_INSTRUMENT_VARIABLE(zgh);
  177. BOOST_MATH_INSTRUMENT_VARIABLE(pow(zgh, z - boost::math::constants::half<T>()));
  178. BOOST_MATH_INSTRUMENT_VARIABLE(exp(zgh));
  179. result *= pow(zgh, z - boost::math::constants::half<T>()) / exp(zgh);
  180. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  181. }
  182. }
  183. return result;
  184. }
  185. //
  186. // lgamma(z) with Lanczos support:
  187. //
  188. template <class T, class Policy, class Lanczos>
  189. T lgamma_imp(T z, const Policy& pol, const Lanczos& l, int* sign = 0)
  190. {
  191. #ifdef BOOST_MATH_INSTRUMENT
  192. static bool b = false;
  193. if(!b)
  194. {
  195. std::cout << "lgamma_imp called with " << typeid(z).name() << " " << typeid(l).name() << std::endl;
  196. b = true;
  197. }
  198. #endif
  199. BOOST_MATH_STD_USING
  200. static const char* function = "boost::math::lgamma<%1%>(%1%)";
  201. T result = 0;
  202. int sresult = 1;
  203. if(z <= -tools::root_epsilon<T>())
  204. {
  205. // reflection formula:
  206. if(floor(z) == z)
  207. return policies::raise_pole_error<T>(function, "Evaluation of lgamma at a negative integer %1%.", z, pol);
  208. T t = sinpx(z);
  209. z = -z;
  210. if(t < 0)
  211. {
  212. t = -t;
  213. }
  214. else
  215. {
  216. sresult = -sresult;
  217. }
  218. result = log(boost::math::constants::pi<T>()) - lgamma_imp(z, pol, l) - log(t);
  219. }
  220. else if (z < tools::root_epsilon<T>())
  221. {
  222. if (0 == z)
  223. return policies::raise_pole_error<T>(function, "Evaluation of lgamma at %1%.", z, pol);
  224. if (fabs(z) < 1 / tools::max_value<T>())
  225. result = -log(fabs(z));
  226. else
  227. result = log(fabs(1 / z - constants::euler<T>()));
  228. if (z < 0)
  229. sresult = -1;
  230. }
  231. else if(z < 15)
  232. {
  233. typedef typename policies::precision<T, Policy>::type precision_type;
  234. typedef typename mpl::if_<
  235. mpl::and_<
  236. mpl::less_equal<precision_type, mpl::int_<64> >,
  237. mpl::greater<precision_type, mpl::int_<0> >
  238. >,
  239. mpl::int_<64>,
  240. typename mpl::if_<
  241. mpl::and_<
  242. mpl::less_equal<precision_type, mpl::int_<113> >,
  243. mpl::greater<precision_type, mpl::int_<0> >
  244. >,
  245. mpl::int_<113>, mpl::int_<0> >::type
  246. >::type tag_type;
  247. result = lgamma_small_imp<T>(z, T(z - 1), T(z - 2), tag_type(), pol, l);
  248. }
  249. else if((z >= 3) && (z < 100) && (std::numeric_limits<T>::max_exponent >= 1024))
  250. {
  251. // taking the log of tgamma reduces the error, no danger of overflow here:
  252. result = log(gamma_imp(z, pol, l));
  253. }
  254. else
  255. {
  256. // regular evaluation:
  257. T zgh = static_cast<T>(z + Lanczos::g() - boost::math::constants::half<T>());
  258. result = log(zgh) - 1;
  259. result *= z - 0.5f;
  260. result += log(Lanczos::lanczos_sum_expG_scaled(z));
  261. }
  262. if(sign)
  263. *sign = sresult;
  264. return result;
  265. }
  266. //
  267. // Incomplete gamma functions follow:
  268. //
  269. template <class T>
  270. struct upper_incomplete_gamma_fract
  271. {
  272. private:
  273. T z, a;
  274. int k;
  275. public:
  276. typedef std::pair<T,T> result_type;
  277. upper_incomplete_gamma_fract(T a1, T z1)
  278. : z(z1-a1+1), a(a1), k(0)
  279. {
  280. }
  281. result_type operator()()
  282. {
  283. ++k;
  284. z += 2;
  285. return result_type(k * (a - k), z);
  286. }
  287. };
  288. template <class T>
  289. inline T upper_gamma_fraction(T a, T z, T eps)
  290. {
  291. // Multiply result by z^a * e^-z to get the full
  292. // upper incomplete integral. Divide by tgamma(z)
  293. // to normalise.
  294. upper_incomplete_gamma_fract<T> f(a, z);
  295. return 1 / (z - a + 1 + boost::math::tools::continued_fraction_a(f, eps));
  296. }
  297. template <class T>
  298. struct lower_incomplete_gamma_series
  299. {
  300. private:
  301. T a, z, result;
  302. public:
  303. typedef T result_type;
  304. lower_incomplete_gamma_series(T a1, T z1) : a(a1), z(z1), result(1){}
  305. T operator()()
  306. {
  307. T r = result;
  308. a += 1;
  309. result *= z/a;
  310. return r;
  311. }
  312. };
  313. template <class T, class Policy>
  314. inline T lower_gamma_series(T a, T z, const Policy& pol, T init_value = 0)
  315. {
  316. // Multiply result by ((z^a) * (e^-z) / a) to get the full
  317. // lower incomplete integral. Then divide by tgamma(a)
  318. // to get the normalised value.
  319. lower_incomplete_gamma_series<T> s(a, z);
  320. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  321. T factor = policies::get_epsilon<T, Policy>();
  322. T result = boost::math::tools::sum_series(s, factor, max_iter, init_value);
  323. policies::check_series_iterations<T>("boost::math::detail::lower_gamma_series<%1%>(%1%)", max_iter, pol);
  324. return result;
  325. }
  326. //
  327. // Fully generic tgamma and lgamma use Stirling's approximation
  328. // with Bernoulli numbers.
  329. //
  330. template<class T>
  331. std::size_t highest_bernoulli_index()
  332. {
  333. const float digits10_of_type = (std::numeric_limits<T>::is_specialized
  334. ? static_cast<float>(std::numeric_limits<T>::digits10)
  335. : static_cast<float>(boost::math::tools::digits<T>() * 0.301F));
  336. // Find the high index n for Bn to produce the desired precision in Stirling's calculation.
  337. return static_cast<std::size_t>(18.0F + (0.6F * digits10_of_type));
  338. }
  339. template<class T>
  340. T minimum_argument_for_bernoulli_recursion()
  341. {
  342. const float digits10_of_type = (std::numeric_limits<T>::is_specialized
  343. ? static_cast<float>(std::numeric_limits<T>::digits10)
  344. : static_cast<float>(boost::math::tools::digits<T>() * 0.301F));
  345. return T(digits10_of_type * 1.7F);
  346. }
  347. // Forward declaration of the lgamma_imp template specialization.
  348. template <class T, class Policy>
  349. T lgamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&, int* sign = 0);
  350. template <class T, class Policy>
  351. T gamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&)
  352. {
  353. BOOST_MATH_STD_USING
  354. static const char* function = "boost::math::tgamma<%1%>(%1%)";
  355. // Check if the argument of tgamma is identically zero.
  356. const bool is_at_zero = (z == 0);
  357. if(is_at_zero)
  358. return policies::raise_domain_error<T>(function, "Evaluation of tgamma at zero %1%.", z, pol);
  359. const bool b_neg = (z < 0);
  360. const bool floor_of_z_is_equal_to_z = (floor(z) == z);
  361. // Special case handling of small factorials:
  362. if((!b_neg) && floor_of_z_is_equal_to_z && (z < boost::math::max_factorial<T>::value))
  363. {
  364. return boost::math::unchecked_factorial<T>(itrunc(z) - 1);
  365. }
  366. // Make a local, unsigned copy of the input argument.
  367. T zz((!b_neg) ? z : -z);
  368. // Special case for ultra-small z:
  369. if(zz < tools::cbrt_epsilon<T>())
  370. {
  371. const T a0(1);
  372. const T a1(boost::math::constants::euler<T>());
  373. const T six_euler_squared((boost::math::constants::euler<T>() * boost::math::constants::euler<T>()) * 6);
  374. const T a2((six_euler_squared - boost::math::constants::pi_sqr<T>()) / 12);
  375. const T inverse_tgamma_series = z * ((a2 * z + a1) * z + a0);
  376. return 1 / inverse_tgamma_series;
  377. }
  378. // Scale the argument up for the calculation of lgamma,
  379. // and use downward recursion later for the final result.
  380. const T min_arg_for_recursion = minimum_argument_for_bernoulli_recursion<T>();
  381. int n_recur;
  382. if(zz < min_arg_for_recursion)
  383. {
  384. n_recur = boost::math::itrunc(min_arg_for_recursion - zz) + 1;
  385. zz += n_recur;
  386. }
  387. else
  388. {
  389. n_recur = 0;
  390. }
  391. const T log_gamma_value = lgamma_imp(zz, pol, lanczos::undefined_lanczos());
  392. if(log_gamma_value > tools::log_max_value<T>())
  393. return policies::raise_overflow_error<T>(function, 0, pol);
  394. T gamma_value = exp(log_gamma_value);
  395. // Rescale the result using downward recursion if necessary.
  396. if(n_recur)
  397. {
  398. // The order of divides is important, if we keep subtracting 1 from zz
  399. // we DO NOT get back to z (cancellation error). Further if z < epsilon
  400. // we would end up dividing by zero. Also in order to prevent spurious
  401. // overflow with the first division, we must save dividing by |z| till last,
  402. // so the optimal order of divides is z+1, z+2, z+3...z+n_recur-1,z.
  403. zz = fabs(z) + 1;
  404. for(int k = 1; k < n_recur; ++k)
  405. {
  406. gamma_value /= zz;
  407. zz += 1;
  408. }
  409. gamma_value /= fabs(z);
  410. }
  411. // Return the result, accounting for possible negative arguments.
  412. if(b_neg)
  413. {
  414. // Provide special error analysis for:
  415. // * arguments in the neighborhood of a negative integer
  416. // * arguments exactly equal to a negative integer.
  417. // Check if the argument of tgamma is exactly equal to a negative integer.
  418. if(floor_of_z_is_equal_to_z)
  419. return policies::raise_pole_error<T>(function, "Evaluation of tgamma at a negative integer %1%.", z, pol);
  420. gamma_value *= sinpx(z);
  421. BOOST_MATH_INSTRUMENT_VARIABLE(gamma_value);
  422. const bool result_is_too_large_to_represent = ( (abs(gamma_value) < 1)
  423. && ((tools::max_value<T>() * abs(gamma_value)) < boost::math::constants::pi<T>()));
  424. if(result_is_too_large_to_represent)
  425. return policies::raise_overflow_error<T>(function, "Result of tgamma is too large to represent.", pol);
  426. gamma_value = -boost::math::constants::pi<T>() / gamma_value;
  427. BOOST_MATH_INSTRUMENT_VARIABLE(gamma_value);
  428. if(gamma_value == 0)
  429. return policies::raise_underflow_error<T>(function, "Result of tgamma is too small to represent.", pol);
  430. if((boost::math::fpclassify)(gamma_value) == static_cast<int>(FP_SUBNORMAL))
  431. return policies::raise_denorm_error<T>(function, "Result of tgamma is denormalized.", gamma_value, pol);
  432. }
  433. return gamma_value;
  434. }
  435. template <class T, class Policy>
  436. T lgamma_imp(T z, const Policy& pol, const lanczos::undefined_lanczos&, int* sign)
  437. {
  438. BOOST_MATH_STD_USING
  439. static const char* function = "boost::math::lgamma<%1%>(%1%)";
  440. // Check if the argument of lgamma is identically zero.
  441. const bool is_at_zero = (z == 0);
  442. if(is_at_zero)
  443. return policies::raise_domain_error<T>(function, "Evaluation of lgamma at zero %1%.", z, pol);
  444. const bool b_neg = (z < 0);
  445. const bool floor_of_z_is_equal_to_z = (floor(z) == z);
  446. // Special case handling of small factorials:
  447. if((!b_neg) && floor_of_z_is_equal_to_z && (z < boost::math::max_factorial<T>::value))
  448. {
  449. return log(boost::math::unchecked_factorial<T>(itrunc(z) - 1));
  450. }
  451. // Make a local, unsigned copy of the input argument.
  452. T zz((!b_neg) ? z : -z);
  453. const T min_arg_for_recursion = minimum_argument_for_bernoulli_recursion<T>();
  454. T log_gamma_value;
  455. if (zz < min_arg_for_recursion)
  456. {
  457. // Here we simply take the logarithm of tgamma(). This is somewhat
  458. // inefficient, but simple. The rationale is that the argument here
  459. // is relatively small and overflow is not expected to be likely.
  460. if (z > -tools::root_epsilon<T>())
  461. {
  462. // Reflection formula may fail if z is very close to zero, let the series
  463. // expansion for tgamma close to zero do the work:
  464. log_gamma_value = log(abs(gamma_imp(z, pol, lanczos::undefined_lanczos())));
  465. if (sign)
  466. {
  467. *sign = z < 0 ? -1 : 1;
  468. }
  469. return log_gamma_value;
  470. }
  471. else
  472. {
  473. // No issue with spurious overflow in reflection formula,
  474. // just fall through to regular code:
  475. log_gamma_value = log(abs(gamma_imp(zz, pol, lanczos::undefined_lanczos())));
  476. }
  477. }
  478. else
  479. {
  480. // Perform the Bernoulli series expansion of Stirling's approximation.
  481. const std::size_t number_of_bernoullis_b2n = highest_bernoulli_index<T>();
  482. T one_over_x_pow_two_n_minus_one = 1 / zz;
  483. const T one_over_x2 = one_over_x_pow_two_n_minus_one * one_over_x_pow_two_n_minus_one;
  484. T sum = (boost::math::bernoulli_b2n<T>(1) / 2) * one_over_x_pow_two_n_minus_one;
  485. const T target_epsilon_to_break_loop = (sum * boost::math::tools::epsilon<T>()) * T(1.0E-10F);
  486. for(std::size_t n = 2U; n < number_of_bernoullis_b2n; ++n)
  487. {
  488. one_over_x_pow_two_n_minus_one *= one_over_x2;
  489. const std::size_t n2 = static_cast<std::size_t>(n * 2U);
  490. const T term = (boost::math::bernoulli_b2n<T>(static_cast<int>(n)) * one_over_x_pow_two_n_minus_one) / (n2 * (n2 - 1U));
  491. if((n >= 8U) && (abs(term) < target_epsilon_to_break_loop))
  492. {
  493. // We have reached the desired precision in Stirling's expansion.
  494. // Adding additional terms to the sum of this divergent asymptotic
  495. // expansion will not improve the result.
  496. // Break from the loop.
  497. break;
  498. }
  499. sum += term;
  500. }
  501. // Complete Stirling's approximation.
  502. const T half_ln_two_pi = log(boost::math::constants::two_pi<T>()) / 2;
  503. log_gamma_value = ((((zz - boost::math::constants::half<T>()) * log(zz)) - zz) + half_ln_two_pi) + sum;
  504. }
  505. int sign_of_result = 1;
  506. if(b_neg)
  507. {
  508. // Provide special error analysis if the argument is exactly
  509. // equal to a negative integer.
  510. // Check if the argument of lgamma is exactly equal to a negative integer.
  511. if(floor_of_z_is_equal_to_z)
  512. return policies::raise_pole_error<T>(function, "Evaluation of lgamma at a negative integer %1%.", z, pol);
  513. T t = sinpx(z);
  514. if(t < 0)
  515. {
  516. t = -t;
  517. }
  518. else
  519. {
  520. sign_of_result = -sign_of_result;
  521. }
  522. log_gamma_value = - log_gamma_value
  523. + log(boost::math::constants::pi<T>())
  524. - log(t);
  525. }
  526. if(sign != static_cast<int*>(0U)) { *sign = sign_of_result; }
  527. return log_gamma_value;
  528. }
  529. //
  530. // This helper calculates tgamma(dz+1)-1 without cancellation errors,
  531. // used by the upper incomplete gamma with z < 1:
  532. //
  533. template <class T, class Policy, class Lanczos>
  534. T tgammap1m1_imp(T dz, Policy const& pol, const Lanczos& l)
  535. {
  536. BOOST_MATH_STD_USING
  537. typedef typename policies::precision<T,Policy>::type precision_type;
  538. typedef typename mpl::if_<
  539. mpl::or_<
  540. mpl::less_equal<precision_type, mpl::int_<0> >,
  541. mpl::greater<precision_type, mpl::int_<113> >
  542. >,
  543. typename mpl::if_<
  544. is_same<Lanczos, lanczos::lanczos24m113>,
  545. mpl::int_<113>,
  546. mpl::int_<0>
  547. >::type,
  548. typename mpl::if_<
  549. mpl::less_equal<precision_type, mpl::int_<64> >,
  550. mpl::int_<64>, mpl::int_<113> >::type
  551. >::type tag_type;
  552. T result;
  553. if(dz < 0)
  554. {
  555. if(dz < -0.5)
  556. {
  557. // Best method is simply to subtract 1 from tgamma:
  558. result = boost::math::tgamma(1+dz, pol) - 1;
  559. BOOST_MATH_INSTRUMENT_CODE(result);
  560. }
  561. else
  562. {
  563. // Use expm1 on lgamma:
  564. result = boost::math::expm1(-boost::math::log1p(dz, pol)
  565. + lgamma_small_imp<T>(dz+2, dz + 1, dz, tag_type(), pol, l));
  566. BOOST_MATH_INSTRUMENT_CODE(result);
  567. }
  568. }
  569. else
  570. {
  571. if(dz < 2)
  572. {
  573. // Use expm1 on lgamma:
  574. result = boost::math::expm1(lgamma_small_imp<T>(dz+1, dz, dz-1, tag_type(), pol, l), pol);
  575. BOOST_MATH_INSTRUMENT_CODE(result);
  576. }
  577. else
  578. {
  579. // Best method is simply to subtract 1 from tgamma:
  580. result = boost::math::tgamma(1+dz, pol) - 1;
  581. BOOST_MATH_INSTRUMENT_CODE(result);
  582. }
  583. }
  584. return result;
  585. }
  586. template <class T, class Policy>
  587. inline T tgammap1m1_imp(T dz, Policy const& pol,
  588. const ::boost::math::lanczos::undefined_lanczos& l)
  589. {
  590. BOOST_MATH_STD_USING // ADL of std names
  591. //
  592. // There should be a better solution than this, but the
  593. // algebra isn't easy for the general case....
  594. // Start by subracting 1 from tgamma:
  595. //
  596. T result = gamma_imp(T(1 + dz), pol, l) - 1;
  597. BOOST_MATH_INSTRUMENT_CODE(result);
  598. //
  599. // Test the level of cancellation error observed: we loose one bit
  600. // for each power of 2 the result is less than 1. If we would get
  601. // more bits from our most precise lgamma rational approximation,
  602. // then use that instead:
  603. //
  604. BOOST_MATH_INSTRUMENT_CODE((dz > -0.5));
  605. BOOST_MATH_INSTRUMENT_CODE((dz < 2));
  606. BOOST_MATH_INSTRUMENT_CODE((ldexp(1.0, boost::math::policies::digits<T, Policy>()) * fabs(result) < 1e34));
  607. if((dz > -0.5) && (dz < 2) && (ldexp(1.0, boost::math::policies::digits<T, Policy>()) * fabs(result) < 1e34))
  608. {
  609. result = tgammap1m1_imp(dz, pol, boost::math::lanczos::lanczos24m113());
  610. BOOST_MATH_INSTRUMENT_CODE(result);
  611. }
  612. return result;
  613. }
  614. //
  615. // Series representation for upper fraction when z is small:
  616. //
  617. template <class T>
  618. struct small_gamma2_series
  619. {
  620. typedef T result_type;
  621. small_gamma2_series(T a_, T x_) : result(-x_), x(-x_), apn(a_+1), n(1){}
  622. T operator()()
  623. {
  624. T r = result / (apn);
  625. result *= x;
  626. result /= ++n;
  627. apn += 1;
  628. return r;
  629. }
  630. private:
  631. T result, x, apn;
  632. int n;
  633. };
  634. //
  635. // calculate power term prefix (z^a)(e^-z) used in the non-normalised
  636. // incomplete gammas:
  637. //
  638. template <class T, class Policy>
  639. T full_igamma_prefix(T a, T z, const Policy& pol)
  640. {
  641. BOOST_MATH_STD_USING
  642. T prefix;
  643. T alz = a * log(z);
  644. if(z >= 1)
  645. {
  646. if((alz < tools::log_max_value<T>()) && (-z > tools::log_min_value<T>()))
  647. {
  648. prefix = pow(z, a) * exp(-z);
  649. }
  650. else if(a >= 1)
  651. {
  652. prefix = pow(z / exp(z/a), a);
  653. }
  654. else
  655. {
  656. prefix = exp(alz - z);
  657. }
  658. }
  659. else
  660. {
  661. if(alz > tools::log_min_value<T>())
  662. {
  663. prefix = pow(z, a) * exp(-z);
  664. }
  665. else if(z/a < tools::log_max_value<T>())
  666. {
  667. prefix = pow(z / exp(z/a), a);
  668. }
  669. else
  670. {
  671. prefix = exp(alz - z);
  672. }
  673. }
  674. //
  675. // This error handling isn't very good: it happens after the fact
  676. // rather than before it...
  677. //
  678. if((boost::math::fpclassify)(prefix) == (int)FP_INFINITE)
  679. return policies::raise_overflow_error<T>("boost::math::detail::full_igamma_prefix<%1%>(%1%, %1%)", "Result of incomplete gamma function is too large to represent.", pol);
  680. return prefix;
  681. }
  682. //
  683. // Compute (z^a)(e^-z)/tgamma(a)
  684. // most if the error occurs in this function:
  685. //
  686. template <class T, class Policy, class Lanczos>
  687. T regularised_gamma_prefix(T a, T z, const Policy& pol, const Lanczos& l)
  688. {
  689. BOOST_MATH_STD_USING
  690. T agh = a + static_cast<T>(Lanczos::g()) - T(0.5);
  691. T prefix;
  692. T d = ((z - a) - static_cast<T>(Lanczos::g()) + T(0.5)) / agh;
  693. if(a < 1)
  694. {
  695. //
  696. // We have to treat a < 1 as a special case because our Lanczos
  697. // approximations are optimised against the factorials with a > 1,
  698. // and for high precision types especially (128-bit reals for example)
  699. // very small values of a can give rather eroneous results for gamma
  700. // unless we do this:
  701. //
  702. // TODO: is this still required? Lanczos approx should be better now?
  703. //
  704. if(z <= tools::log_min_value<T>())
  705. {
  706. // Oh dear, have to use logs, should be free of cancellation errors though:
  707. return exp(a * log(z) - z - lgamma_imp(a, pol, l));
  708. }
  709. else
  710. {
  711. // direct calculation, no danger of overflow as gamma(a) < 1/a
  712. // for small a.
  713. return pow(z, a) * exp(-z) / gamma_imp(a, pol, l);
  714. }
  715. }
  716. else if((fabs(d*d*a) <= 100) && (a > 150))
  717. {
  718. // special case for large a and a ~ z.
  719. prefix = a * boost::math::log1pmx(d, pol) + z * static_cast<T>(0.5 - Lanczos::g()) / agh;
  720. prefix = exp(prefix);
  721. }
  722. else
  723. {
  724. //
  725. // general case.
  726. // direct computation is most accurate, but use various fallbacks
  727. // for different parts of the problem domain:
  728. //
  729. T alz = a * log(z / agh);
  730. T amz = a - z;
  731. if(((std::min)(alz, amz) <= tools::log_min_value<T>()) || ((std::max)(alz, amz) >= tools::log_max_value<T>()))
  732. {
  733. T amza = amz / a;
  734. if(((std::min)(alz, amz)/2 > tools::log_min_value<T>()) && ((std::max)(alz, amz)/2 < tools::log_max_value<T>()))
  735. {
  736. // compute square root of the result and then square it:
  737. T sq = pow(z / agh, a / 2) * exp(amz / 2);
  738. prefix = sq * sq;
  739. }
  740. else if(((std::min)(alz, amz)/4 > tools::log_min_value<T>()) && ((std::max)(alz, amz)/4 < tools::log_max_value<T>()) && (z > a))
  741. {
  742. // compute the 4th root of the result then square it twice:
  743. T sq = pow(z / agh, a / 4) * exp(amz / 4);
  744. prefix = sq * sq;
  745. prefix *= prefix;
  746. }
  747. else if((amza > tools::log_min_value<T>()) && (amza < tools::log_max_value<T>()))
  748. {
  749. prefix = pow((z * exp(amza)) / agh, a);
  750. }
  751. else
  752. {
  753. prefix = exp(alz + amz);
  754. }
  755. }
  756. else
  757. {
  758. prefix = pow(z / agh, a) * exp(amz);
  759. }
  760. }
  761. prefix *= sqrt(agh / boost::math::constants::e<T>()) / Lanczos::lanczos_sum_expG_scaled(a);
  762. return prefix;
  763. }
  764. //
  765. // And again, without Lanczos support:
  766. //
  767. template <class T, class Policy>
  768. T regularised_gamma_prefix(T a, T z, const Policy& pol, const lanczos::undefined_lanczos&)
  769. {
  770. BOOST_MATH_STD_USING
  771. T limit = (std::max)(T(10), a);
  772. T sum = detail::lower_gamma_series(a, limit, pol) / a;
  773. sum += detail::upper_gamma_fraction(a, limit, ::boost::math::policies::get_epsilon<T, Policy>());
  774. if(a < 10)
  775. {
  776. // special case for small a:
  777. T prefix = pow(z / 10, a);
  778. prefix *= exp(10-z);
  779. if(0 == prefix)
  780. {
  781. prefix = pow((z * exp((10-z)/a)) / 10, a);
  782. }
  783. prefix /= sum;
  784. return prefix;
  785. }
  786. T zoa = z / a;
  787. T amz = a - z;
  788. T alzoa = a * log(zoa);
  789. T prefix;
  790. if(((std::min)(alzoa, amz) <= tools::log_min_value<T>()) || ((std::max)(alzoa, amz) >= tools::log_max_value<T>()))
  791. {
  792. T amza = amz / a;
  793. if((amza <= tools::log_min_value<T>()) || (amza >= tools::log_max_value<T>()))
  794. {
  795. prefix = exp(alzoa + amz);
  796. }
  797. else
  798. {
  799. prefix = pow(zoa * exp(amza), a);
  800. }
  801. }
  802. else
  803. {
  804. prefix = pow(zoa, a) * exp(amz);
  805. }
  806. prefix /= sum;
  807. return prefix;
  808. }
  809. //
  810. // Upper gamma fraction for very small a:
  811. //
  812. template <class T, class Policy>
  813. inline T tgamma_small_upper_part(T a, T x, const Policy& pol, T* pgam = 0, bool invert = false, T* pderivative = 0)
  814. {
  815. BOOST_MATH_STD_USING // ADL of std functions.
  816. //
  817. // Compute the full upper fraction (Q) when a is very small:
  818. //
  819. T result;
  820. result = boost::math::tgamma1pm1(a, pol);
  821. if(pgam)
  822. *pgam = (result + 1) / a;
  823. T p = boost::math::powm1(x, a, pol);
  824. result -= p;
  825. result /= a;
  826. detail::small_gamma2_series<T> s(a, x);
  827. boost::uintmax_t max_iter = policies::get_max_series_iterations<Policy>() - 10;
  828. p += 1;
  829. if(pderivative)
  830. *pderivative = p / (*pgam * exp(x));
  831. T init_value = invert ? *pgam : 0;
  832. result = -p * tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter, (init_value - result) / p);
  833. policies::check_series_iterations<T>("boost::math::tgamma_small_upper_part<%1%>(%1%, %1%)", max_iter, pol);
  834. if(invert)
  835. result = -result;
  836. return result;
  837. }
  838. //
  839. // Upper gamma fraction for integer a:
  840. //
  841. template <class T, class Policy>
  842. inline T finite_gamma_q(T a, T x, Policy const& pol, T* pderivative = 0)
  843. {
  844. //
  845. // Calculates normalised Q when a is an integer:
  846. //
  847. BOOST_MATH_STD_USING
  848. T e = exp(-x);
  849. T sum = e;
  850. if(sum != 0)
  851. {
  852. T term = sum;
  853. for(unsigned n = 1; n < a; ++n)
  854. {
  855. term /= n;
  856. term *= x;
  857. sum += term;
  858. }
  859. }
  860. if(pderivative)
  861. {
  862. *pderivative = e * pow(x, a) / boost::math::unchecked_factorial<T>(itrunc(T(a - 1), pol));
  863. }
  864. return sum;
  865. }
  866. //
  867. // Upper gamma fraction for half integer a:
  868. //
  869. template <class T, class Policy>
  870. T finite_half_gamma_q(T a, T x, T* p_derivative, const Policy& pol)
  871. {
  872. //
  873. // Calculates normalised Q when a is a half-integer:
  874. //
  875. BOOST_MATH_STD_USING
  876. T e = boost::math::erfc(sqrt(x), pol);
  877. if((e != 0) && (a > 1))
  878. {
  879. T term = exp(-x) / sqrt(constants::pi<T>() * x);
  880. term *= x;
  881. static const T half = T(1) / 2;
  882. term /= half;
  883. T sum = term;
  884. for(unsigned n = 2; n < a; ++n)
  885. {
  886. term /= n - half;
  887. term *= x;
  888. sum += term;
  889. }
  890. e += sum;
  891. if(p_derivative)
  892. {
  893. *p_derivative = 0;
  894. }
  895. }
  896. else if(p_derivative)
  897. {
  898. // We'll be dividing by x later, so calculate derivative * x:
  899. *p_derivative = sqrt(x) * exp(-x) / constants::root_pi<T>();
  900. }
  901. return e;
  902. }
  903. //
  904. // Main incomplete gamma entry point, handles all four incomplete gamma's:
  905. //
  906. template <class T, class Policy>
  907. T gamma_incomplete_imp(T a, T x, bool normalised, bool invert,
  908. const Policy& pol, T* p_derivative)
  909. {
  910. static const char* function = "boost::math::gamma_p<%1%>(%1%, %1%)";
  911. if(a <= 0)
  912. return policies::raise_domain_error<T>(function, "Argument a to the incomplete gamma function must be greater than zero (got a=%1%).", a, pol);
  913. if(x < 0)
  914. return policies::raise_domain_error<T>(function, "Argument x to the incomplete gamma function must be >= 0 (got x=%1%).", x, pol);
  915. BOOST_MATH_STD_USING
  916. typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
  917. T result = 0; // Just to avoid warning C4701: potentially uninitialized local variable 'result' used
  918. if(a >= max_factorial<T>::value && !normalised)
  919. {
  920. //
  921. // When we're computing the non-normalized incomplete gamma
  922. // and a is large the result is rather hard to compute unless
  923. // we use logs. There are really two options - if x is a long
  924. // way from a in value then we can reliably use methods 2 and 4
  925. // below in logarithmic form and go straight to the result.
  926. // Otherwise we let the regularized gamma take the strain
  927. // (the result is unlikely to unerflow in the central region anyway)
  928. // and combine with lgamma in the hopes that we get a finite result.
  929. //
  930. if(invert && (a * 4 < x))
  931. {
  932. // This is method 4 below, done in logs:
  933. result = a * log(x) - x;
  934. if(p_derivative)
  935. *p_derivative = exp(result);
  936. result += log(upper_gamma_fraction(a, x, policies::get_epsilon<T, Policy>()));
  937. }
  938. else if(!invert && (a > 4 * x))
  939. {
  940. // This is method 2 below, done in logs:
  941. result = a * log(x) - x;
  942. if(p_derivative)
  943. *p_derivative = exp(result);
  944. T init_value = 0;
  945. result += log(detail::lower_gamma_series(a, x, pol, init_value) / a);
  946. }
  947. else
  948. {
  949. result = gamma_incomplete_imp(a, x, true, invert, pol, p_derivative);
  950. if(result == 0)
  951. {
  952. if(invert)
  953. {
  954. // Try http://functions.wolfram.com/06.06.06.0039.01
  955. result = 1 + 1 / (12 * a) + 1 / (288 * a * a);
  956. result = log(result) - a + (a - 0.5f) * log(a) + log(boost::math::constants::root_two_pi<T>());
  957. if(p_derivative)
  958. *p_derivative = exp(a * log(x) - x);
  959. }
  960. else
  961. {
  962. // This is method 2 below, done in logs, we're really outside the
  963. // range of this method, but since the result is almost certainly
  964. // infinite, we should probably be OK:
  965. result = a * log(x) - x;
  966. if(p_derivative)
  967. *p_derivative = exp(result);
  968. T init_value = 0;
  969. result += log(detail::lower_gamma_series(a, x, pol, init_value) / a);
  970. }
  971. }
  972. else
  973. {
  974. result = log(result) + boost::math::lgamma(a, pol);
  975. }
  976. }
  977. if(result > tools::log_max_value<T>())
  978. return policies::raise_overflow_error<T>(function, 0, pol);
  979. return exp(result);
  980. }
  981. BOOST_ASSERT((p_derivative == 0) || (normalised == true));
  982. bool is_int, is_half_int;
  983. bool is_small_a = (a < 30) && (a <= x + 1) && (x < tools::log_max_value<T>());
  984. if(is_small_a)
  985. {
  986. T fa = floor(a);
  987. is_int = (fa == a);
  988. is_half_int = is_int ? false : (fabs(fa - a) == 0.5f);
  989. }
  990. else
  991. {
  992. is_int = is_half_int = false;
  993. }
  994. int eval_method;
  995. if(is_int && (x > 0.6))
  996. {
  997. // calculate Q via finite sum:
  998. invert = !invert;
  999. eval_method = 0;
  1000. }
  1001. else if(is_half_int && (x > 0.2))
  1002. {
  1003. // calculate Q via finite sum for half integer a:
  1004. invert = !invert;
  1005. eval_method = 1;
  1006. }
  1007. else if((x < tools::root_epsilon<T>()) && (a > 1))
  1008. {
  1009. eval_method = 6;
  1010. }
  1011. else if(x < 0.5)
  1012. {
  1013. //
  1014. // Changeover criterion chosen to give a changeover at Q ~ 0.33
  1015. //
  1016. if(-0.4 / log(x) < a)
  1017. {
  1018. eval_method = 2;
  1019. }
  1020. else
  1021. {
  1022. eval_method = 3;
  1023. }
  1024. }
  1025. else if(x < 1.1)
  1026. {
  1027. //
  1028. // Changover here occurs when P ~ 0.75 or Q ~ 0.25:
  1029. //
  1030. if(x * 0.75f < a)
  1031. {
  1032. eval_method = 2;
  1033. }
  1034. else
  1035. {
  1036. eval_method = 3;
  1037. }
  1038. }
  1039. else
  1040. {
  1041. //
  1042. // Begin by testing whether we're in the "bad" zone
  1043. // where the result will be near 0.5 and the usual
  1044. // series and continued fractions are slow to converge:
  1045. //
  1046. bool use_temme = false;
  1047. if(normalised && std::numeric_limits<T>::is_specialized && (a > 20))
  1048. {
  1049. T sigma = fabs((x-a)/a);
  1050. if((a > 200) && (policies::digits<T, Policy>() <= 113))
  1051. {
  1052. //
  1053. // This limit is chosen so that we use Temme's expansion
  1054. // only if the result would be larger than about 10^-6.
  1055. // Below that the regular series and continued fractions
  1056. // converge OK, and if we use Temme's method we get increasing
  1057. // errors from the dominant erfc term as it's (inexact) argument
  1058. // increases in magnitude.
  1059. //
  1060. if(20 / a > sigma * sigma)
  1061. use_temme = true;
  1062. }
  1063. else if(policies::digits<T, Policy>() <= 64)
  1064. {
  1065. // Note in this zone we can't use Temme's expansion for
  1066. // types longer than an 80-bit real:
  1067. // it would require too many terms in the polynomials.
  1068. if(sigma < 0.4)
  1069. use_temme = true;
  1070. }
  1071. }
  1072. if(use_temme)
  1073. {
  1074. eval_method = 5;
  1075. }
  1076. else
  1077. {
  1078. //
  1079. // Regular case where the result will not be too close to 0.5.
  1080. //
  1081. // Changeover here occurs at P ~ Q ~ 0.5
  1082. // Note that series computation of P is about x2 faster than continued fraction
  1083. // calculation of Q, so try and use the CF only when really necessary, especially
  1084. // for small x.
  1085. //
  1086. if(x - (1 / (3 * x)) < a)
  1087. {
  1088. eval_method = 2;
  1089. }
  1090. else
  1091. {
  1092. eval_method = 4;
  1093. invert = !invert;
  1094. }
  1095. }
  1096. }
  1097. switch(eval_method)
  1098. {
  1099. case 0:
  1100. {
  1101. result = finite_gamma_q(a, x, pol, p_derivative);
  1102. if(normalised == false)
  1103. result *= boost::math::tgamma(a, pol);
  1104. break;
  1105. }
  1106. case 1:
  1107. {
  1108. result = finite_half_gamma_q(a, x, p_derivative, pol);
  1109. if(normalised == false)
  1110. result *= boost::math::tgamma(a, pol);
  1111. if(p_derivative && (*p_derivative == 0))
  1112. *p_derivative = regularised_gamma_prefix(a, x, pol, lanczos_type());
  1113. break;
  1114. }
  1115. case 2:
  1116. {
  1117. // Compute P:
  1118. result = normalised ? regularised_gamma_prefix(a, x, pol, lanczos_type()) : full_igamma_prefix(a, x, pol);
  1119. if(p_derivative)
  1120. *p_derivative = result;
  1121. if(result != 0)
  1122. {
  1123. //
  1124. // If we're going to be inverting the result then we can
  1125. // reduce the number of series evaluations by quite
  1126. // a few iterations if we set an initial value for the
  1127. // series sum based on what we'll end up subtracting it from
  1128. // at the end.
  1129. // Have to be careful though that this optimization doesn't
  1130. // lead to spurious numberic overflow. Note that the
  1131. // scary/expensive overflow checks below are more often
  1132. // than not bypassed in practice for "sensible" input
  1133. // values:
  1134. //
  1135. T init_value = 0;
  1136. bool optimised_invert = false;
  1137. if(invert)
  1138. {
  1139. init_value = (normalised ? 1 : boost::math::tgamma(a, pol));
  1140. if(normalised || (result >= 1) || (tools::max_value<T>() * result > init_value))
  1141. {
  1142. init_value /= result;
  1143. if(normalised || (a < 1) || (tools::max_value<T>() / a > init_value))
  1144. {
  1145. init_value *= -a;
  1146. optimised_invert = true;
  1147. }
  1148. else
  1149. init_value = 0;
  1150. }
  1151. else
  1152. init_value = 0;
  1153. }
  1154. result *= detail::lower_gamma_series(a, x, pol, init_value) / a;
  1155. if(optimised_invert)
  1156. {
  1157. invert = false;
  1158. result = -result;
  1159. }
  1160. }
  1161. break;
  1162. }
  1163. case 3:
  1164. {
  1165. // Compute Q:
  1166. invert = !invert;
  1167. T g;
  1168. result = tgamma_small_upper_part(a, x, pol, &g, invert, p_derivative);
  1169. invert = false;
  1170. if(normalised)
  1171. result /= g;
  1172. break;
  1173. }
  1174. case 4:
  1175. {
  1176. // Compute Q:
  1177. result = normalised ? regularised_gamma_prefix(a, x, pol, lanczos_type()) : full_igamma_prefix(a, x, pol);
  1178. if(p_derivative)
  1179. *p_derivative = result;
  1180. if(result != 0)
  1181. result *= upper_gamma_fraction(a, x, policies::get_epsilon<T, Policy>());
  1182. break;
  1183. }
  1184. case 5:
  1185. {
  1186. //
  1187. // Use compile time dispatch to the appropriate
  1188. // Temme asymptotic expansion. This may be dead code
  1189. // if T does not have numeric limits support, or has
  1190. // too many digits for the most precise version of
  1191. // these expansions, in that case we'll be calling
  1192. // an empty function.
  1193. //
  1194. typedef typename policies::precision<T, Policy>::type precision_type;
  1195. typedef typename mpl::if_<
  1196. mpl::or_<mpl::equal_to<precision_type, mpl::int_<0> >,
  1197. mpl::greater<precision_type, mpl::int_<113> > >,
  1198. mpl::int_<0>,
  1199. typename mpl::if_<
  1200. mpl::less_equal<precision_type, mpl::int_<53> >,
  1201. mpl::int_<53>,
  1202. typename mpl::if_<
  1203. mpl::less_equal<precision_type, mpl::int_<64> >,
  1204. mpl::int_<64>,
  1205. mpl::int_<113>
  1206. >::type
  1207. >::type
  1208. >::type tag_type;
  1209. result = igamma_temme_large(a, x, pol, static_cast<tag_type const*>(0));
  1210. if(x >= a)
  1211. invert = !invert;
  1212. if(p_derivative)
  1213. *p_derivative = regularised_gamma_prefix(a, x, pol, lanczos_type());
  1214. break;
  1215. }
  1216. case 6:
  1217. {
  1218. // x is so small that P is necessarily very small too,
  1219. // use http://functions.wolfram.com/GammaBetaErf/GammaRegularized/06/01/05/01/01/
  1220. result = !normalised ? pow(x, a) / (a) : pow(x, a) / boost::math::tgamma(a + 1, pol);
  1221. result *= 1 - a * x / (a + 1);
  1222. }
  1223. }
  1224. if(normalised && (result > 1))
  1225. result = 1;
  1226. if(invert)
  1227. {
  1228. T gam = normalised ? 1 : boost::math::tgamma(a, pol);
  1229. result = gam - result;
  1230. }
  1231. if(p_derivative)
  1232. {
  1233. //
  1234. // Need to convert prefix term to derivative:
  1235. //
  1236. if((x < 1) && (tools::max_value<T>() * x < *p_derivative))
  1237. {
  1238. // overflow, just return an arbitrarily large value:
  1239. *p_derivative = tools::max_value<T>() / 2;
  1240. }
  1241. *p_derivative /= x;
  1242. }
  1243. return result;
  1244. }
  1245. //
  1246. // Ratios of two gamma functions:
  1247. //
  1248. template <class T, class Policy, class Lanczos>
  1249. T tgamma_delta_ratio_imp_lanczos(T z, T delta, const Policy& pol, const Lanczos& l)
  1250. {
  1251. BOOST_MATH_STD_USING
  1252. if(z < tools::epsilon<T>())
  1253. {
  1254. //
  1255. // We get spurious numeric overflow unless we're very careful, this
  1256. // can occur either inside Lanczos::lanczos_sum(z) or in the
  1257. // final combination of terms, to avoid this, split the product up
  1258. // into 2 (or 3) parts:
  1259. //
  1260. // G(z) / G(L) = 1 / (z * G(L)) ; z < eps, L = z + delta = delta
  1261. // z * G(L) = z * G(lim) * (G(L)/G(lim)) ; lim = largest factorial
  1262. //
  1263. if(boost::math::max_factorial<T>::value < delta)
  1264. {
  1265. T ratio = tgamma_delta_ratio_imp_lanczos(delta, T(boost::math::max_factorial<T>::value - delta), pol, l);
  1266. ratio *= z;
  1267. ratio *= boost::math::unchecked_factorial<T>(boost::math::max_factorial<T>::value - 1);
  1268. return 1 / ratio;
  1269. }
  1270. else
  1271. {
  1272. return 1 / (z * boost::math::tgamma(z + delta, pol));
  1273. }
  1274. }
  1275. T zgh = static_cast<T>(z + Lanczos::g() - constants::half<T>());
  1276. T result;
  1277. if(fabs(delta) < 10)
  1278. {
  1279. result = exp((constants::half<T>() - z) * boost::math::log1p(delta / zgh, pol));
  1280. }
  1281. else
  1282. {
  1283. result = pow(zgh / (zgh + delta), z - constants::half<T>());
  1284. }
  1285. // Split the calculation up to avoid spurious overflow:
  1286. result *= Lanczos::lanczos_sum(z) / Lanczos::lanczos_sum(T(z + delta));
  1287. result *= pow(constants::e<T>() / (zgh + delta), delta);
  1288. return result;
  1289. }
  1290. //
  1291. // And again without Lanczos support this time:
  1292. //
  1293. template <class T, class Policy>
  1294. T tgamma_delta_ratio_imp_lanczos(T z, T delta, const Policy& pol, const lanczos::undefined_lanczos&)
  1295. {
  1296. BOOST_MATH_STD_USING
  1297. //
  1298. // The upper gamma fraction is *very* slow for z < 6, actually it's very
  1299. // slow to converge everywhere but recursing until z > 6 gets rid of the
  1300. // worst of it's behaviour.
  1301. //
  1302. T prefix = 1;
  1303. T zd = z + delta;
  1304. while((zd < 6) && (z < 6))
  1305. {
  1306. prefix /= z;
  1307. prefix *= zd;
  1308. z += 1;
  1309. zd += 1;
  1310. }
  1311. if(delta < 10)
  1312. {
  1313. prefix *= exp(-z * boost::math::log1p(delta / z, pol));
  1314. }
  1315. else
  1316. {
  1317. prefix *= pow(z / zd, z);
  1318. }
  1319. prefix *= pow(constants::e<T>() / zd, delta);
  1320. T sum = detail::lower_gamma_series(z, z, pol) / z;
  1321. sum += detail::upper_gamma_fraction(z, z, ::boost::math::policies::get_epsilon<T, Policy>());
  1322. T sumd = detail::lower_gamma_series(zd, zd, pol) / zd;
  1323. sumd += detail::upper_gamma_fraction(zd, zd, ::boost::math::policies::get_epsilon<T, Policy>());
  1324. sum /= sumd;
  1325. if(fabs(tools::max_value<T>() / prefix) < fabs(sum))
  1326. return policies::raise_overflow_error<T>("boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)", "Result of tgamma is too large to represent.", pol);
  1327. return sum * prefix;
  1328. }
  1329. template <class T, class Policy>
  1330. T tgamma_delta_ratio_imp(T z, T delta, const Policy& pol)
  1331. {
  1332. BOOST_MATH_STD_USING
  1333. if((z <= 0) || (z + delta <= 0))
  1334. {
  1335. // This isn't very sofisticated, or accurate, but it does work:
  1336. return boost::math::tgamma(z, pol) / boost::math::tgamma(z + delta, pol);
  1337. }
  1338. if(floor(delta) == delta)
  1339. {
  1340. if(floor(z) == z)
  1341. {
  1342. //
  1343. // Both z and delta are integers, see if we can just use table lookup
  1344. // of the factorials to get the result:
  1345. //
  1346. if((z <= max_factorial<T>::value) && (z + delta <= max_factorial<T>::value))
  1347. {
  1348. return unchecked_factorial<T>((unsigned)itrunc(z, pol) - 1) / unchecked_factorial<T>((unsigned)itrunc(T(z + delta), pol) - 1);
  1349. }
  1350. }
  1351. if(fabs(delta) < 20)
  1352. {
  1353. //
  1354. // delta is a small integer, we can use a finite product:
  1355. //
  1356. if(delta == 0)
  1357. return 1;
  1358. if(delta < 0)
  1359. {
  1360. z -= 1;
  1361. T result = z;
  1362. while(0 != (delta += 1))
  1363. {
  1364. z -= 1;
  1365. result *= z;
  1366. }
  1367. return result;
  1368. }
  1369. else
  1370. {
  1371. T result = 1 / z;
  1372. while(0 != (delta -= 1))
  1373. {
  1374. z += 1;
  1375. result /= z;
  1376. }
  1377. return result;
  1378. }
  1379. }
  1380. }
  1381. typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
  1382. return tgamma_delta_ratio_imp_lanczos(z, delta, pol, lanczos_type());
  1383. }
  1384. template <class T, class Policy>
  1385. T tgamma_ratio_imp(T x, T y, const Policy& pol)
  1386. {
  1387. BOOST_MATH_STD_USING
  1388. if((x <= 0) || (boost::math::isinf)(x))
  1389. return policies::raise_domain_error<T>("boost::math::tgamma_ratio<%1%>(%1%, %1%)", "Gamma function ratios only implemented for positive arguments (got a=%1%).", x, pol);
  1390. if((y <= 0) || (boost::math::isinf)(y))
  1391. return policies::raise_domain_error<T>("boost::math::tgamma_ratio<%1%>(%1%, %1%)", "Gamma function ratios only implemented for positive arguments (got b=%1%).", y, pol);
  1392. if(x <= tools::min_value<T>())
  1393. {
  1394. // Special case for denorms...Ugh.
  1395. T shift = ldexp(T(1), tools::digits<T>());
  1396. return shift * tgamma_ratio_imp(T(x * shift), y, pol);
  1397. }
  1398. if((x < max_factorial<T>::value) && (y < max_factorial<T>::value))
  1399. {
  1400. // Rather than subtracting values, lets just call the gamma functions directly:
  1401. return boost::math::tgamma(x, pol) / boost::math::tgamma(y, pol);
  1402. }
  1403. T prefix = 1;
  1404. if(x < 1)
  1405. {
  1406. if(y < 2 * max_factorial<T>::value)
  1407. {
  1408. // We need to sidestep on x as well, otherwise we'll underflow
  1409. // before we get to factor in the prefix term:
  1410. prefix /= x;
  1411. x += 1;
  1412. while(y >= max_factorial<T>::value)
  1413. {
  1414. y -= 1;
  1415. prefix /= y;
  1416. }
  1417. return prefix * boost::math::tgamma(x, pol) / boost::math::tgamma(y, pol);
  1418. }
  1419. //
  1420. // result is almost certainly going to underflow to zero, try logs just in case:
  1421. //
  1422. return exp(boost::math::lgamma(x, pol) - boost::math::lgamma(y, pol));
  1423. }
  1424. if(y < 1)
  1425. {
  1426. if(x < 2 * max_factorial<T>::value)
  1427. {
  1428. // We need to sidestep on y as well, otherwise we'll overflow
  1429. // before we get to factor in the prefix term:
  1430. prefix *= y;
  1431. y += 1;
  1432. while(x >= max_factorial<T>::value)
  1433. {
  1434. x -= 1;
  1435. prefix *= x;
  1436. }
  1437. return prefix * boost::math::tgamma(x, pol) / boost::math::tgamma(y, pol);
  1438. }
  1439. //
  1440. // Result will almost certainly overflow, try logs just in case:
  1441. //
  1442. return exp(boost::math::lgamma(x, pol) - boost::math::lgamma(y, pol));
  1443. }
  1444. //
  1445. // Regular case, x and y both large and similar in magnitude:
  1446. //
  1447. return boost::math::tgamma_delta_ratio(x, y - x, pol);
  1448. }
  1449. template <class T, class Policy>
  1450. T gamma_p_derivative_imp(T a, T x, const Policy& pol)
  1451. {
  1452. BOOST_MATH_STD_USING
  1453. //
  1454. // Usual error checks first:
  1455. //
  1456. if(a <= 0)
  1457. return policies::raise_domain_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", "Argument a to the incomplete gamma function must be greater than zero (got a=%1%).", a, pol);
  1458. if(x < 0)
  1459. return policies::raise_domain_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", "Argument x to the incomplete gamma function must be >= 0 (got x=%1%).", x, pol);
  1460. //
  1461. // Now special cases:
  1462. //
  1463. if(x == 0)
  1464. {
  1465. return (a > 1) ? 0 :
  1466. (a == 1) ? 1 : policies::raise_overflow_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", 0, pol);
  1467. }
  1468. //
  1469. // Normal case:
  1470. //
  1471. typedef typename lanczos::lanczos<T, Policy>::type lanczos_type;
  1472. T f1 = detail::regularised_gamma_prefix(a, x, pol, lanczos_type());
  1473. if((x < 1) && (tools::max_value<T>() * x < f1))
  1474. {
  1475. // overflow:
  1476. return policies::raise_overflow_error<T>("boost::math::gamma_p_derivative<%1%>(%1%, %1%)", 0, pol);
  1477. }
  1478. if(f1 == 0)
  1479. {
  1480. // Underflow in calculation, use logs instead:
  1481. f1 = a * log(x) - x - lgamma(a, pol) - log(x);
  1482. f1 = exp(f1);
  1483. }
  1484. else
  1485. f1 /= x;
  1486. return f1;
  1487. }
  1488. template <class T, class Policy>
  1489. inline typename tools::promote_args<T>::type
  1490. tgamma(T z, const Policy& /* pol */, const mpl::true_)
  1491. {
  1492. BOOST_FPU_EXCEPTION_GUARD
  1493. typedef typename tools::promote_args<T>::type result_type;
  1494. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1495. typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  1496. typedef typename policies::normalise<
  1497. Policy,
  1498. policies::promote_float<false>,
  1499. policies::promote_double<false>,
  1500. policies::discrete_quantile<>,
  1501. policies::assert_undefined<> >::type forwarding_policy;
  1502. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::gamma_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type()), "boost::math::tgamma<%1%>(%1%)");
  1503. }
  1504. template <class T, class Policy>
  1505. struct igamma_initializer
  1506. {
  1507. struct init
  1508. {
  1509. init()
  1510. {
  1511. typedef typename policies::precision<T, Policy>::type precision_type;
  1512. typedef typename mpl::if_<
  1513. mpl::or_<mpl::equal_to<precision_type, mpl::int_<0> >,
  1514. mpl::greater<precision_type, mpl::int_<113> > >,
  1515. mpl::int_<0>,
  1516. typename mpl::if_<
  1517. mpl::less_equal<precision_type, mpl::int_<53> >,
  1518. mpl::int_<53>,
  1519. typename mpl::if_<
  1520. mpl::less_equal<precision_type, mpl::int_<64> >,
  1521. mpl::int_<64>,
  1522. mpl::int_<113>
  1523. >::type
  1524. >::type
  1525. >::type tag_type;
  1526. do_init(tag_type());
  1527. }
  1528. template <int N>
  1529. static void do_init(const mpl::int_<N>&)
  1530. {
  1531. boost::math::gamma_p(static_cast<T>(400), static_cast<T>(400), Policy());
  1532. }
  1533. static void do_init(const mpl::int_<53>&){}
  1534. void force_instantiate()const{}
  1535. };
  1536. static const init initializer;
  1537. static void force_instantiate()
  1538. {
  1539. initializer.force_instantiate();
  1540. }
  1541. };
  1542. template <class T, class Policy>
  1543. const typename igamma_initializer<T, Policy>::init igamma_initializer<T, Policy>::initializer;
  1544. template <class T, class Policy>
  1545. struct lgamma_initializer
  1546. {
  1547. struct init
  1548. {
  1549. init()
  1550. {
  1551. typedef typename policies::precision<T, Policy>::type precision_type;
  1552. typedef typename mpl::if_<
  1553. mpl::and_<
  1554. mpl::less_equal<precision_type, mpl::int_<64> >,
  1555. mpl::greater<precision_type, mpl::int_<0> >
  1556. >,
  1557. mpl::int_<64>,
  1558. typename mpl::if_<
  1559. mpl::and_<
  1560. mpl::less_equal<precision_type, mpl::int_<113> >,
  1561. mpl::greater<precision_type, mpl::int_<0> >
  1562. >,
  1563. mpl::int_<113>, mpl::int_<0> >::type
  1564. >::type tag_type;
  1565. do_init(tag_type());
  1566. }
  1567. static void do_init(const mpl::int_<64>&)
  1568. {
  1569. boost::math::lgamma(static_cast<T>(2.5), Policy());
  1570. boost::math::lgamma(static_cast<T>(1.25), Policy());
  1571. boost::math::lgamma(static_cast<T>(1.75), Policy());
  1572. }
  1573. static void do_init(const mpl::int_<113>&)
  1574. {
  1575. boost::math::lgamma(static_cast<T>(2.5), Policy());
  1576. boost::math::lgamma(static_cast<T>(1.25), Policy());
  1577. boost::math::lgamma(static_cast<T>(1.5), Policy());
  1578. boost::math::lgamma(static_cast<T>(1.75), Policy());
  1579. }
  1580. static void do_init(const mpl::int_<0>&)
  1581. {
  1582. }
  1583. void force_instantiate()const{}
  1584. };
  1585. static const init initializer;
  1586. static void force_instantiate()
  1587. {
  1588. initializer.force_instantiate();
  1589. }
  1590. };
  1591. template <class T, class Policy>
  1592. const typename lgamma_initializer<T, Policy>::init lgamma_initializer<T, Policy>::initializer;
  1593. template <class T1, class T2, class Policy>
  1594. inline typename tools::promote_args<T1, T2>::type
  1595. tgamma(T1 a, T2 z, const Policy&, const mpl::false_)
  1596. {
  1597. BOOST_FPU_EXCEPTION_GUARD
  1598. typedef typename tools::promote_args<T1, T2>::type result_type;
  1599. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1600. // typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  1601. typedef typename policies::normalise<
  1602. Policy,
  1603. policies::promote_float<false>,
  1604. policies::promote_double<false>,
  1605. policies::discrete_quantile<>,
  1606. policies::assert_undefined<> >::type forwarding_policy;
  1607. igamma_initializer<value_type, forwarding_policy>::force_instantiate();
  1608. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  1609. detail::gamma_incomplete_imp(static_cast<value_type>(a),
  1610. static_cast<value_type>(z), false, true,
  1611. forwarding_policy(), static_cast<value_type*>(0)), "boost::math::tgamma<%1%>(%1%, %1%)");
  1612. }
  1613. template <class T1, class T2>
  1614. inline typename tools::promote_args<T1, T2>::type
  1615. tgamma(T1 a, T2 z, const mpl::false_ tag)
  1616. {
  1617. return tgamma(a, z, policies::policy<>(), tag);
  1618. }
  1619. } // namespace detail
  1620. template <class T>
  1621. inline typename tools::promote_args<T>::type
  1622. tgamma(T z)
  1623. {
  1624. return tgamma(z, policies::policy<>());
  1625. }
  1626. template <class T, class Policy>
  1627. inline typename tools::promote_args<T>::type
  1628. lgamma(T z, int* sign, const Policy&)
  1629. {
  1630. BOOST_FPU_EXCEPTION_GUARD
  1631. typedef typename tools::promote_args<T>::type result_type;
  1632. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1633. typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  1634. typedef typename policies::normalise<
  1635. Policy,
  1636. policies::promote_float<false>,
  1637. policies::promote_double<false>,
  1638. policies::discrete_quantile<>,
  1639. policies::assert_undefined<> >::type forwarding_policy;
  1640. detail::lgamma_initializer<value_type, forwarding_policy>::force_instantiate();
  1641. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::lgamma_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type(), sign), "boost::math::lgamma<%1%>(%1%)");
  1642. }
  1643. template <class T>
  1644. inline typename tools::promote_args<T>::type
  1645. lgamma(T z, int* sign)
  1646. {
  1647. return lgamma(z, sign, policies::policy<>());
  1648. }
  1649. template <class T, class Policy>
  1650. inline typename tools::promote_args<T>::type
  1651. lgamma(T x, const Policy& pol)
  1652. {
  1653. return ::boost::math::lgamma(x, 0, pol);
  1654. }
  1655. template <class T>
  1656. inline typename tools::promote_args<T>::type
  1657. lgamma(T x)
  1658. {
  1659. return ::boost::math::lgamma(x, 0, policies::policy<>());
  1660. }
  1661. template <class T, class Policy>
  1662. inline typename tools::promote_args<T>::type
  1663. tgamma1pm1(T z, const Policy& /* pol */)
  1664. {
  1665. BOOST_FPU_EXCEPTION_GUARD
  1666. typedef typename tools::promote_args<T>::type result_type;
  1667. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1668. typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  1669. typedef typename policies::normalise<
  1670. Policy,
  1671. policies::promote_float<false>,
  1672. policies::promote_double<false>,
  1673. policies::discrete_quantile<>,
  1674. policies::assert_undefined<> >::type forwarding_policy;
  1675. return policies::checked_narrowing_cast<typename remove_cv<result_type>::type, forwarding_policy>(detail::tgammap1m1_imp(static_cast<value_type>(z), forwarding_policy(), evaluation_type()), "boost::math::tgamma1pm1<%!%>(%1%)");
  1676. }
  1677. template <class T>
  1678. inline typename tools::promote_args<T>::type
  1679. tgamma1pm1(T z)
  1680. {
  1681. return tgamma1pm1(z, policies::policy<>());
  1682. }
  1683. //
  1684. // Full upper incomplete gamma:
  1685. //
  1686. template <class T1, class T2>
  1687. inline typename tools::promote_args<T1, T2>::type
  1688. tgamma(T1 a, T2 z)
  1689. {
  1690. //
  1691. // Type T2 could be a policy object, or a value, select the
  1692. // right overload based on T2:
  1693. //
  1694. typedef typename policies::is_policy<T2>::type maybe_policy;
  1695. return detail::tgamma(a, z, maybe_policy());
  1696. }
  1697. template <class T1, class T2, class Policy>
  1698. inline typename tools::promote_args<T1, T2>::type
  1699. tgamma(T1 a, T2 z, const Policy& pol)
  1700. {
  1701. return detail::tgamma(a, z, pol, mpl::false_());
  1702. }
  1703. //
  1704. // Full lower incomplete gamma:
  1705. //
  1706. template <class T1, class T2, class Policy>
  1707. inline typename tools::promote_args<T1, T2>::type
  1708. tgamma_lower(T1 a, T2 z, const Policy&)
  1709. {
  1710. BOOST_FPU_EXCEPTION_GUARD
  1711. typedef typename tools::promote_args<T1, T2>::type result_type;
  1712. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1713. // typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  1714. typedef typename policies::normalise<
  1715. Policy,
  1716. policies::promote_float<false>,
  1717. policies::promote_double<false>,
  1718. policies::discrete_quantile<>,
  1719. policies::assert_undefined<> >::type forwarding_policy;
  1720. detail::igamma_initializer<value_type, forwarding_policy>::force_instantiate();
  1721. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  1722. detail::gamma_incomplete_imp(static_cast<value_type>(a),
  1723. static_cast<value_type>(z), false, false,
  1724. forwarding_policy(), static_cast<value_type*>(0)), "tgamma_lower<%1%>(%1%, %1%)");
  1725. }
  1726. template <class T1, class T2>
  1727. inline typename tools::promote_args<T1, T2>::type
  1728. tgamma_lower(T1 a, T2 z)
  1729. {
  1730. return tgamma_lower(a, z, policies::policy<>());
  1731. }
  1732. //
  1733. // Regularised upper incomplete gamma:
  1734. //
  1735. template <class T1, class T2, class Policy>
  1736. inline typename tools::promote_args<T1, T2>::type
  1737. gamma_q(T1 a, T2 z, const Policy& /* pol */)
  1738. {
  1739. BOOST_FPU_EXCEPTION_GUARD
  1740. typedef typename tools::promote_args<T1, T2>::type result_type;
  1741. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1742. // typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  1743. typedef typename policies::normalise<
  1744. Policy,
  1745. policies::promote_float<false>,
  1746. policies::promote_double<false>,
  1747. policies::discrete_quantile<>,
  1748. policies::assert_undefined<> >::type forwarding_policy;
  1749. detail::igamma_initializer<value_type, forwarding_policy>::force_instantiate();
  1750. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  1751. detail::gamma_incomplete_imp(static_cast<value_type>(a),
  1752. static_cast<value_type>(z), true, true,
  1753. forwarding_policy(), static_cast<value_type*>(0)), "gamma_q<%1%>(%1%, %1%)");
  1754. }
  1755. template <class T1, class T2>
  1756. inline typename tools::promote_args<T1, T2>::type
  1757. gamma_q(T1 a, T2 z)
  1758. {
  1759. return gamma_q(a, z, policies::policy<>());
  1760. }
  1761. //
  1762. // Regularised lower incomplete gamma:
  1763. //
  1764. template <class T1, class T2, class Policy>
  1765. inline typename tools::promote_args<T1, T2>::type
  1766. gamma_p(T1 a, T2 z, const Policy&)
  1767. {
  1768. BOOST_FPU_EXCEPTION_GUARD
  1769. typedef typename tools::promote_args<T1, T2>::type result_type;
  1770. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1771. // typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  1772. typedef typename policies::normalise<
  1773. Policy,
  1774. policies::promote_float<false>,
  1775. policies::promote_double<false>,
  1776. policies::discrete_quantile<>,
  1777. policies::assert_undefined<> >::type forwarding_policy;
  1778. detail::igamma_initializer<value_type, forwarding_policy>::force_instantiate();
  1779. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  1780. detail::gamma_incomplete_imp(static_cast<value_type>(a),
  1781. static_cast<value_type>(z), true, false,
  1782. forwarding_policy(), static_cast<value_type*>(0)), "gamma_p<%1%>(%1%, %1%)");
  1783. }
  1784. template <class T1, class T2>
  1785. inline typename tools::promote_args<T1, T2>::type
  1786. gamma_p(T1 a, T2 z)
  1787. {
  1788. return gamma_p(a, z, policies::policy<>());
  1789. }
  1790. // ratios of gamma functions:
  1791. template <class T1, class T2, class Policy>
  1792. inline typename tools::promote_args<T1, T2>::type
  1793. tgamma_delta_ratio(T1 z, T2 delta, const Policy& /* pol */)
  1794. {
  1795. BOOST_FPU_EXCEPTION_GUARD
  1796. typedef typename tools::promote_args<T1, T2>::type result_type;
  1797. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1798. typedef typename policies::normalise<
  1799. Policy,
  1800. policies::promote_float<false>,
  1801. policies::promote_double<false>,
  1802. policies::discrete_quantile<>,
  1803. policies::assert_undefined<> >::type forwarding_policy;
  1804. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::tgamma_delta_ratio_imp(static_cast<value_type>(z), static_cast<value_type>(delta), forwarding_policy()), "boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)");
  1805. }
  1806. template <class T1, class T2>
  1807. inline typename tools::promote_args<T1, T2>::type
  1808. tgamma_delta_ratio(T1 z, T2 delta)
  1809. {
  1810. return tgamma_delta_ratio(z, delta, policies::policy<>());
  1811. }
  1812. template <class T1, class T2, class Policy>
  1813. inline typename tools::promote_args<T1, T2>::type
  1814. tgamma_ratio(T1 a, T2 b, const Policy&)
  1815. {
  1816. typedef typename tools::promote_args<T1, T2>::type result_type;
  1817. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1818. typedef typename policies::normalise<
  1819. Policy,
  1820. policies::promote_float<false>,
  1821. policies::promote_double<false>,
  1822. policies::discrete_quantile<>,
  1823. policies::assert_undefined<> >::type forwarding_policy;
  1824. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::tgamma_ratio_imp(static_cast<value_type>(a), static_cast<value_type>(b), forwarding_policy()), "boost::math::tgamma_delta_ratio<%1%>(%1%, %1%)");
  1825. }
  1826. template <class T1, class T2>
  1827. inline typename tools::promote_args<T1, T2>::type
  1828. tgamma_ratio(T1 a, T2 b)
  1829. {
  1830. return tgamma_ratio(a, b, policies::policy<>());
  1831. }
  1832. template <class T1, class T2, class Policy>
  1833. inline typename tools::promote_args<T1, T2>::type
  1834. gamma_p_derivative(T1 a, T2 x, const Policy&)
  1835. {
  1836. BOOST_FPU_EXCEPTION_GUARD
  1837. typedef typename tools::promote_args<T1, T2>::type result_type;
  1838. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  1839. typedef typename policies::normalise<
  1840. Policy,
  1841. policies::promote_float<false>,
  1842. policies::promote_double<false>,
  1843. policies::discrete_quantile<>,
  1844. policies::assert_undefined<> >::type forwarding_policy;
  1845. return policies::checked_narrowing_cast<result_type, forwarding_policy>(detail::gamma_p_derivative_imp(static_cast<value_type>(a), static_cast<value_type>(x), forwarding_policy()), "boost::math::gamma_p_derivative<%1%>(%1%, %1%)");
  1846. }
  1847. template <class T1, class T2>
  1848. inline typename tools::promote_args<T1, T2>::type
  1849. gamma_p_derivative(T1 a, T2 x)
  1850. {
  1851. return gamma_p_derivative(a, x, policies::policy<>());
  1852. }
  1853. } // namespace math
  1854. } // namespace boost
  1855. #ifdef BOOST_MSVC
  1856. # pragma warning(pop)
  1857. #endif
  1858. #include <boost/math/special_functions/detail/igamma_inverse.hpp>
  1859. #include <boost/math/special_functions/detail/gamma_inva.hpp>
  1860. #include <boost/math/special_functions/erf.hpp>
  1861. #endif // BOOST_MATH_SF_GAMMA_HPP