11#ifndef EIGEN_SPECIAL_FUNCTIONS_H
12#define EIGEN_SPECIAL_FUNCTIONS_H
15#include "./InternalHeaderCheck.h"
47template <
typename Scalar>
48struct lgamma_default_impl {
49 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(
const Scalar) {
50 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, Scalar>::value), THIS_TYPE_IS_NOT_SUPPORTED)
55template <
typename Scalar>
57 : special_function_overload_or<special_function_overloads::lgamma_fn, lgamma_default_impl<Scalar>, Scalar> {};
60#if defined(__GLIBC__) && ((__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 19) || __GLIBC__ > 2) && \
61 (defined(_DEFAULT_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
62#define EIGEN_HAS_LGAMMA_R
66#if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ < 19) || __GLIBC__ < 2) && \
67 (defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
68#define EIGEN_HAS_LGAMMA_R
72struct lgamma_impl<float> {
73 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float run(
float x) {
74#if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(__SYCL_DEVICE_ONLY__) && defined(EIGEN_HAS_LGAMMA_R) && \
77 return ::lgammaf_r(x, &dummy);
78#elif defined(EIGEN_SYCL_DEVICE_ONLY)
79 return cl::sycl::lgamma(x);
87struct lgamma_impl<double> {
88 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double run(
double x) {
89#if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(__SYCL_DEVICE_ONLY__) && defined(EIGEN_HAS_LGAMMA_R) && \
92 return ::lgamma_r(x, &dummy);
93#elif defined(EIGEN_SYCL_DEVICE_ONLY)
94 return cl::sycl::lgamma(x);
102struct lgamma_impl<long double> {
103 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
long double run(
long double x) {
104#if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(__SYCL_DEVICE_ONLY__) && defined(EIGEN_HAS_LGAMMA_R) && \
107 return ::lgammal_r(x, &dummy);
109 return std::lgamma(x);
114#undef EIGEN_HAS_LGAMMA_R
133template <
typename Scalar>
134struct digamma_impl_maybe_poly {
135 EIGEN_STATIC_ASSERT((std::is_same<Scalar, Scalar>::value ==
false), THIS_TYPE_IS_NOT_SUPPORTED)
137 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(
const Scalar) {
return Scalar(0); }
141struct digamma_impl_maybe_poly<float> {
142 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float run(
const float s) {
143 constexpr float A[] = {-4.16666666666666666667E-3f, 3.96825396825396825397E-3f, -8.33333333333333333333E-3f,
144 8.33333333333333333333E-2f};
149 return z * internal::ppolevl<float, 3>::run(z, A);
156struct digamma_impl_maybe_poly<double> {
157 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double run(
const double s) {
158 constexpr double A[] = {8.33333333333333333333E-2, -2.10927960927960927961E-2, 7.57575757575757575758E-3,
159 -4.16666666666666666667E-3, 3.96825396825396825397E-3, -8.33333333333333333333E-3,
160 8.33333333333333333333E-2};
165 return z * internal::ppolevl<double, 6>::run(z, A);
171template <
typename Scalar>
172struct digamma_default_impl {
173 EIGEN_DEVICE_FUNC
static Scalar run(Scalar x) {
231 Scalar p, q, nz, s, w, y;
232 bool negative =
false;
234 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
235 const Scalar m_pi = Scalar(EIGEN_PI);
237 const Scalar zero = Scalar(0);
238 const Scalar one = Scalar(1);
239 const Scalar half = Scalar(0.5);
246 return (std::signbit(x)) ? NumTraits<Scalar>::infinity() : -NumTraits<Scalar>::infinity();
251 p = numext::floor(q);
264 nz = m_pi / numext::tan(m_pi * nz);
274 while (s < Scalar(10)) {
279 y = digamma_impl_maybe_poly<Scalar>::run(s);
281 y = numext::log(s) - (half / s) - y - w;
283 return (negative) ? y - nz : y;
287template <
typename Scalar>
289 : special_function_overload_or<special_function_overloads::digamma_fn, digamma_default_impl<Scalar>, Scalar> {};
292struct has_erf : has_special_function_overload<special_function_overloads::erf_fn, T> {};
294struct has_erfc : has_special_function_overload<special_function_overloads::erfc_fn, T> {};
299template <
typename Scalar>
300struct generic_fast_erfc {
301 template <
typename T>
302 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& x_in);
307EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erfc<float>::run(
const T& x_in) {
308 constexpr float kClamp = 11.0f;
309 const T x = pmin<PropagateNaN>(pmax<PropagateNaN>(x_in, pset1<T>(-kClamp)), pset1<T>(kClamp));
316 constexpr float alpha[] = {5.61802298761904239654541015625e-04, -4.91381669417023658752441406250e-03,
317 2.67075151205062866210937500000e-02, -1.12800106406211853027343750000e-01,
318 3.76122951507568359375000000000e-01, -1.12837910652160644531250000000e+00};
319 const T x2 = pmul(x, x);
320 const T one = pset1<T>(1.0f);
321 const T erfc_small = pmadd(x, ppolevl<T, 5>::run(x2, alpha), one);
325 const T x_abs_gt_one_mask = pcmp_lt(one, x2);
326 if (!predux_any(x_abs_gt_one_mask))
return erfc_small;
334 constexpr float gamma[] = {1.0208116471767425537109375e-01f, 4.2920666933059692382812500e-01f,
335 3.2379078865051269531250000e-01f, 5.3971976041793823242187500e-02f};
336 constexpr float delta[] = {1.7251677811145782470703125e-02f, 3.9137163758277893066406250e-01f,
337 1.0000000000000000000000000e+00f, 6.2173241376876831054687500e-01f,
338 9.5662862062454223632812500e-02f};
339 const T x2_lo = twoprod_low(x, x, x2);
344 const T exp2_hi = pexp(pnegate(x2));
345 const T z = pnmadd(exp2_hi, x2_lo, exp2_hi);
346 const T q2 = preciprocal(x2);
347 const T num = ppolevl<T, 3>::run(q2, gamma);
348 const T denom = pmul(x, ppolevl<T, 4>::run(q2, delta));
349 const T r = pdiv(num, denom);
350 const T maybe_two = pselect(pcmp_lt(x, pset1<T>(0.0f)), pset1<T>(2.0f), pset1<T>(0.0f));
351 const T erfc_large = pmadd(z, r, maybe_two);
352 return pselect(x_abs_gt_one_mask, erfc_large, erfc_small);
360EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T erf_over_x_double_small(
const T& x2) {
366 constexpr double alpha[] = {1.9493725660006057018823477644531294572516344487667083740234375e-04,
367 1.8272566210022942682217328425053892715368419885635375976562500e-03,
368 4.5303363351690106863856044583371840417385101318359375000000000e-02,
369 1.4215015503619179981775744181504705920815467834472656250000000e-01,
370 1.1283791670955125585606992899556644260883331298828125000000000e+00};
371 constexpr double beta[] = {2.0294484101083099089526257108317963684385176748037338256835938e-05,
372 6.8117805899186819641732970609382391558028757572174072265625000e-04,
373 1.0582026056098614921752165685120417037978768348693847656250000e-02,
374 9.3252603143757495374188692949246615171432495117187500000000000e-02,
375 4.5931062818368939559832142549566924571990966796875000000000000e-01,
377 const T num_small = ppolevl<T, 4>::run(x2, alpha);
378 const T denom_small = ppolevl<T, 5>::run(x2, beta);
379 return pdiv(num_small, denom_small);
390EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T erfc_double_large(
const T& x,
const T& x2) {
391 constexpr double gamma[] = {1.5252844933226974316088642158462107545346952974796295166015625e-04,
392 1.0909912393738931124520519233556115068495273590087890625000000e-02,
393 1.0628604636755033252537572252549580298364162445068359375000000e-01,
394 3.3492472973137982217295416376146022230386734008789062500000000e-01,
395 4.5065776215933289750026347064704168587923049926757812500000000e-01,
396 2.9433039130294824659017649537418037652969360351562500000000000e-01,
397 9.8792676360600226170838311645638896152377128601074218750000000e-02,
398 1.7095935395503719655962981960328761488199234008789062500000000e-02,
399 1.4249109729504577659398023570247460156679153442382812500000000e-03,
400 4.4567378313647954771875570045835956989321857690811157226562500e-05};
401 constexpr double delta[] = {2.041985103115789845773520028160419315099716186523437500000000e-03,
402 5.316030659946043707142493417450168635696172714233398437500000e-02,
403 3.426242193784684864077405563875799998641014099121093750000000e-01,
404 8.565637124308049799026321124983951449394226074218750000000000e-01,
405 1.000000000000000000000000000000000000000000000000000000000000e+00,
406 5.968805280570776972126623149961233139038085937500000000000000e-01,
407 1.890922854723317836356244470152887515723705291748046875000000e-01,
408 3.152505418656005586885981983868987299501895904541015625000000e-02,
409 2.565085751861882583380047861965067568235099315643310546875000e-03,
410 7.899362131678837697403017248376499992446042597293853759765625e-05};
412 const T x2_lo = twoprod_low(x, x, x2);
417 const T exp2_hi = pexp(pnegate(x2));
418 const T z = pnmadd(exp2_hi, x2_lo, exp2_hi);
420 const T q2 = preciprocal(x2);
421 const T num_large = ppolevl<T, 9>::run(q2, gamma);
422 const T denom_large = pmul(x, ppolevl<T, 9>::run(q2, delta));
423 const T r = pdiv(num_large, denom_large);
424 const T maybe_two = pselect(pcmp_lt(x, pset1<T>(0.0)), pset1<T>(2.0), pset1<T>(0.0));
425 return pmadd(z, r, maybe_two);
430EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erfc<double>::run(
const T& x_in) {
433 constexpr double kClamp = 28.0;
434 const T x = pmin<PropagateNaN>(pmax<PropagateNaN>(x_in, pset1<T>(-kClamp)), pset1<T>(kClamp));
437 const T x2 = pmul(x, x);
438 const T one = pset1<T>(1.0);
439 const T erfc_small = pnmadd(x, erf_over_x_double_small(x2), one);
443 const T x_abs_gt_one_mask = pcmp_lt(one, x2);
444 if (!predux_any(x_abs_gt_one_mask))
return erfc_small;
446 const T erfc_large = erfc_double_large(x, x2);
447 return pselect(x_abs_gt_one_mask, erfc_large, erfc_small);
452 using Scalar =
typename unpacket_traits<T>::type;
453 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(
const T& x) {
return run_impl(x, std::is_same<T, Scalar>()); }
457 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_impl(
const T& x, std::false_type) {
458 return generic_fast_erfc<Scalar>::run(x);
463 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_impl(
const T& x, std::true_type) {
464 EIGEN_STATIC_ASSERT_NON_INTEGER(T)
465 return run_scalar(x, has_erfc<T>());
467 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_scalar(
const T& x, std::true_type) {
468 return special_function_overloads::erfc_fn::call<T>(x);
474 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_scalar(
const T& x, std::false_type) {
475 EIGEN_STATIC_ASSERT(has_erfc<T>::value, SCALAR_TYPE_MUST_PROVIDE_AN_ERFC_OVERLOAD_FOUND_BY_ADL_OR_IN_NAMESPACE_STD)
481struct erfc_impl<float> {
482 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float run(
const float x) {
483#if defined(EIGEN_SYCL_DEVICE_ONLY)
484 return cl::sycl::erfc(x);
486 return generic_fast_erfc<float>::run(x);
492struct erfc_impl<double> {
493 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double run(
const double x) {
494#if defined(EIGEN_SYCL_DEVICE_ONLY)
495 return cl::sycl::erfc(x);
497 return generic_fast_erfc<double>::run(x);
506template <
typename Scalar>
507struct generic_fast_erf {
508 template <
typename T>
509 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& x_in);
520EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erf<float>::run(
const T& x) {
522 constexpr float alpha[] = {2.123732201653183437883853912353515625e-06f, 2.861979592125862836837768554687500000e-04f,
523 3.658048342913389205932617187500000000e-03f, 5.243302136659622192382812500000000000e-02f,
524 1.874160766601562500000000000000000000e-01f, 1.128379106521606445312500000000000000e+00f};
527 constexpr float beta[] = {3.89185734093189239501953125000e-05f, 1.14329601638019084930419921875e-03f,
528 1.47520881146192550659179687500e-02f, 1.12945675849914550781250000000e-01f,
529 4.99425798654556274414062500000e-01f, 1.0f};
534 const T x2 = pmin(pset1<T>(16.0f), pmul(x, x));
537 T p = ppolevl<T, 5>::run(x2, alpha);
541 T q = ppolevl<T, 5>::run(x2, beta);
542 const T r = pdiv(p, q);
545 return pmax<PropagateNaN>(pmin<PropagateNaN>(r, pset1<T>(1.0f)), pset1<T>(-1.0f));
550EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erf<double>::run(
const T& x_in) {
553 constexpr double kClamp = 28.0;
554 const T x = pmin<PropagateNaN>(pmax<PropagateNaN>(x_in, pset1<T>(-kClamp)), pset1<T>(kClamp));
556 T erf_small = pmul(x, erf_over_x_double_small(x2));
560 const T one = pset1<T>(1.0);
561 const T x_abs_gt_one_mask = pcmp_lt(one, x2);
562 if (!predux_any(x_abs_gt_one_mask))
return erf_small;
565 const T erf_large = psub(one, erfc_double_large(x, x2));
566 return pselect(x_abs_gt_one_mask, erf_large, erf_small);
571 using Scalar =
typename unpacket_traits<T>::type;
572 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(
const T& x) {
return run_impl(x, std::is_same<T, Scalar>()); }
576 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_impl(
const T& x, std::false_type) {
577 return generic_fast_erf<Scalar>::run(x);
582 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_impl(
const T& x, std::true_type) {
583 EIGEN_STATIC_ASSERT_NON_INTEGER(T)
584 return run_scalar(x, has_erf<T>());
586 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_scalar(
const T& x, std::true_type) {
587 return special_function_overloads::erf_fn::call<T>(x);
593 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_scalar(
const T& x, std::false_type) {
594 EIGEN_STATIC_ASSERT(has_erf<T>::value, SCALAR_TYPE_MUST_PROVIDE_AN_ERF_OVERLOAD_FOUND_BY_ADL_OR_IN_NAMESPACE_STD)
600struct erf_impl<float> {
601 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float run(
const float x) {
602#if defined(EIGEN_SYCL_DEVICE_ONLY)
603 return cl::sycl::erf(x);
605 return generic_fast_erf<float>::run(x);
611struct erf_impl<double> {
612 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double run(
const double x) {
613#if defined(EIGEN_SYCL_DEVICE_ONLY)
614 return cl::sycl::erf(x);
616 return generic_fast_erf<double>::run(x);
676template <typename T, bool IsScalar = is_scalar<T>::value>
680struct flipsign_impl<T, false> {
681 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& should_flipsign,
const T& x) {
682 const T sign_mask = psignmask<T>();
683 const T sign_bit = pand<T>(should_flipsign, sign_mask);
684 return pxor<T>(sign_bit, x);
689struct flipsign_impl<T, true> {
690 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& should_flipsign,
const T& x) {
691 return should_flipsign == T(0) ? x : -x;
696EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T flipsign(
const T& should_flipsign,
const T& x) {
697 return flipsign_impl<T>::run(should_flipsign, x);
700template <typename T, bool IsScalar = is_scalar<T>::value>
701struct ndtri_negative_infinity_impl;
704struct ndtri_negative_infinity_impl<T, false> {
705 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& positive_infinity) {
706 return por(psignmask<T>(), positive_infinity);
711struct ndtri_negative_infinity_impl<T, true> {
712 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& positive_infinity) {
return -positive_infinity; }
719template <
typename T,
typename ScalarType>
720EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_ndtri_gt_exp_neg_two(
const T& b) {
721 const ScalarType p0[] = {ScalarType(-5.99633501014107895267e1), ScalarType(9.80010754185999661536e1),
722 ScalarType(-5.66762857469070293439e1), ScalarType(1.39312609387279679503e1),
723 ScalarType(-1.23916583867381258016e0)};
724 const ScalarType q0[] = {ScalarType(1.0),
725 ScalarType(1.95448858338141759834e0),
726 ScalarType(4.67627912898881538453e0),
727 ScalarType(8.63602421390890590575e1),
728 ScalarType(-2.25462687854119370527e2),
729 ScalarType(2.00260212380060660359e2),
730 ScalarType(-8.20372256168333339912e1),
731 ScalarType(1.59056225126211695515e1),
732 ScalarType(-1.18331621121330003142e0)};
733 const T sqrt2pi = pset1<T>(ScalarType(2.50662827463100050242e0));
734 const T half = pset1<T>(ScalarType(0.5));
735 T c, c2, ndtri_gt_exp_neg_two;
739 ndtri_gt_exp_neg_two =
740 pmadd(c, pmul(c2, pdiv(internal::ppolevl<T, 4>::run(c2, p0), internal::ppolevl<T, 8>::run(c2, q0))), c);
741 return pmul(ndtri_gt_exp_neg_two, sqrt2pi);
744template <
typename T,
typename ScalarType>
745EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_ndtri_lt_exp_neg_two(
const T& b,
const T& should_flipsign) {
749 const ScalarType p1[] = {ScalarType(4.05544892305962419923e0), ScalarType(3.15251094599893866154e1),
750 ScalarType(5.71628192246421288162e1), ScalarType(4.40805073893200834700e1),
751 ScalarType(1.46849561928858024014e1), ScalarType(2.18663306850790267539e0),
752 ScalarType(-1.40256079171354495875e-1), ScalarType(-3.50424626827848203418e-2),
753 ScalarType(-8.57456785154685413611e-4)};
754 const ScalarType q1[] = {ScalarType(1.0),
755 ScalarType(1.57799883256466749731e1),
756 ScalarType(4.53907635128879210584e1),
757 ScalarType(4.13172038254672030440e1),
758 ScalarType(1.50425385692907503408e1),
759 ScalarType(2.50464946208309415979e0),
760 ScalarType(-1.42182922854787788574e-1),
761 ScalarType(-3.80806407691578277194e-2),
762 ScalarType(-9.33259480895457427372e-4)};
766 const ScalarType p2[] = {ScalarType(3.23774891776946035970e0), ScalarType(6.91522889068984211695e0),
767 ScalarType(3.93881025292474443415e0), ScalarType(1.33303460815807542389e0),
768 ScalarType(2.01485389549179081538e-1), ScalarType(1.23716634817820021358e-2),
769 ScalarType(3.01581553508235416007e-4), ScalarType(2.65806974686737550832e-6),
770 ScalarType(6.23974539184983293730e-9)};
771 const ScalarType q2[] = {ScalarType(1.0),
772 ScalarType(6.02427039364742014255e0),
773 ScalarType(3.67983563856160859403e0),
774 ScalarType(1.37702099489081330271e0),
775 ScalarType(2.16236993594496635890e-1),
776 ScalarType(1.34204006088543189037e-2),
777 ScalarType(3.28014464682127739104e-4),
778 ScalarType(2.89247864745380683936e-6),
779 ScalarType(6.79019408009981274425e-9)};
780 const T eight = pset1<T>(ScalarType(8.0));
781 const T neg_two = pset1<T>(ScalarType(-2));
782 T x, x0, correction, z;
784 x = psqrt(pmul(neg_two, plog(b)));
785 x0 = psub(x, pdiv(plog(x), x));
788 pselect(pcmp_lt(x, eight), pdiv(internal::ppolevl<T, 8>::run(z, p1), internal::ppolevl<T, 8>::run(z, q1)),
789 pdiv(internal::ppolevl<T, 8>::run(z, p2), internal::ppolevl<T, 8>::run(z, q2)));
790 return flipsign(should_flipsign, pnmadd(z, correction, x0));
793template <
typename T,
typename ScalarType>
794EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T generic_ndtri(
const T& a) {
795 const T maxnum = pinf<T>();
796 const T neg_maxnum = ndtri_negative_infinity_impl<T>::run(maxnum);
798 const T zero = pset1<T>(ScalarType(0));
799 const T one = pset1<T>(ScalarType(1));
801 const T exp_neg_two = pset1<T>(ScalarType(0.13533528323661269189));
802 T b, ndtri, should_flipsign;
804 should_flipsign = pcmp_le(a, psub(one, exp_neg_two));
805 b = pselect(should_flipsign, a, psub(one, a));
807 ndtri = pselect(pcmp_lt(exp_neg_two, b), generic_ndtri_gt_exp_neg_two<T, ScalarType>(b),
808 generic_ndtri_lt_exp_neg_two<T, ScalarType>(b, should_flipsign));
810 return pselect(pcmp_eq(a, zero), neg_maxnum, pselect(pcmp_eq(one, a), maxnum, ndtri));
813template <
typename Scalar>
814struct ndtri_default_impl {
815 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(
const Scalar x) {
return generic_ndtri<Scalar, Scalar>(x); }
818template <
typename Scalar>
820 : special_function_overload_or<special_function_overloads::ndtri_fn, ndtri_default_impl<Scalar>, Scalar> {};
827template <
typename Scalar>
828struct cephes_helper {
829 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar machep() {
830 eigen_assert(
false &&
"machep not supported for this type");
833 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar big() {
834 eigen_assert(
false &&
"big not supported for this type");
837 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar biginv() {
838 eigen_assert(
false &&
"biginv not supported for this type");
844struct cephes_helper<float> {
845 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float machep() {
846 return NumTraits<float>::epsilon() / 2;
848 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float big() {
850 return 1.0f / (NumTraits<float>::epsilon() / 2);
852 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float biginv() {
859struct cephes_helper<double> {
860 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double machep() {
861 return NumTraits<double>::epsilon() / 2;
863 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double big() {
return 1.0 / NumTraits<double>::epsilon(); }
864 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double biginv() {
866 return NumTraits<double>::epsilon();
870enum IgammaComputationMode { VALUE, DERIVATIVE, SAMPLE_DERIVATIVE };
872template <
typename Scalar>
873EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar main_igamma_term(Scalar a, Scalar x) {
875 Scalar logax = a * numext::log(x) - x - lgamma_impl<Scalar>::run(a);
876 if (logax < -numext::log(NumTraits<Scalar>::highest()) ||
878 (numext::isnan)(logax)) {
881 return numext::exp(logax);
886template <
typename Scalar>
887EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar igamma_clamp_to_unit_interval(Scalar r) {
888 return (numext::isnan)(r) ? r : numext::mini(Scalar(1), numext::maxi(Scalar(0), r));
891template <
typename Scalar, IgammaComputationMode mode>
892EIGEN_DEVICE_FUNC
constexpr int igamma_num_iterations() {
895 return mode == VALUE ? 2000
896 : std::is_same<Scalar, float>::value ? 200
897 : std::is_same<Scalar, double>::value ? 500
903template <
typename Scalar>
904EIGEN_DEVICE_FUNC
static Scalar igamma_log1p_minus_d(Scalar d) {
905 const Scalar s = d / (Scalar(2) + d);
906 const Scalar s2 = s * s;
907 Scalar power = s * s2;
908 Scalar sum = Scalar(0);
909 for (
int k = 3; k <= 21; k += 2) {
910 sum += power / Scalar(k);
913 return Scalar(2) * sum - d * s;
935template <
typename Scalar>
936EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
bool igamma_use_temme(Scalar a) {
937 return (numext::isfinite)(a) && a >= (
sizeof(Scalar) == 4 ? Scalar(1e4) : Scalar(1e5));
940template <
typename Scalar>
941EIGEN_DEVICE_FUNC
static void igamma_temme(Scalar a, Scalar x, Scalar* p, Scalar* q) {
942 const Scalar zero = 0, one = 1, half = Scalar(0.5);
943 const Scalar d = (x - a) / a;
945 const Scalar psi = numext::abs(d) < Scalar(0.25) ? -igamma_log1p_minus_d(d) : Scalar(1);
946 if (!(Scalar(2) * psi <= Scalar(0.15) * Scalar(0.15))) {
947 *p = d > zero ? one : zero;
951 const Scalar eta = (d < zero ? -one : one) * numext::sqrt(Scalar(2) * psi);
952 const Scalar c0[] = {Scalar(-1) / Scalar(3),
953 Scalar(1) / Scalar(12),
954 Scalar(-2) / Scalar(135),
955 Scalar(1) / Scalar(864),
956 Scalar(1) / Scalar(2835),
957 Scalar(-139) / Scalar(777600),
958 Scalar(1) / Scalar(25515),
959 Scalar(-571) / Scalar(261273600),
960 Scalar(-281) / Scalar(151559100),
961 Scalar(163879) / Scalar(197522841600.0),
962 Scalar(-5221) / Scalar(29554024500.0),
963 Scalar(5246819) / Scalar(782190452736000.0),
964 Scalar(5459) / Scalar(531972441000.0)};
965 const Scalar c1[] = {Scalar(-1) / Scalar(540), Scalar(-1) / Scalar(288),
966 Scalar(1) / Scalar(378), Scalar(-77) / Scalar(77760),
967 Scalar(1) / Scalar(4860), Scalar(-1) / Scalar(2488320),
968 Scalar(-2743) / Scalar(151559100), Scalar(41969) / Scalar(5486745600.0),
969 Scalar(-11) / Scalar(6823440)};
970 Scalar c0_eta = c0[12], c1_eta = c1[8];
971 for (
int k = 11; k >= 0; --k) c0_eta = c0_eta * eta + c0[k];
972 for (
int k = 7; k >= 0; --k) c1_eta = c1_eta * eta + c1[k];
973 const Scalar a_psi = a * psi;
974 const Scalar r = numext::exp(-a_psi) / (numext::sqrt(Scalar(2 * EIGEN_PI)) * numext::sqrt(a)) * (c0_eta + c1_eta / a);
975 const Scalar half_erfc = half * erfc_impl<Scalar>::run(numext::sqrt(a_psi));
977 *q = igamma_clamp_to_unit_interval(half_erfc + r);
980 *p = igamma_clamp_to_unit_interval(half_erfc - r);
990template <
typename Scalar>
991EIGEN_DEVICE_FUNC
static int igamma_series_num_iterations(Scalar a) {
992 const Scalar n = Scalar(1.25) * numext::sqrt(-Scalar(2) * a * numext::log(cephes_helper<Scalar>::machep()));
993 return !(n > Scalar(2000)) ? 2000 : n > Scalar(100000) ? 100000 : static_cast<int>(n);
1003template <
typename Scalar>
1004EIGEN_DEVICE_FUNC
static Scalar main_igamma_term_stirling(Scalar a, Scalar x) {
1005 const Scalar d = (x - a) / a;
1007 if (d >= Scalar(0.25)) {
1008 log1pmx = numext::log1p(d) - d;
1009 }
else if (d <= Scalar(-0.25)) {
1010 log1pmx = numext::log(x / a) - d;
1012 log1pmx = igamma_log1p_minus_d(d);
1014 const Scalar logax =
1015 a * log1pmx + Scalar(0.5) * (numext::log(a) - numext::log(Scalar(2 * EIGEN_PI))) - Scalar(1) / (Scalar(12) * a);
1017 return !(logax >= -numext::log(NumTraits<Scalar>::highest())) ? Scalar(0) : numext::exp(logax);
1025template <
typename Scalar>
1026EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
bool igamma_use_stirling_main_term(Scalar a) {
1027 return Scalar(360) * NumTraits<Scalar>::epsilon() * a * a * a >= Scalar(1);
1045template <
typename Scalar>
1046EIGEN_DEVICE_FUNC
static Scalar igammac_cf_lentz(Scalar a, Scalar x,
bool* converged) {
1047 const Scalar one = 1;
1048 const Scalar tiny = (numext::numeric_limits<Scalar>::min)();
1049 const Scalar machep = cephes_helper<Scalar>::machep();
1050 Scalar b = (x - a) + one;
1051 Scalar c = one / tiny;
1054 for (
int i = 1; i <= igamma_num_iterations<Scalar, VALUE>(); i++) {
1055 const Scalar ci = Scalar(i);
1057 d = b - ci * ((ci - a) * d);
1058 if (numext::abs(d) < tiny) d = tiny;
1059 c = b - ci * ((ci - a) / c);
1060 if (numext::abs(c) < tiny) c = tiny;
1062 const Scalar delta = d * c;
1064 if (numext::abs(delta - one) <= machep) {
1073template <
typename Scalar, IgammaComputationMode mode>
1074struct igammac_cf_impl {
1084 EIGEN_DEVICE_FUNC
static Scalar run(Scalar a, Scalar x) {
1085 const Scalar zero = 0;
1086 const Scalar one = 1;
1087 const Scalar two = 2;
1088 const Scalar machep = cephes_helper<Scalar>::machep();
1089 const Scalar big = cephes_helper<Scalar>::big();
1090 const Scalar biginv = cephes_helper<Scalar>::biginv();
1092 if ((numext::isinf)(x)) {
1096 if (mode == VALUE && igamma_use_stirling_main_term(a)) {
1099 const Scalar ax_stirling = main_igamma_term_stirling<Scalar>(a, x);
1100 if (ax_stirling == zero)
return zero;
1102 const Scalar h = igammac_cf_lentz<Scalar>(a, x, &converged);
1106 if (!converged)
return NumTraits<Scalar>::quiet_NaN();
1107 return ax_stirling * h;
1110 Scalar ax = main_igamma_term<Scalar>(a, x);
1121 Scalar z = x + y + one;
1125 Scalar pkm1 = x + one;
1126 Scalar qkm1 = z * x;
1127 Scalar ans = pkm1 / qkm1;
1129 Scalar dpkm2_da = zero;
1130 Scalar dqkm2_da = zero;
1131 Scalar dpkm1_da = zero;
1132 Scalar dqkm1_da = -x;
1133 Scalar dans_da = (dpkm1_da - ans * dqkm1_da) / qkm1;
1135 for (
int i = 0; i < igamma_num_iterations<Scalar, mode>(); i++) {
1141 Scalar pk = pkm1 * z - pkm2 * yc;
1142 Scalar qk = qkm1 * z - qkm2 * yc;
1144 Scalar dpk_da = dpkm1_da * z - pkm1 - dpkm2_da * yc + pkm2 * c;
1145 Scalar dqk_da = dqkm1_da * z - qkm1 - dqkm2_da * yc + qkm2 * c;
1148 Scalar ans_prev = ans;
1151 Scalar dans_da_prev = dans_da;
1152 dans_da = (dpk_da - ans * dqk_da) / qk;
1154 if (mode == VALUE) {
1155 if (numext::abs(ans_prev - ans) <= machep * numext::abs(ans)) {
1159 if (numext::abs(dans_da - dans_da_prev) <= machep) {
1170 dpkm2_da = dpkm1_da;
1172 dqkm2_da = dqkm1_da;
1175 if (numext::abs(pk) > big) {
1189 Scalar dlogax_da = numext::log(x) - digamma_impl<Scalar>::run(a);
1190 Scalar dax_da = ax * dlogax_da;
1195 const Scalar result = ans * ax;
1196 return EIGEN_PREDICT_FALSE(!(numext::isfinite)(result)) ? NumTraits<Scalar>::quiet_NaN() : result;
1199 return ans * dax_da + dans_da * ax;
1200 case SAMPLE_DERIVATIVE:
1202 return -(dans_da + ans * dlogax_da) * x;
1207template <
typename Scalar, IgammaComputationMode mode>
1208struct igamma_series_impl {
1217 EIGEN_DEVICE_FUNC
static Scalar run(Scalar a, Scalar x) {
1218 const Scalar zero = 0;
1219 const Scalar one = 1;
1220 const Scalar machep = cephes_helper<Scalar>::machep();
1222 const bool stirling = mode == VALUE && igamma_use_stirling_main_term(a);
1223 Scalar ax = stirling ? main_igamma_term_stirling<Scalar>(a, x) : main_igamma_term<Scalar>(a, x);
1240 Scalar dc_da = zero;
1241 Scalar dans_da = zero;
1243 const int num_iterations = mode == VALUE ? igamma_series_num_iterations(a) : igamma_num_iterations<Scalar, mode>();
1244 bool converged =
false;
1245 for (
int i = 0; i < num_iterations; i++) {
1247 r = mode == VALUE ? a + Scalar(i + 1) : r + one;
1248 Scalar term = x / r;
1249 Scalar dterm_da = -x / (r * r);
1250 dc_da = term * dc_da + dterm_da * c;
1255 if (mode == VALUE) {
1256 if (c <= machep * ans) {
1261 if (numext::abs(dc_da) <= machep * numext::abs(dans_da)) {
1268 if (mode == VALUE && !converged)
return NumTraits<Scalar>::quiet_NaN();
1270 Scalar dlogax_da = numext::log(x) - digamma_impl<Scalar>::run(a + one);
1271 Scalar dax_da = ax * dlogax_da;
1277 return ans * dax_da + dans_da * ax;
1278 case SAMPLE_DERIVATIVE:
1280 return -(dans_da + ans * dlogax_da) * x / a;
1285template <
typename Scalar>
1286struct igammac_default_impl {
1287 EIGEN_DEVICE_FUNC
static Scalar run(Scalar a, Scalar x) {
1342 const Scalar zero = 0;
1343 const Scalar one = 1;
1344 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1346 if ((x < zero) || (a <= zero)) {
1351 if ((numext::isnan)(a) || (numext::isnan)(x)) {
1355 if (igamma_use_temme(a)) {
1357 igamma_temme(a, x, &p, &q);
1361 if ((x < one) || (x < a)) {
1364 return igamma_clamp_to_unit_interval(one - igamma_series_impl<Scalar, VALUE>::run(a, x));
1368 return igamma_clamp_to_unit_interval(igammac_cf_impl<Scalar, VALUE>::run(a, x));
1372template <
typename Scalar>
1373struct igammac_impl : special_function_overload_or<special_function_overloads::igammac_fn, igammac_default_impl<Scalar>,
1380template <
typename Scalar, IgammaComputationMode mode>
1381struct igamma_generic_impl {
1382 EIGEN_DEVICE_FUNC
static Scalar run(Scalar a, Scalar x) {
1391 const Scalar zero = 0;
1392 const Scalar one = 1;
1393 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1395 if ((numext::isnan)(a) || (numext::isnan)(x)) {
1399 if (x == zero)
return zero;
1401 if ((x < zero) || (a <= zero)) {
1405 if (mode == VALUE && igamma_use_temme(a)) {
1407 igamma_temme(a, x, &p, &q);
1411 if ((x > one) && (x > a)) {
1412 Scalar ret = igammac_cf_impl<Scalar, mode>::run(a, x);
1413 if (mode == VALUE) {
1416 return igamma_clamp_to_unit_interval(one - ret);
1422 Scalar ret = igamma_series_impl<Scalar, mode>::run(a, x);
1423 if (mode == VALUE) {
1426 return igamma_clamp_to_unit_interval(ret);
1432template <
typename Scalar>
1433struct igamma_impl : special_function_overload_or<special_function_overloads::igamma_fn,
1434 igamma_generic_impl<Scalar, VALUE>, Scalar, Scalar> {
1504template <
typename Scalar>
1505struct igamma_der_a_impl : special_function_overload_or<special_function_overloads::igamma_der_a_fn,
1506 igamma_generic_impl<Scalar, DERIVATIVE>, Scalar, Scalar> {
1523template <
typename Scalar>
1524struct gamma_sample_der_alpha_impl
1525 : special_function_overload_or<special_function_overloads::gamma_sample_der_alpha_fn,
1526 igamma_generic_impl<Scalar, SAMPLE_DERIVATIVE>, Scalar, Scalar> {
1571constexpr int kZetaMaxNegativeQ = 1000;
1573template <
typename Scalar>
1574struct zeta_impl_series {
1575 EIGEN_STATIC_ASSERT((std::is_same<Scalar, Scalar>::value ==
false), THIS_TYPE_IS_NOT_SUPPORTED)
1577 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(
const Scalar) {
return Scalar(0); }
1581struct zeta_impl_series<float> {
1582 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
bool run(
float& a,
float& b,
float& s,
const float x,
const float machep,
1583 const float inv_scale) {
1585 while ((i < 9) || (a <= 9.0f)) {
1588 b = numext::pow(a * inv_scale, -x);
1590 if (numext::abs(b / s) < machep)
return true;
1599struct zeta_impl_series<double> {
1600 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
bool run(
double& a,
double& b,
double& s,
const double x,
1601 const double machep,
const double inv_scale) {
1603 while ((i < 9) || (a <= 9.0)) {
1606 b = numext::pow(a * inv_scale, -x);
1608 if (numext::abs(b / s) < machep)
return true;
1616template <
typename Scalar>
1617struct zeta_default_impl {
1622 EIGEN_DEVICE_FUNC
static Scalar sum(Scalar x, Scalar q, Scalar inv_scale) {
1624 Scalar a, b, k, s, t, w;
1626 const Scalar A[] = {
1632 Scalar(-1.8924375803183791606e9),
1633 Scalar(7.47242496e10),
1634 Scalar(-2.950130727918164224e12),
1635 Scalar(1.1646782814350067249e14),
1636 Scalar(-4.5979787224074726105e15),
1637 Scalar(1.8152105401943546773e17),
1638 Scalar(-7.1661652561756670113e18)
1641 const Scalar zero = Scalar(0.0), half = Scalar(0.5), one = Scalar(1.0);
1642 const Scalar machep = cephes_helper<Scalar>::machep();
1646 s = numext::pow(q * inv_scale, -x);
1650 if (zeta_impl_series<Scalar>::run(a, b, s, x, machep, inv_scale)) {
1657 if (numext::equal_strict(b, zero)) {
1662 s += b * w / (x - one);
1667 for (i = 0; i < 12; i++) {
1672 t = numext::abs(t / s);
1686 EIGEN_DEVICE_FUNC
static Scalar log_domain_sum_scaled_by_q(Scalar x, Scalar q) {
1687 return numext::exp(numext::log(sum(x, q, Scalar(1) / q)) - x * numext::log(q));
1696 EIGEN_DEVICE_FUNC
static Scalar log_domain_sum_rescaled(Scalar x, Scalar q) {
1697 const Scalar log_q = numext::log(q);
1698 const Scalar q_c = numext::mini(q, Scalar(1) / (Scalar(16) * (numext::numeric_limits<Scalar>::min)()));
1699 const Scalar e = (numext::log(q_c) - log_q) - (x - Scalar(1)) * log_q;
1700 const Scalar log_denorm_min = numext::log((numext::numeric_limits<Scalar>::min)()) -
1701 Scalar(numext::numeric_limits<Scalar>::digits - 1) * Scalar(0.6931471805599453);
1702 if (e < log_denorm_min - Scalar(6))
return Scalar(0);
1703 return numext::exp(numext::log(sum(x, q, numext::exp(e / x))) + e);
1712 EIGEN_DEVICE_FUNC
static Scalar sum_without_underflow(Scalar x, Scalar q) {
1713 if (q >= Scalar(1) && !(numext::isinf)(x)) {
1714 const Scalar log_q = numext::log(q);
1715 const Scalar x_log_q = x * log_q;
1716 const Scalar log_min = numext::log((numext::numeric_limits<Scalar>::min)());
1717 if (x_log_q > -log_min && x_log_q + log_min > numext::log(Scalar(2) * (Scalar(1) + (x - Scalar(1)) * log_q))) {
1718 if (x >= Scalar(2) && q <= (numext::numeric_limits<Scalar>::max)() / Scalar(16)) {
1719 return log_domain_sum_scaled_by_q(x, q);
1721 return log_domain_sum_rescaled(x, q);
1724 return sum(x, q, Scalar(1));
1727 EIGEN_DEVICE_FUNC
static Scalar run(Scalar x, Scalar q) {
1791 const Scalar maxnum = NumTraits<Scalar>::infinity();
1792 const Scalar zero = Scalar(0.0), half = Scalar(0.5), one = Scalar(1.0);
1793 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1795 if (x == one)
return maxnum;
1802 if (q == numext::floor(q)) {
1803 if (numext::rint(Scalar(0.5) * x) == Scalar(0.5) * x) {
1810 r = numext::floor(p);
1811 if (p != r)
return nan;
1821 if (q < -Scalar(kZetaMaxNegativeQ)) {
1822 const Scalar f = q - numext::floor(q);
1823 const Scalar x_is_even = (numext::rint(half * x) == half * x) ? one : -one;
1824 return (sum(x, f, one) + x_is_even * sum(x, one - f, one)) - x_is_even * sum_without_underflow(x, one - q);
1827 return sum_without_underflow(x, q);
1831template <
typename Scalar>
1833 : special_function_overload_or<special_function_overloads::zeta_fn, zeta_default_impl<Scalar>, Scalar, Scalar> {};
1839template <
typename Scalar>
1840struct polygamma_default_impl {
1841 EIGEN_DEVICE_FUNC
static Scalar run(Scalar n, Scalar x) {
1842 Scalar zero = 0.0, one = 1.0;
1843 Scalar nplus = n + one;
1844 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1847 if (numext::floor(n) != n || n < zero || (numext::isinf)(n)) {
1851 else if (n == zero) {
1852 return digamma_impl<Scalar>::run(x);
1857 const Scalar half = Scalar(0.5);
1858 const bool n_is_even = numext::floor(half * n) == half * n;
1859 const Scalar sign = n_is_even ? -one : one;
1862 if (n_is_even && x < -Scalar(kZetaMaxNegativeQ) && x - numext::floor(x) == half) x = one - x;
1863 Scalar factorial = numext::exp(lgamma_impl<Scalar>::run(nplus));
1864 Scalar
zeta = zeta_impl<Scalar>::run(nplus, x);
1866 if (x == NumTraits<Scalar>::infinity())
return sign * zero;
1869 if (x > zero && x <= (numext::numeric_limits<Scalar>::max)() / Scalar(16) &&
1870 ((numext::isinf)(factorial) || !(
zeta >= (numext::numeric_limits<Scalar>::min)()))) {
1875 const Scalar log_z = numext::log(zeta_default_impl<Scalar>::sum(nplus, x, one / x));
1876 if ((numext::isfinite)(log_z)) {
1877 return sign * numext::exp(lgamma_impl<Scalar>::run(nplus) - nplus * numext::log(x) + log_z);
1882 if ((numext::isinf)(factorial) &&
zeta == zero)
return sign * zero;
1883 return sign * factorial *
zeta;
1888template <
typename Scalar>
1889struct polygamma_impl : special_function_overload_or<special_function_overloads::polygamma_fn,
1890 polygamma_default_impl<Scalar>, Scalar, Scalar> {};
1896template <
typename Scalar>
1897struct betainc_default_impl {
1898 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(Scalar, Scalar, Scalar) {
1899 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, Scalar>::value), THIS_TYPE_IS_NOT_SUPPORTED)
1972template <
typename Scalar>
1973struct betainc_impl : special_function_overload_or<special_function_overloads::betainc_fn, betainc_default_impl<Scalar>,
1974 Scalar, Scalar, Scalar> {};
1979template <
typename Scalar>
1981 EIGEN_STATIC_ASSERT((std::is_same<Scalar, float>::value || std::is_same<Scalar, double>::value),
1982 THIS_TYPE_IS_NOT_SUPPORTED)
1985 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(Scalar a, Scalar b, Scalar x, Scalar xc,
bool small_branch) {
1986 const Scalar big = cephes_helper<Scalar>::big();
1987 const Scalar machep = cephes_helper<Scalar>::machep();
1988 const Scalar biginv = cephes_helper<Scalar>::biginv();
1990 const Scalar zero = 0;
1991 const Scalar one = 1;
1992 const Scalar two = 2;
1994 Scalar xk, pk, pkm1, pkm2, qk, qkm1, qkm2;
1995 Scalar k1, k2, k3, k4, k5, k6, k7, k8, k26update;
1999 constexpr int num_iters = (std::is_same<Scalar, float>::value) ? 100 : 300;
2000 const Scalar thresh = (std::is_same<Scalar, float>::value) ? machep : Scalar(3) * machep;
2001 Scalar r = (std::is_same<Scalar, float>::value) ? zero : one;
2034 xk = -(x * k1 * k2) / (k3 * k4);
2035 pk = pkm1 + pkm2 * xk;
2036 qk = qkm1 + qkm2 * xk;
2042 xk = (x * k5 * k6) / (k7 * k8);
2043 pk = pkm1 + pkm2 * xk;
2044 qk = qkm1 + qkm2 * xk;
2052 if (numext::abs(ans - r) < numext::abs(r) * thresh) {
2067 if ((numext::abs(qk) + numext::abs(pk)) > big) {
2073 if ((numext::abs(qk) < biginv) || (numext::abs(pk) < biginv)) {
2079 }
while (++n < num_iters);
2092template <
typename Scalar>
2093EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
bool betainc_use_stirling_ratio(Scalar a, Scalar b) {
2094 const Scalar big = numext::maxi(a, b);
2095 return big * NumTraits<Scalar>::epsilon() >= Scalar(1.0 / 64) && Scalar(4) * numext::mini(a, b) <= big;
2101template <
typename Scalar>
2102EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar betainc_stirling_ratio(Scalar a, Scalar b) {
2103 const Scalar M = numext::maxi(a, b);
2104 const Scalar m = numext::mini(a, b);
2105 const Scalar sum = M + m;
2106 const Scalar sum_inv = Scalar(1) / sum;
2107 const Scalar M_inv = Scalar(1) / M;
2108 Scalar d = m * numext::log(M) + (sum - Scalar(0.5)) * numext::log1p(m / M) - m;
2109 d += (Scalar(1) / Scalar(12)) * (sum_inv - M_inv) -
2110 (Scalar(1) / Scalar(360)) * (sum_inv * sum_inv * sum_inv - M_inv * M_inv * M_inv);
2111 return d - lgamma_impl<Scalar>::run(m);
2115template <
typename Scalar>
2116struct betainc_helper {};
2119struct betainc_helper<float> {
2121 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float incbsa(
float aa,
float bb,
float xx) {
2122 float ans, a, b, t, x, onemx, xc, logx;
2123 bool reversed_a_b =
false;
2127 const bool onemx_is_one = (onemx == 1.0f);
2130 if (xx > (aa / (aa + bb))) {
2131 reversed_a_b =
true;
2144 const bool degenerate = reversed_a_b && onemx_is_one;
2145 xc = degenerate ? t : 1.0f - x;
2146 logx = degenerate ? numext::log1p(-xx) : numext::log(x);
2150 if (numext::abs(b * x / a) < 0.3f) {
2151 t = betainc_helper<float>::incbps(a, b, x, xc, logx, degenerate ? numext::log(t) : numext::log1p(-x),
2153 if (reversed_a_b) t = 1.0f - t;
2158 const float log_t = (onemx_is_one && !reversed_a_b) ? numext::log1p(-xx) : numext::log(t);
2159 ans = x * (a + b - 2.0f) / (a - 1.0f);
2161 ans = incbeta_cfe<float>::run(a, b, x, xc,
true );
2164 ans = incbeta_cfe<float>::run(a, b, x, xc,
false );
2165 t = (b - 1.0f) * log_t;
2168 if (onemx_is_one && betainc_use_stirling_ratio(a, b)) {
2169 t += a * logx + betainc_stirling_ratio(a, b);
2171 t += a * logx + lgamma_impl<float>::run(a + b) - lgamma_impl<float>::run(a) - lgamma_impl<float>::run(b);
2173 t += numext::log(ans / a);
2176 if (reversed_a_b) t = 1.0f - t;
2180 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float incbps(
float a,
float b,
float x,
float xc,
float logx,
2181 float logxc,
bool xc_rounds_to_one) {
2183 const float machep = cephes_helper<float>::machep();
2185 y = a * logx + (b - 1.0f) * logxc - numext::log(a);
2186 if (xc_rounds_to_one && betainc_use_stirling_ratio(a, b)) {
2187 y += betainc_stirling_ratio(a, b);
2189 y -= lgamma_impl<float>::run(a) + lgamma_impl<float>::run(b);
2190 y += lgamma_impl<float>::run(a + b);
2207 }
while (numext::abs(u) > machep && ++n < (1 << 24));
2209 return numext::exp(y) * (1.0f + s);
2214struct betainc_impl<float> {
2215 EIGEN_DEVICE_FUNC
static float run(
float a,
float b,
float x) {
2216 const float nan = NumTraits<float>::quiet_NaN();
2219 if ((numext::isnan)(a) || (numext::isnan)(b) || (numext::isnan)(x))
return nan;
2220 if (a == 0.0f && b == 0.0f)
return nan;
2221 if (x < 0.0f || x > 1.0f)
return nan;
2222 if (a < 0.0f)
return nan;
2223 if (b < 0.0f)
return nan;
2224 if (a == 0.0f)
return 1.0f;
2225 if (b == 0.0f)
return 0.0f;
2226 if (x == 0.0f)
return 0.0f;
2227 if (x == 1.0f)
return 1.0f;
2232 ans = betainc_helper<float>::incbsa(a + 1.0f, b, x);
2233 if (1.0f - x == 1.0f && betainc_use_stirling_ratio(a + 1.0f, b)) {
2235 t = a * numext::log(x) + b * numext::log1p(-x) + betainc_stirling_ratio(a + 1.0f, b) - numext::log(a + b);
2237 t = a * numext::log(x) + b * numext::log1p(-x) + lgamma_impl<float>::run(a + b) -
2238 lgamma_impl<float>::run(a + 1.0f) - lgamma_impl<float>::run(b);
2240 return (ans + numext::exp(t));
2242 return betainc_helper<float>::incbsa(a, b, x);
2248struct betainc_helper<double> {
2249 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double incbps(
double a,
double b,
double x) {
2250 const double machep = cephes_helper<double>::machep();
2252 double s, t, u, v, n, t1, z, ai;
2262 while (numext::abs(v) > z) {
2263 u = (n - b) * x / n;
2272 u = a * numext::log(x);
2280 if (1.0 - x == 1.0 && betainc_use_stirling_ratio(a, b)) {
2281 t = betainc_stirling_ratio(a, b) + u + numext::log(s);
2283 t = lgamma_impl<double>::run(a + b) - lgamma_impl<double>::run(a) - lgamma_impl<double>::run(b) + u +
2286 return numext::exp(t);
2291struct betainc_impl<double> {
2292 EIGEN_DEVICE_FUNC
static double run(
double aa,
double bb,
double xx) {
2293 const double nan = NumTraits<double>::quiet_NaN();
2294 const double machep = cephes_helper<double>::machep();
2295 double a, b, t, x, xc, w, y;
2296 bool reversed_a_b =
false;
2298 if ((numext::isnan)(aa) || (numext::isnan)(bb) || (numext::isnan)(xx))
return nan;
2299 if (aa == 0.0 && bb == 0.0)
return nan;
2300 if (xx < 0.0 || xx > 1.0)
return nan;
2301 if (aa < 0.0)
return nan;
2302 if (bb < 0.0)
return nan;
2303 if (aa == 0.0)
return 1.0;
2304 if (bb == 0.0)
return 0.0;
2305 if (xx == 0.0)
return 0.0;
2306 if (xx == 1.0)
return 1.0;
2309 if ((bb * xx) <= 1.0 && xx <= 0.95) {
2310 return betainc_helper<double>::incbps(aa, bb, xx);
2317 const bool w_is_one = (w == 1.0);
2318 if (xx > (aa / (aa + bb))) {
2319 reversed_a_b =
true;
2331 if (reversed_a_b && (b * x) <= 1.0 && x <= 0.95) {
2332 t = betainc_helper<double>::incbps(a, b, x);
2342 y = x * (a + b - 2.0) - (a - 1.0);
2344 w = incbeta_cfe<double>::run(a, b, x, reversed_a_b && w_is_one ? xc : 1.0 - x,
true );
2346 w = incbeta_cfe<double>::run(a, b, x, reversed_a_b && w_is_one ? xc : 1.0 - x,
false ) / xc;
2353 y = a * ((reversed_a_b && w_is_one) ? numext::log1p(-xx) : numext::log(x));
2354 t = b * ((xc < 1.0) ? numext::log(xc) : numext::log1p(-xx));
2367 if (w_is_one && betainc_use_stirling_ratio(a, b)) {
2368 y += t + betainc_stirling_ratio(a, b);
2370 y += t + lgamma_impl<double>::run(a + b) - lgamma_impl<double>::run(a) - lgamma_impl<double>::run(b);
2372 y += numext::log(w / a);
2393template <
typename Scalar>
2394EIGEN_DEVICE_FUNC
inline auto lgamma(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(lgamma, Scalar)::run(x)) {
2395 return EIGEN_MATHFUNC_IMPL(lgamma, Scalar)::run(x);
2398template <
typename Scalar>
2399EIGEN_DEVICE_FUNC
inline auto digamma(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(digamma, Scalar)::run(x)) {
2400 return EIGEN_MATHFUNC_IMPL(digamma, Scalar)::run(x);
2403template <
typename Scalar>
2404EIGEN_DEVICE_FUNC
inline auto zeta(
const Scalar& x,
const Scalar& q)
2405 ->
decltype(EIGEN_MATHFUNC_IMPL(zeta, Scalar)::run(x, q)) {
2406 return EIGEN_MATHFUNC_IMPL(zeta, Scalar)::run(x, q);
2409template <
typename Scalar>
2410EIGEN_DEVICE_FUNC
inline auto polygamma(
const Scalar& n,
const Scalar& x)
2411 ->
decltype(EIGEN_MATHFUNC_IMPL(polygamma, Scalar)::run(n, x)) {
2412 return EIGEN_MATHFUNC_IMPL(polygamma, Scalar)::run(n, x);
2415template <
typename Scalar>
2416EIGEN_DEVICE_FUNC
inline auto erf(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(erf, Scalar)::run(x)) {
2417 return EIGEN_MATHFUNC_IMPL(erf, Scalar)::run(x);
2420template <
typename Scalar>
2421EIGEN_DEVICE_FUNC
inline auto erfc(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(erfc, Scalar)::run(x)) {
2422 return EIGEN_MATHFUNC_IMPL(erfc, Scalar)::run(x);
2425template <
typename Scalar>
2426EIGEN_DEVICE_FUNC
inline auto ndtri(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(ndtri, Scalar)::run(x)) {
2427 return EIGEN_MATHFUNC_IMPL(ndtri, Scalar)::run(x);
2430template <
typename Scalar>
2431EIGEN_DEVICE_FUNC
inline auto igamma(
const Scalar& a,
const Scalar& x)
2432 ->
decltype(EIGEN_MATHFUNC_IMPL(igamma, Scalar)::run(a, x)) {
2433 return EIGEN_MATHFUNC_IMPL(igamma, Scalar)::run(a, x);
2436template <
typename Scalar>
2437EIGEN_DEVICE_FUNC
inline auto igamma_der_a(
const Scalar& a,
const Scalar& x)
2438 ->
decltype(EIGEN_MATHFUNC_IMPL(igamma_der_a, Scalar)::run(a, x)) {
2439 return EIGEN_MATHFUNC_IMPL(igamma_der_a, Scalar)::run(a, x);
2442template <
typename Scalar>
2443EIGEN_DEVICE_FUNC
inline auto gamma_sample_der_alpha(
const Scalar& a,
const Scalar& x)
2444 ->
decltype(EIGEN_MATHFUNC_IMPL(gamma_sample_der_alpha, Scalar)::run(a, x)) {
2445 return EIGEN_MATHFUNC_IMPL(gamma_sample_der_alpha, Scalar)::run(a, x);
2448template <
typename Scalar>
2449EIGEN_DEVICE_FUNC
inline auto igammac(
const Scalar& a,
const Scalar& x)
2450 ->
decltype(EIGEN_MATHFUNC_IMPL(igammac, Scalar)::run(a, x)) {
2451 return EIGEN_MATHFUNC_IMPL(igammac, Scalar)::run(a, x);
2454template <
typename Scalar>
2455EIGEN_DEVICE_FUNC
inline auto betainc(
const Scalar& a,
const Scalar& b,
const Scalar& x)
2456 ->
decltype(EIGEN_MATHFUNC_IMPL(betainc, Scalar)::run(a, b, x)) {
2457 return EIGEN_MATHFUNC_IMPL(betainc, Scalar)::run(a, b, x);
Namespace containing all symbols from the Eigen library.
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_zeta_op< typename DerivedX::Scalar >, const DerivedX, const DerivedQ > zeta(const Eigen::ArrayBase< DerivedX > &x, const Eigen::ArrayBase< DerivedQ > &q)
Definition SpecialFunctionsArrayAPI.h:141