Automatic Differentiation
 
Loading...
Searching...
No Matches
gamma_p.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_GAMMA_P_HPP
2#define STAN_MATH_FWD_FUN_GAMMA_P_HPP
3
10#include <cmath>
11
12namespace stan {
13namespace math {
14
15template <typename T>
16inline fvar<T> gamma_p(const fvar<T> &x1, const fvar<T> &x2) {
17 using std::exp;
18 using std::log;
19
20 T u = gamma_p(x1.val_, x2.val_);
21 if (is_inf(x1.val_)) {
22 return fvar<T>(u, NOT_A_NUMBER);
23 }
24 if (is_inf(x2.val_)) {
25 return fvar<T>(u, NOT_A_NUMBER);
26 }
27
28 T der1 = grad_reg_lower_inc_gamma(x1.val_, x2.val_, 1.0e-10);
29 T der2 = exp(-x2.val_ + (x1.val_ - 1.0) * log(x2.val_) - lgamma(x1.val_));
30
31 return fvar<T>(u, x1.d_ * der1 + x2.d_ * der2);
32}
33
34template <typename T>
35inline fvar<T> gamma_p(const fvar<T> &x1, double x2) {
36 T u = gamma_p(x1.val_, x2);
37 if (is_inf(x1.val_)) {
38 return fvar<T>(u, NOT_A_NUMBER);
39 }
40 if (is_inf(x2)) {
41 return fvar<T>(u, NOT_A_NUMBER);
42 }
43
44 T der1 = grad_reg_lower_inc_gamma(x1.val_, x2, 1.0e-10);
45
46 return fvar<T>(u, x1.d_ * der1);
47}
48
49template <typename T>
50inline fvar<T> gamma_p(double x1, const fvar<T> &x2) {
51 using std::exp;
52 using std::log;
53
54 T u = gamma_p(x1, x2.val_);
55 if (is_inf(x1)) {
56 return fvar<T>(u, NOT_A_NUMBER);
57 }
58
59 T der2 = exp(-x2.val_ + (x1 - 1.0) * log(x2.val_) - lgamma(x1));
60
61 return fvar<T>(u, x2.d_ * der2);
62}
63
64} // namespace math
65} // namespace stan
66#endif
static constexpr double NOT_A_NUMBER
(Quiet) not-a-number value.
Definition constants.hpp:56
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
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 > gamma_p(const fvar< T > &x1, const fvar< T > &x2)
Definition gamma_p.hpp:16
int is_inf(const fvar< T > &x)
Returns 1 if the input's value is infinite and 0 otherwise.
Definition is_inf.hpp:21
return_type_t< T1, T2 > 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.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:13
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Scalar val_
The value of this variable.
Definition fvar.hpp:49
Scalar d_
The tangent (derivative) of this variable.
Definition fvar.hpp:61
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40