1 #ifndef STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_SPD_HPP 2 #define STAN_MATH_REV_MAT_FUN_LOG_DETERMINANT_SPD_HPP 14 template <
int R,
int C>
20 Matrix<double, R, C> m_d(m.rows(), m.cols());
21 for (
int i = 0; i < m.size(); ++i)
24 Eigen::LDLT<Matrix<double, R, C> > ldlt(m_d);
25 if (ldlt.info() != Eigen::Success) {
27 domain_error(
"log_determinant_spd",
"matrix argument", y,
28 "failed LDLT factorization");
32 m_d.setIdentity(m.rows(), m.cols());
33 ldlt.solveInPlace(m_d);
35 if (ldlt.isNegative() || (ldlt.vectorD().array() <= 1
e-16).any()) {
37 domain_error(
"log_determinant_spd",
"matrix argument", y,
38 "matrix is negative definite");
41 double val = ldlt.vectorD().array().log().sum();
44 "log determininant of the matrix argument", val);
48 for (
int i = 0; i < m.size(); ++i)
49 operands[i] = m(i).vi_;
53 for (
int i = 0; i < m.size(); ++i)
54 gradients[i] = m_d(i);
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
The variable implementation base class.
static STAN_THREADS_DEF AutodiffStackStorage * instance_
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.
T log_determinant_spd(const Eigen::Matrix< T, R, C > &m)
Returns the log absolute determinant of the specified square matrix.