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 
7 #include <cmath>
8 #include <limits>
9 #include <sstream>
10 #include <stdexcept>
11 
12 namespace stan {
13 namespace math {
14 
15 /**
16  * Check if the hypergeometric function (3F2) called with
17  * supplied arguments will converge, assuming arguments are
18  * finite values.
19  *
20  * @tparam T_a1 Type of a1
21  * @tparam T_a2 Type of a2
22  * @tparam T_a3 Type of a3
23  * @tparam T_b1 Type of b1
24  * @tparam T_b2 Type of b2
25  * @tparam T_z Type of z
26  *
27  * @param function Name of function ultimately relying on 3F2 (for error
28  & messages)
29  * @param a1 Variable to check
30  * @param a2 Variable to check
31  * @param a3 Variable to check
32  * @param b1 Variable to check
33  * @param b2 Variable to check
34  * @param z Variable to check
35  *
36  * @throw <code>domain_error</code> if 3F2(a1, a2, a3, b1, b2, z)
37  * does not meet convergence conditions, or if any coefficient is NaN.
38  */
39 template <typename T_a1, typename T_a2, typename T_a3, typename T_b1,
40  typename T_b2, typename T_z>
41 inline void check_3F2_converges(const char* function, const T_a1& a1,
42  const T_a2& a2, const T_a3& a3, const T_b1& b1,
43  const T_b2& b2, const T_z& z) {
44  using std::fabs;
45  using std::floor;
46 
47  check_not_nan("check_3F2_converges", "a1", a1);
48  check_not_nan("check_3F2_converges", "a2", a2);
49  check_not_nan("check_3F2_converges", "a3", a3);
50  check_not_nan("check_3F2_converges", "b1", b1);
51  check_not_nan("check_3F2_converges", "b2", b2);
52  check_not_nan("check_3F2_converges", "z", z);
53 
54  int num_terms = 0;
55  bool is_polynomial = false;
56 
57  if (is_nonpositive_integer(a1) && fabs(a1) >= num_terms) {
58  is_polynomial = true;
59  num_terms = floor(fabs(value_of_rec(a1)));
60  }
61  if (is_nonpositive_integer(a2) && fabs(a2) >= num_terms) {
62  is_polynomial = true;
63  num_terms = floor(fabs(value_of_rec(a2)));
64  }
65  if (is_nonpositive_integer(a3) && fabs(a3) >= num_terms) {
66  is_polynomial = true;
67  num_terms = floor(fabs(value_of_rec(a3)));
68  }
69 
70  bool is_undefined = (is_nonpositive_integer(b1) && fabs(b1) <= num_terms)
71  || (is_nonpositive_integer(b2) && fabs(b2) <= num_terms);
72 
73  if (is_polynomial && !is_undefined)
74  return;
75  if (fabs(z) < 1.0 && !is_undefined)
76  return;
77  if (fabs(z) == 1.0 && !is_undefined && b1 + b2 > a1 + a2 + a3)
78  return;
79 
80  std::stringstream msg;
81  msg << "called from function '" << function << "', "
82  << "hypergeometric function 3F2 does not meet convergence "
83  << "conditions with given arguments. "
84  << "a1: " << a1 << ", a2: " << a2 << ", a3: " << a3 << ", b1: " << b1
85  << ", b2: " << b2 << ", z: " << z;
86  throw std::domain_error(msg.str());
87 }
88 
89 } // namespace math
90 } // namespace stan
91 #endif
TH1F * a3
Definition: f2_nu.C:606
TH1F * a2
Definition: f2_nu.C:545
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)
double value_of_rec(const fvar< T > &v)
bool is_nonpositive_integer(T x)
TH1F * a1
Definition: f2_nu.C:476
void check_not_nan(const char *function, const char *name, const T_y &y)
z
Definition: test.py:28
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
fvar< T > floor(const fvar< T > &x)
Definition: floor.hpp:11