Stan Math Library  2.9.0
reverse mode automatic differentiation
inverse_spd.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_INVERSE_SPD_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_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  using Eigen::Dynamic;
21  using Eigen::LDLT;
22  using Eigen::Matrix;
23  stan::math::check_square("inverse_spd", "m", m);
24  stan::math::check_symmetric("inverse_spd", "m", m);
25  Matrix<T, Dynamic, Dynamic> mmt = T(0.5) * (m + m.transpose());
26  // mmt = T(0.5) * mmt;
27  LDLT<Matrix<T, Dynamic, Dynamic> > ldlt(mmt); // 0.5*(m+m.transpose()));
28  if (ldlt.info() != Eigen::Success)
29  throw std::domain_error("Error in inverse_spd, LDLT "
30  "factorization failed");
31  if (!ldlt.isPositive())
32  throw std::domain_error("Error in inverse_spd, matrix "
33  "not positive definite");
34  Matrix<T, Dynamic, 1> diag_ldlt = ldlt.vectorD();
35  for (int i = 0; i < diag_ldlt.size(); ++i)
36  if (diag_ldlt(i) <= 0)
37  throw std::domain_error("Error in inverse_spd, matrix "
38  "not positive definite");
39  return ldlt.solve(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
40  ::Identity(m.rows(), m.cols()));
41  }
42 
43  }
44 }
45 #endif
void 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.
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 char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is symmetric.
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.

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