Stan Math Library  2.14.0
reverse mode automatic differentiation
grad_2F1.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_GRAD_2F1_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_GRAD_2F1_HPP
3 
4 #include <cmath>
5 
6 namespace stan {
7  namespace math {
8 
9  // Gradient of the hypergeometric function 2F1(a, b | c | z)
10  // with respect to a and c
11  template<typename T>
12  void grad_2F1(T& gradA, T& gradC, T a, T b, T c, T z, T precision = 1e-6) {
13  using std::fabs;
14 
15  gradA = 0;
16  gradC = 0;
17 
18  T gradAold = 0;
19  T gradCold = 0;
20 
21  int k = 0;
22  T tDak = 1.0 / (a - 1);
23 
24  while (fabs(tDak * (a + (k - 1)) ) > precision || k == 0) {
25  const T r = ( (a + k) / (c + k) ) * ( (b + k) / (T)(k + 1) ) * z;
26  tDak = r * tDak * (a + (k - 1)) / (a + k);
27 
28  if (r == 0) break;
29 
30  gradAold = r * gradAold + tDak;
31  gradCold = r * gradCold - tDak * ((a + k) / (c + k));
32 
33  gradA += gradAold;
34  gradC += gradCold;
35 
36  ++k;
37 
38  if (k > 200) break;
39  }
40  }
41 
42  }
43 }
44 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94
void grad_2F1(T &gradA, T &gradC, T a, T b, T c, T z, T precision=1e-6)
Definition: grad_2F1.hpp:12

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