Automatic Differentiation
 
Loading...
Searching...
No Matches
check_3F2_converges.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_3F2_CONVERGES_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_3F2_CONVERGES_HPP
3
10#include <cmath>
11#include <sstream>
12#include <stdexcept>
13
14namespace stan {
15namespace math {
16
38template <typename T_a1, typename T_a2, typename T_a3, typename T_b1,
39 typename T_b2, typename T_z>
40inline void check_3F2_converges(const char* function, const T_a1& a1,
41 const T_a2& a2, const T_a3& a3, const T_b1& b1,
42 const T_b2& b2, const T_z& z) {
43 using std::fabs;
44 using std::floor;
45
46 check_not_nan("check_3F2_converges", "a1", a1);
47 check_not_nan("check_3F2_converges", "a2", a2);
48 check_not_nan("check_3F2_converges", "a3", a3);
49 check_not_nan("check_3F2_converges", "b1", b1);
50 check_not_nan("check_3F2_converges", "b2", b2);
51 check_not_nan("check_3F2_converges", "z", z);
52
53 int num_terms = 0;
54 bool is_polynomial = false;
55
56 if (is_nonpositive_integer(a1) && fabs(a1) >= num_terms) {
57 is_polynomial = true;
58 num_terms = floor(fabs(value_of_rec(a1)));
59 }
60 if (is_nonpositive_integer(a2) && fabs(a2) >= num_terms) {
61 is_polynomial = true;
62 num_terms = floor(fabs(value_of_rec(a2)));
63 }
64 if (is_nonpositive_integer(a3) && fabs(a3) >= num_terms) {
65 is_polynomial = true;
66 num_terms = floor(fabs(value_of_rec(a3)));
67 }
68
69 bool is_undefined = (is_nonpositive_integer(b1) && fabs(b1) <= num_terms)
70 || (is_nonpositive_integer(b2) && fabs(b2) <= num_terms);
71
72 if (is_polynomial && !is_undefined) {
73 return;
74 }
75 if (fabs(z) < 1.0 && !is_undefined) {
76 return;
77 }
78 if (fabs(z) == 1.0 && !is_undefined && b1 + b2 > a1 + a2 + a3) {
79 return;
80 }
81
82 std::stringstream msg;
83 msg << "called from function '" << function << "', "
84 << "hypergeometric function 3F2 does not meet convergence "
85 << "conditions with given arguments. "
86 << "a1: " << a1 << ", a2: " << a2 << ", a3: " << a3 << ", b1: " << b1
87 << ", b2: " << b2 << ", z: " << z;
88 throw std::domain_error(msg.str());
89}
90
91} // namespace math
92} // namespace stan
93#endif
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
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,...
fvar< T > floor(const fvar< T > &x)
Definition floor.hpp:12
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
bool is_nonpositive_integer(T x)
Returns true if the input is a nonpositive integer and false otherwise.
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...