Stan Math Library  2.15.0
reverse mode automatic differentiation
F32.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_FUN_F32_HPP
2 #define STAN_MATH_PRIM_SCAL_FUN_F32_HPP
3 
8 #include <cmath>
9 
10 namespace stan {
11  namespace math {
12 
50  template<typename T>
51  T F32(const T& a1, const T& a2, const T& a3, const T& b1, const T& b2,
52  const T& z, double precision = 1e-6, int max_steps = 1e5) {
53  check_3F2_converges("F32", a1, a2, a3, b1, b2, z);
54 
55  using std::exp;
56  using std::log;
57  using std::fabs;
58 
59  T t_acc = 1.0;
60  T log_t = 0.0;
61  T log_z = log(z);
62  double t_sign = 1.0;
63 
64  for (int k=0; k <= max_steps; ++k) {
65  T p = (a1 + k) * (a2 + k) * (a3 + k) / ((b1 + k) * (b2 + k) * (k + 1));
66  if (p == 0.0)
67  return t_acc;
68 
69  log_t += log(fabs(p)) + log_z;
70  t_sign = p >= 0.0 ? t_sign : -t_sign;
71  T t_new = t_sign > 0.0 ? exp(log_t) : -exp(log_t);
72  t_acc += t_new;
73 
74  if (fabs(t_new) <= precision)
75  return t_acc;
76 
77  if (is_inf(t_acc)) {
78  domain_error("F32", "sum (output)", t_acc,
79  "overflow ", " hypergeometric function did not converge.");
80  }
81  }
82  domain_error("F32", "k (internal counter)", max_steps, "exceeded ",
83  " iterations, hypergeometric function did not converge.");
84  return t_acc; // to silence warning.
85  }
86 
87  }
88 }
89 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
T F32(const T &a1, const T &a2, const T &a3, const T &b1, const T &b2, const T &z, double precision=1e-6, int max_steps=1e5)
Hypergeometric function (3F2).
Definition: F32.hpp:51
void check_3F2_converges(const char *function, const T_a1 &a1, const T_a2 &a2, const T_a3 &a3, const T_b1 &b1, const T_b2 &b2, const T_z &z)
Check if the hypergeometric function (3F2) called with supplied arguments will converge, assuming arguments are finite values.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:14
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
int is_inf(const fvar< T > &x)
Returns 1 if the input&#39;s value is infinite and 0 otherwise.
Definition: is_inf.hpp:21
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:94

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