1 #ifndef TEST_UNIT_MATH_TEST_IS_FINITE_HPP
2 #define TEST_UNIT_MATH_TEST_IS_FINITE_HPP
3 
4 #include <stan/math.hpp>
5 #include <vector>
6 
7 namespace stan {
8 namespace test {
9 
10 /**
11  * Return true if the specified value is finite.
12  *
13  * @param x value to test
14  * @return true if value is finite
15  */
is_finite(double x)16 bool is_finite(double x) {
17   return !stan::math::is_inf(x) && !stan::math::is_nan(x);
18 }
19 
20 /**
21  * Return true if all of the elements in the container are finite
22  *
23  * @tparam T scalar type
24  * @tparam R row type
25  * @tparam C col type
26  * @param x container to test
27  * @return true if all container values are finite
28  */
29 template <typename T, int R, int C>
is_finite(const Eigen::Matrix<T,R,C> & x)30 bool is_finite(const Eigen::Matrix<T, R, C>& x) {
31   for (int i = 0; i < x.size(); ++i)
32     if (!is_finite(x(i)))
33       return false;
34   return true;
35 }
36 
37 /**
38  * Return true if all of the elements in the container are finite
39  *
40  * @tparam T contained type
41  * @param x container to test
42  * @return true if all container values are finite
43  */
44 template <typename T>
is_finite(const std::vector<T> & x)45 bool is_finite(const std::vector<T>& x) {
46   for (size_t i = 0; i < x.size(); ++i)
47     if (!is_finite(x[i]))
48       return false;
49   return true;
50 }
51 
52 }  // namespace test
53 }  // namespace stan
54 #endif
55