Stan Math Library  2.15.0
reverse mode automatic differentiation
check_2F1_converges.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_2F1_CONVERGES_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_2F1_CONVERGES_HPP
3 
7 #include <cmath>
8 #include <stdexcept>
9 #include <sstream>
10 #include <limits>
11 
12 namespace stan {
13  namespace math {
14 
35  template <typename T_a1, typename T_a2, typename T_b1, typename T_z>
36  inline void check_2F1_converges(const char* function,
37  const T_a1& a1, const T_a2& a2, const T_b1& b1, const T_z& z
38  ) {
39  using std::floor;
40  using std::fabs;
41 
42  check_not_nan("check_3F2_converges", "a1", a1);
43  check_not_nan("check_3F2_converges", "a2", a2);
44  check_not_nan("check_3F2_converges", "b1", b1);
45  check_not_nan("check_3F2_converges", "z", z);
46 
47  int num_terms = 0;
48  bool is_polynomial = false;
49 
50  if (is_nonpositive_integer(a1) && fabs(a1) >= num_terms) {
51  is_polynomial = true;
52  num_terms = floor(fabs(value_of_rec(a1)));
53  }
54  if (is_nonpositive_integer(a2) && fabs(a2) >= num_terms) {
55  is_polynomial = true;
56  num_terms = floor(fabs(value_of_rec(a2)));
57  }
58 
59  bool is_undefined = is_nonpositive_integer(b1) && fabs(b1) <= num_terms;
60 
61  if (is_polynomial && !is_undefined) return;
62  if (fabs(z) < 1.0 && !is_undefined) return;
63  if (fabs(z) == 1.0 && !is_undefined && b1 > a1 + a2) return;
64 
65  std::stringstream msg;
66  msg << "called from function '" << function << "', "
67  << "hypergeometric function 2F1 does not meet convergence "
68  << "conditions with given arguments. "
69  << "a1: " << a1 << ", a2: " << a2 << ", "
70  << "b1: " << b1 << ", z: " << z;
71  throw std::domain_error(msg.str());
72  }
73 
74  }
75 }
76 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
bool is_nonpositive_integer(T x)
Returns true if the input is a nonpositive integer and false otherwise.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
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.
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:11
void check_2F1_converges(const char *function, const T_a1 &a1, const T_a2 &a2, const T_b1 &b1, const T_z &z)
Check if the hypergeometric function (2F1) called with supplied arguments will converge, assuming arguments are finite values.

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