All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
check_bounded.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_SCAL_ERR_CHECK_BOUNDED_HPP
#define STAN_MATH_PRIM_SCAL_ERR_CHECK_BOUNDED_HPP
#include <stan/math/prim/scal/err/domain_error.hpp>
#include <stan/math/prim/scal/err/domain_error_vec.hpp>
#include <stan/math/prim/scal/meta/max_size.hpp>
#include <stan/math/prim/scal/meta/VectorView.hpp>
#include <string>
namespace stan {
namespace math {
namespace detail {
// implemented using structs because there is no partial specialization
// for templated functions
//
// default implementation works for scalar T_y. T_low and T_high can
// be either scalar or vector
//
// throws if y, low, or high is nan
template <typename T_y, typename T_low, typename T_high,
bool y_is_vec>
struct bounded {
static bool check(const char* function,
const char* name,
const T_y& y,
const T_low& low,
const T_high& high) {
using stan::max_size;
VectorView<const T_low> low_vec(low);
VectorView<const T_high> high_vec(high);
for (size_t n = 0; n < max_size(low, high); n++) {
if (!(low_vec[n] <= y && y <= high_vec[n])) {
std::stringstream msg;
msg << ", but must be between ";
msg << "(" << low_vec[n] << ", " << high_vec[n] << ")";
std::string msg_str(msg.str());
domain_error(function, name, y,
"is ", msg_str.c_str());
}
}
return true;
}
};
template <typename T_y, typename T_low, typename T_high>
struct bounded<T_y, T_low, T_high, true> {
static bool check(const char* function,
const char* name,
const T_y& y,
const T_low& low,
const T_high& high) {
using stan::length;
using stan::get;
VectorView<const T_low> low_vec(low);
VectorView<const T_high> high_vec(high);
for (size_t n = 0; n < length(y); n++) {
if (!(low_vec[n] <= get(y, n) && get(y, n) <= high_vec[n])) {
std::stringstream msg;
msg << ", but must be between ";
msg << "(" << low_vec[n] << ", " << high_vec[n] << ")";
std::string msg_str(msg.str());
domain_error_vec(function, name, y, n,
"is ", msg_str.c_str());
}
}
return true;
}
};
}
template <typename T_y, typename T_low, typename T_high>
inline bool check_bounded(const char* function,
const char* name,
const T_y& y,
const T_low& low,
const T_high& high) {
return detail::bounded<T_y, T_low, T_high,
is_vector_like<T_y>::value>
::check(function, name, y, low, high);
}
}
}
#endif
stan::is_vector_like
Definition: is_vector_like.hpp:12
stan::length
size_t length(const std::vector< T > &x)
Definition: length.hpp:10
stan::math::check_bounded
bool check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Return true if the value is between the low and high values, inclusively.
Definition: check_bounded.hpp:95
stan::math::detail::bounded< T_y, T_low, T_high, true >::check
static bool check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Definition: check_bounded.hpp:50
domain_error_vec.hpp
VectorView.hpp
stan::math::detail::bounded::check
static bool check(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Definition: check_bounded.hpp:25
stan::math::domain_error_vec
void domain_error_vec(const char *function, const char *name, const T &y, const size_t i, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
Definition: domain_error_vec.hpp:38
stan::get
T get(const std::vector< T > &x, size_t n)
Definition: get.hpp:10
stan::max_size
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
stan::math::domain_error
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
Definition: domain_error.hpp:32
stan::math::detail::bounded
Definition: check_bounded.hpp:24
domain_error.hpp
stan::VectorView
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
Definition: VectorView.hpp:41
max_size.hpp

[ Stan Home Page ] © 2011–2015, Stan Development Team.