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
16 /**
17 * Check if the specified vector is simplex.
18 * To be a simplex, all values must be greater than or equal to 0
19 * and the values must sum to 1.
20 *
21 * A valid simplex is one where the sum of hte elements is equal
22 * to 1. This function tests that the sum is within the tolerance
23 * specified by <code>CONSTRAINT_TOLERANCE</code>. This function
24 * only accepts Eigen vectors, statically typed vectors, not
25 * general matrices with 1 column.
26 *
27 * @tparam T_prob Scalar type of the vector
28 *
29 * @param function Function name (for error messages)
30 * @param name Variable name (for error messages)
31 * @param theta Vector to test.
32 *
33 * @throw <code>std::invalid_argument</code> if <code>theta</code>
34 * is a 0-vector.
35 * @throw <code>std::domain_error</code> if the vector is not a
36 * simplex or if any element is <code>NaN</code>.
37 */
38 template <typename T_prob>
check_simplex(const char * function,const char * name,const Eigen::Matrix<T_prob,Eigen::Dynamic,1> & theta)39 void check_simplex(const char* function, const char* name,
40 const Eigen::Matrix<T_prob, Eigen::Dynamic, 1>& theta) {
41 using Eigen::Dynamic;
42 using Eigen::Matrix;
43
44 typedef typename index_type<Matrix<T_prob, Dynamic, 1> >::type size_t;
45
46 check_nonzero_size(function, name, theta);
47 if (!(fabs(1.0 - theta.sum()) <= CONSTRAINT_TOLERANCE)) {
48 std::stringstream msg;
49 T_prob sum = theta.sum();
50 msg << "is not a valid simplex.";
51 msg.precision(10);
52 msg << " sum(" << name << ") = " << sum << ", but should be ";
53 std::string msg_str(msg.str());
54 domain_error(function, name, 1.0, msg_str.c_str());
55 }
56 for (size_t n = 0; n < theta.size(); n++) {
57 if (!(theta[n] >= 0)) {
58 std::ostringstream msg;
59 msg << "is not a valid simplex. " << name << "["
60 << n + stan::error_index::value << "]"
61 << " = ";
62 std::string msg_str(msg.str());
63 domain_error(function, name, theta[n], msg_str.c_str(),
64 ", but should be greater than or equal to 0");
65 }
66 }
67 }
68 } // namespace math
69 } // namespace stan
70 #endif
71