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
12#include <cmath>
13
14namespace stan {
15namespace math {
16
17template <typename T>
18inline fvar<T> gamma_p(const fvar<T> &x1, const fvar<T> &x2) {
19 T u = gamma_p(x1.val_, x2.val_);
20 if (is_inf(x1.val_)) {
21 return fvar<T>(u, NOT_A_NUMBER);
22 }
23 if (is_inf(x2.val_)) {
24 return fvar<T>(u, NOT_A_NUMBER);
25 }
26
27 T der1 = grad_reg_lower_inc_gamma(x1.val_, x2.val_, 1.0e-10);
28 T der2 = exp(-x2.val_ + (x1.val_ - 1.0) * log(x2.val_) - lgamma(x1.val_));
29
30 return fvar<T>(u, x1.d_ * der1 + x2.d_ * der2);
31}
32
33template <typename T>
34inline fvar<T> gamma_p(const fvar<T> &x1, double x2) {
35 T u = gamma_p(x1.val_, x2);
36 if (is_inf(x1.val_)) {
37 return fvar<T>(u, NOT_A_NUMBER);
38 }
39 if (is_inf(x2)) {
40 return fvar<T>(u, NOT_A_NUMBER);
41 }
42
43 T der1 = grad_reg_lower_inc_gamma(x1.val_, x2, 1.0e-10);
44
45 return fvar<T>(u, x1.d_ * der1);
46}
47
48template <typename T>
49inline fvar<T> gamma_p(double x1, const fvar<T> &x2) {
50 T u = gamma_p(x1, x2.val_);
51 if (is_inf(x1)) {
52 return fvar<T>(u, NOT_A_NUMBER);
53 }
54
55 T der2 = exp(-x2.val_ + (x1 - 1.0) * log(x2.val_) - lgamma(x1));
56
57 return fvar<T>(u, x2.d_ * der2);
58}
59
60} // namespace math
61} // namespace stan
62#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:18
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:18
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:15
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