10 #ifndef EIGEN_SPECIAL_FUNCTIONS_H
11 #define EIGEN_SPECIAL_FUNCTIONS_H
44 template <
typename Scalar>
49 THIS_TYPE_IS_NOT_SUPPORTED);
54 template <
typename Scalar>
59 #if EIGEN_HAS_C99_MATH
61 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 19) || __GLIBC__>2) \
62 && (defined(_DEFAULT_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
63 #define EIGEN_HAS_LGAMMA_R
67 #if defined(__GLIBC__) && ((__GLIBC__==2 && __GLIBC_MINOR__ < 19) || __GLIBC__<2) \
68 && (defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
69 #define EIGEN_HAS_LGAMMA_R
76 #if !defined(EIGEN_GPU_COMPILE_PHASE) && defined (EIGEN_HAS_LGAMMA_R) && !defined(__APPLE__)
78 return ::lgammaf_r(
x, &
dummy);
79 #elif defined(SYCL_DEVICE_ONLY)
88 struct lgamma_impl<double> {
91 #if !defined(EIGEN_GPU_COMPILE_PHASE) && defined(EIGEN_HAS_LGAMMA_R) && !defined(__APPLE__)
93 return ::lgamma_r(
x, &
dummy);
94 #elif defined(SYCL_DEVICE_ONLY)
102 #undef EIGEN_HAS_LGAMMA_R
109 template <
typename Scalar>
127 template <
typename Scalar>
132 THIS_TYPE_IS_NOT_SUPPORTED);
143 -4.16666666666666666667E-3
f,
144 3.96825396825396825397E-3
f,
145 -8.33333333333333333333E-3
f,
146 8.33333333333333333333E-2
f
162 8.33333333333333333333E-2,
163 -2.10927960927960927961E-2,
164 7.57575757575757575758E-3,
165 -4.16666666666666666667E-3,
166 3.96825396825396825397E-3,
167 -8.33333333333333333333E-3,
168 8.33333333333333333333E-2
180 template <
typename Scalar>
242 bool negative =
false;
288 return (negative) ?
y - nz :
y;
303 template <
typename T>
307 const T plus_4 = pset1<T>(4.
f);
308 const T minus_4 = pset1<T>(-4.
f);
311 const T alpha_1 = pset1<T>(-1.60960333262415
e-02
f);
312 const T alpha_3 = pset1<T>(-2.95459980854025
e-03
f);
313 const T alpha_5 = pset1<T>(-7.34990630326855
e-04
f);
314 const T alpha_7 = pset1<T>(-5.69250639462346
e-05
f);
315 const T alpha_9 = pset1<T>(-2.10102402082508
e-06
f);
316 const T alpha_11 = pset1<T>(2.77068142495902
e-08
f);
317 const T alpha_13 = pset1<T>(-2.72614225801306
e-10
f);
320 const T beta_0 = pset1<T>(-1.42647390514189
e-02
f);
321 const T beta_2 = pset1<T>(-7.37332916720468
e-03
f);
322 const T beta_4 = pset1<T>(-1.68282697438203
e-03
f);
323 const T beta_6 = pset1<T>(-2.13374055278905
e-04
f);
324 const T beta_8 = pset1<T>(-1.45660718464996
e-05
f);
348 template <
typename T>
356 template <
typename Scalar>
361 #if EIGEN_HAS_C99_MATH
366 #if defined(SYCL_DEVICE_ONLY)
375 struct erf_impl<double> {
378 #if defined(SYCL_DEVICE_ONLY)
385 #endif // EIGEN_HAS_C99_MATH
391 template <
typename Scalar>
396 THIS_TYPE_IS_NOT_SUPPORTED);
401 template <
typename Scalar>
406 #if EIGEN_HAS_C99_MATH
411 #if defined(SYCL_DEVICE_ONLY)
420 struct erfc_impl<double> {
423 #if defined(SYCL_DEVICE_ONLY)
430 #endif // EIGEN_HAS_C99_MATH
492 const T& should_flipsign,
const T&
x) {
494 const T sign_mask = pset1<T>(
Scalar(-0.0));
495 T sign_bit = pand<T>(should_flipsign, sign_mask);
496 return pxor<T>(sign_bit,
x);
501 const double& should_flipsign,
const double&
x) {
502 return should_flipsign == 0 ?
x : -
x;
507 const float& should_flipsign,
const float&
x) {
508 return should_flipsign == 0 ?
x : -
x;
515 template <
typename T,
typename ScalarType>
517 const ScalarType
p0[] = {
518 ScalarType(-5.99633501014107895267e1),
519 ScalarType(9.80010754185999661536e1),
520 ScalarType(-5.66762857469070293439e1),
521 ScalarType(1.39312609387279679503e1),
522 ScalarType(-1.23916583867381258016e0)
524 const ScalarType q0[] = {
526 ScalarType(1.95448858338141759834e0),
527 ScalarType(4.67627912898881538453e0),
528 ScalarType(8.63602421390890590575e1),
529 ScalarType(-2.25462687854119370527e2),
530 ScalarType(2.00260212380060660359e2),
531 ScalarType(-8.20372256168333339912e1),
532 ScalarType(1.59056225126211695515e1),
533 ScalarType(-1.18331621121330003142e0)
535 const T sqrt2pi = pset1<T>(ScalarType(2.50662827463100050242e0));
536 const T half = pset1<T>(ScalarType(0.5));
537 T c,
c2, ndtri_gt_exp_neg_two;
545 return pmul(ndtri_gt_exp_neg_two, sqrt2pi);
548 template <
typename T,
typename ScalarType>
550 const T&
b,
const T& should_flipsign) {
554 const ScalarType
p1[] = {
555 ScalarType(4.05544892305962419923e0),
556 ScalarType(3.15251094599893866154e1),
557 ScalarType(5.71628192246421288162e1),
558 ScalarType(4.40805073893200834700e1),
559 ScalarType(1.46849561928858024014e1),
560 ScalarType(2.18663306850790267539e0),
561 ScalarType(-1.40256079171354495875
e-1),
562 ScalarType(-3.50424626827848203418
e-2),
563 ScalarType(-8.57456785154685413611
e-4)
565 const ScalarType q1[] = {
567 ScalarType(1.57799883256466749731e1),
568 ScalarType(4.53907635128879210584e1),
569 ScalarType(4.13172038254672030440e1),
570 ScalarType(1.50425385692907503408e1),
571 ScalarType(2.50464946208309415979e0),
572 ScalarType(-1.42182922854787788574
e-1),
573 ScalarType(-3.80806407691578277194
e-2),
574 ScalarType(-9.33259480895457427372
e-4)
579 const ScalarType
p2[] = {
580 ScalarType(3.23774891776946035970e0),
581 ScalarType(6.91522889068984211695e0),
582 ScalarType(3.93881025292474443415e0),
583 ScalarType(1.33303460815807542389e0),
584 ScalarType(2.01485389549179081538
e-1),
585 ScalarType(1.23716634817820021358
e-2),
586 ScalarType(3.01581553508235416007
e-4),
587 ScalarType(2.65806974686737550832
e-6),
588 ScalarType(6.23974539184983293730
e-9)
590 const ScalarType q2[] = {
592 ScalarType(6.02427039364742014255e0),
593 ScalarType(3.67983563856160859403e0),
594 ScalarType(1.37702099489081330271e0),
595 ScalarType(2.16236993594496635890
e-1),
596 ScalarType(1.34204006088543189037
e-2),
597 ScalarType(3.28014464682127739104
e-4),
598 ScalarType(2.89247864745380683936
e-6),
599 ScalarType(6.79019408009981274425
e-9)
601 const T eight = pset1<T>(ScalarType(8.0));
602 const T one = pset1<T>(ScalarType(1));
603 const T neg_two = pset1<T>(ScalarType(-2));
619 template <
typename T,
typename ScalarType>
625 const T zero = pset1<T>(ScalarType(0));
626 const T one = pset1<T>(ScalarType(1));
628 const T exp_neg_two = pset1<T>(ScalarType(0.13533528323661269189));
636 generic_ndtri_gt_exp_neg_two<T, ScalarType>(
b),
637 generic_ndtri_lt_exp_neg_two<T, ScalarType>(
b, should_flipsign));
644 template <
typename Scalar>
649 #if !EIGEN_HAS_C99_MATH
651 template <
typename Scalar>
656 THIS_TYPE_IS_NOT_SUPPORTED);
663 template <
typename Scalar>
667 return generic_ndtri<Scalar, Scalar>(
x);
671 #endif // EIGEN_HAS_C99_MATH
678 template <
typename Scalar>
684 template <
typename Scalar>
731 template <
typename Scalar>
744 template <
typename Scalar, IgammaComputationMode mode>
762 template <
typename Scalar, IgammaComputationMode mode>
786 Scalar ax = main_igamma_term<Scalar>(
a,
x);
809 Scalar dans_da = (dpkm1_da - ans * dqkm1_da) / qkm1;
811 for (
int i = 0; i < igamma_num_iterations<Scalar, mode>();
i++) {
817 Scalar pk = pkm1 *
z - pkm2 * yc;
818 Scalar qk = qkm1 *
z - qkm2 * yc;
820 Scalar dpk_da = dpkm1_da *
z - pkm1 - dpkm2_da * yc + pkm2 *
c;
821 Scalar dqk_da = dqkm1_da *
z - qkm1 - dqkm2_da * yc + qkm2 *
c;
827 Scalar dans_da_prev = dans_da;
828 dans_da = (dpk_da - ans * dqk_da) / qk;
835 if (
numext::abs(dans_da - dans_da_prev) <= machep) {
866 Scalar dax_da = ax * dlogax_da;
872 return ans * dax_da + dans_da * ax;
875 return -(dans_da + ans * dlogax_da) *
x;
880 template <
typename Scalar, IgammaComputationMode mode>
896 Scalar ax = main_igamma_term<Scalar>(
a,
x);
916 for (
int i = 0; i < igamma_num_iterations<Scalar, mode>();
i++) {
919 Scalar dterm_da = -
x / (r * r);
920 dc_da = term * dc_da + dterm_da *
c;
926 if (
c <= machep * ans) {
937 Scalar dax_da = ax * dlogax_da;
943 return ans * dax_da + dans_da * ax;
946 return -(dans_da + ans * dlogax_da) *
x /
a;
951 #if !EIGEN_HAS_C99_MATH
953 template <
typename Scalar>
958 THIS_TYPE_IS_NOT_SUPPORTED);
965 template <
typename Scalar>
966 struct igammac_impl {
1036 if ((
x < one) || (
x <
a)) {
1044 #endif // EIGEN_HAS_C99_MATH
1050 #if !EIGEN_HAS_C99_MATH
1052 template <
typename Scalar, IgammaComputationMode mode>
1057 THIS_TYPE_IS_NOT_SUPPORTED);
1064 template <
typename Scalar, IgammaComputationMode mode>
1065 struct igamma_generic_impl {
1090 if ((
x > one) && (
x >
a)) {
1103 #endif // EIGEN_HAS_C99_MATH
1105 template <
typename Scalar>
1110 template <
typename Scalar>
1181 template <
typename Scalar>
1184 template <
typename Scalar>
1202 template <
typename Scalar>
1205 template <
typename Scalar>
1251 template <
typename Scalar>
1256 template <
typename Scalar>
1261 THIS_TYPE_IS_NOT_SUPPORTED);
1291 while( (
i < 9) || (
a <= 9.0) )
1306 template <
typename Scalar>
1380 Scalar(-1.8924375803183791606e9),
1382 Scalar(-2.950130727918164224e12),
1383 Scalar(1.1646782814350067249e14),
1384 Scalar(-4.5979787224074726105e15),
1385 Scalar(1.8152105401943546773e17),
1386 Scalar(-7.1661652561756670113e18)
1437 for(
i=0;
i<12;
i++ )
1460 template <
typename Scalar>
1465 #if !EIGEN_HAS_C99_MATH
1467 template <
typename Scalar>
1472 THIS_TYPE_IS_NOT_SUPPORTED);
1479 template <
typename Scalar>
1480 struct polygamma_impl {
1492 else if (
n ==
zero) {
1503 #endif // EIGEN_HAS_C99_MATH
1509 template <
typename Scalar>
1514 #if !EIGEN_HAS_C99_MATH
1516 template <
typename Scalar>
1521 THIS_TYPE_IS_NOT_SUPPORTED);
1528 template <
typename Scalar>
1529 struct betainc_impl {
1602 THIS_TYPE_IS_NOT_SUPPORTED);
1610 template <
typename Scalar>
1611 struct incbeta_cfe {
1616 THIS_TYPE_IS_NOT_SUPPORTED);
1625 Scalar xk, pk, pkm1, pkm2, qk, qkm1, qkm2;
1626 Scalar k1, k2, k3, k4, k5, k6, k7, k8, k26update;
1666 xk = -(
x *
k1 * k2) / (k3 * k4);
1667 pk = pkm1 + pkm2 * xk;
1668 qk = qkm1 + qkm2 * xk;
1674 xk = (
x * k5 * k6) / (k7 * k8);
1675 pk = pkm1 + pkm2 * xk;
1676 qk = qkm1 + qkm2 * xk;
1711 }
while (++
n < num_iters);
1718 template <
typename Scalar>
1719 struct betainc_helper {};
1722 struct betainc_helper<
float> {
1726 float ans,
a,
b,
t,
x, onemx;
1727 bool reversed_a_b =
false;
1732 if (xx > (aa / (aa + bb))) {
1733 reversed_a_b =
true;
1748 t = betainc_helper<float>::incbps(
a,
b,
x);
1749 if (reversed_a_b)
t = 1.0f -
t;
1754 ans =
x * (
a +
b - 2.0f) / (
a - 1.0
f);
1768 if (reversed_a_b)
t = 1.0f -
t;
1799 struct betainc_impl<
float> {
1801 static float run(
float a,
float b,
float x) {
1802 const float nan = NumTraits<float>::quiet_NaN();
1805 if (
a <= 0.0
f)
return nan;
1806 if (
b <= 0.0
f)
return nan;
1807 if ((
x <= 0.0
f) || (
x >= 1.0
f)) {
1808 if (
x == 0.0
f)
return 0.0f;
1809 if (
x == 1.0
f)
return 1.0f;
1816 ans = betainc_helper<float>::incbsa(
a + 1.0
f,
b,
x);
1822 return betainc_helper<float>::incbsa(
a,
b,
x);
1828 struct betainc_helper<double> {
1833 double s,
t, u,
v,
n, t1,
z, ai;
1844 u = (
n -
b) *
x /
n;
1868 struct betainc_impl<double> {
1870 static double run(
double aa,
double bb,
double xx) {
1871 const double nan = NumTraits<double>::quiet_NaN();
1875 double a,
b,
t,
x, xc,
w,
y;
1876 bool reversed_a_b =
false;
1878 if (aa <= 0.0 || bb <= 0.0) {
1882 if ((xx <= 0.0) || (xx >= 1.0)) {
1883 if (xx == 0.0)
return (0.0);
1884 if (xx == 1.0)
return (1.0);
1889 if ((bb * xx) <= 1.0 && xx <= 0.95) {
1890 return betainc_helper<double>::incbps(aa, bb, xx);
1896 if (xx > (aa / (aa + bb))) {
1897 reversed_a_b =
true;
1909 if (reversed_a_b && (
b *
x) <= 1.0 &&
x <= 0.95) {
1910 t = betainc_helper<double>::incbps(
a,
b,
x);
1920 y =
x * (
a +
b - 2.0) - (
a - 1.0);
1964 #endif // EIGEN_HAS_C99_MATH
1970 template <
typename Scalar>
1976 template <
typename Scalar>
1982 template <
typename Scalar>
1988 template <
typename Scalar>
1994 template <
typename Scalar>
2000 template <
typename Scalar>
2006 template <
typename Scalar>
2012 template <
typename Scalar>
2018 template <
typename Scalar>
2024 template <
typename Scalar>
2030 template <
typename Scalar>
2036 template <
typename Scalar>
2045 #endif // EIGEN_SPECIAL_FUNCTIONS_H