Stan Math Library  2.20.0
reverse mode automatic differentiation
gamma_p.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_SCAL_FUN_GAMMA_P_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_GAMMA_P_HPP
3 
4 #include <stan/math/fwd/meta.hpp>
5 #include <stan/math/fwd/core.hpp>
9 #include <limits>
10 
11 namespace stan {
12 namespace math {
13 
14 template <typename T>
15 inline fvar<T> gamma_p(const fvar<T> &x1, const fvar<T> &x2) {
16  using std::exp;
17  using std::log;
18 
19  T u = gamma_p(x1.val_, x2.val_);
20  if (is_inf(x1.val_))
21  return fvar<T>(u, std::numeric_limits<double>::quiet_NaN());
22  if (is_inf(x2.val_))
23  return fvar<T>(u, std::numeric_limits<double>::quiet_NaN());
24 
25  T der1 = grad_reg_lower_inc_gamma(x1.val_, x2.val_, 1.0e-10);
26  T der2 = exp(-x2.val_ + (x1.val_ - 1.0) * log(x2.val_) - lgamma(x1.val_));
27 
28  return fvar<T>(u, x1.d_ * der1 + x2.d_ * der2);
29 }
30 
31 template <typename T>
32 inline fvar<T> gamma_p(const fvar<T> &x1, double x2) {
33  T u = gamma_p(x1.val_, x2);
34  if (is_inf(x1.val_))
35  return fvar<T>(u, std::numeric_limits<double>::quiet_NaN());
36  if (is_inf(x2))
37  return fvar<T>(u, std::numeric_limits<double>::quiet_NaN());
38 
39  T der1 = grad_reg_lower_inc_gamma(x1.val_, x2, 1.0e-10);
40 
41  return fvar<T>(u, x1.d_ * der1);
42 }
43 
44 template <typename T>
45 inline fvar<T> gamma_p(double x1, const fvar<T> &x2) {
46  using std::exp;
47  using std::log;
48 
49  T u = gamma_p(x1, x2.val_);
50  if (is_inf(x1))
51  return fvar<T>(u, std::numeric_limits<double>::quiet_NaN());
52 
53  T der2 = exp(-x2.val_ + (x1 - 1.0) * log(x2.val_) - lgamma(x1));
54 
55  return fvar<T>(u, x2.d_ * der2);
56 }
57 } // namespace math
58 } // namespace stan
59 #endif
T d_
The tangent (derivative) of this variable.
Definition: fvar.hpp:50
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition: lgamma.hpp:21
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
T val_
The value of this variable.
Definition: fvar.hpp:45
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
int is_inf(const fvar< T > &x)
Returns 1 if the input&#39;s value is infinite and 0 otherwise.
Definition: is_inf.hpp:20
fvar< T > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_p.hpp:15
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41
return_type< T1, T2 >::type grad_reg_lower_inc_gamma(const T1 &a, const T2 &z, double precision=1e-10, int max_steps=1e5)
Computes the gradient of the lower regularized incomplete gamma function.

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