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

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