Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
check_pos_semidefinite.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__ERROR_HANDLING__MATRIX__CHECK_POS_SEMIDEFINITE_HPP
2 #define STAN__MATH__ERROR_HANDLING__MATRIX__CHECK_POS_SEMIDEFINITE_HPP
3 
4 #include <sstream>
5 
6 #include <boost/math/special_functions/fpclassify.hpp>
7 
12 
13 namespace stan {
14 
15  namespace math {
16 
30  // FIXME: update warnings (message has (0,0) item)
31  template <typename T_y, typename T_result>
32  inline bool
33  check_pos_semidefinite(const char* function,
34  const Eigen::Matrix<T_y,
35  Eigen::Dynamic,
36  Eigen::Dynamic>& y,
37  const char* name,
38  T_result* result) {
39  using Eigen::Dynamic;
40  using Eigen::Matrix;
41  typedef typename index_type<Matrix<T_y,Dynamic,Dynamic> >::type size_type;
42 
43  if (y.rows() == 1 && !(y(0,0) >= 0.0)) {
44  std::ostringstream message;
45  message << name << " is not positive semi-definite. "
46  << name << "(0,0) is %1%.";
47  std::string msg(message.str());
48  return dom_err(function,y(0,0),name,msg.c_str(),"",result);
49  }
50  Eigen::LDLT< Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> > cholesky
51  = y.ldlt();
52  if(cholesky.info() != Eigen::Success || (cholesky.vectorD().array() < 0.0).any()) {
53  std::ostringstream message;
54  message << name << " is not positive semi-definite. "
55  << name << "(0,0) is %1%.";
56  std::string msg(message.str());
57  return dom_err(function,y(0,0),name,msg.c_str(),"",result);
58  }
59  for (int i = 0; i < y.size(); i++)
60  if (boost::math::isnan(y(i))) {
61  std::ostringstream message;
62  message << name << " is not positive semi-definite. "
63  << name << "(0,0) is %1%.";
64  std::string msg(message.str());
65  return dom_err(function,y(0,0),name,msg.c_str(),"",result);
66  }
67  return true;
68  }
69 
70  }
71 }
72 #endif
bool isnan(const stan::agrad::var &v)
Checks if the given number is NaN.
bool dom_err(const char *function, const T &y, const char *name, const char *error_msg, const T_msg error_msg2, T_result *result)
Definition: dom_err.hpp:36
bool check_pos_semidefinite(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is positive definite.
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:21
index_type< Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > >::type size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:16

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