11#ifndef EIGEN_SPECIAL_FUNCTIONS_H
12#define EIGEN_SPECIAL_FUNCTIONS_H
15#include "./InternalHeaderCheck.h"
47template <
typename Scalar>
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)
56#if defined(__GLIBC__) && ((__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 19) || __GLIBC__ > 2) && \
57 (defined(_DEFAULT_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
58#define EIGEN_HAS_LGAMMA_R
62#if defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ < 19) || __GLIBC__ < 2) && \
63 (defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
64#define EIGEN_HAS_LGAMMA_R
68struct lgamma_impl<float> {
69 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float run(
float x) {
70#if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(__SYCL_DEVICE_ONLY__) && defined(EIGEN_HAS_LGAMMA_R) && \
73 return ::lgammaf_r(x, &dummy);
74#elif defined(EIGEN_SYCL_DEVICE_ONLY)
75 return cl::sycl::lgamma(x);
83struct lgamma_impl<double> {
84 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double run(
double x) {
85#if !defined(EIGEN_GPU_COMPILE_PHASE) && !defined(__SYCL_DEVICE_ONLY__) && defined(EIGEN_HAS_LGAMMA_R) && \
88 return ::lgamma_r(x, &dummy);
89#elif defined(EIGEN_SYCL_DEVICE_ONLY)
90 return cl::sycl::lgamma(x);
97#undef EIGEN_HAS_LGAMMA_R
116template <
typename Scalar>
117struct digamma_impl_maybe_poly {
118 EIGEN_STATIC_ASSERT((std::is_same<Scalar, Scalar>::value ==
false), THIS_TYPE_IS_NOT_SUPPORTED)
120 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(
const Scalar) {
return Scalar(0); }
124struct digamma_impl_maybe_poly<float> {
125 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float run(
const float s) {
126 constexpr float A[] = {-4.16666666666666666667E-3f, 3.96825396825396825397E-3f, -8.33333333333333333333E-3f,
127 8.33333333333333333333E-2f};
132 return z * internal::ppolevl<float, 3>::run(z, A);
139struct digamma_impl_maybe_poly<double> {
140 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double run(
const double s) {
141 constexpr double A[] = {8.33333333333333333333E-2, -2.10927960927960927961E-2, 7.57575757575757575758E-3,
142 -4.16666666666666666667E-3, 3.96825396825396825397E-3, -8.33333333333333333333E-3,
143 8.33333333333333333333E-2};
148 return z * internal::ppolevl<double, 6>::run(z, A);
154template <
typename Scalar>
156 EIGEN_DEVICE_FUNC
static Scalar run(Scalar x) {
214 Scalar p, q, nz, s, w, y;
215 bool negative =
false;
217 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
218 const Scalar m_pi = Scalar(EIGEN_PI);
220 const Scalar zero = Scalar(0);
221 const Scalar one = Scalar(1);
222 const Scalar half = Scalar(0.5);
229 return (std::signbit(x)) ? NumTraits<Scalar>::infinity() : -NumTraits<Scalar>::infinity();
234 p = numext::floor(q);
247 nz = m_pi / numext::tan(m_pi * nz);
257 while (s < Scalar(10)) {
262 y = digamma_impl_maybe_poly<Scalar>::run(s);
264 y = numext::log(s) - (half / s) - y - w;
266 return (negative) ? y - nz : y;
273namespace unqualified_erf {
277auto test_erf(
int) ->
decltype(void(erf(std::declval<const T&>())), std::true_type{});
279std::false_type test_erf(...);
281auto test_erfc(
int) ->
decltype(void(erfc(std::declval<const T&>())), std::true_type{});
283std::false_type test_erfc(...);
287struct has_erf : decltype(unqualified_erf::test_erf<T>(0)) {};
289struct has_erfc : decltype(unqualified_erf::test_erfc<T>(0)) {};
294template <
typename Scalar>
295struct generic_fast_erfc {
296 template <
typename T>
297 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& x_in);
302EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erfc<float>::run(
const T& x_in) {
303 constexpr float kClamp = 11.0f;
304 const T x = pmin<PropagateNaN>(pmax<PropagateNaN>(x_in, pset1<T>(-kClamp)), pset1<T>(kClamp));
311 constexpr float alpha[] = {5.61802298761904239654541015625e-04, -4.91381669417023658752441406250e-03,
312 2.67075151205062866210937500000e-02, -1.12800106406211853027343750000e-01,
313 3.76122951507568359375000000000e-01, -1.12837910652160644531250000000e+00};
314 const T x2 = pmul(x, x);
315 const T one = pset1<T>(1.0f);
316 const T erfc_small = pmadd(x, ppolevl<T, 5>::run(x2, alpha), one);
320 const T x_abs_gt_one_mask = pcmp_lt(one, x2);
321 if (!predux_any(x_abs_gt_one_mask))
return erfc_small;
329 constexpr float gamma[] = {1.0208116471767425537109375e-01f, 4.2920666933059692382812500e-01f,
330 3.2379078865051269531250000e-01f, 5.3971976041793823242187500e-02f};
331 constexpr float delta[] = {1.7251677811145782470703125e-02f, 3.9137163758277893066406250e-01f,
332 1.0000000000000000000000000e+00f, 6.2173241376876831054687500e-01f,
333 9.5662862062454223632812500e-02f};
334 const T x2_lo = twoprod_low(x, x, x2);
339 const T exp2_hi = pexp(pnegate(x2));
340 const T z = pnmadd(exp2_hi, x2_lo, exp2_hi);
341 const T q2 = preciprocal(x2);
342 const T num = ppolevl<T, 3>::run(q2, gamma);
343 const T denom = pmul(x, ppolevl<T, 4>::run(q2, delta));
344 const T r = pdiv(num, denom);
345 const T maybe_two = pselect(pcmp_lt(x, pset1<T>(0.0f)), pset1<T>(2.0f), pset1<T>(0.0f));
346 const T erfc_large = pmadd(z, r, maybe_two);
347 return pselect(x_abs_gt_one_mask, erfc_large, erfc_small);
355EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T erf_over_x_double_small(
const T& x2) {
361 constexpr double alpha[] = {1.9493725660006057018823477644531294572516344487667083740234375e-04,
362 1.8272566210022942682217328425053892715368419885635375976562500e-03,
363 4.5303363351690106863856044583371840417385101318359375000000000e-02,
364 1.4215015503619179981775744181504705920815467834472656250000000e-01,
365 1.1283791670955125585606992899556644260883331298828125000000000e+00};
366 constexpr double beta[] = {2.0294484101083099089526257108317963684385176748037338256835938e-05,
367 6.8117805899186819641732970609382391558028757572174072265625000e-04,
368 1.0582026056098614921752165685120417037978768348693847656250000e-02,
369 9.3252603143757495374188692949246615171432495117187500000000000e-02,
370 4.5931062818368939559832142549566924571990966796875000000000000e-01,
372 const T num_small = ppolevl<T, 4>::run(x2, alpha);
373 const T denom_small = ppolevl<T, 5>::run(x2, beta);
374 return pdiv(num_small, denom_small);
385EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T erfc_double_large(
const T& x,
const T& x2) {
386 constexpr double gamma[] = {1.5252844933226974316088642158462107545346952974796295166015625e-04,
387 1.0909912393738931124520519233556115068495273590087890625000000e-02,
388 1.0628604636755033252537572252549580298364162445068359375000000e-01,
389 3.3492472973137982217295416376146022230386734008789062500000000e-01,
390 4.5065776215933289750026347064704168587923049926757812500000000e-01,
391 2.9433039130294824659017649537418037652969360351562500000000000e-01,
392 9.8792676360600226170838311645638896152377128601074218750000000e-02,
393 1.7095935395503719655962981960328761488199234008789062500000000e-02,
394 1.4249109729504577659398023570247460156679153442382812500000000e-03,
395 4.4567378313647954771875570045835956989321857690811157226562500e-05};
396 constexpr double delta[] = {2.041985103115789845773520028160419315099716186523437500000000e-03,
397 5.316030659946043707142493417450168635696172714233398437500000e-02,
398 3.426242193784684864077405563875799998641014099121093750000000e-01,
399 8.565637124308049799026321124983951449394226074218750000000000e-01,
400 1.000000000000000000000000000000000000000000000000000000000000e+00,
401 5.968805280570776972126623149961233139038085937500000000000000e-01,
402 1.890922854723317836356244470152887515723705291748046875000000e-01,
403 3.152505418656005586885981983868987299501895904541015625000000e-02,
404 2.565085751861882583380047861965067568235099315643310546875000e-03,
405 7.899362131678837697403017248376499992446042597293853759765625e-05};
407 const T x2_lo = twoprod_low(x, x, x2);
412 const T exp2_hi = pexp(pnegate(x2));
413 const T z = pnmadd(exp2_hi, x2_lo, exp2_hi);
415 const T q2 = preciprocal(x2);
416 const T num_large = ppolevl<T, 9>::run(q2, gamma);
417 const T denom_large = pmul(x, ppolevl<T, 9>::run(q2, delta));
418 const T r = pdiv(num_large, denom_large);
419 const T maybe_two = pselect(pcmp_lt(x, pset1<T>(0.0)), pset1<T>(2.0), pset1<T>(0.0));
420 return pmadd(z, r, maybe_two);
425EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erfc<double>::run(
const T& x_in) {
428 constexpr double kClamp = 28.0;
429 const T x = pmin<PropagateNaN>(pmax<PropagateNaN>(x_in, pset1<T>(-kClamp)), pset1<T>(kClamp));
432 const T x2 = pmul(x, x);
433 const T one = pset1<T>(1.0);
434 const T erfc_small = pnmadd(x, erf_over_x_double_small(x2), one);
438 const T x_abs_gt_one_mask = pcmp_lt(one, x2);
439 if (!predux_any(x_abs_gt_one_mask))
return erfc_small;
441 const T erfc_large = erfc_double_large(x, x2);
442 return pselect(x_abs_gt_one_mask, erfc_large, erfc_small);
447 using Scalar =
typename unpacket_traits<T>::type;
448 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(
const T& x) {
return run_impl(x, std::is_same<T, Scalar>()); }
452 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_impl(
const T& x, std::false_type) {
453 return generic_fast_erfc<Scalar>::run(x);
458 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_impl(
const T& x, std::true_type) {
459 EIGEN_STATIC_ASSERT_NON_INTEGER(T)
460 return run_scalar(x, has_erfc<T>());
462 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_scalar(
const T& x, std::true_type) {
463 EIGEN_USING_STD(erfc);
470 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_scalar(
const T& x, std::false_type) {
471 EIGEN_STATIC_ASSERT(has_erfc<T>::value, SCALAR_TYPE_MUST_PROVIDE_AN_ERFC_OVERLOAD_FOUND_BY_ADL_OR_IN_NAMESPACE_STD)
477struct erfc_impl<float> {
478 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float run(
const float x) {
479#if defined(EIGEN_SYCL_DEVICE_ONLY)
480 return cl::sycl::erfc(x);
482 return generic_fast_erfc<float>::run(x);
488struct erfc_impl<double> {
489 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double run(
const double x) {
490#if defined(EIGEN_SYCL_DEVICE_ONLY)
491 return cl::sycl::erfc(x);
493 return generic_fast_erfc<double>::run(x);
502template <
typename Scalar>
503struct generic_fast_erf {
504 template <
typename T>
505 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& x_in);
516EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erf<float>::run(
const T& x) {
518 constexpr float alpha[] = {2.123732201653183437883853912353515625e-06f, 2.861979592125862836837768554687500000e-04f,
519 3.658048342913389205932617187500000000e-03f, 5.243302136659622192382812500000000000e-02f,
520 1.874160766601562500000000000000000000e-01f, 1.128379106521606445312500000000000000e+00f};
523 constexpr float beta[] = {3.89185734093189239501953125000e-05f, 1.14329601638019084930419921875e-03f,
524 1.47520881146192550659179687500e-02f, 1.12945675849914550781250000000e-01f,
525 4.99425798654556274414062500000e-01f, 1.0f};
530 const T x2 = pmin(pset1<T>(16.0f), pmul(x, x));
533 T p = ppolevl<T, 5>::run(x2, alpha);
537 T q = ppolevl<T, 5>::run(x2, beta);
538 const T r = pdiv(p, q);
541 return pmax<PropagateNaN>(pmin<PropagateNaN>(r, pset1<T>(1.0f)), pset1<T>(-1.0f));
546EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erf<double>::run(
const T& x_in) {
549 constexpr double kClamp = 28.0;
550 const T x = pmin<PropagateNaN>(pmax<PropagateNaN>(x_in, pset1<T>(-kClamp)), pset1<T>(kClamp));
552 T erf_small = pmul(x, erf_over_x_double_small(x2));
556 const T one = pset1<T>(1.0);
557 const T x_abs_gt_one_mask = pcmp_lt(one, x2);
558 if (!predux_any(x_abs_gt_one_mask))
return erf_small;
561 const T erf_large = psub(one, erfc_double_large(x, x2));
562 return pselect(x_abs_gt_one_mask, erf_large, erf_small);
567 using Scalar =
typename unpacket_traits<T>::type;
568 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run(
const T& x) {
return run_impl(x, std::is_same<T, Scalar>()); }
572 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_impl(
const T& x, std::false_type) {
573 return generic_fast_erf<Scalar>::run(x);
578 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_impl(
const T& x, std::true_type) {
579 EIGEN_STATIC_ASSERT_NON_INTEGER(T)
580 return run_scalar(x, has_erf<T>());
582 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_scalar(
const T& x, std::true_type) {
583 EIGEN_USING_STD(erf);
590 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE T run_scalar(
const T& x, std::false_type) {
591 EIGEN_STATIC_ASSERT(has_erf<T>::value, SCALAR_TYPE_MUST_PROVIDE_AN_ERF_OVERLOAD_FOUND_BY_ADL_OR_IN_NAMESPACE_STD)
597struct erf_impl<float> {
598 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float run(
const float x) {
599#if defined(EIGEN_SYCL_DEVICE_ONLY)
600 return cl::sycl::erf(x);
602 return generic_fast_erf<float>::run(x);
608struct erf_impl<double> {
609 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double run(
const double x) {
610#if defined(EIGEN_SYCL_DEVICE_ONLY)
611 return cl::sycl::erf(x);
613 return generic_fast_erf<double>::run(x);
673template <typename T, bool IsScalar = is_scalar<T>::value>
677struct flipsign_impl<T, false> {
678 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& should_flipsign,
const T& x) {
679 const T sign_mask = psignmask<T>();
680 const T sign_bit = pand<T>(should_flipsign, sign_mask);
681 return pxor<T>(sign_bit, x);
686struct flipsign_impl<T, true> {
687 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& should_flipsign,
const T& x) {
688 return should_flipsign == T(0) ? x : -x;
693EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T flipsign(
const T& should_flipsign,
const T& x) {
694 return flipsign_impl<T>::run(should_flipsign, x);
697template <typename T, bool IsScalar = is_scalar<T>::value>
698struct ndtri_negative_infinity_impl;
701struct ndtri_negative_infinity_impl<T, false> {
702 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& positive_infinity) {
703 return por(psignmask<T>(), positive_infinity);
708struct ndtri_negative_infinity_impl<T, true> {
709 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T run(
const T& positive_infinity) {
return -positive_infinity; }
716template <
typename T,
typename ScalarType>
717EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_ndtri_gt_exp_neg_two(
const T& b) {
718 const ScalarType p0[] = {ScalarType(-5.99633501014107895267e1), ScalarType(9.80010754185999661536e1),
719 ScalarType(-5.66762857469070293439e1), ScalarType(1.39312609387279679503e1),
720 ScalarType(-1.23916583867381258016e0)};
721 const ScalarType q0[] = {ScalarType(1.0),
722 ScalarType(1.95448858338141759834e0),
723 ScalarType(4.67627912898881538453e0),
724 ScalarType(8.63602421390890590575e1),
725 ScalarType(-2.25462687854119370527e2),
726 ScalarType(2.00260212380060660359e2),
727 ScalarType(-8.20372256168333339912e1),
728 ScalarType(1.59056225126211695515e1),
729 ScalarType(-1.18331621121330003142e0)};
730 const T sqrt2pi = pset1<T>(ScalarType(2.50662827463100050242e0));
731 const T half = pset1<T>(ScalarType(0.5));
732 T c, c2, ndtri_gt_exp_neg_two;
736 ndtri_gt_exp_neg_two =
737 pmadd(c, pmul(c2, pdiv(internal::ppolevl<T, 4>::run(c2, p0), internal::ppolevl<T, 8>::run(c2, q0))), c);
738 return pmul(ndtri_gt_exp_neg_two, sqrt2pi);
741template <
typename T,
typename ScalarType>
742EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_ndtri_lt_exp_neg_two(
const T& b,
const T& should_flipsign) {
746 const ScalarType p1[] = {ScalarType(4.05544892305962419923e0), ScalarType(3.15251094599893866154e1),
747 ScalarType(5.71628192246421288162e1), ScalarType(4.40805073893200834700e1),
748 ScalarType(1.46849561928858024014e1), ScalarType(2.18663306850790267539e0),
749 ScalarType(-1.40256079171354495875e-1), ScalarType(-3.50424626827848203418e-2),
750 ScalarType(-8.57456785154685413611e-4)};
751 const ScalarType q1[] = {ScalarType(1.0),
752 ScalarType(1.57799883256466749731e1),
753 ScalarType(4.53907635128879210584e1),
754 ScalarType(4.13172038254672030440e1),
755 ScalarType(1.50425385692907503408e1),
756 ScalarType(2.50464946208309415979e0),
757 ScalarType(-1.42182922854787788574e-1),
758 ScalarType(-3.80806407691578277194e-2),
759 ScalarType(-9.33259480895457427372e-4)};
763 const ScalarType p2[] = {ScalarType(3.23774891776946035970e0), ScalarType(6.91522889068984211695e0),
764 ScalarType(3.93881025292474443415e0), ScalarType(1.33303460815807542389e0),
765 ScalarType(2.01485389549179081538e-1), ScalarType(1.23716634817820021358e-2),
766 ScalarType(3.01581553508235416007e-4), ScalarType(2.65806974686737550832e-6),
767 ScalarType(6.23974539184983293730e-9)};
768 const ScalarType q2[] = {ScalarType(1.0),
769 ScalarType(6.02427039364742014255e0),
770 ScalarType(3.67983563856160859403e0),
771 ScalarType(1.37702099489081330271e0),
772 ScalarType(2.16236993594496635890e-1),
773 ScalarType(1.34204006088543189037e-2),
774 ScalarType(3.28014464682127739104e-4),
775 ScalarType(2.89247864745380683936e-6),
776 ScalarType(6.79019408009981274425e-9)};
777 const T eight = pset1<T>(ScalarType(8.0));
778 const T neg_two = pset1<T>(ScalarType(-2));
779 T x, x0, correction, z;
781 x = psqrt(pmul(neg_two, plog(b)));
782 x0 = psub(x, pdiv(plog(x), x));
785 pselect(pcmp_lt(x, eight), pdiv(internal::ppolevl<T, 8>::run(z, p1), internal::ppolevl<T, 8>::run(z, q1)),
786 pdiv(internal::ppolevl<T, 8>::run(z, p2), internal::ppolevl<T, 8>::run(z, q2)));
787 return flipsign(should_flipsign, pnmadd(z, correction, x0));
790template <
typename T,
typename ScalarType>
791EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T generic_ndtri(
const T& a) {
792 const T maxnum = pinf<T>();
793 const T neg_maxnum = ndtri_negative_infinity_impl<T>::run(maxnum);
795 const T zero = pset1<T>(ScalarType(0));
796 const T one = pset1<T>(ScalarType(1));
798 const T exp_neg_two = pset1<T>(ScalarType(0.13533528323661269189));
799 T b, ndtri, should_flipsign;
801 should_flipsign = pcmp_le(a, psub(one, exp_neg_two));
802 b = pselect(should_flipsign, a, psub(one, a));
804 ndtri = pselect(pcmp_lt(exp_neg_two, b), generic_ndtri_gt_exp_neg_two<T, ScalarType>(b),
805 generic_ndtri_lt_exp_neg_two<T, ScalarType>(b, should_flipsign));
807 return pselect(pcmp_eq(a, zero), neg_maxnum, pselect(pcmp_eq(one, a), maxnum, ndtri));
810template <
typename Scalar>
812 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(
const Scalar x) {
return generic_ndtri<Scalar, Scalar>(x); }
820template <
typename Scalar>
821struct cephes_helper {
822 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar machep() {
823 eigen_assert(
false &&
"machep not supported for this type");
826 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar big() {
827 eigen_assert(
false &&
"big not supported for this type");
830 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar biginv() {
831 eigen_assert(
false &&
"biginv not supported for this type");
837struct cephes_helper<float> {
838 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float machep() {
839 return NumTraits<float>::epsilon() / 2;
841 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float big() {
843 return 1.0f / (NumTraits<float>::epsilon() / 2);
845 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float biginv() {
852struct cephes_helper<double> {
853 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double machep() {
854 return NumTraits<double>::epsilon() / 2;
856 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double big() {
return 1.0 / NumTraits<double>::epsilon(); }
857 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double biginv() {
859 return NumTraits<double>::epsilon();
863enum IgammaComputationMode { VALUE, DERIVATIVE, SAMPLE_DERIVATIVE };
865template <
typename Scalar>
866EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar main_igamma_term(Scalar a, Scalar x) {
868 Scalar logax = a * numext::log(x) - x - lgamma_impl<Scalar>::run(a);
869 if (logax < -numext::log(NumTraits<Scalar>::highest()) ||
871 (numext::isnan)(logax)) {
874 return numext::exp(logax);
877template <
typename Scalar, IgammaComputationMode mode>
878EIGEN_DEVICE_FUNC
constexpr int igamma_num_iterations() {
881 return mode == VALUE ? 2000
882 : std::is_same<Scalar, float>::value ? 200
883 : std::is_same<Scalar, double>::value ? 500
887template <
typename Scalar, IgammaComputationMode mode>
888struct igammac_cf_impl {
898 EIGEN_DEVICE_FUNC
static Scalar run(Scalar a, Scalar x) {
899 const Scalar zero = 0;
900 const Scalar one = 1;
901 const Scalar two = 2;
902 const Scalar machep = cephes_helper<Scalar>::machep();
903 const Scalar big = cephes_helper<Scalar>::big();
904 const Scalar biginv = cephes_helper<Scalar>::biginv();
906 if ((numext::isinf)(x)) {
910 Scalar ax = main_igamma_term<Scalar>(a, x);
921 Scalar z = x + y + one;
925 Scalar pkm1 = x + one;
927 Scalar ans = pkm1 / qkm1;
929 Scalar dpkm2_da = zero;
930 Scalar dqkm2_da = zero;
931 Scalar dpkm1_da = zero;
932 Scalar dqkm1_da = -x;
933 Scalar dans_da = (dpkm1_da - ans * dqkm1_da) / qkm1;
935 for (
int i = 0; i < igamma_num_iterations<Scalar, mode>(); i++) {
941 Scalar pk = pkm1 * z - pkm2 * yc;
942 Scalar qk = qkm1 * z - qkm2 * yc;
944 Scalar dpk_da = dpkm1_da * z - pkm1 - dpkm2_da * yc + pkm2 * c;
945 Scalar dqk_da = dqkm1_da * z - qkm1 - dqkm2_da * yc + qkm2 * c;
948 Scalar ans_prev = ans;
951 Scalar dans_da_prev = dans_da;
952 dans_da = (dpk_da - ans * dqk_da) / qk;
955 if (numext::abs(ans_prev - ans) <= machep * numext::abs(ans)) {
959 if (numext::abs(dans_da - dans_da_prev) <= machep) {
975 if (numext::abs(pk) > big) {
989 Scalar dlogax_da = numext::log(x) - digamma_impl<Scalar>::run(a);
990 Scalar dax_da = ax * dlogax_da;
996 return ans * dax_da + dans_da * ax;
997 case SAMPLE_DERIVATIVE:
999 return -(dans_da + ans * dlogax_da) * x;
1004template <
typename Scalar, IgammaComputationMode mode>
1005struct igamma_series_impl {
1014 EIGEN_DEVICE_FUNC
static Scalar run(Scalar a, Scalar x) {
1015 const Scalar zero = 0;
1016 const Scalar one = 1;
1017 const Scalar machep = cephes_helper<Scalar>::machep();
1019 Scalar ax = main_igamma_term<Scalar>(a, x);
1036 Scalar dc_da = zero;
1037 Scalar dans_da = zero;
1039 for (
int i = 0; i < igamma_num_iterations<Scalar, mode>(); i++) {
1041 Scalar term = x / r;
1042 Scalar dterm_da = -x / (r * r);
1043 dc_da = term * dc_da + dterm_da * c;
1048 if (mode == VALUE) {
1049 if (c <= machep * ans) {
1053 if (numext::abs(dc_da) <= machep * numext::abs(dans_da)) {
1059 Scalar dlogax_da = numext::log(x) - digamma_impl<Scalar>::run(a + one);
1060 Scalar dax_da = ax * dlogax_da;
1066 return ans * dax_da + dans_da * ax;
1067 case SAMPLE_DERIVATIVE:
1069 return -(dans_da + ans * dlogax_da) * x / a;
1074template <
typename Scalar>
1075struct igammac_impl {
1076 EIGEN_DEVICE_FUNC
static Scalar run(Scalar a, Scalar x) {
1131 const Scalar zero = 0;
1132 const Scalar one = 1;
1133 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1135 if ((x < zero) || (a <= zero)) {
1140 if ((numext::isnan)(a) || (numext::isnan)(x)) {
1144 if ((x < one) || (x < a)) {
1147 return numext::mini(one, numext::maxi(zero, one - igamma_series_impl<Scalar, VALUE>::run(a, x)));
1150 return igammac_cf_impl<Scalar, VALUE>::run(a, x);
1158template <
typename Scalar, IgammaComputationMode mode>
1159struct igamma_generic_impl {
1160 EIGEN_DEVICE_FUNC
static Scalar run(Scalar a, Scalar x) {
1169 const Scalar zero = 0;
1170 const Scalar one = 1;
1171 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1173 if ((numext::isnan)(a) || (numext::isnan)(x)) {
1177 if (x == zero)
return zero;
1179 if ((x < zero) || (a <= zero)) {
1183 if ((x > one) && (x > a)) {
1184 Scalar ret = igammac_cf_impl<Scalar, mode>::run(a, x);
1185 if (mode == VALUE) {
1188 return numext::mini(one, numext::maxi(zero, one - ret));
1194 Scalar ret = igamma_series_impl<Scalar, mode>::run(a, x);
1195 if (mode == VALUE) {
1198 return numext::mini(one, numext::maxi(zero, ret));
1204template <
typename Scalar>
1205struct igamma_impl : igamma_generic_impl<Scalar, VALUE> {
1275template <
typename Scalar>
1276struct igamma_der_a_impl : igamma_generic_impl<Scalar, DERIVATIVE> {
1293template <
typename Scalar>
1294struct gamma_sample_der_alpha_impl : igamma_generic_impl<Scalar, SAMPLE_DERIVATIVE> {
1338template <
typename Scalar>
1339struct zeta_impl_series {
1340 EIGEN_STATIC_ASSERT((std::is_same<Scalar, Scalar>::value ==
false), THIS_TYPE_IS_NOT_SUPPORTED)
1342 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(
const Scalar) {
return Scalar(0); }
1346struct zeta_impl_series<float> {
1347 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
bool run(
float& a,
float& b,
float& s,
const float x,
1348 const float machep) {
1350 while ((i < 9) || (a <= 9.0f)) {
1353 b = numext::pow(a, -x);
1355 if (numext::abs(b / s) < machep)
return true;
1364struct zeta_impl_series<double> {
1365 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
bool run(
double& a,
double& b,
double& s,
const double x,
1366 const double machep) {
1368 while ((i < 9) || (a <= 9.0)) {
1371 b = numext::pow(a, -x);
1373 if (numext::abs(b / s) < machep)
return true;
1381template <
typename Scalar>
1383 EIGEN_DEVICE_FUNC
static Scalar run(Scalar x, Scalar q) {
1446 Scalar p, r, a, b, k, s, t, w;
1448 const Scalar A[] = {
1454 Scalar(-1.8924375803183791606e9),
1455 Scalar(7.47242496e10),
1456 Scalar(-2.950130727918164224e12),
1457 Scalar(1.1646782814350067249e14),
1458 Scalar(-4.5979787224074726105e15),
1459 Scalar(1.8152105401943546773e17),
1460 Scalar(-7.1661652561756670113e18)
1463 const Scalar maxnum = NumTraits<Scalar>::infinity();
1464 const Scalar zero = Scalar(0.0), half = Scalar(0.5), one = Scalar(1.0);
1465 const Scalar machep = cephes_helper<Scalar>::machep();
1466 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1468 if (x == one)
return maxnum;
1475 if (q == numext::floor(q)) {
1476 if (numext::rint(Scalar(0.5) * x) == Scalar(0.5) * x) {
1483 r = numext::floor(p);
1484 if (p != r)
return nan;
1492 s = numext::pow(q, -x);
1496 if (zeta_impl_series<Scalar>::run(a, b, s, x, machep)) {
1503 if (numext::equal_strict(b, zero)) {
1508 s += b * w / (x - one);
1513 for (i = 0; i < 12; i++) {
1518 t = numext::abs(t / s);
1535template <
typename Scalar>
1536struct polygamma_impl {
1537 EIGEN_DEVICE_FUNC
static Scalar run(Scalar n, Scalar x) {
1538 Scalar zero = 0.0, one = 1.0;
1539 Scalar nplus = n + one;
1540 const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1543 if (numext::floor(n) != n || n < zero) {
1547 else if (n == zero) {
1548 return digamma_impl<Scalar>::run(x);
1552 Scalar factorial = numext::exp(lgamma_impl<Scalar>::run(nplus));
1553 return numext::pow(-one, nplus) * factorial * zeta_impl<Scalar>::run(nplus, x);
1562template <
typename Scalar>
1563struct betainc_impl {
1564 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(Scalar, Scalar, Scalar) {
1565 EIGEN_STATIC_ASSERT((!std::is_same<Scalar, Scalar>::value), THIS_TYPE_IS_NOT_SUPPORTED)
1641template <
typename Scalar>
1643 EIGEN_STATIC_ASSERT((std::is_same<Scalar, float>::value || std::is_same<Scalar, double>::value),
1644 THIS_TYPE_IS_NOT_SUPPORTED)
1646 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE Scalar run(Scalar a, Scalar b, Scalar x,
bool small_branch) {
1647 const Scalar big = cephes_helper<Scalar>::big();
1648 const Scalar machep = cephes_helper<Scalar>::machep();
1649 const Scalar biginv = cephes_helper<Scalar>::biginv();
1651 const Scalar zero = 0;
1652 const Scalar one = 1;
1653 const Scalar two = 2;
1655 Scalar xk, pk, pkm1, pkm2, qk, qkm1, qkm2;
1656 Scalar k1, k2, k3, k4, k5, k6, k7, k8, k26update;
1660 constexpr int num_iters = (std::is_same<Scalar, float>::value) ? 100 : 300;
1661 const Scalar thresh = (std::is_same<Scalar, float>::value) ? machep : Scalar(3) * machep;
1662 Scalar r = (std::is_same<Scalar, float>::value) ? zero : one;
1695 xk = -(x * k1 * k2) / (k3 * k4);
1696 pk = pkm1 + pkm2 * xk;
1697 qk = qkm1 + qkm2 * xk;
1703 xk = (x * k5 * k6) / (k7 * k8);
1704 pk = pkm1 + pkm2 * xk;
1705 qk = qkm1 + qkm2 * xk;
1713 if (numext::abs(ans - r) < numext::abs(r) * thresh) {
1728 if ((numext::abs(qk) + numext::abs(pk)) > big) {
1734 if ((numext::abs(qk) < biginv) || (numext::abs(pk) < biginv)) {
1740 }
while (++n < num_iters);
1747template <
typename Scalar>
1748struct betainc_helper {};
1751struct betainc_helper<float> {
1753 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float incbsa(
float aa,
float bb,
float xx) {
1754 float ans, a, b, t, x, onemx;
1755 bool reversed_a_b =
false;
1760 if (xx > (aa / (aa + bb))) {
1761 reversed_a_b =
true;
1775 if (numext::abs(b * x / a) < 0.3f) {
1776 t = betainc_helper<float>::incbps(a, b, x);
1777 if (reversed_a_b) t = 1.0f - t;
1782 ans = x * (a + b - 2.0f) / (a - 1.0f);
1784 ans = incbeta_cfe<float>::run(a, b, x,
true );
1785 t = b * numext::log(t);
1787 ans = incbeta_cfe<float>::run(a, b, x,
false );
1788 t = (b - 1.0f) * numext::log(t);
1791 t += a * numext::log(x) + lgamma_impl<float>::run(a + b) - lgamma_impl<float>::run(a) - lgamma_impl<float>::run(b);
1792 t += numext::log(ans / a);
1795 if (reversed_a_b) t = 1.0f - t;
1799 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
float incbps(
float a,
float b,
float x) {
1801 const float machep = cephes_helper<float>::machep();
1803 y = a * numext::log(x) + (b - 1.0f) * numext::log1p(-x) - numext::log(a);
1804 y -= lgamma_impl<float>::run(a) + lgamma_impl<float>::run(b);
1805 y += lgamma_impl<float>::run(a + b);
1818 }
while (numext::abs(u) > machep);
1820 return numext::exp(y) * (1.0f + s);
1825struct betainc_impl<float> {
1826 EIGEN_DEVICE_FUNC
static float run(
float a,
float b,
float x) {
1827 const float nan = NumTraits<float>::quiet_NaN();
1830 if ((numext::isnan)(a) || (numext::isnan)(b) || (numext::isnan)(x))
return nan;
1831 if (a == 0.0f && b == 0.0f)
return nan;
1832 if (x < 0.0f || x > 1.0f)
return nan;
1833 if (a < 0.0f)
return nan;
1834 if (b < 0.0f)
return nan;
1835 if (a == 0.0f)
return 1.0f;
1836 if (b == 0.0f)
return 0.0f;
1837 if (x == 0.0f)
return 0.0f;
1838 if (x == 1.0f)
return 1.0f;
1843 ans = betainc_helper<float>::incbsa(a + 1.0f, b, x);
1844 t = a * numext::log(x) + b * numext::log1p(-x) + lgamma_impl<float>::run(a + b) -
1845 lgamma_impl<float>::run(a + 1.0f) - lgamma_impl<float>::run(b);
1846 return (ans + numext::exp(t));
1848 return betainc_helper<float>::incbsa(a, b, x);
1854struct betainc_helper<double> {
1855 EIGEN_DEVICE_FUNC
static EIGEN_STRONG_INLINE
double incbps(
double a,
double b,
double x) {
1856 const double machep = cephes_helper<double>::machep();
1858 double s, t, u, v, n, t1, z, ai;
1868 while (numext::abs(v) > z) {
1869 u = (n - b) * x / n;
1878 u = a * numext::log(x);
1886 t = lgamma_impl<double>::run(a + b) - lgamma_impl<double>::run(a) - lgamma_impl<double>::run(b) + u +
1888 return numext::exp(t);
1893struct betainc_impl<double> {
1894 EIGEN_DEVICE_FUNC
static double run(
double aa,
double bb,
double xx) {
1895 const double nan = NumTraits<double>::quiet_NaN();
1896 const double machep = cephes_helper<double>::machep();
1897 double a, b, t, x, xc, w, y;
1898 bool reversed_a_b =
false;
1900 if ((numext::isnan)(aa) || (numext::isnan)(bb) || (numext::isnan)(xx))
return nan;
1901 if (aa == 0.0 && bb == 0.0)
return nan;
1902 if (xx < 0.0 || xx > 1.0)
return nan;
1903 if (aa < 0.0)
return nan;
1904 if (bb < 0.0)
return nan;
1905 if (aa == 0.0)
return 1.0;
1906 if (bb == 0.0)
return 0.0;
1907 if (xx == 0.0)
return 0.0;
1908 if (xx == 1.0)
return 1.0;
1911 if ((bb * xx) <= 1.0 && xx <= 0.95) {
1912 return betainc_helper<double>::incbps(aa, bb, xx);
1918 if (xx > (aa / (aa + bb))) {
1919 reversed_a_b =
true;
1931 if (reversed_a_b && (b * x) <= 1.0 && x <= 0.95) {
1932 t = betainc_helper<double>::incbps(a, b, x);
1942 y = x * (a + b - 2.0) - (a - 1.0);
1944 w = incbeta_cfe<double>::run(a, b, x,
true );
1946 w = incbeta_cfe<double>::run(a, b, x,
false ) / xc;
1953 y = a * numext::log(x);
1954 t = b * numext::log(xc);
1967 y += t + lgamma_impl<double>::run(a + b) - lgamma_impl<double>::run(a) - lgamma_impl<double>::run(b);
1968 y += numext::log(w / a);
1989template <
typename Scalar>
1990EIGEN_DEVICE_FUNC
inline auto lgamma(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(lgamma, Scalar)::run(x)) {
1991 return EIGEN_MATHFUNC_IMPL(lgamma, Scalar)::run(x);
1994template <
typename Scalar>
1995EIGEN_DEVICE_FUNC
inline auto digamma(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(digamma, Scalar)::run(x)) {
1996 return EIGEN_MATHFUNC_IMPL(digamma, Scalar)::run(x);
1999template <
typename Scalar>
2000EIGEN_DEVICE_FUNC
inline auto zeta(
const Scalar& x,
const Scalar& q)
2001 ->
decltype(EIGEN_MATHFUNC_IMPL(zeta, Scalar)::run(x, q)) {
2002 return EIGEN_MATHFUNC_IMPL(zeta, Scalar)::run(x, q);
2005template <
typename Scalar>
2006EIGEN_DEVICE_FUNC
inline auto polygamma(
const Scalar& n,
const Scalar& x)
2007 ->
decltype(EIGEN_MATHFUNC_IMPL(polygamma, Scalar)::run(n, x)) {
2008 return EIGEN_MATHFUNC_IMPL(polygamma, Scalar)::run(n, x);
2011template <
typename Scalar>
2012EIGEN_DEVICE_FUNC
inline auto erf(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(erf, Scalar)::run(x)) {
2013 return EIGEN_MATHFUNC_IMPL(erf, Scalar)::run(x);
2016template <
typename Scalar>
2017EIGEN_DEVICE_FUNC
inline auto erfc(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(erfc, Scalar)::run(x)) {
2018 return EIGEN_MATHFUNC_IMPL(erfc, Scalar)::run(x);
2021template <
typename Scalar>
2022EIGEN_DEVICE_FUNC
inline auto ndtri(
const Scalar& x) ->
decltype(EIGEN_MATHFUNC_IMPL(ndtri, Scalar)::run(x)) {
2023 return EIGEN_MATHFUNC_IMPL(ndtri, Scalar)::run(x);
2026template <
typename Scalar>
2027EIGEN_DEVICE_FUNC
inline auto igamma(
const Scalar& a,
const Scalar& x)
2028 ->
decltype(EIGEN_MATHFUNC_IMPL(igamma, Scalar)::run(a, x)) {
2029 return EIGEN_MATHFUNC_IMPL(igamma, Scalar)::run(a, x);
2032template <
typename Scalar>
2033EIGEN_DEVICE_FUNC
inline auto igamma_der_a(
const Scalar& a,
const Scalar& x)
2034 ->
decltype(EIGEN_MATHFUNC_IMPL(igamma_der_a, Scalar)::run(a, x)) {
2035 return EIGEN_MATHFUNC_IMPL(igamma_der_a, Scalar)::run(a, x);
2038template <
typename Scalar>
2039EIGEN_DEVICE_FUNC
inline auto gamma_sample_der_alpha(
const Scalar& a,
const Scalar& x)
2040 ->
decltype(EIGEN_MATHFUNC_IMPL(gamma_sample_der_alpha, Scalar)::run(a, x)) {
2041 return EIGEN_MATHFUNC_IMPL(gamma_sample_der_alpha, Scalar)::run(a, x);
2044template <
typename Scalar>
2045EIGEN_DEVICE_FUNC
inline auto igammac(
const Scalar& a,
const Scalar& x)
2046 ->
decltype(EIGEN_MATHFUNC_IMPL(igammac, Scalar)::run(a, x)) {
2047 return EIGEN_MATHFUNC_IMPL(igammac, Scalar)::run(a, x);
2050template <
typename Scalar>
2051EIGEN_DEVICE_FUNC
inline auto betainc(
const Scalar& a,
const Scalar& b,
const Scalar& x)
2052 ->
decltype(EIGEN_MATHFUNC_IMPL(betainc, Scalar)::run(a, b, x)) {
2053 return EIGEN_MATHFUNC_IMPL(betainc, Scalar)::run(a, b, x);
Namespace containing all symbols from the Eigen library.