Automatic Differentiation
 
Loading...
Searching...
No Matches
trace.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_TRACE_HPP
2#define STAN_MATH_REV_FUN_TRACE_HPP
3
9
10namespace stan {
11namespace math {
12
23template <typename T, require_rev_matrix_t<T>* = nullptr>
24inline auto trace(const T& m) {
25 arena_t<T> arena_m = m;
26
27 return make_callback_var(arena_m.val_op().trace(),
28 [arena_m](const auto& vi) mutable {
29 arena_m.adj().diagonal().array() += vi.adj();
30 });
31}
32
33} // namespace math
34} // namespace stan
35
36#endif
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
value_type_t< T > trace(const T &m)
Calculates trace (sum of diagonal) of given kernel generator expression.
Definition trace.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 ...