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
10
11namespace stan {
12namespace math {
13
21template <typename T, require_rev_matrix_t<T>* = nullptr>
23 if (A.matrix().size() == 0) {
24 return 0;
25 }
26
27 var log_det = sum(log(A.ldlt().vectorD().array()));
28
29 arena_t<T> arena_A = A.matrix();
30 arena_t<Eigen::MatrixXd> arena_A_inv
31 = Eigen::MatrixXd::Identity(A.matrix().rows(), A.matrix().cols());
32
33 A.ldlt().solveInPlace(arena_A_inv);
34
35 reverse_pass_callback([arena_A, log_det, arena_A_inv]() mutable {
36 arena_A.adj() += log_det.adj() * arena_A_inv;
37 });
38
39 return log_det;
40}
41
42} // namespace math
43} // namespace stan
44#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:18
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
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 ...