| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

grad_F32.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_GRAD_F32_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_GRAD_F32_HPP
3
4 #include <cmath>
5
6 namespace stan {
7 namespace math {
8
9 template<typename T>
10 void grad_F32(T* g, T a, T b, T c, T d, T e, T z, T precision = 1e-6) {
11 using std::log;
12 using std::fabs;
13 using std::exp;
14
15 T gOld[6];
16
17 for (T *p = g; p != g + 6; ++p) *p = 0;
18 for (T *p = gOld; p != gOld + 6; ++p) *p = 0;
19
20 T tOld = 1;
21 T tNew = 0;
22
23 T logT = 0;
24
25 T logZ = log(z);
26
27 int k = 0;
28
29 while (fabs(tNew) > precision || k == 0) {
30 T C = (a + k) / (d + k);
31 C *= (b + k) / (e + k);
32 C *= (c + k) / (1 + k);
33
34 // If a, b, or c is a negative integer then the series terminates
35 // after a finite number of interations
36 if (C == 0) break;
37
38 logT += (C > 0 ? 1 : -1) * log(fabs(C)) + logZ;
39
40 tNew = exp(logT);
41
42 gOld[0] = tNew * (gOld[0] / tOld + 1.0 / (a + k));
43 gOld[1] = tNew * (gOld[1] / tOld + 1.0 / (b + k));
44 gOld[2] = tNew * (gOld[2] / tOld + 1.0 / (c + k));
45
46 gOld[3] = tNew * (gOld[3] / tOld - 1.0 / (d + k));
47 gOld[4] = tNew * (gOld[4] / tOld - 1.0 / (e + k));
48
49 gOld[5] = tNew * (gOld[5] / tOld + 1.0 / z);
50
51 for (int i = 0; i < 6; ++i) g[i] += gOld[i];
52
53 tOld = tNew;
54
55 ++k;
56 }
57 }
58
59 }
60 }
61 #endif
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
stan
Definition: log_sum_exp.hpp:8
stan::math::log
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
stan::math::exp
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
stan::math::e
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
stan::math::grad_F32
void grad_F32(T *g, T a, T b, T c, T d, T e, T z, T precision=1e-6)
Definition: grad_F32.hpp:10

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