| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

check_simplex.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_ERR_CHECK_SIMPLEX_HPP
2 #define STAN_MATH_PRIM_MAT_ERR_CHECK_SIMPLEX_HPP
3
4 #include <stan/math/prim/arr/err/check_nonzero_size.hpp>
5 #include <stan/math/prim/mat/err/constraint_tolerance.hpp>
6 #include <stan/math/prim/mat/fun/Eigen.hpp>
7 #include <stan/math/prim/mat/meta/index_type.hpp>
8 #include <stan/math/prim/scal/err/domain_error.hpp>
9 #include <stan/math/prim/scal/meta/error_index.hpp>
10 #include <sstream>
11 #include <string>
12
13 namespace stan {
14 namespace math {
15
39 template <typename T_prob>
40 bool check_simplex(const char* function,
41 const char* name,
42 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
43 using Eigen::Dynamic;
44 using Eigen::Matrix;
45
46 typedef typename index_type<Matrix<T_prob, Dynamic, 1> >::type size_t;
47
48 check_nonzero_size(function, name, theta);
49 if (!(fabs(1.0 - theta.sum()) <= CONSTRAINT_TOLERANCE)) {
50 std::stringstream msg;
51 T_prob sum = theta.sum();
52 msg << "is not a valid simplex.";
53 msg.precision(10);
54 msg << " sum(" << name << ") = " << sum
55 << ", but should be ";
56 std::string msg_str(msg.str());
57 domain_error(function, name, 1.0,
58 msg_str.c_str());
59 return false;
60 }
61 for (size_t n = 0; n < theta.size(); n++) {
62 if (!(theta[n] >= 0)) {
63 std::ostringstream msg;
64 msg << "is not a valid simplex. "
65 << name << "[" << n + stan::error_index::value << "]"
66 << " = ";
67 std::string msg_str(msg.str());
68 domain_error(function, name, theta[n],
69 msg_str.c_str(),
70 ", but should be greater than or equal to 0");
71 return false;
72 }
73 }
74 return true;
75 }
76 }
77 }
78 #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:15
stan
Definition: log_sum_exp.hpp:8
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:29
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:18
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:30
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:40
error_index.hpp
check_nonzero_size.hpp

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