|
1 | 1 | #ifndef STAN_MATH_PRIM_ERR_CHECK_FINITE_HPP |
2 | 2 | #define STAN_MATH_PRIM_ERR_CHECK_FINITE_HPP |
3 | 3 |
|
4 | | -#include <stan/math/prim/meta.hpp> |
5 | | -#include <stan/math/prim/err/is_scal_finite.hpp> |
6 | | -#include <stan/math/prim/err/throw_domain_error.hpp> |
7 | | -#include <stan/math/prim/err/throw_domain_error_vec.hpp> |
8 | | -#include <stan/math/prim/fun/Eigen.hpp> |
9 | | -#include <stan/math/prim/fun/get.hpp> |
10 | | -#include <stan/math/prim/fun/size.hpp> |
11 | | -#include <stan/math/prim/fun/value_of.hpp> |
12 | | -#include <stan/math/prim/fun/value_of_rec.hpp> |
13 | | -#include <cmath> |
| 4 | +#include <stan/math/prim/err/elementwise_check.hpp> |
14 | 5 |
|
15 | 6 | namespace stan { |
16 | 7 | namespace math { |
17 | 8 |
|
18 | | -namespace internal { |
19 | | -template <typename T_y, bool is_vec> |
20 | | -struct finite { |
21 | | - static void check(const char* function, const char* name, const T_y& y) { |
22 | | - if (!is_scal_finite(y)) { |
23 | | - throw_domain_error(function, name, y, "is ", ", but must be finite!"); |
24 | | - } |
25 | | - } |
26 | | -}; |
27 | | - |
28 | | -template <typename T_y> |
29 | | -struct finite<T_y, true> { |
30 | | - static void check(const char* function, const char* name, const T_y& y) { |
31 | | - for (size_t n = 0; n < stan::math::size(y); n++) { |
32 | | - if (!is_scal_finite(stan::get(y, n))) { |
33 | | - throw_domain_error_vec(function, name, y, n, "is ", |
34 | | - ", but must be finite!"); |
35 | | - } |
36 | | - } |
37 | | - } |
38 | | -}; |
39 | | -} // namespace internal |
40 | | - |
41 | 9 | /** |
42 | 10 | * Check if <code>y</code> is finite. |
43 | 11 | * This function is vectorized and will check each element of |
44 | 12 | * <code>y</code>. |
45 | | - * @tparam T_y Type of y |
46 | | - * @param function Function name (for error messages) |
47 | | - * @param name Variable name (for error messages) |
48 | | - * @param y Variable to check |
| 13 | + * @tparam T_y type of y |
| 14 | + * @param function function name (for error messages) |
| 15 | + * @param name variable name (for error messages) |
| 16 | + * @param y variable to check |
49 | 17 | * @throw <code>domain_error</code> if y is infinity, -infinity, or NaN |
50 | 18 | */ |
51 | 19 | template <typename T_y> |
52 | 20 | inline void check_finite(const char* function, const char* name, const T_y& y) { |
53 | | - internal::finite<T_y, is_vector_like<T_y>::value>::check(function, name, y); |
| 21 | + auto is_good = [](const auto& y) { return std::isfinite(y); }; |
| 22 | + elementwise_check(is_good, function, name, y, ", but must be finite!"); |
54 | 23 | } |
55 | 24 |
|
56 | | -/** |
57 | | - * Return <code>true</code> is the specified matrix is finite. |
58 | | - * |
59 | | - * @tparam T type of elements in the matrix |
60 | | - * @tparam R number of rows, can be Eigen::Dynamic |
61 | | - * @tparam C number of columns, can be Eigen::Dynamic |
62 | | - * |
63 | | - * @param function name of function (for error messages) |
64 | | - * @param name variable name (for error messages) |
65 | | - * @param y matrix to test |
66 | | - * @return <code>true</code> if the matrix is finite |
67 | | - **/ |
68 | | -namespace internal { |
69 | | -template <typename T, int R, int C> |
70 | | -struct finite<Eigen::Matrix<T, R, C>, true> { |
71 | | - static void check(const char* function, const char* name, |
72 | | - const Eigen::Matrix<T, R, C>& y) { |
73 | | - if (!value_of(y).allFinite()) { |
74 | | - for (int n = 0; n < y.size(); ++n) { |
75 | | - if (!std::isfinite(value_of_rec(y(n)))) { |
76 | | - throw_domain_error_vec(function, name, y, n, "is ", |
77 | | - ", but must be finite!"); |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - } |
82 | | -}; |
83 | | - |
84 | | -} // namespace internal |
85 | 25 | } // namespace math |
86 | 26 | } // namespace stan |
87 | 27 |
|
|
0 commit comments