Automatic Differentiation
 
Loading...
Searching...
No Matches
log_determinant_ldlt.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_LOG_DETERMINANT_LDLT_HPP
2#define STAN_MATH_REV_FUN_LOG_DETERMINANT_LDLT_HPP
3
9
10namespace stan {
11namespace math {
12
20template <typename T, require_rev_matrix_t<T>* = nullptr>
22 if (A.matrix().size() == 0) {
23 return 0;
24 }
25
26 var log_det = sum(log(A.ldlt().vectorD().array()));
27
28 arena_t<T> arena_A = A.matrix();
29 arena_t<Eigen::MatrixXd> arena_A_inv
30 = Eigen::MatrixXd::Identity(A.matrix().rows(), A.matrix().cols());
31
32 A.ldlt().solveInPlace(arena_A_inv);
33
34 reverse_pass_callback([arena_A, log_det, arena_A_inv]() mutable {
35 arena_A.adj() += log_det.adj() * arena_A_inv;
36 });
37
38 return log_det;
39}
40
41} // namespace math
42} // namespace stan
43#endif
LDLT_factor is a structure that holds a matrix of type T and the LDLT of its values.
value_type_t< T > log_determinant_ldlt(LDLT_factor< T > &A)
Returns log(abs(det(A))) given a LDLT_factor of A.
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:22
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...