Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions stan/math/prim/err/check_finite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T_y>
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 <typename T_y, int R, int C>
bool is_finite(const Eigen::Matrix<T_y, R, C>& y) {
bool all = true;
Expand All @@ -29,6 +43,13 @@ bool is_finite(const Eigen::Matrix<T_y, R, C>& 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 <typename T_y>
bool is_finite(const std::vector<T_y>& y) {
bool all = true;
Expand Down
42 changes: 30 additions & 12 deletions test/unit/math/prim/err/check_finite_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> x = {-1, 0, 1};
std::vector<std::vector<double>> xx = {x};
ASSERT_NO_THROW(check_finite(function, "x", xx))
<< "check_finite should be true with finite x";

x = {-1, 0, std::numeric_limits<double>::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<double>::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<double>::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";
Expand Down Expand Up @@ -94,26 +118,20 @@ TEST(ErrorHandlingMat, CheckFinite_std_vector_Matrix) {

x.resize(3);
x << -1, 0, std::numeric_limits<double>::infinity();

std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1>> 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<double>::infinity();

std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1>> 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<double>::quiet_NaN();

std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1>> 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";
}

Expand Down