All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
check_simplex.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_SIMPLEX_HPP
#define STAN_MATH_PRIM_MAT_ERR_CHECK_SIMPLEX_HPP
#include <stan/math/prim/scal/err/domain_error.hpp>
#include <stan/math/prim/mat/err/constraint_tolerance.hpp>
#include <stan/math/prim/scal/err/check_nonzero_size.hpp>
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/meta/index_type.hpp>
#include <stan/math/prim/scal/meta/error_index.hpp>
#include <sstream>
#include <string>
namespace stan {
namespace math {
template <typename T_prob>
bool check_simplex(const char* function,
const char* name,
const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
using Eigen::Dynamic;
using Eigen::Matrix;
using stan::math::index_type;
typedef typename index_type<Matrix<T_prob, Dynamic, 1> >::type size_t;
check_nonzero_size(function, name, theta);
if (!(fabs(1.0 - theta.sum()) <= CONSTRAINT_TOLERANCE)) {
std::stringstream msg;
T_prob sum = theta.sum();
msg << "is not a valid simplex.";
msg.precision(10);
msg << " sum(" << name << ") = " << sum
<< ", but should be ";
std::string msg_str(msg.str());
domain_error(function, name, 1.0,
msg_str.c_str());
return false;
}
for (size_t n = 0; n < theta.size(); n++) {
if (!(theta[n] >= 0)) {
std::ostringstream msg;
msg << "is not a valid simplex. "
<< name << "[" << n + stan::error_index::value << "]"
<< " = ";
std::string msg_str(msg.str());
domain_error(function, name, theta[n],
msg_str.c_str(),
", but should be greater than or equal to 0");
return false;
}
}
return true;
}
}
}
#endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
constraint_tolerance.hpp
stan::math::CONSTRAINT_TOLERANCE
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
Definition: constraint_tolerance.hpp:11
stan::math::check_nonzero_size
bool check_nonzero_size(const char *function, const char *name, const T_y &y)
Return true if the specified matrix/vector is of non-zero size.
Definition: check_nonzero_size.hpp:31
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
index_type.hpp
stan::error_index::value
Definition: error_index.hpp:7
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
Eigen.hpp
domain_error.hpp
stan::math::check_simplex
bool check_simplex(const char *function, const char *name, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta)
Return true if the specified vector is simplex.
Definition: check_simplex.hpp:41
error_index.hpp
check_nonzero_size.hpp

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