1 #ifndef __STAN__MATH__MATRIX_ERROR_HANDLING_HPP__
2 #define __STAN__MATH__MATRIX_ERROR_HANDLING_HPP__
7 #include <boost/type_traits/common_type.hpp>
14 template <
typename T_size1,
typename T_size2,
typename T_result,
22 typedef typename boost::common_type<T_size1,T_size2>::type
common_type;
24 std::ostringstream msg;
25 msg <<
"i and j must be same. Found i=%1%, j=" << j;
26 T_result tmp = raise_domain_error<T_result,T_size1>(
function,
37 template <
typename T_size1,
typename T_size2,
typename T_result>
46 template <
typename T_size1,
typename T_size2>
51 T_size1* result = 0) {
69 template <
typename T_y,
typename T_result,
class Policy>
71 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
84 std::ostringstream message;
85 message << name <<
" is not symmetric. "
86 << name <<
"[" << m <<
"," << n <<
"] is %1%, but "
87 << name <<
"[" << n <<
"," << m
88 <<
"] element is " << y(n,m);
90 = policies::raise_domain_error<T_y>(
function,
91 message.str().c_str(),
103 template <
typename T_y,
typename T_result>
105 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
111 template <
typename T>
113 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
135 template <
typename T_y,
typename T_result,
class Policy>
137 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
145 std::ostringstream message;
146 message << name <<
" is not positive definite. "
147 << name <<
"(0,0) is %1%.";
148 T_result tmp = policies::raise_domain_error<T_y>(
function,
149 message.str().c_str(),
155 Eigen::LDLT< Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> > cholesky
158 std::ostringstream message;
159 message << name <<
" is not positive definite. "
160 << name <<
"(0,0) is %1%.";
161 T_result tmp = policies::raise_domain_error<T_y>(
function,
162 message.str().c_str(),
172 template <
typename T_y,
typename T_result>
174 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
181 template <
typename T>
183 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
205 template <
typename T_y,
typename T_result,
class Policy>
207 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
213 if (!
check_positive(
function, y.rows(),
"rows", result, Policy()))
222 template <
typename T_y,
typename T_result>
224 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
231 template <
typename T>
233 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
257 template <
typename T_y,
typename T_result,
class Policy>
259 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
265 if (!
check_positive(
function, y.rows(),
"rows", result, Policy()))
270 k = 0; k < y.rows(); ++k) {
272 std::ostringstream message;
273 message << name <<
" is not a valid correlation matrix. "
274 << name <<
"(" << k <<
"," << k
275 <<
") is %1%, but should be near 1.0";
277 = policies::raise_domain_error<T_y>(
function,
278 message.str().c_str(),
291 template <
typename T_y,
typename T_result>
293 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
299 template <
typename T>
301 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
319 template <
typename T_covar,
typename T_result,
class Policy>
321 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
327 if (!
check_positive(
function, Sigma.rows(),
"rows", result, Policy()))
333 template <
typename T_covar,
typename T_result>
335 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
340 template <
typename T>
342 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
365 template <
typename T_prob,
369 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
375 if (theta.size() == 0) {
376 std::string message(name);
377 message +=
" is not a valid simplex. %1% elements in the vector.";
378 T_result tmp = raise_domain_error<size_t, size_t>(
function,
387 std::stringstream msg;
388 T_prob
sum = theta.sum();
389 msg <<
"in function check_simplex(%1%), ";
390 msg << name <<
" is not a valid simplex.";
391 msg <<
" The sum of the elements should be 1, but is " <<
sum;
392 T_result tmp = raise_domain_error<T_result,T_prob>(
function,
400 for (
size_t n = 0; n < theta.size(); n++) {
401 if (!(theta[n] >= 0)) {
402 std::ostringstream stream;
403 stream << name <<
" is not a valid simplex."
404 <<
" The element at " << n
405 <<
" is %1%, but should be greater than or equal to 0";
407 = raise_domain_error<T_result,T_prob>(
function,
408 stream.str().c_str(),
418 template <
typename T_y,
421 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& theta,
426 template <
typename T>
428 const Eigen::Matrix<T,Eigen::Dynamic,1>& theta,
451 template <
typename T_y,
typename T_result,
class Policy>
453 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
462 for (
size_t n = 1; n < y.size(); n++) {
463 if (!(y[n] > y[n-1])) {
464 std::ostringstream stream;
465 stream << name <<
" is not a valid ordered vector."
466 <<
" The element at " << n
467 <<
" is %1%, but should be greater than the previous element, "
469 T_result tmp = raise_domain_error<T_result,T_y>(
function,
470 stream.str().c_str(),
480 template <
typename T_y,
typename T_result>
482 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
487 template <
typename T>
489 const Eigen::Matrix<T,Eigen::Dynamic,1>& y,
509 template <
typename T_y,
typename T_result,
class Policy>
511 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
521 std::ostringstream stream;
522 stream << name <<
" is not a valid positive_ordered vector."
523 <<
" The element at 0 is %1%, but should be postive.";
524 T_result tmp = raise_domain_error<T_result,T_y>(
function,
525 stream.str().c_str(),
532 for (
size_t n = 1; n < y.size(); n++) {
533 if (!(y[n] > y[n-1])) {
534 std::ostringstream stream;
535 stream << name <<
" is not a valid ordered vector."
536 <<
" The element at " << n
537 <<
" is %1%, but should be greater than the previous element, "
539 T_result tmp = raise_domain_error<T_result,T_y>(
function,
540 stream.str().c_str(),
550 template <
typename T_y,
typename T_result>
552 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
557 template <
typename T>
559 const Eigen::Matrix<T,Eigen::Dynamic,1>& y,
569 template <
typename T_y,
typename T_result,
class Policy>
571 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
575 for (
int i = 0; i < y.rows(); i++) {
576 for (
int j = 0; j < y.cols(); j++) {
578 std::ostringstream message;
579 message << name <<
"[" << i <<
"," << j
580 <<
"] is %1%, but must not be nan!";
582 = policies::raise_domain_error<T_y>(
function,
583 message.str().c_str(),
593 template <
typename T_y,
typename T_result>
595 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
600 template <
typename T>
602 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
internal::traits< Derived >::Index size_type
bool isnan(const stan::agrad::var v)
Checks if the given number is NaN.
var fabs(const var &a)
Return the absolute value of the variable (cmath).
T_result raise_domain_error(const char *function, const char *message, const T_val &val, const Policy &)
bool check_corr_matrix(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result, const Policy &)
Return true if the specified matrix is a valid correlation matrix.
bool check_simplex(const char *function, const Eigen::Matrix< T_prob, Eigen::Dynamic, 1 > &theta, const char *name, T_result *result, const Policy &)
Return true if the specified vector is simplex.
bool check_ordered(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const char *name, T_result *result, const Policy &)
Return true if the specified vector is sorted into increasing order.
bool check_symmetric(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result, const Policy &)
Return true if the specified matrix is symmetric.
T sum(const std::vector< T > &xs)
Return the sum of the values in the specified standard vector.
bool check_pos_definite(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result, const Policy &)
Return true if the specified matrix is positive definite.
bool check_positive_ordered(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, 1 > &y, const char *name, T_result *result, const Policy &)
Return true if the specified vector contains only non-negative values and is sorted into increasing o...
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
bool check_cov_matrix(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result, const Policy &)
Return true if the specified matrix is a valid covariance matrix.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
bool check_size_match(const char *function, T_size1 i, T_size2 j, T_result *result, const Policy &)
Probability, optimization and sampling library.