Automatic Differentiation
 
Loading...
Searching...
No Matches
determinant.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_DETERMINANT_HPP
2#define STAN_MATH_REV_FUN_DETERMINANT_HPP
3
9
10namespace stan {
11namespace math {
12
13template <typename T, require_rev_matrix_t<T>* = nullptr>
14inline var determinant(const T& m) {
15 check_square("determinant", "m", m);
16
17 if (m.size() == 0) {
18 return var(1.0);
19 }
20
21 arena_t<T> arena_m = m;
22 auto m_lu = arena_m.val().partialPivLu();
23 auto arena_m_inv_transpose = to_arena(m_lu.inverse().transpose());
24 var det = m_lu.determinant();
25
26 reverse_pass_callback([arena_m, det, arena_m_inv_transpose]() mutable {
27 arena_m.adj() += (det.adj() * det.val()) * arena_m_inv_transpose;
28 });
29
30 return det;
31}
32
33} // namespace math
34} // namespace stan
35#endif
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
value_type_t< EigMat > determinant(const EigMat &m)
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
var_value< double > var
Definition var.hpp:1187
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 ...