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
4 changes: 2 additions & 2 deletions stan/math/fwd/scal/fun/fmod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <stan/math/prim/scal/fun/value_of.hpp>

namespace stan {
Expand All @@ -21,7 +21,7 @@ inline fvar<T> fmod(const fvar<T>& x1, const fvar<T>& x2) {
template <typename T>
inline fvar<T> fmod(const fvar<T>& x1, double x2) {
using std::fmod;
if (unlikely(is_nan(value_of(x1.val_)) || is_nan(x2)))
if (unlikely(is_any_nan(value_of(x1.val_), x2)))
return fvar<T>(fmod(x1.val_, x2), NOT_A_NUMBER);
else
return fvar<T>(fmod(x1.val_, x2), x1.d_ / x2);
Expand Down
1 change: 1 addition & 0 deletions stan/math/prim/scal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
#include <stan/math/prim/scal/fun/inv_sqrt.hpp>
#include <stan/math/prim/scal/fun/inv_square.hpp>
#include <stan/math/prim/scal/fun/is_inf.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_uninitialized.hpp>
#include <stan/math/prim/scal/fun/lb_constrain.hpp>
Expand Down
5 changes: 4 additions & 1 deletion stan/math/prim/scal/fun/fdim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <boost/math/tools/promotion.hpp>

namespace stan {
Expand All @@ -23,7 +24,9 @@ namespace math {
template <typename T1, typename T2>
inline typename boost::math::tools::promote_args<T1, T2>::type fdim(T1 x,
T2 y) {
if (is_nan(x) || is_nan(y))
typedef typename boost::math::tools::promote_args<T1, T2>::type return_t;
using std::numeric_limits;
if (is_any_nan(x, y))
return NOT_A_NUMBER;
return (x <= y) ? 0 : x - y;
}
Expand Down
5 changes: 3 additions & 2 deletions stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/fun/multiply_log.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <cmath>
#include <limits>

Expand Down Expand Up @@ -53,8 +54,8 @@ typename return_type<T1, T2>::type grad_reg_inc_gamma(T1 a, T2 z, T1 g, T1 dig,
using std::log;
typedef typename return_type<T1, T2>::type TP;

if (is_nan(a) || is_nan(z) || is_nan(g) || is_nan(dig))
return NOT_A_NUMBER;
if (is_any_nan(a, z, g, dig))
return std::numeric_limits<TP>::quiet_NaN();

T2 l = log(z);
if (z >= a && z >= 8) {
Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/scal/fun/grad_reg_lower_inc_gamma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <stan/math/prim/scal/fun/gamma_p.hpp>
#include <stan/math/prim/scal/fun/log1p.hpp>
#include <stan/math/prim/scal/fun/digamma.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <stan/math/prim/scal/fun/is_inf.hpp>
#include <stan/math/prim/scal/fun/grad_reg_inc_gamma.hpp>
#include <stan/math/prim/scal/fun/value_of_rec.hpp>
Expand Down Expand Up @@ -110,7 +110,7 @@ typename return_type<T1, T2>::type grad_reg_lower_inc_gamma(
using std::pow;
typedef typename return_type<T1, T2>::type TP;

if (is_nan(a) || is_nan(z))
if (is_any_nan(a, z))
return std::numeric_limits<TP>::quiet_NaN();

check_positive_finite("grad_reg_lower_inc_gamma", "a", a);
Expand Down
41 changes: 41 additions & 0 deletions stan/math/prim/scal/fun/is_any_nan.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef STAN_MATH_PRIM_SCAL_FUN_IS_ANY_NAN_HPP
#define STAN_MATH_PRIM_SCAL_FUN_IS_ANY_NAN_HPP

#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <utility>

namespace stan {
namespace math {

/**
* Returns true if the input is NaN and false otherwise.
*
* Delegates to <code>stan::math::is_nan</code> so that
* appropriate specializations can be loaded for autodiff
* types.
*
* @param x Value to test.
* @return <code>true</code> if the value is NaN.
*/
template <typename T>
inline bool is_any_nan(const T& x) {
return is_nan(x);
}

/**
* Returns <code>true</code> if any input is NaN and false otherwise.
*
* Delegates to <code>stan::math::is_nan</code>.
*
* @param x first argument
* @param xs parameter pack of remaining arguments to forward to function
* @return <code>true</code> if any value is NaN
*/
template <typename T, typename... Ts>
inline bool is_any_nan(const T& x, const Ts&... xs) {
return is_any_nan(x) || is_any_nan(std::forward<const Ts>(xs)...);
}
} // namespace math
} // namespace stan

#endif
6 changes: 5 additions & 1 deletion stan/math/prim/scal/fun/is_nan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ namespace math {
* @param x Value to test.
* @return <code>true</code> if the value is NaN.
*/
inline bool is_nan(double x) { return std::isnan(x); }
template <typename T>
inline typename std::enable_if_t<std::is_arithmetic<T>::value, bool> is_nan(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here as in the issue for changing all of these. At most, this should be

template <typename T>
inline typename std::enable_if_t<std::is_arithmetic<T>::value, bool> is_nan(T x) {
  return std::isnan(x);
}

so primitives get passed by value.

T x) {
return std::isnan(x);
}

} // namespace math
} // namespace stan
Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/scal/fun/log_falling_factorial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define STAN_MATH_PRIM_SCAL_FUN_LOG_FALLING_FACTORIAL_HPP

#include <stan/math/prim/scal/err/check_positive.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <stan/math/prim/scal/fun/lgamma.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/meta/return_type.hpp>
Expand Down Expand Up @@ -54,7 +54,7 @@ namespace math {
template <typename T1, typename T2>
inline typename return_type<T1, T2>::type log_falling_factorial(const T1 x,
const T2 n) {
if (is_nan(x) || is_nan(n))
if (is_any_nan(x, n))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[comment]
The changes look awesome! It does look like there's an impact on how we will write functions here on out.

return NOT_A_NUMBER;
static const char* function = "log_falling_factorial";
check_positive(function, "first argument", x);
Expand Down
6 changes: 3 additions & 3 deletions stan/math/prim/scal/fun/log_rising_factorial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define STAN_MATH_PRIM_SCAL_FUN_LOG_RISING_FACTORIAL_HPP

#include <stan/math/prim/scal/err/check_positive.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <stan/math/prim/scal/fun/lgamma.hpp>
#include <stan/math/prim/scal/fun/constants.hpp>
#include <stan/math/prim/scal/meta/return_type.hpp>
Expand Down Expand Up @@ -52,8 +52,8 @@ namespace math {
template <typename T1, typename T2>
inline typename return_type<T1, T2>::type log_rising_factorial(const T1& x,
const T2& n) {
if (is_nan(x) || is_nan(n))
return NOT_A_NUMBER;
if (is_any_nan(x, n))
return std::numeric_limits<double>::quiet_NaN();
static const char* function = "log_rising_factorial";
check_positive(function, "first argument", x);
return lgamma(x + n) - lgamma(x);
Expand Down
6 changes: 3 additions & 3 deletions stan/math/rev/core/operator_addition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stan/math/rev/core/var.hpp>
#include <stan/math/rev/core/vv_vari.hpp>
#include <stan/math/rev/core/vd_vari.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <limits>

namespace stan {
Expand All @@ -16,7 +16,7 @@ class add_vv_vari : public op_vv_vari {
add_vv_vari(vari* avi, vari* bvi)
: op_vv_vari(avi->val_ + bvi->val_, avi, bvi) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bvi_->val_))) {
if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
Expand All @@ -30,7 +30,7 @@ class add_vd_vari : public op_vd_vari {
public:
add_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ + b, avi, b) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bd_)))
if (unlikely(is_any_nan(avi_->val_, bd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_;
Expand Down
6 changes: 3 additions & 3 deletions stan/math/rev/core/operator_division.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stan/math/rev/core/vv_vari.hpp>
#include <stan/math/rev/core/vd_vari.hpp>
#include <stan/math/rev/core/dv_vari.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <limits>

namespace stan {
Expand All @@ -18,7 +18,7 @@ class divide_vv_vari : public op_vv_vari {
divide_vv_vari(vari* avi, vari* bvi)
: op_vv_vari(avi->val_ / bvi->val_, avi, bvi) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bvi_->val_))) {
if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
Expand All @@ -32,7 +32,7 @@ class divide_vd_vari : public op_vd_vari {
public:
divide_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ / b, avi, b) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bd_)))
if (unlikely(is_any_nan(avi_->val_, bd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_ / bd_;
Expand Down
6 changes: 3 additions & 3 deletions stan/math/rev/core/operator_multiplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stan/math/rev/core/var.hpp>
#include <stan/math/rev/core/vv_vari.hpp>
#include <stan/math/rev/core/vd_vari.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <limits>

namespace stan {
Expand All @@ -16,7 +16,7 @@ class multiply_vv_vari : public op_vv_vari {
multiply_vv_vari(vari* avi, vari* bvi)
: op_vv_vari(avi->val_ * bvi->val_, avi, bvi) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bvi_->val_))) {
if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
Expand All @@ -30,7 +30,7 @@ class multiply_vd_vari : public op_vd_vari {
public:
multiply_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ * b, avi, b) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bd_)))
if (unlikely(is_any_nan(avi_->val_, bd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_ * bd_;
Expand Down
8 changes: 4 additions & 4 deletions stan/math/rev/core/operator_subtraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stan/math/rev/core/vv_vari.hpp>
#include <stan/math/rev/core/vd_vari.hpp>
#include <stan/math/rev/core/dv_vari.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <limits>

namespace stan {
Expand All @@ -17,7 +17,7 @@ class subtract_vv_vari : public op_vv_vari {
subtract_vv_vari(vari* avi, vari* bvi)
: op_vv_vari(avi->val_ - bvi->val_, avi, bvi) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bvi_->val_))) {
if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
Expand All @@ -31,7 +31,7 @@ class subtract_vd_vari : public op_vd_vari {
public:
subtract_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ - b, avi, b) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bd_)))
if (unlikely(is_any_nan(avi_->val_, bd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_;
Expand All @@ -42,7 +42,7 @@ class subtract_dv_vari : public op_dv_vari {
public:
subtract_dv_vari(double a, vari* bvi) : op_dv_vari(a - bvi->val_, a, bvi) {}
void chain() {
if (unlikely(is_nan(ad_) || is_nan(bvi_->val_)))
if (unlikely(is_any_nan(ad_, bvi_->val_)))
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
bvi_->adj_ -= adj_;
Expand Down
8 changes: 4 additions & 4 deletions stan/math/rev/scal/fun/fdim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define STAN_MATH_REV_SCAL_FUN_FDIM_HPP

#include <stan/math/rev/meta.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <stan/math/rev/core.hpp>
#include <limits>

Expand All @@ -15,7 +15,7 @@ class fdim_vv_vari : public op_vv_vari {
fdim_vv_vari(vari* avi, vari* bvi)
: op_vv_vari(avi->val_ - bvi->val_, avi, bvi) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bvi_->val_))) {
if (unlikely(is_any_nan(avi_->val_, bvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
Expand All @@ -29,7 +29,7 @@ class fdim_vd_vari : public op_vd_vari {
public:
fdim_vd_vari(vari* avi, double b) : op_vd_vari(avi->val_ - b, avi, b) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bd_)))
if (unlikely(is_any_nan(avi_->val_, bd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_;
Expand All @@ -40,7 +40,7 @@ class fdim_dv_vari : public op_dv_vari {
public:
fdim_dv_vari(double a, vari* bvi) : op_dv_vari(a - bvi->val_, a, bvi) {}
void chain() {
if (unlikely(is_nan(bvi_->val_) || is_nan(ad_)))
if (unlikely(is_any_nan(bvi_->val_, ad_)))
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
bvi_->adj_ -= adj_;
Expand Down
13 changes: 6 additions & 7 deletions stan/math/rev/scal/fun/fma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stan/math/rev/meta.hpp>
#include <stan/math/rev/core.hpp>
#include <stan/math/prim/scal/fun/fma.hpp>
#include <stan/math/prim/scal/fun/is_nan.hpp>
#include <stan/math/prim/scal/fun/is_any_nan.hpp>
#include <limits>

namespace stan {
Expand All @@ -16,8 +16,7 @@ class fma_vvv_vari : public op_vvv_vari {
fma_vvv_vari(vari* avi, vari* bvi, vari* cvi)
: op_vvv_vari(fma(avi->val_, bvi->val_, cvi->val_), avi, bvi, cvi) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bvi_->val_)
|| is_nan(cvi_->val_))) {
if (unlikely(is_any_nan(avi_->val_, bvi_->val_, cvi_->val_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
cvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
Expand All @@ -34,7 +33,7 @@ class fma_vvd_vari : public op_vvd_vari {
fma_vvd_vari(vari* avi, vari* bvi, double c)
: op_vvd_vari(fma(avi->val_, bvi->val_, c), avi, bvi, c) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bvi_->val_) || is_nan(cd_))) {
if (unlikely(is_any_nan(avi_->val_, bvi_->val_, cd_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
bvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
Expand All @@ -49,7 +48,7 @@ class fma_vdv_vari : public op_vdv_vari {
fma_vdv_vari(vari* avi, double b, vari* cvi)
: op_vdv_vari(fma(avi->val_, b, cvi->val_), avi, b, cvi) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(cvi_->val_) || is_nan(bd_))) {
if (unlikely(is_any_nan(avi_->val_, cvi_->val_, bd_))) {
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
cvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
} else {
Expand All @@ -64,7 +63,7 @@ class fma_vdd_vari : public op_vdd_vari {
fma_vdd_vari(vari* avi, double b, double c)
: op_vdd_vari(fma(avi->val_, b, c), avi, b, c) {}
void chain() {
if (unlikely(is_nan(avi_->val_) || is_nan(bd_) || is_nan(cd_)))
if (unlikely(is_any_nan(avi_->val_, bd_, cd_)))
avi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
avi_->adj_ += adj_ * bd_;
Expand All @@ -76,7 +75,7 @@ class fma_ddv_vari : public op_ddv_vari {
fma_ddv_vari(double a, double b, vari* cvi)
: op_ddv_vari(fma(a, b, cvi->val_), a, b, cvi) {}
void chain() {
if (unlikely(is_nan(cvi_->val_) || is_nan(ad_) || is_nan(bd_)))
if (unlikely(is_any_nan(cvi_->val_, ad_, bd_)))
cvi_->adj_ = std::numeric_limits<double>::quiet_NaN();
else
cvi_->adj_ += adj_;
Expand Down
Loading