SpecialFunctionsImpl.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2015 Eugene Brevdo <ebrevdo@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPECIAL_FUNCTIONS_H
11 #define EIGEN_SPECIAL_FUNCTIONS_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 namespace internal {
17 
18 // Parts of this code are based on the Cephes Math Library.
19 //
20 // Cephes Math Library Release 2.8: June, 2000
21 // Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier
22 //
23 // Permission has been kindly provided by the original author
24 // to incorporate the Cephes software into the Eigen codebase:
25 //
26 // From: Stephen Moshier
27 // To: Eugene Brevdo
28 // Subject: Re: Permission to wrap several cephes functions in Eigen
29 //
30 // Hello Eugene,
31 //
32 // Thank you for writing.
33 //
34 // If your licensing is similar to BSD, the formal way that has been
35 // handled is simply to add a statement to the effect that you are incorporating
36 // the Cephes software by permission of the author.
37 //
38 // Good luck with your project,
39 // Steve
40 
41 
42 
46 template <typename Scalar>
47 struct lgamma_impl {
48  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
49  THIS_TYPE_IS_NOT_SUPPORTED)
50 
51  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Scalar) {
52  return Scalar(0);
53  }
54 };
55 
56 template <typename Scalar>
57 struct lgamma_retval {
58  typedef Scalar type;
59 };
60 
61 #if EIGEN_HAS_C99_MATH
62 // Since glibc 2.19
63 #if defined(__GLIBC__) && ((__GLIBC__>=2 && __GLIBC_MINOR__ >= 19) || __GLIBC__>2) \
64  && (defined(_DEFAULT_SOURCE) || defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
65 #define EIGEN_HAS_LGAMMA_R
66 #endif
67 
68 // Glibc versions before 2.19
69 #if defined(__GLIBC__) && ((__GLIBC__==2 && __GLIBC_MINOR__ < 19) || __GLIBC__<2) \
70  && (defined(_BSD_SOURCE) || defined(_SVID_SOURCE))
71 #define EIGEN_HAS_LGAMMA_R
72 #endif
73 
74 template <>
75 struct lgamma_impl<float> {
77  static EIGEN_STRONG_INLINE float run(float x) {
78 #if !defined(EIGEN_GPU_COMPILE_PHASE) && defined (EIGEN_HAS_LGAMMA_R) && !defined(__APPLE__)
79  int dummy;
80  return ::lgammaf_r(x, &dummy);
81 #elif defined(SYCL_DEVICE_ONLY)
82  return cl::sycl::lgamma(x);
83 #else
84  return ::lgammaf(x);
85 #endif
86  }
87 };
88 
89 template <>
90 struct lgamma_impl<double> {
92  static EIGEN_STRONG_INLINE double run(double x) {
93 #if !defined(EIGEN_GPU_COMPILE_PHASE) && defined(EIGEN_HAS_LGAMMA_R) && !defined(__APPLE__)
94  int dummy;
95  return ::lgamma_r(x, &dummy);
96 #elif defined(SYCL_DEVICE_ONLY)
97  return cl::sycl::lgamma(x);
98 #else
99  return ::lgamma(x);
100 #endif
101  }
102 };
103 
104 #undef EIGEN_HAS_LGAMMA_R
105 #endif
106 
107 
111 template <typename Scalar>
112 struct digamma_retval {
113  typedef Scalar type;
114 };
115 
116 /*
117  *
118  * Polynomial evaluation helper for the Psi (digamma) function.
119  *
120  * digamma_impl_maybe_poly::run(s) evaluates the asymptotic Psi expansion for
121  * input Scalar s, assuming s is above 10.0.
122  *
123  * If s is above a certain threshold for the given Scalar type, zero
124  * is returned. Otherwise the polynomial is evaluated with enough
125  * coefficients for results matching Scalar machine precision.
126  *
127  *
128  */
129 template <typename Scalar>
130 struct digamma_impl_maybe_poly {
131  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
132  THIS_TYPE_IS_NOT_SUPPORTED)
133 
134  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Scalar) {
135  return Scalar(0);
136  }
137 };
138 
139 
140 template <>
141 struct digamma_impl_maybe_poly<float> {
143  static EIGEN_STRONG_INLINE float run(const float s) {
144  const float A[] = {
145  -4.16666666666666666667E-3f,
146  3.96825396825396825397E-3f,
147  -8.33333333333333333333E-3f,
148  8.33333333333333333333E-2f
149  };
150 
151  float z;
152  if (s < 1.0e8f) {
153  z = 1.0f / (s * s);
154  return z * internal::ppolevl<float, 3>::run(z, A);
155  } else return 0.0f;
156  }
157 };
158 
159 template <>
160 struct digamma_impl_maybe_poly<double> {
162  static EIGEN_STRONG_INLINE double run(const double s) {
163  const double A[] = {
164  8.33333333333333333333E-2,
165  -2.10927960927960927961E-2,
166  7.57575757575757575758E-3,
167  -4.16666666666666666667E-3,
168  3.96825396825396825397E-3,
169  -8.33333333333333333333E-3,
170  8.33333333333333333333E-2
171  };
172 
173  double z;
174  if (s < 1.0e17) {
175  z = 1.0 / (s * s);
176  return z * internal::ppolevl<double, 6>::run(z, A);
177  }
178  else return 0.0;
179  }
180 };
181 
182 template <typename Scalar>
183 struct digamma_impl {
185  static Scalar run(Scalar x) {
186  /*
187  *
188  * Psi (digamma) function (modified for Eigen)
189  *
190  *
191  * SYNOPSIS:
192  *
193  * double x, y, psi();
194  *
195  * y = psi( x );
196  *
197  *
198  * DESCRIPTION:
199  *
200  * d -
201  * psi(x) = -- ln | (x)
202  * dx
203  *
204  * is the logarithmic derivative of the gamma function.
205  * For integer x,
206  * n-1
207  * -
208  * psi(n) = -EUL + > 1/k.
209  * -
210  * k=1
211  *
212  * If x is negative, it is transformed to a positive argument by the
213  * reflection formula psi(1-x) = psi(x) + pi cot(pi x).
214  * For general positive x, the argument is made greater than 10
215  * using the recurrence psi(x+1) = psi(x) + 1/x.
216  * Then the following asymptotic expansion is applied:
217  *
218  * inf. B
219  * - 2k
220  * psi(x) = log(x) - 1/2x - > -------
221  * - 2k
222  * k=1 2k x
223  *
224  * where the B2k are Bernoulli numbers.
225  *
226  * ACCURACY (float):
227  * Relative error (except absolute when |psi| < 1):
228  * arithmetic domain # trials peak rms
229  * IEEE 0,30 30000 1.3e-15 1.4e-16
230  * IEEE -30,0 40000 1.5e-15 2.2e-16
231  *
232  * ACCURACY (double):
233  * Absolute error, relative when |psi| > 1 :
234  * arithmetic domain # trials peak rms
235  * IEEE -33,0 30000 8.2e-7 1.2e-7
236  * IEEE 0,33 100000 7.3e-7 7.7e-8
237  *
238  * ERROR MESSAGES:
239  * message condition value returned
240  * psi singularity x integer <=0 INFINITY
241  */
242 
243  Scalar p, q, nz, s, w, y;
244  bool negative = false;
245 
246  const Scalar nan = NumTraits<Scalar>::quiet_NaN();
247  const Scalar m_pi = Scalar(EIGEN_PI);
248 
249  const Scalar zero = Scalar(0);
250  const Scalar one = Scalar(1);
251  const Scalar half = Scalar(0.5);
252  nz = zero;
253 
254  if (x <= zero) {
255  negative = true;
256  q = x;
257  p = numext::floor(q);
258  if (p == q) {
259  return nan;
260  }
261  /* Remove the zeros of tan(m_pi x)
262  * by subtracting the nearest integer from x
263  */
264  nz = q - p;
265  if (nz != half) {
266  if (nz > half) {
267  p += one;
268  nz = q - p;
269  }
270  nz = m_pi / numext::tan(m_pi * nz);
271  }
272  else {
273  nz = zero;
274  }
275  x = one - x;
276  }
277 
278  /* use the recurrence psi(x+1) = psi(x) + 1/x. */
279  s = x;
280  w = zero;
281  while (s < Scalar(10)) {
282  w += one / s;
283  s += one;
284  }
285 
286  y = digamma_impl_maybe_poly<Scalar>::run(s);
287 
288  y = numext::log(s) - (half / s) - y - w;
289 
290  return (negative) ? y - nz : y;
291  }
292 };
293 
294 
307 template <typename T>
308 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erf_float(const T& x) {
309  constexpr float kErfInvOneMinusHalfULP = 3.832506856900711f;
310  const T clamp = pcmp_le(pset1<T>(kErfInvOneMinusHalfULP), pabs(x));
311  // The monomial coefficients of the numerator polynomial (odd).
312  const T alpha_1 = pset1<T>(1.128379143519084f);
313  const T alpha_3 = pset1<T>(0.18520832239976145f);
314  const T alpha_5 = pset1<T>(0.050955695062380861f);
315  const T alpha_7 = pset1<T>(0.0034082910107109506f);
316  const T alpha_9 = pset1<T>(0.00022905065861350646f);
317 
318  // The monomial coefficients of the denominator polynomial (even).
319  const T beta_0 = pset1<T>(1.0f);
320  const T beta_2 = pset1<T>(0.49746925110067538f);
321  const T beta_4 = pset1<T>(0.11098505178285362f);
322  const T beta_6 = pset1<T>(0.014070470171167667f);
323  const T beta_8 = pset1<T>(0.0010179625278914885f);
324  const T beta_10 = pset1<T>(0.000023547966471313185f);
325  const T beta_12 = pset1<T>(-1.1791602954361697e-7f);
326 
327  // Since the polynomials are odd/even, we need x^2.
328  const T x2 = pmul(x, x);
329 
330  // Evaluate the numerator polynomial p.
331  T p = pmadd(x2, alpha_9, alpha_7);
332  p = pmadd(x2, p, alpha_5);
333  p = pmadd(x2, p, alpha_3);
334  p = pmadd(x2, p, alpha_1);
335  p = pmul(x, p);
336 
337  // Evaluate the denominator polynomial p.
338  T q = pmadd(x2, beta_12, beta_10);
339  q = pmadd(x2, q, beta_8);
340  q = pmadd(x2, q, beta_6);
341  q = pmadd(x2, q, beta_4);
342  q = pmadd(x2, q, beta_2);
343  q = pmadd(x2, q, beta_0);
344 
345  // Divide the numerator by the denominator.
346  return pselect(clamp, psign(x), pdiv(p, q));
347 }
348 
349 template <typename T>
350 struct erf_impl {
352  static EIGEN_STRONG_INLINE T run(const T& x) {
353  return generic_fast_erf_float(x);
354  }
355 };
356 
357 template <typename Scalar>
358 struct erf_retval {
359  typedef Scalar type;
360 };
361 
362 #if EIGEN_HAS_C99_MATH
363 template <>
364 struct erf_impl<float> {
366  static EIGEN_STRONG_INLINE float run(float x) {
367 #if defined(SYCL_DEVICE_ONLY)
368  return cl::sycl::erf(x);
369 #else
370  return generic_fast_erf_float(x);
371 #endif
372  }
373 };
374 
375 template <>
376 struct erf_impl<double> {
378  static EIGEN_STRONG_INLINE double run(double x) {
379 #if defined(SYCL_DEVICE_ONLY)
380  return cl::sycl::erf(x);
381 #else
382  return ::erf(x);
383 #endif
384  }
385 };
386 #endif // EIGEN_HAS_C99_MATH
387 
388 
392 template <typename Scalar>
393 struct erfc_impl {
394  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
395  THIS_TYPE_IS_NOT_SUPPORTED)
396 
397  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Scalar) {
398  return Scalar(0);
399  }
400 };
401 
402 template <typename Scalar>
403 struct erfc_retval {
404  typedef Scalar type;
405 };
406 
407 #if EIGEN_HAS_C99_MATH
408 template <>
409 struct erfc_impl<float> {
411  static EIGEN_STRONG_INLINE float run(const float x) {
412 #if defined(SYCL_DEVICE_ONLY)
413  return cl::sycl::erfc(x);
414 #else
415  return ::erfcf(x);
416 #endif
417  }
418 };
419 
420 template <>
421 struct erfc_impl<double> {
423  static EIGEN_STRONG_INLINE double run(const double x) {
424 #if defined(SYCL_DEVICE_ONLY)
425  return cl::sycl::erfc(x);
426 #else
427  return ::erfc(x);
428 #endif
429  }
430 };
431 #endif // EIGEN_HAS_C99_MATH
432 
433 
434 
438 /* Inverse of Normal distribution function (modified for Eigen).
439  *
440  *
441  * SYNOPSIS:
442  *
443  * double x, y, ndtri();
444  *
445  * x = ndtri( y );
446  *
447  *
448  *
449  * DESCRIPTION:
450  *
451  * Returns the argument, x, for which the area under the
452  * Gaussian probability density function (integrated from
453  * minus infinity to x) is equal to y.
454  *
455  *
456  * For small arguments 0 < y < exp(-2), the program computes
457  * z = sqrt( -2.0 * log(y) ); then the approximation is
458  * x = z - log(z)/z - (1/z) P(1/z) / Q(1/z).
459  * There are two rational functions P/Q, one for 0 < y < exp(-32)
460  * and the other for y up to exp(-2). For larger arguments,
461  * w = y - 0.5, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2)).
462  *
463  *
464  * ACCURACY:
465  *
466  * Relative error:
467  * arithmetic domain # trials peak rms
468  * DEC 0.125, 1 5500 9.5e-17 2.1e-17
469  * DEC 6e-39, 0.135 3500 5.7e-17 1.3e-17
470  * IEEE 0.125, 1 20000 7.2e-16 1.3e-16
471  * IEEE 3e-308, 0.135 50000 4.6e-16 9.8e-17
472  *
473  *
474  * ERROR MESSAGES:
475  *
476  * message condition value returned
477  * ndtri domain x == 0 -INF
478  * ndtri domain x == 1 INF
479  * ndtri domain x < 0, x > 1 NAN
480  */
481  /*
482  Cephes Math Library Release 2.2: June, 1992
483  Copyright 1985, 1987, 1992 by Stephen L. Moshier
484  Direct inquiries to 30 Frost Street, Cambridge, MA 02140
485  */
486 
487 
488 // TODO: Add a cheaper approximation for float.
489 
490 
491 template<typename T>
492 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T flipsign(
493  const T& should_flipsign, const T& x) {
494  typedef typename unpacket_traits<T>::type Scalar;
495  const T sign_mask = pset1<T>(Scalar(-0.0));
496  T sign_bit = pand<T>(should_flipsign, sign_mask);
497  return pxor<T>(sign_bit, x);
498 }
499 
500 template<>
501 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE double flipsign<double>(
502  const double& should_flipsign, const double& x) {
503  return should_flipsign == 0 ? x : -x;
504 }
505 
506 template<>
507 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE float flipsign<float>(
508  const float& should_flipsign, const float& x) {
509  return should_flipsign == 0 ? x : -x;
510 }
511 
512 // We split this computation in to two so that in the scalar path
513 // only one branch is evaluated (due to our template specialization of pselect
514 // being an if statement.)
515 
516 template <typename T, typename ScalarType>
517 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_ndtri_gt_exp_neg_two(const T& b) {
518  const ScalarType p0[] = {
519  ScalarType(-5.99633501014107895267e1),
520  ScalarType(9.80010754185999661536e1),
521  ScalarType(-5.66762857469070293439e1),
522  ScalarType(1.39312609387279679503e1),
523  ScalarType(-1.23916583867381258016e0)
524  };
525  const ScalarType q0[] = {
526  ScalarType(1.0),
527  ScalarType(1.95448858338141759834e0),
528  ScalarType(4.67627912898881538453e0),
529  ScalarType(8.63602421390890590575e1),
530  ScalarType(-2.25462687854119370527e2),
531  ScalarType(2.00260212380060660359e2),
532  ScalarType(-8.20372256168333339912e1),
533  ScalarType(1.59056225126211695515e1),
534  ScalarType(-1.18331621121330003142e0)
535  };
536  const T sqrt2pi = pset1<T>(ScalarType(2.50662827463100050242e0));
537  const T half = pset1<T>(ScalarType(0.5));
538  T c, c2, ndtri_gt_exp_neg_two;
539 
540  c = psub(b, half);
541  c2 = pmul(c, c);
542  ndtri_gt_exp_neg_two = pmadd(c, pmul(
543  c2, pdiv(
544  internal::ppolevl<T, 4>::run(c2, p0),
545  internal::ppolevl<T, 8>::run(c2, q0))), c);
546  return pmul(ndtri_gt_exp_neg_two, sqrt2pi);
547 }
548 
549 template <typename T, typename ScalarType>
551  const T& b, const T& should_flipsign) {
552  /* Approximation for interval z = sqrt(-2 log a ) between 2 and 8
553  * i.e., a between exp(-2) = .135 and exp(-32) = 1.27e-14.
554  */
555  const ScalarType p1[] = {
556  ScalarType(4.05544892305962419923e0),
557  ScalarType(3.15251094599893866154e1),
558  ScalarType(5.71628192246421288162e1),
559  ScalarType(4.40805073893200834700e1),
560  ScalarType(1.46849561928858024014e1),
561  ScalarType(2.18663306850790267539e0),
562  ScalarType(-1.40256079171354495875e-1),
563  ScalarType(-3.50424626827848203418e-2),
564  ScalarType(-8.57456785154685413611e-4)
565  };
566  const ScalarType q1[] = {
567  ScalarType(1.0),
568  ScalarType(1.57799883256466749731e1),
569  ScalarType(4.53907635128879210584e1),
570  ScalarType(4.13172038254672030440e1),
571  ScalarType(1.50425385692907503408e1),
572  ScalarType(2.50464946208309415979e0),
573  ScalarType(-1.42182922854787788574e-1),
574  ScalarType(-3.80806407691578277194e-2),
575  ScalarType(-9.33259480895457427372e-4)
576  };
577  /* Approximation for interval z = sqrt(-2 log a ) between 8 and 64
578  * i.e., a between exp(-32) = 1.27e-14 and exp(-2048) = 3.67e-890.
579  */
580  const ScalarType p2[] = {
581  ScalarType(3.23774891776946035970e0),
582  ScalarType(6.91522889068984211695e0),
583  ScalarType(3.93881025292474443415e0),
584  ScalarType(1.33303460815807542389e0),
585  ScalarType(2.01485389549179081538e-1),
586  ScalarType(1.23716634817820021358e-2),
587  ScalarType(3.01581553508235416007e-4),
588  ScalarType(2.65806974686737550832e-6),
589  ScalarType(6.23974539184983293730e-9)
590  };
591  const ScalarType q2[] = {
592  ScalarType(1.0),
593  ScalarType(6.02427039364742014255e0),
594  ScalarType(3.67983563856160859403e0),
595  ScalarType(1.37702099489081330271e0),
596  ScalarType(2.16236993594496635890e-1),
597  ScalarType(1.34204006088543189037e-2),
598  ScalarType(3.28014464682127739104e-4),
599  ScalarType(2.89247864745380683936e-6),
600  ScalarType(6.79019408009981274425e-9)
601  };
602  const T eight = pset1<T>(ScalarType(8.0));
603  const T neg_two = pset1<T>(ScalarType(-2));
604  T x, x0, x1, z;
605 
606  x = psqrt(pmul(neg_two, plog(b)));
607  x0 = psub(x, pdiv(plog(x), x));
608  z = preciprocal(x);
609  x1 = pmul(
610  z, pselect(
611  pcmp_lt(x, eight),
612  pdiv(internal::ppolevl<T, 8>::run(z, p1),
613  internal::ppolevl<T, 8>::run(z, q1)),
614  pdiv(internal::ppolevl<T, 8>::run(z, p2),
615  internal::ppolevl<T, 8>::run(z, q2))));
616  return flipsign(should_flipsign, psub(x0, x1));
617 }
618 
619 template <typename T, typename ScalarType>
621 T generic_ndtri(const T& a) {
622  const T maxnum = pset1<T>(NumTraits<ScalarType>::infinity());
623  const T neg_maxnum = pset1<T>(-NumTraits<ScalarType>::infinity());
624 
625  const T zero = pset1<T>(ScalarType(0));
626  const T one = pset1<T>(ScalarType(1));
627  // exp(-2)
628  const T exp_neg_two = pset1<T>(ScalarType(0.13533528323661269189));
629  T b, ndtri, should_flipsign;
630 
631  should_flipsign = pcmp_le(a, psub(one, exp_neg_two));
632  b = pselect(should_flipsign, a, psub(one, a));
633 
634  ndtri = pselect(
635  pcmp_lt(exp_neg_two, b),
636  generic_ndtri_gt_exp_neg_two<T, ScalarType>(b),
637  generic_ndtri_lt_exp_neg_two<T, ScalarType>(b, should_flipsign));
638 
639  return pselect(
640  pcmp_eq(a, zero), neg_maxnum,
641  pselect(pcmp_eq(one, a), maxnum, ndtri));
642 }
643 
644 template <typename Scalar>
645 struct ndtri_retval {
646  typedef Scalar type;
647 };
648 
649 #if !EIGEN_HAS_C99_MATH
650 
651 template <typename Scalar>
652 struct ndtri_impl {
653  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
654  THIS_TYPE_IS_NOT_SUPPORTED)
655 
656  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Scalar) {
657  return Scalar(0);
658  }
659 };
660 
661 # else
662 
663 template <typename Scalar>
664 struct ndtri_impl {
666  static EIGEN_STRONG_INLINE Scalar run(const Scalar x) {
667  return generic_ndtri<Scalar, Scalar>(x);
668  }
669 };
670 
671 #endif // EIGEN_HAS_C99_MATH
672 
673 
674 
678 template <typename Scalar>
679 struct igammac_retval {
680  typedef Scalar type;
681 };
682 
683 // NOTE: cephes_helper is also used to implement zeta
684 template <typename Scalar>
685 struct cephes_helper {
687  static EIGEN_STRONG_INLINE Scalar machep() { eigen_assert(false && "machep not supported for this type"); return 0.0; }
689  static EIGEN_STRONG_INLINE Scalar big() { eigen_assert(false && "big not supported for this type"); return 0.0; }
691  static EIGEN_STRONG_INLINE Scalar biginv() { eigen_assert(false && "biginv not supported for this type"); return 0.0; }
692 };
693 
694 template <>
695 struct cephes_helper<float> {
697  static EIGEN_STRONG_INLINE float machep() {
698  return NumTraits<float>::epsilon() / 2; // 1.0 - machep == 1.0
699  }
701  static EIGEN_STRONG_INLINE float big() {
702  // use epsneg (1.0 - epsneg == 1.0)
703  return 1.0f / (NumTraits<float>::epsilon() / 2);
704  }
706  static EIGEN_STRONG_INLINE float biginv() {
707  // epsneg
708  return machep();
709  }
710 };
711 
712 template <>
713 struct cephes_helper<double> {
715  static EIGEN_STRONG_INLINE double machep() {
716  return NumTraits<double>::epsilon() / 2; // 1.0 - machep == 1.0
717  }
719  static EIGEN_STRONG_INLINE double big() {
720  return 1.0 / NumTraits<double>::epsilon();
721  }
723  static EIGEN_STRONG_INLINE double biginv() {
724  // inverse of eps
725  return NumTraits<double>::epsilon();
726  }
727 };
728 
730 
731 template <typename Scalar>
733 static EIGEN_STRONG_INLINE Scalar main_igamma_term(Scalar a, Scalar x) {
734  /* Compute x**a * exp(-x) / gamma(a) */
735  Scalar logax = a * numext::log(x) - x - lgamma_impl<Scalar>::run(a);
736  if (logax < -numext::log(NumTraits<Scalar>::highest()) ||
737  // Assuming x and a aren't Nan.
738  (numext::isnan)(logax)) {
739  return Scalar(0);
740  }
741  return numext::exp(logax);
742 }
743 
744 template <typename Scalar, IgammaComputationMode mode>
747  /* Returns the maximum number of internal iterations for igamma computation.
748  */
749  if (mode == VALUE) {
750  return 2000;
751  }
752 
753  if (internal::is_same<Scalar, float>::value) {
754  return 200;
755  } else if (internal::is_same<Scalar, double>::value) {
756  return 500;
757  } else {
758  return 2000;
759  }
760 }
761 
762 template <typename Scalar, IgammaComputationMode mode>
763 struct igammac_cf_impl {
764  /* Computes igamc(a, x) or derivative (depending on the mode)
765  * using the continued fraction expansion of the complementary
766  * incomplete Gamma function.
767  *
768  * Preconditions:
769  * a > 0
770  * x >= 1
771  * x >= a
772  */
774  static Scalar run(Scalar a, Scalar x) {
775  const Scalar zero = 0;
776  const Scalar one = 1;
777  const Scalar two = 2;
778  const Scalar machep = cephes_helper<Scalar>::machep();
779  const Scalar big = cephes_helper<Scalar>::big();
780  const Scalar biginv = cephes_helper<Scalar>::biginv();
781 
782  if ((numext::isinf)(x)) {
783  return zero;
784  }
785 
786  Scalar ax = main_igamma_term<Scalar>(a, x);
787  // This is independent of mode. If this value is zero,
788  // then the function value is zero. If the function value is zero,
789  // then we are in a neighborhood where the function value evaluates to zero,
790  // so the derivative is zero.
791  if (ax == zero) {
792  return zero;
793  }
794 
795  // continued fraction
796  Scalar y = one - a;
797  Scalar z = x + y + one;
798  Scalar c = zero;
799  Scalar pkm2 = one;
800  Scalar qkm2 = x;
801  Scalar pkm1 = x + one;
802  Scalar qkm1 = z * x;
803  Scalar ans = pkm1 / qkm1;
804 
805  Scalar dpkm2_da = zero;
806  Scalar dqkm2_da = zero;
807  Scalar dpkm1_da = zero;
808  Scalar dqkm1_da = -x;
809  Scalar dans_da = (dpkm1_da - ans * dqkm1_da) / qkm1;
810 
811  for (int i = 0; i < igamma_num_iterations<Scalar, mode>(); i++) {
812  c += one;
813  y += one;
814  z += two;
815 
816  Scalar yc = y * c;
817  Scalar pk = pkm1 * z - pkm2 * yc;
818  Scalar qk = qkm1 * z - qkm2 * yc;
819 
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;
822 
823  if (qk != zero) {
824  Scalar ans_prev = ans;
825  ans = pk / qk;
826 
827  Scalar dans_da_prev = dans_da;
828  dans_da = (dpk_da - ans * dqk_da) / qk;
829 
830  if (mode == VALUE) {
831  if (numext::abs(ans_prev - ans) <= machep * numext::abs(ans)) {
832  break;
833  }
834  } else {
835  if (numext::abs(dans_da - dans_da_prev) <= machep) {
836  break;
837  }
838  }
839  }
840 
841  pkm2 = pkm1;
842  pkm1 = pk;
843  qkm2 = qkm1;
844  qkm1 = qk;
845 
846  dpkm2_da = dpkm1_da;
847  dpkm1_da = dpk_da;
848  dqkm2_da = dqkm1_da;
849  dqkm1_da = dqk_da;
850 
851  if (numext::abs(pk) > big) {
852  pkm2 *= biginv;
853  pkm1 *= biginv;
854  qkm2 *= biginv;
855  qkm1 *= biginv;
856 
857  dpkm2_da *= biginv;
858  dpkm1_da *= biginv;
859  dqkm2_da *= biginv;
860  dqkm1_da *= biginv;
861  }
862  }
863 
864  /* Compute x**a * exp(-x) / gamma(a) */
865  Scalar dlogax_da = numext::log(x) - digamma_impl<Scalar>::run(a);
866  Scalar dax_da = ax * dlogax_da;
867 
868  switch (mode) {
869  case VALUE:
870  return ans * ax;
871  case DERIVATIVE:
872  return ans * dax_da + dans_da * ax;
873  case SAMPLE_DERIVATIVE:
874  default: // this is needed to suppress clang warning
875  return -(dans_da + ans * dlogax_da) * x;
876  }
877  }
878 };
879 
880 template <typename Scalar, IgammaComputationMode mode>
881 struct igamma_series_impl {
882  /* Computes igam(a, x) or its derivative (depending on the mode)
883  * using the series expansion of the incomplete Gamma function.
884  *
885  * Preconditions:
886  * x > 0
887  * a > 0
888  * !(x > 1 && x > a)
889  */
891  static Scalar run(Scalar a, Scalar x) {
892  const Scalar zero = 0;
893  const Scalar one = 1;
894  const Scalar machep = cephes_helper<Scalar>::machep();
895 
896  Scalar ax = main_igamma_term<Scalar>(a, x);
897 
898  // This is independent of mode. If this value is zero,
899  // then the function value is zero. If the function value is zero,
900  // then we are in a neighborhood where the function value evaluates to zero,
901  // so the derivative is zero.
902  if (ax == zero) {
903  return zero;
904  }
905 
906  ax /= a;
907 
908  /* power series */
909  Scalar r = a;
910  Scalar c = one;
911  Scalar ans = one;
912 
913  Scalar dc_da = zero;
914  Scalar dans_da = zero;
915 
916  for (int i = 0; i < igamma_num_iterations<Scalar, mode>(); i++) {
917  r += one;
918  Scalar term = x / r;
919  Scalar dterm_da = -x / (r * r);
920  dc_da = term * dc_da + dterm_da * c;
921  dans_da += dc_da;
922  c *= term;
923  ans += c;
924 
925  if (mode == VALUE) {
926  if (c <= machep * ans) {
927  break;
928  }
929  } else {
930  if (numext::abs(dc_da) <= machep * numext::abs(dans_da)) {
931  break;
932  }
933  }
934  }
935 
936  Scalar dlogax_da = numext::log(x) - digamma_impl<Scalar>::run(a + one);
937  Scalar dax_da = ax * dlogax_da;
938 
939  switch (mode) {
940  case VALUE:
941  return ans * ax;
942  case DERIVATIVE:
943  return ans * dax_da + dans_da * ax;
944  case SAMPLE_DERIVATIVE:
945  default: // this is needed to suppress clang warning
946  return -(dans_da + ans * dlogax_da) * x / a;
947  }
948  }
949 };
950 
951 #if !EIGEN_HAS_C99_MATH
952 
953 template <typename Scalar>
954 struct igammac_impl {
955  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
956  THIS_TYPE_IS_NOT_SUPPORTED)
957 
958  EIGEN_DEVICE_FUNC static Scalar run(Scalar a, Scalar x) {
959  return Scalar(0);
960  }
961 };
962 
963 #else
964 
965 template <typename Scalar>
966 struct igammac_impl {
968  static Scalar run(Scalar a, Scalar x) {
969  /* igamc()
970  *
971  * Incomplete gamma integral (modified for Eigen)
972  *
973  *
974  *
975  * SYNOPSIS:
976  *
977  * double a, x, y, igamc();
978  *
979  * y = igamc( a, x );
980  *
981  * DESCRIPTION:
982  *
983  * The function is defined by
984  *
985  *
986  * igamc(a,x) = 1 - igam(a,x)
987  *
988  * inf.
989  * -
990  * 1 | | -t a-1
991  * = ----- | e t dt.
992  * - | |
993  * | (a) -
994  * x
995  *
996  *
997  * In this implementation both arguments must be positive.
998  * The integral is evaluated by either a power series or
999  * continued fraction expansion, depending on the relative
1000  * values of a and x.
1001  *
1002  * ACCURACY (float):
1003  *
1004  * Relative error:
1005  * arithmetic domain # trials peak rms
1006  * IEEE 0,30 30000 7.8e-6 5.9e-7
1007  *
1008  *
1009  * ACCURACY (double):
1010  *
1011  * Tested at random a, x.
1012  * a x Relative error:
1013  * arithmetic domain domain # trials peak rms
1014  * IEEE 0.5,100 0,100 200000 1.9e-14 1.7e-15
1015  * IEEE 0.01,0.5 0,100 200000 1.4e-13 1.6e-15
1016  *
1017  */
1018  /*
1019  Cephes Math Library Release 2.2: June, 1992
1020  Copyright 1985, 1987, 1992 by Stephen L. Moshier
1021  Direct inquiries to 30 Frost Street, Cambridge, MA 02140
1022  */
1023  const Scalar zero = 0;
1024  const Scalar one = 1;
1025  const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1026 
1027  if ((x < zero) || (a <= zero)) {
1028  // domain error
1029  return nan;
1030  }
1031 
1032  if ((numext::isnan)(a) || (numext::isnan)(x)) { // propagate nans
1033  return nan;
1034  }
1035 
1036  if ((x < one) || (x < a)) {
1037  return (one - igamma_series_impl<Scalar, VALUE>::run(a, x));
1038  }
1039 
1040  return igammac_cf_impl<Scalar, VALUE>::run(a, x);
1041  }
1042 };
1043 
1044 #endif // EIGEN_HAS_C99_MATH
1045 
1046 
1050 #if !EIGEN_HAS_C99_MATH
1051 
1052 template <typename Scalar, IgammaComputationMode mode>
1053 struct igamma_generic_impl {
1054  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
1055  THIS_TYPE_IS_NOT_SUPPORTED)
1056 
1057  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(Scalar a, Scalar x) {
1058  return Scalar(0);
1059  }
1060 };
1061 
1062 #else
1063 
1064 template <typename Scalar, IgammaComputationMode mode>
1065 struct igamma_generic_impl {
1067  static Scalar run(Scalar a, Scalar x) {
1068  /* Depending on the mode, returns
1069  * - VALUE: incomplete Gamma function igamma(a, x)
1070  * - DERIVATIVE: derivative of incomplete Gamma function d/da igamma(a, x)
1071  * - SAMPLE_DERIVATIVE: implicit derivative of a Gamma random variable
1072  * x ~ Gamma(x | a, 1), dx/da = -1 / Gamma(x | a, 1) * d igamma(a, x) / dx
1073  *
1074  * Derivatives are implemented by forward-mode differentiation.
1075  */
1076  const Scalar zero = 0;
1077  const Scalar one = 1;
1078  const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1079 
1080  if (x == zero) return zero;
1081 
1082  if ((x < zero) || (a <= zero)) { // domain error
1083  return nan;
1084  }
1085 
1086  if ((numext::isnan)(a) || (numext::isnan)(x)) { // propagate nans
1087  return nan;
1088  }
1089 
1090  if ((x > one) && (x > a)) {
1091  Scalar ret = igammac_cf_impl<Scalar, mode>::run(a, x);
1092  if (mode == VALUE) {
1093  return one - ret;
1094  } else {
1095  return -ret;
1096  }
1097  }
1098 
1099  return igamma_series_impl<Scalar, mode>::run(a, x);
1100  }
1101 };
1102 
1103 #endif // EIGEN_HAS_C99_MATH
1104 
1105 template <typename Scalar>
1106 struct igamma_retval {
1107  typedef Scalar type;
1108 };
1109 
1110 template <typename Scalar>
1111 struct igamma_impl : igamma_generic_impl<Scalar, VALUE> {
1112  /* igam()
1113  * Incomplete gamma integral.
1114  *
1115  * The CDF of Gamma(a, 1) random variable at the point x.
1116  *
1117  * Accuracy estimation. For each a in [10^-2, 10^-1...10^3] we sample
1118  * 50 Gamma random variables x ~ Gamma(x | a, 1), a total of 300 points.
1119  * The ground truth is computed by mpmath. Mean absolute error:
1120  * float: 1.26713e-05
1121  * double: 2.33606e-12
1122  *
1123  * Cephes documentation below.
1124  *
1125  * SYNOPSIS:
1126  *
1127  * double a, x, y, igam();
1128  *
1129  * y = igam( a, x );
1130  *
1131  * DESCRIPTION:
1132  *
1133  * The function is defined by
1134  *
1135  * x
1136  * -
1137  * 1 | | -t a-1
1138  * igam(a,x) = ----- | e t dt.
1139  * - | |
1140  * | (a) -
1141  * 0
1142  *
1143  *
1144  * In this implementation both arguments must be positive.
1145  * The integral is evaluated by either a power series or
1146  * continued fraction expansion, depending on the relative
1147  * values of a and x.
1148  *
1149  * ACCURACY (double):
1150  *
1151  * Relative error:
1152  * arithmetic domain # trials peak rms
1153  * IEEE 0,30 200000 3.6e-14 2.9e-15
1154  * IEEE 0,100 300000 9.9e-14 1.5e-14
1155  *
1156  *
1157  * ACCURACY (float):
1158  *
1159  * Relative error:
1160  * arithmetic domain # trials peak rms
1161  * IEEE 0,30 20000 7.8e-6 5.9e-7
1162  *
1163  */
1164  /*
1165  Cephes Math Library Release 2.2: June, 1992
1166  Copyright 1985, 1987, 1992 by Stephen L. Moshier
1167  Direct inquiries to 30 Frost Street, Cambridge, MA 02140
1168  */
1169 
1170  /* left tail of incomplete gamma function:
1171  *
1172  * inf. k
1173  * a -x - x
1174  * x e > ----------
1175  * - -
1176  * k=0 | (a+k+1)
1177  *
1178  */
1179 };
1180 
1181 template <typename Scalar>
1182 struct igamma_der_a_retval : igamma_retval<Scalar> {};
1183 
1184 template <typename Scalar>
1185 struct igamma_der_a_impl : igamma_generic_impl<Scalar, DERIVATIVE> {
1186  /* Derivative of the incomplete Gamma function with respect to a.
1187  *
1188  * Computes d/da igamma(a, x) by forward differentiation of the igamma code.
1189  *
1190  * Accuracy estimation. For each a in [10^-2, 10^-1...10^3] we sample
1191  * 50 Gamma random variables x ~ Gamma(x | a, 1), a total of 300 points.
1192  * The ground truth is computed by mpmath. Mean absolute error:
1193  * float: 6.17992e-07
1194  * double: 4.60453e-12
1195  *
1196  * Reference:
1197  * R. Moore. "Algorithm AS 187: Derivatives of the incomplete gamma
1198  * integral". Journal of the Royal Statistical Society. 1982
1199  */
1200 };
1201 
1202 template <typename Scalar>
1203 struct gamma_sample_der_alpha_retval : igamma_retval<Scalar> {};
1204 
1205 template <typename Scalar>
1206 struct gamma_sample_der_alpha_impl
1207  : igamma_generic_impl<Scalar, SAMPLE_DERIVATIVE> {
1208  /* Derivative of a Gamma random variable sample with respect to alpha.
1209  *
1210  * Consider a sample of a Gamma random variable with the concentration
1211  * parameter alpha: sample ~ Gamma(alpha, 1). The reparameterization
1212  * derivative that we want to compute is dsample / dalpha =
1213  * d igammainv(alpha, u) / dalpha, where u = igamma(alpha, sample).
1214  * However, this formula is numerically unstable and expensive, so instead
1215  * we use implicit differentiation:
1216  *
1217  * igamma(alpha, sample) = u, where u ~ Uniform(0, 1).
1218  * Apply d / dalpha to both sides:
1219  * d igamma(alpha, sample) / dalpha
1220  * + d igamma(alpha, sample) / dsample * dsample/dalpha = 0
1221  * d igamma(alpha, sample) / dalpha
1222  * + Gamma(sample | alpha, 1) dsample / dalpha = 0
1223  * dsample/dalpha = - (d igamma(alpha, sample) / dalpha)
1224  * / Gamma(sample | alpha, 1)
1225  *
1226  * Here Gamma(sample | alpha, 1) is the PDF of the Gamma distribution
1227  * (note that the derivative of the CDF w.r.t. sample is the PDF).
1228  * See the reference below for more details.
1229  *
1230  * The derivative of igamma(alpha, sample) is computed by forward
1231  * differentiation of the igamma code. Division by the Gamma PDF is performed
1232  * in the same code, increasing the accuracy and speed due to cancellation
1233  * of some terms.
1234  *
1235  * Accuracy estimation. For each alpha in [10^-2, 10^-1...10^3] we sample
1236  * 50 Gamma random variables sample ~ Gamma(sample | alpha, 1), a total of 300
1237  * points. The ground truth is computed by mpmath. Mean absolute error:
1238  * float: 2.1686e-06
1239  * double: 1.4774e-12
1240  *
1241  * Reference:
1242  * M. Figurnov, S. Mohamed, A. Mnih "Implicit Reparameterization Gradients".
1243  * 2018
1244  */
1245 };
1246 
1247 
1251 template <typename Scalar>
1252 struct zeta_retval {
1253  typedef Scalar type;
1254 };
1255 
1256 template <typename Scalar>
1257 struct zeta_impl_series {
1258  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
1259  THIS_TYPE_IS_NOT_SUPPORTED)
1260 
1261  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(const Scalar) {
1262  return Scalar(0);
1263  }
1264 };
1265 
1266 template <>
1267 struct zeta_impl_series<float> {
1269  static EIGEN_STRONG_INLINE bool run(float& a, float& b, float& s, const float x, const float machep) {
1270  int i = 0;
1271  while(i < 9)
1272  {
1273  i += 1;
1274  a += 1.0f;
1275  b = numext::pow( a, -x );
1276  s += b;
1277  if( numext::abs(b/s) < machep )
1278  return true;
1279  }
1280 
1281  //Return whether we are done
1282  return false;
1283  }
1284 };
1285 
1286 template <>
1287 struct zeta_impl_series<double> {
1289  static EIGEN_STRONG_INLINE bool run(double& a, double& b, double& s, const double x, const double machep) {
1290  int i = 0;
1291  while( (i < 9) || (a <= 9.0) )
1292  {
1293  i += 1;
1294  a += 1.0;
1295  b = numext::pow( a, -x );
1296  s += b;
1297  if( numext::abs(b/s) < machep )
1298  return true;
1299  }
1300 
1301  //Return whether we are done
1302  return false;
1303  }
1304 };
1305 
1306 template <typename Scalar>
1307 struct zeta_impl {
1309  static Scalar run(Scalar x, Scalar q) {
1310  /* zeta.c
1311  *
1312  * Riemann zeta function of two arguments
1313  *
1314  *
1315  *
1316  * SYNOPSIS:
1317  *
1318  * double x, q, y, zeta();
1319  *
1320  * y = zeta( x, q );
1321  *
1322  *
1323  *
1324  * DESCRIPTION:
1325  *
1326  *
1327  *
1328  * inf.
1329  * - -x
1330  * zeta(x,q) = > (k+q)
1331  * -
1332  * k=0
1333  *
1334  * where x > 1 and q is not a negative integer or zero.
1335  * The Euler-Maclaurin summation formula is used to obtain
1336  * the expansion
1337  *
1338  * n
1339  * - -x
1340  * zeta(x,q) = > (k+q)
1341  * -
1342  * k=1
1343  *
1344  * 1-x inf. B x(x+1)...(x+2j)
1345  * (n+q) 1 - 2j
1346  * + --------- - ------- + > --------------------
1347  * x-1 x - x+2j+1
1348  * 2(n+q) j=1 (2j)! (n+q)
1349  *
1350  * where the B2j are Bernoulli numbers. Note that (see zetac.c)
1351  * zeta(x,1) = zetac(x) + 1.
1352  *
1353  *
1354  *
1355  * ACCURACY:
1356  *
1357  * Relative error for single precision:
1358  * arithmetic domain # trials peak rms
1359  * IEEE 0,25 10000 6.9e-7 1.0e-7
1360  *
1361  * Large arguments may produce underflow in powf(), in which
1362  * case the results are inaccurate.
1363  *
1364  * REFERENCE:
1365  *
1366  * Gradshteyn, I. S., and I. M. Ryzhik, Tables of Integrals,
1367  * Series, and Products, p. 1073; Academic Press, 1980.
1368  *
1369  */
1370 
1371  int i;
1372  Scalar p, r, a, b, k, s, t, w;
1373 
1374  const Scalar A[] = {
1375  Scalar(12.0),
1376  Scalar(-720.0),
1377  Scalar(30240.0),
1378  Scalar(-1209600.0),
1379  Scalar(47900160.0),
1380  Scalar(-1.8924375803183791606e9), /*1.307674368e12/691*/
1381  Scalar(7.47242496e10),
1382  Scalar(-2.950130727918164224e12), /*1.067062284288e16/3617*/
1383  Scalar(1.1646782814350067249e14), /*5.109094217170944e18/43867*/
1384  Scalar(-4.5979787224074726105e15), /*8.028576626982912e20/174611*/
1385  Scalar(1.8152105401943546773e17), /*1.5511210043330985984e23/854513*/
1386  Scalar(-7.1661652561756670113e18) /*1.6938241367317436694528e27/236364091*/
1387  };
1388 
1389  const Scalar maxnum = NumTraits<Scalar>::infinity();
1390  const Scalar zero = Scalar(0.0), half = Scalar(0.5), one = Scalar(1.0);
1391  const Scalar machep = cephes_helper<Scalar>::machep();
1392  const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1393 
1394  if( x == one )
1395  return maxnum;
1396 
1397  if( x < one )
1398  {
1399  return nan;
1400  }
1401 
1402  if( q <= zero )
1403  {
1404  if(q == numext::floor(q))
1405  {
1406  if (x == numext::floor(x) && long(x) % 2 == 0) {
1407  return maxnum;
1408  }
1409  else {
1410  return nan;
1411  }
1412  }
1413  p = x;
1414  r = numext::floor(p);
1415  if (p != r)
1416  return nan;
1417  }
1418 
1419  /* Permit negative q but continue sum until n+q > +9 .
1420  * This case should be handled by a reflection formula.
1421  * If q<0 and x is an integer, there is a relation to
1422  * the polygamma function.
1423  */
1424  s = numext::pow( q, -x );
1425  a = q;
1426  b = zero;
1427  // Run the summation in a helper function that is specific to the floating precision
1428  if (zeta_impl_series<Scalar>::run(a, b, s, x, machep)) {
1429  return s;
1430  }
1431 
1432  // If b is zero, then the tail sum will also end up being zero.
1433  // Exiting early here can prevent NaNs for some large inputs, where
1434  // the tail sum computed below has term `a` which can overflow to `inf`.
1435  if (numext::equal_strict(b, zero)) {
1436  return s;
1437  }
1438 
1439  w = a;
1440  s += b*w/(x-one);
1441  s -= half * b;
1442  a = one;
1443  k = zero;
1444 
1445  for( i=0; i<12; i++ )
1446  {
1447  a *= x + k;
1448  b /= w;
1449  t = a*b/A[i];
1450  s = s + t;
1451  t = numext::abs(t/s);
1452  if( t < machep ) {
1453  break;
1454  }
1455  k += one;
1456  a *= x + k;
1457  b /= w;
1458  k += one;
1459  }
1460  return s;
1461  }
1462 };
1463 
1464 
1468 template <typename Scalar>
1469 struct polygamma_retval {
1470  typedef Scalar type;
1471 };
1472 
1473 #if !EIGEN_HAS_C99_MATH
1474 
1475 template <typename Scalar>
1476 struct polygamma_impl {
1477  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
1478  THIS_TYPE_IS_NOT_SUPPORTED)
1479 
1480  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(Scalar n, Scalar x) {
1481  return Scalar(0);
1482  }
1483 };
1484 
1485 #else
1486 
1487 template <typename Scalar>
1488 struct polygamma_impl {
1490  static Scalar run(Scalar n, Scalar x) {
1491  Scalar zero = 0.0, one = 1.0;
1492  Scalar nplus = n + one;
1493  const Scalar nan = NumTraits<Scalar>::quiet_NaN();
1494 
1495  // Check that n is a non-negative integer
1496  if (numext::floor(n) != n || n < zero) {
1497  return nan;
1498  }
1499  // Just return the digamma function for n = 0
1500  else if (n == zero) {
1501  return digamma_impl<Scalar>::run(x);
1502  }
1503  // Use the same implementation as scipy
1504  else {
1505  Scalar factorial = numext::exp(lgamma_impl<Scalar>::run(nplus));
1506  return numext::pow(-one, nplus) * factorial * zeta_impl<Scalar>::run(nplus, x);
1507  }
1508  }
1509 };
1510 
1511 #endif // EIGEN_HAS_C99_MATH
1512 
1513 
1517 template <typename Scalar>
1518 struct betainc_retval {
1519  typedef Scalar type;
1520 };
1521 
1522 #if !EIGEN_HAS_C99_MATH
1523 
1524 template <typename Scalar>
1525 struct betainc_impl {
1526  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
1527  THIS_TYPE_IS_NOT_SUPPORTED)
1528 
1529  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(Scalar a, Scalar b, Scalar x) {
1530  return Scalar(0);
1531  }
1532 };
1533 
1534 #else
1535 
1536 template <typename Scalar>
1537 struct betainc_impl {
1538  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, Scalar>::value == false),
1539  THIS_TYPE_IS_NOT_SUPPORTED)
1540 
1541  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(Scalar, Scalar, Scalar) {
1542  /* betaincf.c
1543  *
1544  * Incomplete beta integral
1545  *
1546  *
1547  * SYNOPSIS:
1548  *
1549  * float a, b, x, y, betaincf();
1550  *
1551  * y = betaincf( a, b, x );
1552  *
1553  *
1554  * DESCRIPTION:
1555  *
1556  * Returns incomplete beta integral of the arguments, evaluated
1557  * from zero to x. The function is defined as
1558  *
1559  * x
1560  * - -
1561  * | (a+b) | | a-1 b-1
1562  * ----------- | t (1-t) dt.
1563  * - - | |
1564  * | (a) | (b) -
1565  * 0
1566  *
1567  * The domain of definition is 0 <= x <= 1. In this
1568  * implementation a and b are restricted to positive values.
1569  * The integral from x to 1 may be obtained by the symmetry
1570  * relation
1571  *
1572  * 1 - betainc( a, b, x ) = betainc( b, a, 1-x ).
1573  *
1574  * The integral is evaluated by a continued fraction expansion.
1575  * If a < 1, the function calls itself recursively after a
1576  * transformation to increase a to a+1.
1577  *
1578  * ACCURACY (float):
1579  *
1580  * Tested at random points (a,b,x) with a and b in the indicated
1581  * interval and x between 0 and 1.
1582  *
1583  * arithmetic domain # trials peak rms
1584  * Relative error:
1585  * IEEE 0,30 10000 3.7e-5 5.1e-6
1586  * IEEE 0,100 10000 1.7e-4 2.5e-5
1587  * The useful domain for relative error is limited by underflow
1588  * of the single precision exponential function.
1589  * Absolute error:
1590  * IEEE 0,30 100000 2.2e-5 9.6e-7
1591  * IEEE 0,100 10000 6.5e-5 3.7e-6
1592  *
1593  * Larger errors may occur for extreme ratios of a and b.
1594  *
1595  * ACCURACY (double):
1596  * arithmetic domain # trials peak rms
1597  * IEEE 0,5 10000 6.9e-15 4.5e-16
1598  * IEEE 0,85 250000 2.2e-13 1.7e-14
1599  * IEEE 0,1000 30000 5.3e-12 6.3e-13
1600  * IEEE 0,10000 250000 9.3e-11 7.1e-12
1601  * IEEE 0,100000 10000 8.7e-10 4.8e-11
1602  * Outputs smaller than the IEEE gradual underflow threshold
1603  * were excluded from these statistics.
1604  *
1605  * ERROR MESSAGES:
1606  * message condition value returned
1607  * incbet domain x<0, x>1 nan
1608  * incbet underflow nan
1609  */
1610  return Scalar(0);
1611  }
1612 };
1613 
1614 /* Continued fraction expansion #1 for incomplete beta integral (small_branch = True)
1615  * Continued fraction expansion #2 for incomplete beta integral (small_branch = False)
1616  */
1617 template <typename Scalar>
1618 struct incbeta_cfe {
1619  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, float>::value ||
1620  internal::is_same<Scalar, double>::value),
1621  THIS_TYPE_IS_NOT_SUPPORTED)
1622 
1623  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE Scalar run(Scalar a, Scalar b, Scalar x, bool small_branch) {
1624  const Scalar big = cephes_helper<Scalar>::big();
1625  const Scalar machep = cephes_helper<Scalar>::machep();
1626  const Scalar biginv = cephes_helper<Scalar>::biginv();
1627 
1628  const Scalar zero = 0;
1629  const Scalar one = 1;
1630  const Scalar two = 2;
1631 
1632  Scalar xk, pk, pkm1, pkm2, qk, qkm1, qkm2;
1633  Scalar k1, k2, k3, k4, k5, k6, k7, k8, k26update;
1634  Scalar ans;
1635  int n;
1636 
1637  const int num_iters = (internal::is_same<Scalar, float>::value) ? 100 : 300;
1638  const Scalar thresh =
1639  (internal::is_same<Scalar, float>::value) ? machep : Scalar(3) * machep;
1640  Scalar r = (internal::is_same<Scalar, float>::value) ? zero : one;
1641 
1642  if (small_branch) {
1643  k1 = a;
1644  k2 = a + b;
1645  k3 = a;
1646  k4 = a + one;
1647  k5 = one;
1648  k6 = b - one;
1649  k7 = k4;
1650  k8 = a + two;
1651  k26update = one;
1652  } else {
1653  k1 = a;
1654  k2 = b - one;
1655  k3 = a;
1656  k4 = a + one;
1657  k5 = one;
1658  k6 = a + b;
1659  k7 = a + one;
1660  k8 = a + two;
1661  k26update = -one;
1662  x = x / (one - x);
1663  }
1664 
1665  pkm2 = zero;
1666  qkm2 = one;
1667  pkm1 = one;
1668  qkm1 = one;
1669  ans = one;
1670  n = 0;
1671 
1672  do {
1673  xk = -(x * k1 * k2) / (k3 * k4);
1674  pk = pkm1 + pkm2 * xk;
1675  qk = qkm1 + qkm2 * xk;
1676  pkm2 = pkm1;
1677  pkm1 = pk;
1678  qkm2 = qkm1;
1679  qkm1 = qk;
1680 
1681  xk = (x * k5 * k6) / (k7 * k8);
1682  pk = pkm1 + pkm2 * xk;
1683  qk = qkm1 + qkm2 * xk;
1684  pkm2 = pkm1;
1685  pkm1 = pk;
1686  qkm2 = qkm1;
1687  qkm1 = qk;
1688 
1689  if (qk != zero) {
1690  r = pk / qk;
1691  if (numext::abs(ans - r) < numext::abs(r) * thresh) {
1692  return r;
1693  }
1694  ans = r;
1695  }
1696 
1697  k1 += one;
1698  k2 += k26update;
1699  k3 += two;
1700  k4 += two;
1701  k5 += one;
1702  k6 -= k26update;
1703  k7 += two;
1704  k8 += two;
1705 
1706  if ((numext::abs(qk) + numext::abs(pk)) > big) {
1707  pkm2 *= biginv;
1708  pkm1 *= biginv;
1709  qkm2 *= biginv;
1710  qkm1 *= biginv;
1711  }
1712  if ((numext::abs(qk) < biginv) || (numext::abs(pk) < biginv)) {
1713  pkm2 *= big;
1714  pkm1 *= big;
1715  qkm2 *= big;
1716  qkm1 *= big;
1717  }
1718  } while (++n < num_iters);
1719 
1720  return ans;
1721  }
1722 };
1723 
1724 /* Helper functions depending on the Scalar type */
1725 template <typename Scalar>
1726 struct betainc_helper {};
1727 
1728 template <>
1729 struct betainc_helper<float> {
1730  /* Core implementation, assumes a large (> 1.0) */
1731  EIGEN_DEVICE_FUNC static EIGEN_STRONG_INLINE float incbsa(float aa, float bb,
1732  float xx) {
1733  float ans, a, b, t, x, onemx;
1734  bool reversed_a_b = false;
1735 
1736  onemx = 1.0f - xx;
1737 
1738  /* see if x is greater than the mean */
1739  if (xx > (aa / (aa + bb))) {
1740  reversed_a_b = true;
1741  a = bb;
1742  b = aa;
1743  t = xx;
1744  x = onemx;
1745  } else {
1746  a = aa;
1747  b = bb;
1748  t = onemx;
1749  x = xx;
1750  }
1751 
1752  /* Choose expansion for optimal convergence */
1753  if (b > 10.0f) {
1754  if (numext::abs(b * x / a) < 0.3f) {
1755  t = betainc_helper<float>::incbps(a, b, x);
1756  if (reversed_a_b) t = 1.0f - t;
1757  return t;
1758  }
1759  }
1760 
1761  ans = x * (a + b - 2.0f) / (a - 1.0f);
1762  if (ans < 1.0f) {
1763  ans = incbeta_cfe<float>::run(a, b, x, true /* small_branch */);
1764  t = b * numext::log(t);
1765  } else {
1766  ans = incbeta_cfe<float>::run(a, b, x, false /* small_branch */);
1767  t = (b - 1.0f) * numext::log(t);
1768  }
1769 
1770  t += a * numext::log(x) + lgamma_impl<float>::run(a + b) -
1771  lgamma_impl<float>::run(a) - lgamma_impl<float>::run(b);
1772  t += numext::log(ans / a);
1773  t = numext::exp(t);
1774 
1775  if (reversed_a_b) t = 1.0f - t;
1776  return t;
1777  }
1778 
1780  static EIGEN_STRONG_INLINE float incbps(float a, float b, float x) {
1781  float t, u, y, s;
1782  const float machep = cephes_helper<float>::machep();
1783 
1784  y = a * numext::log(x) + (b - 1.0f) * numext::log1p(-x) - numext::log(a);
1785  y -= lgamma_impl<float>::run(a) + lgamma_impl<float>::run(b);
1786  y += lgamma_impl<float>::run(a + b);
1787 
1788  t = x / (1.0f - x);
1789  s = 0.0f;
1790  u = 1.0f;
1791  do {
1792  b -= 1.0f;
1793  if (b == 0.0f) {
1794  break;
1795  }
1796  a += 1.0f;
1797  u *= t * b / a;
1798  s += u;
1799  } while (numext::abs(u) > machep);
1800 
1801  return numext::exp(y) * (1.0f + s);
1802  }
1803 };
1804 
1805 template <>
1806 struct betainc_impl<float> {
1808  static float run(float a, float b, float x) {
1809  const float nan = NumTraits<float>::quiet_NaN();
1810  float ans, t;
1811 
1812  if (a <= 0.0f) return nan;
1813  if (b <= 0.0f) return nan;
1814  if ((x <= 0.0f) || (x >= 1.0f)) {
1815  if (x == 0.0f) return 0.0f;
1816  if (x == 1.0f) return 1.0f;
1817  // mtherr("betaincf", DOMAIN);
1818  return nan;
1819  }
1820 
1821  /* transformation for small aa */
1822  if (a <= 1.0f) {
1823  ans = betainc_helper<float>::incbsa(a + 1.0f, b, x);
1824  t = a * numext::log(x) + b * numext::log1p(-x) +
1825  lgamma_impl<float>::run(a + b) - lgamma_impl<float>::run(a + 1.0f) -
1826  lgamma_impl<float>::run(b);
1827  return (ans + numext::exp(t));
1828  } else {
1829  return betainc_helper<float>::incbsa(a, b, x);
1830  }
1831  }
1832 };
1833 
1834 template <>
1835 struct betainc_helper<double> {
1837  static EIGEN_STRONG_INLINE double incbps(double a, double b, double x) {
1838  const double machep = cephes_helper<double>::machep();
1839 
1840  double s, t, u, v, n, t1, z, ai;
1841 
1842  ai = 1.0 / a;
1843  u = (1.0 - b) * x;
1844  v = u / (a + 1.0);
1845  t1 = v;
1846  t = u;
1847  n = 2.0;
1848  s = 0.0;
1849  z = machep * ai;
1850  while (numext::abs(v) > z) {
1851  u = (n - b) * x / n;
1852  t *= u;
1853  v = t / (a + n);
1854  s += v;
1855  n += 1.0;
1856  }
1857  s += t1;
1858  s += ai;
1859 
1860  u = a * numext::log(x);
1861  // TODO: gamma() is not directly implemented in Eigen.
1862  /*
1863  if ((a + b) < maxgam && numext::abs(u) < maxlog) {
1864  t = gamma(a + b) / (gamma(a) * gamma(b));
1865  s = s * t * pow(x, a);
1866  }
1867  */
1868  t = lgamma_impl<double>::run(a + b) - lgamma_impl<double>::run(a) -
1869  lgamma_impl<double>::run(b) + u + numext::log(s);
1870  return s = numext::exp(t);
1871  }
1872 };
1873 
1874 template <>
1875 struct betainc_impl<double> {
1877  static double run(double aa, double bb, double xx) {
1878  const double nan = NumTraits<double>::quiet_NaN();
1879  const double machep = cephes_helper<double>::machep();
1880  // const double maxgam = 171.624376956302725;
1881 
1882  double a, b, t, x, xc, w, y;
1883  bool reversed_a_b = false;
1884 
1885  if (aa <= 0.0 || bb <= 0.0) {
1886  return nan; // goto domerr;
1887  }
1888 
1889  if ((xx <= 0.0) || (xx >= 1.0)) {
1890  if (xx == 0.0) return (0.0);
1891  if (xx == 1.0) return (1.0);
1892  // mtherr("incbet", DOMAIN);
1893  return nan;
1894  }
1895 
1896  if ((bb * xx) <= 1.0 && xx <= 0.95) {
1897  return betainc_helper<double>::incbps(aa, bb, xx);
1898  }
1899 
1900  w = 1.0 - xx;
1901 
1902  /* Reverse a and b if x is greater than the mean. */
1903  if (xx > (aa / (aa + bb))) {
1904  reversed_a_b = true;
1905  a = bb;
1906  b = aa;
1907  xc = xx;
1908  x = w;
1909  } else {
1910  a = aa;
1911  b = bb;
1912  xc = w;
1913  x = xx;
1914  }
1915 
1916  if (reversed_a_b && (b * x) <= 1.0 && x <= 0.95) {
1917  t = betainc_helper<double>::incbps(a, b, x);
1918  if (t <= machep) {
1919  t = 1.0 - machep;
1920  } else {
1921  t = 1.0 - t;
1922  }
1923  return t;
1924  }
1925 
1926  /* Choose expansion for better convergence. */
1927  y = x * (a + b - 2.0) - (a - 1.0);
1928  if (y < 0.0) {
1929  w = incbeta_cfe<double>::run(a, b, x, true /* small_branch */);
1930  } else {
1931  w = incbeta_cfe<double>::run(a, b, x, false /* small_branch */) / xc;
1932  }
1933 
1934  /* Multiply w by the factor
1935  a b _ _ _
1936  x (1-x) | (a+b) / ( a | (a) | (b) ) . */
1937 
1938  y = a * numext::log(x);
1939  t = b * numext::log(xc);
1940  // TODO: gamma is not directly implemented in Eigen.
1941  /*
1942  if ((a + b) < maxgam && numext::abs(y) < maxlog && numext::abs(t) < maxlog)
1943  {
1944  t = pow(xc, b);
1945  t *= pow(x, a);
1946  t /= a;
1947  t *= w;
1948  t *= gamma(a + b) / (gamma(a) * gamma(b));
1949  } else {
1950  */
1951  /* Resort to logarithms. */
1952  y += t + lgamma_impl<double>::run(a + b) - lgamma_impl<double>::run(a) -
1953  lgamma_impl<double>::run(b);
1954  y += numext::log(w / a);
1955  t = numext::exp(y);
1956 
1957  /* } */
1958  // done:
1959 
1960  if (reversed_a_b) {
1961  if (t <= machep) {
1962  t = 1.0 - machep;
1963  } else {
1964  t = 1.0 - t;
1965  }
1966  }
1967  return t;
1968  }
1969 };
1970 
1971 #endif // EIGEN_HAS_C99_MATH
1972 
1973 } // end namespace internal
1974 
1975 namespace numext {
1976 
1977 template <typename Scalar>
1979  lgamma(const Scalar& x) {
1980  return EIGEN_MATHFUNC_IMPL(lgamma, Scalar)::run(x);
1981 }
1982 
1983 template <typename Scalar>
1985  digamma(const Scalar& x) {
1986  return EIGEN_MATHFUNC_IMPL(digamma, Scalar)::run(x);
1987 }
1988 
1989 template <typename Scalar>
1991 zeta(const Scalar& x, const Scalar& q) {
1992  return EIGEN_MATHFUNC_IMPL(zeta, Scalar)::run(x, q);
1993 }
1994 
1995 template <typename Scalar>
1997 polygamma(const Scalar& n, const Scalar& x) {
1998  return EIGEN_MATHFUNC_IMPL(polygamma, Scalar)::run(n, x);
1999 }
2000 
2001 template <typename Scalar>
2003  erf(const Scalar& x) {
2004  return EIGEN_MATHFUNC_IMPL(erf, Scalar)::run(x);
2005 }
2006 
2007 template <typename Scalar>
2009  erfc(const Scalar& x) {
2010  return EIGEN_MATHFUNC_IMPL(erfc, Scalar)::run(x);
2011 }
2012 
2013 template <typename Scalar>
2015  ndtri(const Scalar& x) {
2016  return EIGEN_MATHFUNC_IMPL(ndtri, Scalar)::run(x);
2017 }
2018 
2019 template <typename Scalar>
2021  igamma(const Scalar& a, const Scalar& x) {
2022  return EIGEN_MATHFUNC_IMPL(igamma, Scalar)::run(a, x);
2023 }
2024 
2025 template <typename Scalar>
2027  igamma_der_a(const Scalar& a, const Scalar& x) {
2028  return EIGEN_MATHFUNC_IMPL(igamma_der_a, Scalar)::run(a, x);
2029 }
2030 
2031 template <typename Scalar>
2033  gamma_sample_der_alpha(const Scalar& a, const Scalar& x) {
2034  return EIGEN_MATHFUNC_IMPL(gamma_sample_der_alpha, Scalar)::run(a, x);
2035 }
2036 
2037 template <typename Scalar>
2039  igammac(const Scalar& a, const Scalar& x) {
2040  return EIGEN_MATHFUNC_IMPL(igammac, Scalar)::run(a, x);
2041 }
2042 
2043 template <typename Scalar>
2045  betainc(const Scalar& a, const Scalar& b, const Scalar& x) {
2046  return EIGEN_MATHFUNC_IMPL(betainc, Scalar)::run(a, b, x);
2047 }
2048 
2049 } // end namespace numext
2050 } // end namespace Eigen
2051 
2052 #endif // EIGEN_SPECIAL_FUNCTIONS_H
ArrayXXi a
Array< int, Dynamic, 1 > v
int n
SparseMatrix< double > A(n, n)
int i
Array33i c
#define EIGEN_ALWAYS_INLINE
#define EIGEN_DEVICE_FUNC
#define eigen_assert(x)
#define EIGEN_MATHFUNC_IMPL(func, scalar)
Vector3f p0
Vector3f p1
RowVector3d w
#define EIGEN_STATIC_ASSERT(X, MSG)
float * p
static Scalar main_igamma_term(Scalar a, Scalar x)
Packet pselect(const Packet &mask, const Packet &a, const Packet &b)
bool pmul(const bool &a, const bool &b)
T generic_fast_erf_float(const T &x)
const Scalar & y
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet plog(const Packet &a)
Packet pabs(const Packet &a)
Packet pcmp_lt(const Packet &a, const Packet &b)
double flipsign< double >(const double &should_flipsign, const double &x)
T flipsign(const T &should_flipsign, const T &x)
Packet pcmp_le(const Packet &a, const Packet &b)
T generic_ndtri_gt_exp_neg_two(const T &b)
EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS Packet psqrt(const Packet &a)
float flipsign< float >(const float &should_flipsign, const float &x)
Packet psub(const Packet &a, const Packet &b)
Packet pcmp_eq(const Packet &a, const Packet &b)
EIGEN_ALWAYS_INLINE T generic_ndtri(const T &a)
T generic_ndtri_lt_exp_neg_two(const T &b, const T &should_flipsign)
bool psign(const bool &a)
Packet pdiv(const Packet &a, const Packet &b)
Packet preciprocal(const Packet &a)
Packet pmadd(const Packet &a, const Packet &b, const Packet &c)
EIGEN_ALWAYS_INLINE T tan(const T &x)
EIGEN_ALWAYS_INLINE bool() isinf(const Eigen::bfloat16 &h)
EIGEN_MATHFUNC_RETVAL(abs2, Scalar) abs2(const Scalar &x)
internal::pow_impl< ScalarX, ScalarY >::result_type pow(const ScalarX &x, const ScalarY &y)
bool equal_strict(const double &x, const double &y)
EIGEN_ALWAYS_INLINE T exp(const T &x)
Scalar() floor(const Scalar &x)
EIGEN_ALWAYS_INLINE bool() isnan(const Eigen::bfloat16 &h)
EIGEN_ALWAYS_INLINE std::enable_if_t< NumTraits< T >::IsSigned||NumTraits< T >::IsComplex, typename NumTraits< T >::Real > abs(const T &x)
EIGEN_ALWAYS_INLINE T log(const T &x)
: TensorContractionSycl.h, provides various tensor contraction kernel for SYCL backend
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_erfc_op< typename Derived::Scalar >, const Derived > erfc(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_igammac_op< typename Derived::Scalar >, const Derived, const ExponentDerived > igammac(const Eigen::ArrayBase< Derived > &a, const Eigen::ArrayBase< ExponentDerived > &x)
const TensorCwiseTernaryOp< internal::scalar_betainc_op< typename XDerived::Scalar >, const ADerived, const BDerived, const XDerived > betainc(const ADerived &a, const BDerived &b, const XDerived &x)
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_igamma_op< typename Derived::Scalar >, const Derived, const ExponentDerived > igamma(const Eigen::ArrayBase< Derived > &a, const Eigen::ArrayBase< ExponentDerived > &x)
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_polygamma_op< typename DerivedX::Scalar >, const DerivedN, const DerivedX > polygamma(const Eigen::ArrayBase< DerivedN > &n, const Eigen::ArrayBase< DerivedX > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_ndtri_op< typename Derived::Scalar >, const Derived > ndtri(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_lgamma_op< typename Derived::Scalar >, const Derived > lgamma(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_erf_op< typename Derived::Scalar >, const Derived > erf(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_igamma_der_a_op< typename Derived::Scalar >, const Derived, const ExponentDerived > igamma_der_a(const Eigen::ArrayBase< Derived > &a, const Eigen::ArrayBase< ExponentDerived > &x)
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)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_digamma_op< typename Derived::Scalar >, const Derived > digamma(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseBinaryOp< Eigen::internal::scalar_gamma_sample_der_alpha_op< typename AlphaDerived::Scalar >, const AlphaDerived, const SampleDerived > gamma_sample_der_alpha(const Eigen::ArrayBase< AlphaDerived > &alpha, const Eigen::ArrayBase< SampleDerived > &sample)