Stan Math Library  2.10.0
reverse mode automatic differentiation
grad_reg_inc_gamma.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_GRAD_REG_INC_GAMMA_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_GRAD_REG_INC_GAMMA_HPP
3 
5 #include <cmath>
6 #include <stdexcept>
7 
8 namespace stan {
9  namespace math {
10 
11  // Gradient of the regularized incomplete gamma functions igamma(a, g)
12  // Precomputed values
13  // g = boost::math::tgamma(a)
14  // dig = boost::math::digamma(a)
15  template<typename T>
16  T grad_reg_inc_gamma(T a, T z, T g, T dig, T precision = 1e-6) {
17  using boost::math::isinf;
18  using stan::math::gamma_p;
19  using std::domain_error;
20  using std::exp;
21  using std::fabs;
22  using std::log;
23 
24  T S = 0;
25  T s = 1;
26  T l = log(z);
27  int k = 0;
28  T delta = s / (a * a);
29  while (fabs(delta) > precision) {
30  S += delta;
31  ++k;
32  s *= - z / k;
33  delta = s / ((k + a) * (k + a));
34  if (isinf(delta))
35  throw domain_error("stan::math::gradRegIncGamma not converging");
36  }
37  return gamma_p(a, z) * ( dig - l ) + exp( a * l ) * S / g;
38  }
39 
40  }
41 }
42 #endif
int isinf(const stan::math::var &a)
Checks if the given number is infinite.
Definition: std_isinf.hpp:18
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
T grad_reg_inc_gamma(T a, T z, T g, T dig, T precision=1e-6)
bool isinf(const stan::math::var &v)
Checks if the given number is infinite.
Definition: boost_isinf.hpp:22
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:15
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95

     [ Stan Home Page ] © 2011–2016, Stan Development Team.