All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
gamma_p.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_FWD_SCAL_FUN_GAMMA_P_HPP
#define STAN_MATH_FWD_SCAL_FUN_GAMMA_P_HPP
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/scal/fun/gamma_p.hpp>
namespace stan {
namespace math {
template <typename T>
inline
fvar<T>
gamma_p(const fvar<T>& x1, const fvar<T>& x2) {
using stan::math::gamma_p;
using std::log;
using std::exp;
using std::pow;
using std::fabs;
using boost::math::tgamma;
using boost::math::digamma;
T u = gamma_p(x1.val_, x2.val_);
T S = 0;
T s = 1;
T l = log(x2.val_);
T g = tgamma(x1.val_);
T dig = digamma(x1.val_);
int k = 0;
T delta = s / (x1.val_ * x1.val_);
while (fabs(delta) > 1e-6) {
S += delta;
++k;
s *= -x2.val_ / k;
delta = s / ((k + x1.val_) * (k + x1.val_));
}
T der1 = u * (dig - l) + exp(x1.val_ * l) * S / g;
T der2 = exp(-x2.val_) * pow(x2.val_, x1.val_ - 1.0) / g;
return fvar<T>(u, x1.d_ * -der1 + x2.d_ * der2);
}
template <typename T>
inline
fvar<T>
gamma_p(const fvar<T>& x1, const double x2) {
using stan::math::gamma_p;
using std::log;
using std::exp;
using std::pow;
using std::fabs;
using boost::math::tgamma;
using boost::math::digamma;
T u = gamma_p(x1.val_, x2);
T S = 0.0;
double s = 1.0;
double l = log(x2);
T g = tgamma(x1.val_);
T dig = digamma(x1.val_);
int k = 0;
T delta = s / (x1.val_ * x1.val_);
while (fabs(delta) > 1e-6) {
S += delta;
++k;
s *= -x2 / k;
delta = s / ((k + x1.val_) * (k + x1.val_));
}
T der1 = u * (dig - l) + exp(x1.val_ * l) * S / g;
return fvar<T>(u, x1.d_ * -der1);
}
template <typename T>
inline
fvar<T>
gamma_p(const double x1, const fvar<T>& x2) {
using stan::math::gamma_p;
using std::exp;
using std::pow;
T u = gamma_p(x1, x2.val_);
double g = boost::math::tgamma(x1);
T der2 = exp(-x2.val_) * pow(x2.val_, x1 - 1.0) / g;
return fvar<T>(u, x2.d_ * der2);
}
}
}
#endif
core.hpp
stan::math::fvar::d_
T d_
Definition: fvar.hpp:15
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::fvar::val_
T val_
Definition: fvar.hpp:14
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
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::math::pow
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:18
stan::math::tgamma
fvar< T > tgamma(const fvar< T > &x)
Definition: tgamma.hpp:15
stan::math::fvar
Definition: fvar.hpp:13
stan::math::digamma
fvar< T > digamma(const fvar< T > &x)
Definition: digamma.hpp:16

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