Skip to content

Commit 3c548a9

Browse files
bbbales2yashiknostan-buildbot
authored
Revert 1798 feature/elementwise checks part 2 (#1984)
* 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 <mc.stanislaw@gmail.com>
1 parent a1036c0 commit 3c548a9

2 files changed

Lines changed: 51 additions & 12 deletions

File tree

stan/math/prim/err/check_finite.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,25 @@
1515
namespace stan {
1616
namespace math {
1717
namespace internal {
18+
/**
19+
* Return true if y is finite
20+
*
21+
* @tparam T_y type of y
22+
* @param y parameter to check
23+
* @return boolean
24+
*/
1825
template <typename T_y>
1926
bool is_finite(const T_y& y) {
2027
return is_scal_finite(y);
2128
}
2229

30+
/**
31+
* Return true if every element of the matrix y is finite
32+
*
33+
* @tparam T_y type of elements y
34+
* @param y matrix to check
35+
* @return boolean
36+
*/
2337
template <typename T_y, int R, int C>
2438
bool is_finite(const Eigen::Matrix<T_y, R, C>& y) {
2539
bool all = true;
@@ -29,6 +43,13 @@ bool is_finite(const Eigen::Matrix<T_y, R, C>& y) {
2943
return all;
3044
}
3145

46+
/**
47+
* Return true if every element of the vector y is finite
48+
*
49+
* @tparam T_y type of elements y
50+
* @param y vector to check
51+
* @return boolean
52+
*/
3253
template <typename T_y>
3354
bool is_finite(const std::vector<T_y>& y) {
3455
bool all = true;

test/unit/math/prim/err/check_finite_test.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,30 @@ TEST(ErrorHandlingArr, CheckFinite_Vector) {
2424
<< "check_finite should throw exception on NaN";
2525
}
2626

27+
TEST(ErrorHandlingArr, CheckFinite_std_vector_std_vector) {
28+
using stan::math::check_finite;
29+
const char* function = "check_finite";
30+
std::vector<double> x = {-1, 0, 1};
31+
std::vector<std::vector<double>> xx = {x};
32+
ASSERT_NO_THROW(check_finite(function, "x", xx))
33+
<< "check_finite should be true with finite x";
34+
35+
x = {-1, 0, std::numeric_limits<double>::infinity()};
36+
xx = {x};
37+
EXPECT_THROW(check_finite(function, "x", xx), std::domain_error)
38+
<< "check_finite should throw exception on Inf";
39+
40+
x = {-1, 0, -std::numeric_limits<double>::infinity()};
41+
xx = {x};
42+
EXPECT_THROW(check_finite(function, "x", xx), std::domain_error)
43+
<< "check_finite should throw exception on -Inf";
44+
45+
x = {-1, 0, std::numeric_limits<double>::quiet_NaN()};
46+
xx = {x};
47+
EXPECT_THROW(check_finite(function, "x", xx), std::domain_error)
48+
<< "check_finite should throw exception on NaN";
49+
}
50+
2751
TEST(ErrorHandlingArr, CheckFinite_nan) {
2852
using stan::math::check_finite;
2953
const char* function = "check_finite";
@@ -94,26 +118,20 @@ TEST(ErrorHandlingMat, CheckFinite_std_vector_Matrix) {
94118

95119
x.resize(3);
96120
x << -1, 0, std::numeric_limits<double>::infinity();
97-
98-
std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1>> xvi = {x};
99-
100-
EXPECT_THROW(check_finite(function, "x", xvi), std::domain_error)
121+
xv = {x};
122+
EXPECT_THROW(check_finite(function, "x", xv), std::domain_error)
101123
<< "check_finite should throw exception on Inf";
102124

103125
x.resize(3);
104126
x << -1, 0, -std::numeric_limits<double>::infinity();
105-
106-
std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1>> nxvi = {x};
107-
108-
EXPECT_THROW(check_finite(function, "x", nxvi), std::domain_error)
127+
xv = {x};
128+
EXPECT_THROW(check_finite(function, "x", xv), std::domain_error)
109129
<< "check_finite should throw exception on -Inf";
110130

111131
x.resize(3);
112132
x << -1, 0, std::numeric_limits<double>::quiet_NaN();
113-
114-
std::vector<Eigen::Matrix<double, Eigen::Dynamic, 1>> xvn = {x};
115-
116-
EXPECT_THROW(check_finite(function, "x", xvn), std::domain_error)
133+
xv = {x};
134+
EXPECT_THROW(check_finite(function, "x", xv), std::domain_error)
117135
<< "check_finite should throw exception on NaN";
118136
}
119137

0 commit comments

Comments
 (0)