Skip to content

Commit 6176aea

Browse files
committed
add some checks back and fix #1704
1 parent e9903fb commit 6176aea

10 files changed

+38
-24
lines changed

stan/math/prim/fun/acosh.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ namespace math {
1919
* @throw std::domain_error If argument is less than 1.
2020
*/
2121
inline double acosh(double x) {
22-
check_greater_or_equal("acosh", "x", x, 1.0);
23-
return std::acosh(x);
22+
if (is_nan(x)) {
23+
return x;
24+
} else {
25+
check_greater_or_equal("acosh", "x", x, 1.0);
26+
return std::acosh(x);
27+
}
2428
}
2529

2630
/**

stan/math/prim/fun/atanh.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ namespace math {
2020
* @throw std::domain_error If argument is not in [-1, 1].
2121
*/
2222
inline double atanh(double x) {
23-
check_bounded("atanh", "x", x, -1.0, 1.0);
24-
return std::atanh(x);
23+
if (is_nan(x)) {
24+
return x;
25+
} else {
26+
check_bounded("atanh", "x", x, -1.0, 1.0);
27+
return std::atanh(x);
28+
}
2529
}
2630

2731
/**

stan/math/prim/fun/exp.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct exp_fun {
3636
* @param[in] x container
3737
* @return Elementwise application of exponentiation to the argument.
3838
*/
39-
template <typename T, typename = require_not_eigen_vt<std::is_arithmetic, T>>
39+
template <typename T, typename = require_not_eigen_vt<std::is_arithmetic, T>, typename = require_not_double_or_int_t<T>>
4040
inline auto exp(const T& x) {
4141
return apply_scalar_unary<exp_fun, T>::apply(x);
4242
}

stan/math/prim/fun/gp_matern32_cov.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ gp_matern32_cov(const std::vector<T_x> &x, const T_s &sigma,
4141
const T_l &length_scale) {
4242
using std::exp;
4343
using std::pow;
44-
using std::sqrt;
4544

4645
size_t x_size = size(x);
4746
Eigen::Matrix<return_type_t<T_x, T_s, T_l>, Eigen::Dynamic, Eigen::Dynamic>
@@ -66,8 +65,8 @@ gp_matern32_cov(const std::vector<T_x> &x, const T_s &sigma,
6665
check_positive_finite(function, "length scale", length_scale);
6766

6867
T_s sigma_sq = square(sigma);
69-
T_l root_3_inv_l = sqrt(3.0) / length_scale;
70-
T_l neg_root_3_inv_l = -1.0 * sqrt(3.0) / length_scale;
68+
T_l root_3_inv_l = std::sqrt(3.0) / length_scale;
69+
T_l neg_root_3_inv_l = -1.0 * std::sqrt(3.0) / length_scale;
7170

7271
for (size_t i = 0; i < x_size; ++i) {
7372
cov(i, i) = sigma_sq;
@@ -130,8 +129,8 @@ gp_matern32_cov(const std::vector<Eigen::Matrix<T_x, -1, 1>> &x,
130129
"number of length scales", l_size);
131130

132131
T_s sigma_sq = square(sigma);
133-
double root_3 = sqrt(3.0);
134-
double neg_root_3 = -1.0 * sqrt(3.0);
132+
double root_3 = std::sqrt(3.0);
133+
double neg_root_3 = -1.0 * std::sqrt(3.0);
135134

136135
std::vector<Eigen::Matrix<return_type_t<T_x, T_l>, -1, 1>> x_new
137136
= divide_columns(x, length_scale);
@@ -209,8 +208,8 @@ gp_matern32_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2,
209208
check_positive_finite(function, "length scale", length_scale);
210209

211210
T_s sigma_sq = square(sigma);
212-
T_l root_3_inv_l_sq = sqrt(3.0) / length_scale;
213-
T_l neg_root_3_inv_l_sq = -1.0 * sqrt(3.0) / length_scale;
211+
T_l root_3_inv_l_sq = std::sqrt(3.0) / length_scale;
212+
T_l neg_root_3_inv_l_sq = -1.0 * std::sqrt(3.0) / length_scale;
214213

215214
for (size_t i = 0; i < x1_size; ++i) {
216215
for (size_t j = 0; j < x2_size; ++j) {
@@ -290,8 +289,8 @@ gp_matern32_cov(const std::vector<Eigen::Matrix<T_x1, -1, 1>> &x1,
290289
}
291290

292291
T_s sigma_sq = square(sigma);
293-
double root_3 = sqrt(3.0);
294-
double neg_root_3 = -1.0 * sqrt(3.0);
292+
double root_3 = std::sqrt(3.0);
293+
double neg_root_3 = -1.0 * std::sqrt(3.0);
295294

296295
std::vector<Eigen::Matrix<return_type_t<T_x1, T_l>, -1, 1>> x1_new
297296
= divide_columns(x1, length_scale);

stan/math/prim/fun/gp_matern52_cov.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ gp_matern52_cov(const std::vector<T_x> &x, const T_s &sigma,
5959
check_positive_finite("gp_matern52_cov", "length scale", length_scale);
6060

6161
T_s sigma_sq = square(sigma);
62-
T_l root_5_inv_l = sqrt(5.0) / length_scale;
62+
T_l root_5_inv_l = std::sqrt(5.0) / length_scale;
6363
T_l inv_l_sq_5_3 = 5.0 / (3.0 * square(length_scale));
64-
T_l neg_root_5_inv_l = -sqrt(5.0) / length_scale;
64+
T_l neg_root_5_inv_l = -std::sqrt(5.0) / length_scale;
6565

6666
for (size_t i = 0; i < x_size; ++i) {
6767
cov(i, i) = sigma_sq;
@@ -125,7 +125,7 @@ gp_matern52_cov(const std::vector<Eigen::Matrix<T_x, Eigen::Dynamic, 1>> &x,
125125
"number of length scales", l_size);
126126

127127
T_s sigma_sq = square(sigma);
128-
double root_5 = sqrt(5.0);
128+
double root_5 = std::sqrt(5.0);
129129
double five_thirds = 5.0 / 3.0;
130130
double neg_root_5 = -root_5;
131131

@@ -196,9 +196,9 @@ gp_matern52_cov(const std::vector<T_x1> &x1, const std::vector<T_x2> &x2,
196196
check_positive_finite("gp_matern52_cov", "length scale", length_scale);
197197

198198
T_s sigma_sq = square(sigma);
199-
T_l root_5_inv_l = sqrt(5.0) / length_scale;
199+
T_l root_5_inv_l = std::sqrt(5.0) / length_scale;
200200
T_l inv_l_sq_5_3 = 5.0 / (3.0 * square(length_scale));
201-
T_l neg_root_5_inv_l = -sqrt(5.0) / length_scale;
201+
T_l neg_root_5_inv_l = -std::sqrt(5.0) / length_scale;
202202

203203
for (size_t i = 0; i < x1_size; ++i) {
204204
for (size_t j = 0; j < x2_size; ++j) {
@@ -273,7 +273,7 @@ gp_matern52_cov(const std::vector<Eigen::Matrix<T_x1, Eigen::Dynamic, 1>> &x1,
273273
"number of length scales", l_size);
274274

275275
T_s sigma_sq = square(sigma);
276-
double root_5 = sqrt(5.0);
276+
double root_5 = std::sqrt(5.0);
277277
double five_thirds = 5.0 / 3.0;
278278
double neg_root_5 = -root_5;
279279

stan/math/prim/fun/lbeta.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stan/math/prim/fun/log_sum_exp.hpp>
1313
#include <stan/math/prim/fun/log1m.hpp>
1414
#include <stan/math/prim/fun/multiply_log.hpp>
15+
#include <cmath>
1516

1617
namespace stan {
1718
namespace math {
@@ -64,6 +65,7 @@ namespace math {
6465
*/
6566
template <typename T1, typename T2>
6667
return_type_t<T1, T2> lbeta(const T1 a, const T2 b) {
68+
using std::log;
6769
using T_ret = return_type_t<T1, T2>;
6870

6971
if (is_any_nan(a, b)) {

stan/math/prim/fun/lgamma_stirling.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace math {
2323
*/
2424
template <typename T>
2525
return_type_t<T> lgamma_stirling(const T x) {
26+
using std::log;
2627
return HALF_LOG_TWO_PI + (x - 0.5) * log(x) - x;
2728
}
2829

stan/math/prim/fun/log.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct log_fun {
3636
* @param[in] x container
3737
* @return Elementwise application of natural log to the argument.
3838
*/
39-
template <typename T, typename = require_not_eigen_vt<std::is_arithmetic, T>>
39+
template <typename T, typename = require_not_eigen_vt<std::is_arithmetic, T>, typename = require_not_double_or_int_t<T>>
4040
inline auto log(const T& x) {
4141
return apply_scalar_unary<log_fun, T>::apply(x);
4242
}

stan/math/prim/fun/log1p.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ namespace math {
2727
* @throw std::domain_error If argument is less than -1.
2828
*/
2929
inline double log1p(double x) {
30-
check_greater_or_equal("log1p", "x", x, -1.0);
31-
return std::log1p(x);
30+
if (is_nan(x)) {
31+
return x;
32+
} else {
33+
check_greater_or_equal("log1p", "x", x, -1.0);
34+
return std::log1p(x);
35+
}
3236
}
3337

3438
/**

stan/math/prim/fun/sqrt.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct sqrt_fun {
3030
* @param x container
3131
* @return Square root of each value in x.
3232
*/
33-
template <typename T, typename = require_not_eigen_vt<std::is_arithmetic, T>>
33+
template <typename T, typename = require_not_eigen_vt<std::is_arithmetic, T>, typename = require_not_double_or_int_t<T>>
3434
inline auto sqrt(const T& x) {
3535
return apply_scalar_unary<sqrt_fun, T>::apply(x);
3636
}

0 commit comments

Comments
 (0)