Skip to content

Commit

Permalink
Revert 1798 feature/elementwise checks part 2 (#1984)
Browse files Browse the repository at this point in the history
* Added missing tests to check_finite

* [Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.04.1 (tags/RELEASE_600/final)

Co-authored-by: Jenkins <nobody@nowhere>
Co-authored-by: Stan Jenkins <[email protected]>
  • Loading branch information
3 people authored Jul 25, 2020
1 parent a1036c0 commit 3c548a9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
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

0 comments on commit 3c548a9

Please sign in to comment.