Stan Math Library  2.20.0
reverse mode automatic differentiation
gamma_q.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_SCAL_FUN_GAMMA_Q_HPP
2 #define STAN_MATH_FWD_SCAL_FUN_GAMMA_Q_HPP
3 
4 #include <stan/math/fwd/meta.hpp>
5 #include <stan/math/fwd/core.hpp>
8 
9 namespace stan {
10 namespace math {
11 
12 template <typename T>
13 inline fvar<T> gamma_q(const fvar<T>& x1, const fvar<T>& x2) {
15  using std::exp;
16  using std::fabs;
17  using std::log;
18  using std::pow;
19 
20  T u = gamma_q(x1.val_, x2.val_);
21 
22  T S = 0;
23  T s = 1;
24  T l = log(x2.val_);
25  T g = tgamma(x1.val_);
26  T dig = digamma(x1.val_);
27 
28  int k = 0;
29  T delta = s / (x1.val_ * x1.val_);
30 
31  while (fabs(delta) > 1e-6) {
32  S += delta;
33  ++k;
34  s *= -x2.val_ / k;
35  delta = s / ((k + x1.val_) * (k + x1.val_));
36  }
37 
38  T der1 = (1.0 - u) * (dig - l) + exp(x1.val_ * l) * S / g;
39  T der2 = -exp(-x2.val_) * pow(x2.val_, x1.val_ - 1.0) / g;
40 
41  return fvar<T>(u, x1.d_ * der1 + x2.d_ * der2);
42 }
43 
44 template <typename T>
45 inline fvar<T> gamma_q(const fvar<T>& x1, double x2) {
47  using std::exp;
48  using std::fabs;
49  using std::log;
50  using std::pow;
51 
52  T u = gamma_q(x1.val_, x2);
53 
54  T S = 0;
55  double s = 1;
56  double l = log(x2);
57  T g = tgamma(x1.val_);
58  T dig = digamma(x1.val_);
59 
60  int k = 0;
61  T delta = s / (x1.val_ * x1.val_);
62 
63  while (fabs(delta) > 1e-6) {
64  S += delta;
65  ++k;
66  s *= -x2 / k;
67  delta = s / ((k + x1.val_) * (k + x1.val_));
68  }
69 
70  T der1 = (1.0 - u) * (dig - l) + exp(x1.val_ * l) * S / g;
71 
72  return fvar<T>(u, x1.d_ * der1);
73 }
74 
75 template <typename T>
76 inline fvar<T> gamma_q(double x1, const fvar<T>& x2) {
77  using std::exp;
78  using std::pow;
79 
80  T u = gamma_q(x1, x2.val_);
81 
82  double g = tgamma(x1);
83 
84  T der2 = -exp(-x2.val_) * pow(x2.val_, x1 - 1.0) / g;
85 
86  return fvar<T>(u, x2.d_ * der2);
87 }
88 } // namespace math
89 } // namespace stan
90 #endif
T d_
The tangent (derivative) of this variable.
Definition: fvar.hpp:50
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
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
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:87
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:16
fvar< T > tgamma(const fvar< T > &x)
Return the result of applying the gamma function to the specified argument.
Definition: tgamma.hpp:21
fvar< T > gamma_q(const fvar< T > &x1, const fvar< T > &x2)
Definition: gamma_q.hpp:13
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
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition: digamma.hpp:23

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