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 <limits>
9 #include <sstream>
10 #include <stdexcept>
11 
12 namespace stan {
13 namespace math {
14 
15 /**
16  * Check if the hypergeometric function (2F1) 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_b1 Type of b1
23  * @tparam T_z Type of z
24  *
25  * @param function Name of function ultimately relying on 2F1 (for error
26  * messages)
27  * @param a1 Variable to check
28  * @param a2 Variable to check
29  * @param b1 Variable to check
30  * @param z Variable to check
31  *
32  * @throw <code>domain_error</code> if 2F1(a1, a2, b1, z)
33  * does not meet convergence conditions, or if any coefficient is NaN.
34  */
35 template <typename T_a1, typename T_a2, typename T_b1, typename T_z>
36 inline void check_2F1_converges(const char* function, const T_a1& a1,
37  const T_a2& a2, const T_b1& b1, const T_z& z) {
38  using std::fabs;
39  using std::floor;
40 
41  check_not_nan("check_3F2_converges", "a1", a1);
42  check_not_nan("check_3F2_converges", "a2", a2);
43  check_not_nan("check_3F2_converges", "b1", b1);
44  check_not_nan("check_3F2_converges", "z", z);
45 
46  int num_terms = 0;
47  bool is_polynomial = false;
48 
49  if (is_nonpositive_integer(a1) && fabs(a1) >= num_terms) {
50  is_polynomial = true;
51  num_terms = floor(fabs(value_of_rec(a1)));
52  }
53  if (is_nonpositive_integer(a2) && fabs(a2) >= num_terms) {
54  is_polynomial = true;
55  num_terms = floor(fabs(value_of_rec(a2)));
56  }
57 
58  bool is_undefined = is_nonpositive_integer(b1) && fabs(b1) <= num_terms;
59 
60  if (!is_undefined
61  && (is_polynomial || fabs(z) < 1 || (fabs(z) == 1 && b1 > a1 + a2)))
62  return;
63 
64  std::stringstream msg;
65  msg << "called from function '" << function << "', "
66  << "hypergeometric function 2F1 does not meet convergence "
67  << "conditions with given arguments. "
68  << "a1: " << a1 << ", a2: " << a2 << ", "
69  << "b1: " << b1 << ", z: " << z;
70  throw std::domain_error(msg.str());
71 }
72 
73 } // namespace math
74 } // namespace stan
75 #endif
TH1F * a2
Definition: f2_nu.C:545
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
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
void check_2F1_converges(const char *function, const T_a1 &a1, const T_a2 &a2, const T_b1 &b1, const T_z &z)