1 #ifndef __STAN__MATH__MATRIX_ERROR_HANDLING_HPP__
2 #define __STAN__MATH__MATRIX_ERROR_HANDLING_HPP__
8 #include <boost/type_traits/common_type.hpp>
17 const std::string& expr,
20 std::stringstream msg;
21 msg <<
"Found negative dimension size in variable declaration"
22 <<
"; variable=" << var_name
23 <<
"; dimension size expression=" << expr
24 <<
"; expression value=" << val;
25 throw std::invalid_argument(msg.str());
30 template <
typename T_size1,
typename T_size2,
typename T_result,
40 typedef typename boost::common_type<T_size1,T_size2>::type
common_type;
41 if (static_cast<common_type>(i) != static_cast<common_type>(j)) {
42 std::ostringstream msg;
43 msg << name_i <<
" (%1%) and " << name_j <<
" (" << j <<
") must match in size";
44 T_result tmp = policies::raise_domain_error<T_result,T_size1>(
function,
54 template <
typename T_size1,
typename T_size2,
typename T_result>
65 template <
typename T_size1,
typename T_size2>
72 T_size1* result = 0) {
88 template <
typename T_y,
typename T_result,
class Policy>
90 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
97 size_type k = y.rows();
100 for (size_type m = 0; m < k; ++m) {
101 for (size_type n = m + 1; n < k; ++n) {
103 std::ostringstream message;
104 message << name <<
" is not symmetric. "
105 << name <<
"[" << m <<
"," << n <<
"] is %1%, but "
106 << name <<
"[" << n <<
"," << m
107 <<
"] element is " << y(n,m);
109 = policies::raise_domain_error<T_y>(
function,
110 message.str().c_str(),
122 template <
typename T_y,
typename T_result>
124 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
130 template <
typename T>
132 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
154 template <
typename T_y,
typename T_result,
class Policy>
156 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
164 std::ostringstream message;
165 message << name <<
" is not positive definite. "
166 << name <<
"(0,0) is %1%.";
167 T_result tmp = policies::raise_domain_error<T_y>(
function,
168 message.str().c_str(),
174 Eigen::LDLT< Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> > cholesky
177 std::ostringstream message;
178 message << name <<
" is not positive definite. "
179 << name <<
"(0,0) is %1%.";
180 T_result tmp = policies::raise_domain_error<T_y>(
function,
181 message.str().c_str(),
191 template <
typename T_y,
typename T_result>
193 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
200 template <
typename T>
202 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
224 template <
typename T_y,
typename T_result,
class Policy>
226 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
231 y.rows(),
"Rows of covariance matrix",
232 y.cols(),
"columns of covariance matrix",
235 if (!
check_positive(
function, y.rows(),
"rows", result, Policy()))
244 template <
typename T_y,
typename T_result>
246 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
253 template <
typename T>
255 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
279 template <
typename T_y,
typename T_result,
class Policy>
281 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
286 y.rows(),
"Rows of correlation matrix",
287 y.cols(),
"columns of correlation matrix",
290 if (!
check_positive(
function, y.rows(),
"rows", result, Policy()))
295 k = 0; k < y.rows(); ++k) {
297 std::ostringstream message;
298 message << name <<
" is not a valid correlation matrix. "
299 << name <<
"(" << k <<
"," << k
300 <<
") is %1%, but should be near 1.0";
302 = policies::raise_domain_error<T_y>(
function,
303 message.str().c_str(),
316 template <
typename T_y,
typename T_result>
318 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
324 template <
typename T>
326 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,
344 template <
typename T_covar,
typename T_result,
class Policy>
346 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
350 Sigma.rows(),
"Rows of covariance matrix",
351 Sigma.cols(),
"columns of covariance matrix",
354 if (!
check_positive(
function, Sigma.rows(),
"rows", result, Policy()))
360 template <
typename T_covar,
typename T_result>
362 const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
367 template <
typename T>
369 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
392 template <
typename T_prob,
396 const Eigen::Matrix<T_prob,Eigen::Dynamic,1>& theta,
402 if (theta.size() == 0) {
403 std::string message(name);
404 message +=
" is not a valid simplex. %1% elements in the vector.";
405 T_result tmp = raise_domain_error<size_t, size_t>(
function,
414 std::stringstream msg;
415 T_prob
sum = theta.sum();
416 msg <<
"in function check_simplex(%1%), ";
417 msg << name <<
" is not a valid simplex.";
418 msg <<
" The sum of the elements should be 1, but is " <<
sum;
419 T_result tmp = raise_domain_error<T_result,T_prob>(
function,
427 for (
size_t n = 0; n < theta.size(); n++) {
428 if (!(theta[n] >= 0)) {
429 std::ostringstream stream;
430 stream << name <<
" is not a valid simplex."
431 <<
" The element at " << n
432 <<
" is %1%, but should be greater than or equal to 0";
434 = raise_domain_error<T_result,T_prob>(
function,
435 stream.str().c_str(),
445 template <
typename T_y,
448 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& theta,
453 template <
typename T>
455 const Eigen::Matrix<T,Eigen::Dynamic,1>& theta,
478 template <
typename T_y,
typename T_result,
class Policy>
480 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
489 for (
size_t n = 1; n < y.size(); n++) {
490 if (!(y[n] > y[n-1])) {
491 std::ostringstream stream;
492 stream << name <<
" is not a valid ordered vector."
493 <<
" The element at " << n
494 <<
" is %1%, but should be greater than the previous element, "
496 T_result tmp = raise_domain_error<T_result,T_y>(
function,
497 stream.str().c_str(),
507 template <
typename T_y,
typename T_result>
509 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
514 template <
typename T>
516 const Eigen::Matrix<T,Eigen::Dynamic,1>& y,
536 template <
typename T_y,
typename T_result,
class Policy>
538 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
548 std::ostringstream stream;
549 stream << name <<
" is not a valid positive_ordered vector."
550 <<
" The element at 0 is %1%, but should be postive.";
551 T_result tmp = raise_domain_error<T_result,T_y>(
function,
552 stream.str().c_str(),
559 for (
size_t n = 1; n < y.size(); n++) {
560 if (!(y[n] > y[n-1])) {
561 std::ostringstream stream;
562 stream << name <<
" is not a valid ordered vector."
563 <<
" The element at " << n
564 <<
" is %1%, but should be greater than the previous element, "
566 T_result tmp = raise_domain_error<T_result,T_y>(
function,
567 stream.str().c_str(),
577 template <
typename T_y,
typename T_result>
579 const Eigen::Matrix<T_y,Eigen::Dynamic,1>& y,
584 template <
typename T>
586 const Eigen::Matrix<T,Eigen::Dynamic,1>& y,
596 template <
typename T_y,
typename T_result,
class Policy>
598 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
602 for (
int i = 0; i < y.rows(); i++) {
603 for (
int j = 0; j < y.cols(); j++) {
605 std::ostringstream message;
606 message << name <<
"[" << i <<
"," << j
607 <<
"] is %1%, but must not be nan!";
609 = policies::raise_domain_error<T_y>(
function,
610 message.str().c_str(),
620 template <
typename T_y,
typename T_result>
622 const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
627 template <
typename T>
629 const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& y,