All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
grad_reg_inc_gamma.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_SCAL_FUN_GRAD_REG_INC_GAMMA_HPP
#define STAN_MATH_PRIM_SCAL_FUN_GRAD_REG_INC_GAMMA_HPP
#include <stan/math/prim/scal/fun/gamma_p.hpp>
#include <cmath>
namespace stan {
namespace math {
// Gradient of the regularized incomplete gamma functions igamma(a, g)
// Precomputed values
// g = boost::math::tgamma(a)
// dig = boost::math::digamma(a)
template<typename T>
T grad_reg_inc_gamma(T a, T z, T g, T dig, T precision = 1e-6) {
using boost::math::isinf;
using stan::math::gamma_p;
using std::domain_error;
using std::exp;
using std::fabs;
using std::log;
T S = 0;
T s = 1;
T l = log(z);
int k = 0;
T delta = s / (a * a);
while (fabs(delta) > precision) {
S += delta;
++k;
s *= - z / k;
delta = s / ((k + a) * (k + a));
if (isinf(delta))
throw domain_error("stan::math::gradRegIncGamma not converging");
}
return gamma_p(a, z) * ( dig - l ) + exp( a * l ) * S / g;
}
}
}
#endif
std::isinf
int isinf(const stan::math::var &a)
Checks if the given number is infinite.
Definition: std_isinf.hpp:18
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
stan::math::grad_reg_inc_gamma
T grad_reg_inc_gamma(T a, T z, T g, T dig, T precision=1e-6)
Definition: grad_reg_inc_gamma.hpp:15
boost::math::isinf
bool isinf(const stan::math::var &v)
Checks if the given number is infinite.
Definition: boost_isinf.hpp:22
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::domain_error
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.
Definition: domain_error.hpp:32
stan::math::gamma_p
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:15
gamma_p.hpp
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95

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