Automatic Differentiation
 
Loading...
Searching...
No Matches
inverse_spd.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_INVERSE_SPD_HPP
2#define STAN_MATH_PRIM_FUN_INVERSE_SPD_HPP
3
7
8namespace stan {
9namespace math {
10
21template <typename EigMat>
22inline Eigen::Matrix<value_type_t<EigMat>, Eigen::Dynamic, Eigen::Dynamic>
23inverse_spd(const EigMat& m) {
24 using Eigen::Dynamic;
25 using Eigen::LDLT;
26 using Eigen::Matrix;
27 using Scalar = value_type_t<EigMat>;
28 if (m.size() == 0) {
29 return {};
30 }
31 const Eigen::Ref<const plain_type_t<EigMat>>& m_ref = m;
32 check_symmetric("inverse_spd", "m", m_ref);
33 plain_type_t<EigMat> mmt = 0.5 * (m_ref + m_ref.transpose());
34 LDLT<plain_type_t<EigMat>> ldlt(mmt);
35 if (ldlt.info() != Eigen::Success) {
36 throw_domain_error("invese_spd", "LDLT factor failed", "", "");
37 }
38 if (!ldlt.isPositive()) {
39 throw_domain_error("invese_spd", "matrix not positive definite", "", "");
40 }
41 Matrix<Scalar, Dynamic, 1> diag_ldlt = ldlt.vectorD();
42 check_positive("inverse_spd", "matrix not positive definite", diag_ldlt);
43
44 return ldlt.solve(
45 Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>::Identity(
46 m.rows(), m.cols()));
47}
48
49} // namespace math
50} // namespace stan
51
52#endif
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
void throw_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.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
Eigen::Matrix< value_type_t< EigMat >, Eigen::Dynamic, Eigen::Dynamic > inverse_spd(const EigMat &m)
Returns the inverse of the specified symmetric, pos/neg-definite matrix.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...