Automatic Differentiation
 
Loading...
Searching...
No Matches
check_stochastic_column.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_ERR_CHECK_STOCHASTIC_COLUMN_HPP
2#define STAN_MATH_PRIM_ERR_CHECK_STOCHASTIC_COLUMN_HPP
3
12#include <sstream>
13#include <string>
14#include <iostream>
15namespace stan {
16namespace math {
17
34template <typename T, require_matrix_t<T>* = nullptr>
35void check_stochastic_column(const char* function, const char* name,
36 const T& theta) {
37 using std::fabs;
38 check_nonzero_size(function, name, theta);
39 auto&& theta_ref = to_ref(value_of_rec(theta));
40 for (Eigen::Index j = 0; j < theta_ref.cols(); ++j) {
41 value_type_t<decltype(theta_ref)> vec_sum = 0.0;
42 for (Eigen::Index i = 0; i < theta_ref.rows(); ++i) {
43 if (!(theta_ref.coeff(i, j) >= 0)) {
44 [&]() STAN_COLD_PATH {
45 std::ostringstream msg;
46 msg << "is not a valid column stochastic matrix. " << name << "["
47 << std::to_string(i + stan::error_index::value) << ", "
48 << std::to_string(i + stan::error_index::value) << "]"
49 << " = ";
50 std::string msg_str(msg.str());
51 throw_domain_error(function, name, theta_ref.coeff(i, j),
52 msg_str.c_str(),
53 ", but should be greater than or equal to 0");
54 }();
55 }
56 vec_sum += theta_ref.coeff(i, j);
57 }
58 if (!(fabs(1.0 - vec_sum) <= CONSTRAINT_TOLERANCE)) {
59 [&]() STAN_COLD_PATH {
60 std::stringstream msg;
61 msg << "is not a valid column stochastic matrix.";
62 msg.precision(10);
63 msg << " sum(" << name << "[:, " << std::to_string(j + 1)
64 << "]) = " << vec_sum << ", but should be ";
65 std::string msg_str(msg.str());
66 throw_domain_error(function, name, 1.0, msg_str.c_str());
67 }();
68 }
69 }
70}
71
88template <typename T, require_std_vector_t<T>* = nullptr>
89void check_stochastic_column(const char* function, const char* name,
90 const T& theta) {
91 for (size_t i = 0; i < theta.size(); ++i) {
92 check_stochastic_column(function, internal::make_iter_name(name, i).c_str(),
93 theta[i]);
94 }
95}
96
97} // namespace math
98} // namespace stan
99#endif
#define STAN_COLD_PATH
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto make_iter_name(const char *name)
double value_of_rec(const fvar< T > &v)
Return the value of the specified variable.
void throw_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.
void check_stochastic_column(const char *function, const char *name, const T &theta)
Throw an exception if the specified matrix is not a column stochastic matrix.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds in rank and in simplexes.
fvar< T > fabs(const fvar< T > &x)
Definition fabs.hpp:16
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...