Stan Math Library  2.20.0
reverse mode automatic differentiation
check_3F2_converges.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_3F2_CONVERGES_HPP
2 #define STAN_MATH_PRIM_SCAL_ERR_CHECK_3F2_CONVERGES_HPP
3 
8 #include <cmath>
9 #include <limits>
10 #include <sstream>
11 #include <stdexcept>
12 
13 namespace stan {
14 namespace math {
15 
37 template <typename T_a1, typename T_a2, typename T_a3, typename T_b1,
38  typename T_b2, typename T_z>
39 inline void check_3F2_converges(const char* function, const T_a1& a1,
40  const T_a2& a2, const T_a3& a3, const T_b1& b1,
41  const T_b2& b2, const T_z& z) {
42  using std::fabs;
43  using std::floor;
44 
45  check_not_nan("check_3F2_converges", "a1", a1);
46  check_not_nan("check_3F2_converges", "a2", a2);
47  check_not_nan("check_3F2_converges", "a3", a3);
48  check_not_nan("check_3F2_converges", "b1", b1);
49  check_not_nan("check_3F2_converges", "b2", b2);
50  check_not_nan("check_3F2_converges", "z", z);
51 
52  int num_terms = 0;
53  bool is_polynomial = false;
54 
55  if (is_nonpositive_integer(a1) && fabs(a1) >= num_terms) {
56  is_polynomial = true;
57  num_terms = floor(fabs(value_of_rec(a1)));
58  }
59  if (is_nonpositive_integer(a2) && fabs(a2) >= num_terms) {
60  is_polynomial = true;
61  num_terms = floor(fabs(value_of_rec(a2)));
62  }
63  if (is_nonpositive_integer(a3) && fabs(a3) >= num_terms) {
64  is_polynomial = true;
65  num_terms = floor(fabs(value_of_rec(a3)));
66  }
67 
68  bool is_undefined = (is_nonpositive_integer(b1) && fabs(b1) <= num_terms)
69  || (is_nonpositive_integer(b2) && fabs(b2) <= num_terms);
70 
71  if (is_polynomial && !is_undefined)
72  return;
73  if (fabs(z) < 1.0 && !is_undefined)
74  return;
75  if (fabs(z) == 1.0 && !is_undefined && b1 + b2 > a1 + a2 + a3)
76  return;
77 
78  std::stringstream msg;
79  msg << "called from function '" << function << "', "
80  << "hypergeometric function 3F2 does not meet convergence "
81  << "conditions with given arguments. "
82  << "a1: " << a1 << ", a2: " << a2 << ", a3: " << a3 << ", b1: " << b1
83  << ", b2: " << b2 << ", z: " << z;
84  throw std::domain_error(msg.str());
85 }
86 
87 } // namespace math
88 } // namespace stan
89 #endif
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
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.
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:12

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