Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
inverse_spd.hpp
Go to the documentation of this file.
1 #ifndef STAN__MATH__MATRIX__INVERSE_SPD_HPP
2 #define STAN__MATH__MATRIX__INVERSE_SPD_HPP
3 
7 
8 namespace stan {
9  namespace math {
10 
16  template <typename T>
17  inline
18  Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>
19  inverse_spd(const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) {
20  stan::math::check_square("inverse_spd(%1%)",m,"m",(double*)0);
21  stan::math::check_symmetric("inverse_spd(%1%)",m,"m",(double*)0);
22  Eigen::LDLT< Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> > ldlt(0.5*(m+m.transpose()));
23  if (ldlt.info() != Eigen::Success)
24  throw std::domain_error("Error in inverse_spd, LDLT factorization failed");
25  if (!ldlt.isPositive() || (ldlt.vectorD().array() <= 0).any())
26  throw std::domain_error("Error in inverse_spd, matrix not positive definite");
27  return ldlt.solve(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>::Identity(m.rows(),m.cols()));
28  }
29 
30  }
31 }
32 #endif
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > inverse_spd(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &m)
Returns the inverse of the specified symmetric, pos/neg-definite matrix.
Definition: inverse_spd.hpp:19
bool check_symmetric(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 symmetric.
bool check_square(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 square.

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