| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/err/check_square.hpp>
6 #include <stan/math/prim/mat/err/check_symmetric.hpp>
7 #include <stdexcept>
8
9 namespace stan {
10 namespace math {
11
17 template <typename T>
18 inline
19 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
20 inverse_spd(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& m) {
21 using Eigen::Dynamic;
22 using Eigen::LDLT;
23 using Eigen::Matrix;
24 stan::math::check_square("inverse_spd", "m", m);
25 stan::math::check_symmetric("inverse_spd", "m", m);
26 Matrix<T, Dynamic, Dynamic> mmt = T(0.5) * (m + m.transpose());
27 // mmt = T(0.5) * mmt;
28 LDLT<Matrix<T, Dynamic, Dynamic> > ldlt(mmt); // 0.5*(m+m.transpose()));
29 if (ldlt.info() != Eigen::Success)
30 throw std::domain_error("Error in inverse_spd, LDLT "
31 "factorization failed");
32 if (!ldlt.isPositive())
33 throw std::domain_error("Error in inverse_spd, matrix "
34 "not positive definite");
35 Matrix<T, Dynamic, 1> diag_ldlt = ldlt.vectorD();
36 for (int i = 0; i < diag_ldlt.size(); ++i)
37 if (diag_ldlt(i) <= 0)
38 throw std::domain_error("Error in inverse_spd, matrix "
39 "not positive definite");
40 return ldlt.solve(Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
41 ::Identity(m.rows(), m.cols()));
42 }
43
44 }
45 }
46 #endif
stan
Definition: log_sum_exp.hpp:8
check_symmetric.hpp
stan::math::domain_error
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.
Definition: domain_error.hpp:32
Eigen.hpp
stan::math::inverse_spd
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:20
stan::math::check_symmetric
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.
Definition: check_symmetric.hpp:37
stan::math::check_square
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.
Definition: check_square.hpp:28
check_square.hpp

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