1 #ifndef STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_SPD_HPP
2 #define STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_SPD_HPP
4 #include <boost/math/special_functions/fpclassify.hpp>
14 template <
int R,
int C>
21 Matrix<double, R, C> m_d(m.rows(), m.cols());
22 for (
int i = 0; i < m.size(); ++i)
25 Eigen::LDLT<Matrix<double, R, C> > ldlt(m_d);
26 if (ldlt.info() != Eigen::Success) {
30 "failed LDLT factorization");
34 m_d.setIdentity(m.rows(), m.cols());
35 ldlt.solveInPlace(m_d);
37 if (ldlt.isNegative() || (ldlt.vectorD().array() <= 1
e-16).any()) {
41 "matrix is negative definite");
44 double val = ldlt.vectorD().array().log().sum();
50 "log determininant is infinite");
55 for (
int i = 0; i < m.size(); ++i)
56 operands[i] = m(i).vi_;
60 for (
int i = 0; i < m.size(); ++i)
61 gradients[i] = m_d(i);
64 operands, gradients));
bool isfinite(const stan::math::var &v)
Checks if the given number has finite value.
static stack_alloc memalloc_
The variable implementation base class.
Independent (input) and dependent (output) variables for gradients.
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.
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.