diff --git a/stan/math/prim/err/check_finite.hpp b/stan/math/prim/err/check_finite.hpp index 2e92cd2ca80..d3c95f12115 100644 --- a/stan/math/prim/err/check_finite.hpp +++ b/stan/math/prim/err/check_finite.hpp @@ -15,11 +15,25 @@ namespace stan { namespace math { namespace internal { +/** + * Return true if y is finite + * + * @tparam T_y type of y + * @param y parameter to check + * @return boolean + */ template bool is_finite(const T_y& y) { return is_scal_finite(y); } +/** + * Return true if every element of the matrix y is finite + * + * @tparam T_y type of elements y + * @param y matrix to check + * @return boolean + */ template bool is_finite(const Eigen::Matrix& y) { bool all = true; @@ -29,6 +43,13 @@ bool is_finite(const Eigen::Matrix& y) { return all; } +/** + * Return true if every element of the vector y is finite + * + * @tparam T_y type of elements y + * @param y vector to check + * @return boolean + */ template bool is_finite(const std::vector& y) { bool all = true; diff --git a/test/unit/math/prim/err/check_finite_test.cpp b/test/unit/math/prim/err/check_finite_test.cpp index 5416ea3d04e..b439bf99ebc 100644 --- a/test/unit/math/prim/err/check_finite_test.cpp +++ b/test/unit/math/prim/err/check_finite_test.cpp @@ -24,6 +24,30 @@ TEST(ErrorHandlingArr, CheckFinite_Vector) { << "check_finite should throw exception on NaN"; } +TEST(ErrorHandlingArr, CheckFinite_std_vector_std_vector) { + using stan::math::check_finite; + const char* function = "check_finite"; + std::vector x = {-1, 0, 1}; + std::vector> xx = {x}; + ASSERT_NO_THROW(check_finite(function, "x", xx)) + << "check_finite should be true with finite x"; + + x = {-1, 0, std::numeric_limits::infinity()}; + xx = {x}; + EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) + << "check_finite should throw exception on Inf"; + + x = {-1, 0, -std::numeric_limits::infinity()}; + xx = {x}; + EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) + << "check_finite should throw exception on -Inf"; + + x = {-1, 0, std::numeric_limits::quiet_NaN()}; + xx = {x}; + EXPECT_THROW(check_finite(function, "x", xx), std::domain_error) + << "check_finite should throw exception on NaN"; +} + TEST(ErrorHandlingArr, CheckFinite_nan) { using stan::math::check_finite; const char* function = "check_finite"; @@ -94,26 +118,20 @@ TEST(ErrorHandlingMat, CheckFinite_std_vector_Matrix) { x.resize(3); x << -1, 0, std::numeric_limits::infinity(); - - std::vector> xvi = {x}; - - EXPECT_THROW(check_finite(function, "x", xvi), std::domain_error) + xv = {x}; + EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) << "check_finite should throw exception on Inf"; x.resize(3); x << -1, 0, -std::numeric_limits::infinity(); - - std::vector> nxvi = {x}; - - EXPECT_THROW(check_finite(function, "x", nxvi), std::domain_error) + xv = {x}; + EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) << "check_finite should throw exception on -Inf"; x.resize(3); x << -1, 0, std::numeric_limits::quiet_NaN(); - - std::vector> xvn = {x}; - - EXPECT_THROW(check_finite(function, "x", xvn), std::domain_error) + xv = {x}; + EXPECT_THROW(check_finite(function, "x", xv), std::domain_error) << "check_finite should throw exception on NaN"; }