1 #ifndef STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_SPD_HPP
2 #define STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_SPD_HPP
3 
4 #include <stan/math/prim/scal/err/domain_error.hpp>
5 #include <stan/math/prim/scal/err/check_finite.hpp>
6 #include <stan/math/prim/mat/err/check_square.hpp>
7 #include <stan/math/prim/mat/fun/Eigen.hpp>
8 #include <stan/math/rev/core.hpp>
9 
10 namespace stan {
11 namespace math {
12 
13 template <int R, int C>
log_determinant_spd(const Eigen::Matrix<var,R,C> & m)14 inline var log_determinant_spd(const Eigen::Matrix<var, R, C>& m) {
15   using Eigen::Matrix;
16 
17   check_square("log_determinant_spd", "m", m);
18 
19   Matrix<double, R, C> m_d(m.rows(), m.cols());
20   for (int i = 0; i < m.size(); ++i)
21     m_d(i) = m(i).val();
22 
23   Eigen::LDLT<Matrix<double, R, C> > ldlt(m_d);
24   if (ldlt.info() != Eigen::Success) {
25     double y = 0;
26     domain_error("log_determinant_spd", "matrix argument", y,
27                  "failed LDLT factorization");
28   }
29 
30   // compute the inverse of A (needed for the derivative)
31   m_d.setIdentity(m.rows(), m.cols());
32   ldlt.solveInPlace(m_d);
33 
34   if (ldlt.isNegative() || (ldlt.vectorD().array() <= 1e-16).any()) {
35     double y = 0;
36     domain_error("log_determinant_spd", "matrix argument", y,
37                  "matrix is negative definite");
38   }
39 
40   double val = ldlt.vectorD().array().log().sum();
41 
42   check_finite("log_determinant_spd",
43                "log determininant of the matrix argument", val);
44 
45   vari** operands
46       = ChainableStack::instance().memalloc_.alloc_array<vari*>(m.size());
47   for (int i = 0; i < m.size(); ++i)
48     operands[i] = m(i).vi_;
49 
50   double* gradients
51       = ChainableStack::instance().memalloc_.alloc_array<double>(m.size());
52   for (int i = 0; i < m.size(); ++i)
53     gradients[i] = m_d(i);
54 
55   return var(
56       new precomputed_gradients_vari(val, m.size(), operands, gradients));
57 }
58 
59 }  // namespace math
60 
61 }  // namespace stan
62 #endif
63