Stan Math Library  2.9.0
reverse mode automatic differentiation
log_determinant_spd.hpp
Go to the documentation of this file.
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 <boost/math/special_functions/fpclassify.hpp>
8 #include <stan/math/rev/core.hpp>
9 
10 namespace stan {
11 
12  namespace math {
13 
14  template <int R, int C>
15  inline var log_determinant_spd(const Eigen::Matrix<var, R, C>& m) {
17  using Eigen::Matrix;
18 
19  math::check_square("log_determinant_spd", "m", m);
20 
21  Matrix<double, R, C> m_d(m.rows(), m.cols());
22  for (int i = 0; i < m.size(); ++i)
23  m_d(i) = m(i).val();
24 
25  Eigen::LDLT<Matrix<double, R, C> > ldlt(m_d);
26  if (ldlt.info() != Eigen::Success) {
27  double y = 0;
28  domain_error("log_determinant_spd",
29  "matrix argument", y,
30  "failed LDLT factorization");
31  }
32 
33  // compute the inverse of A (needed for the derivative)
34  m_d.setIdentity(m.rows(), m.cols());
35  ldlt.solveInPlace(m_d);
36 
37  if (ldlt.isNegative() || (ldlt.vectorD().array() <= 1e-16).any()) {
38  double y = 0;
39  domain_error("log_determinant_spd",
40  "matrix argument", y,
41  "matrix is negative definite");
42  }
43 
44  double val = ldlt.vectorD().array().log().sum();
45 
46  if (!boost::math::isfinite(val)) {
47  double y = 0;
48  domain_error("log_determinant_spd",
49  "matrix argument", y,
50  "log determininant is infinite");
51  }
52 
53  vari** operands = ChainableStack::memalloc_
54  .alloc_array<vari*>(m.size());
55  for (int i = 0; i < m.size(); ++i)
56  operands[i] = m(i).vi_;
57 
58  double* gradients = ChainableStack::memalloc_
59  .alloc_array<double>(m.size());
60  for (int i = 0; i < m.size(); ++i)
61  gradients[i] = m_d(i);
62 
63  return var(new precomputed_gradients_vari(val, m.size(),
64  operands, gradients));
65  }
66 
67 
68  }
69 
70 }
71 #endif
bool isfinite(const stan::math::var &v)
Checks if the given number has finite value.
The variable implementation base class.
Definition: vari.hpp:30
Independent (input) and dependent (output) variables for gradients.
Definition: var.hpp:31
A variable implementation taking a sequence of operands and partial derivatives with respect to the o...
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.
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
double e()
Return the base of the natural logarithm.
Definition: constants.hpp:95
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.
T log_determinant_spd(const Eigen::Matrix< T, R, C > &m)
Returns the log absolute determinant of the specified square matrix.

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