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
8
9namespace stan {
10namespace math {
11
22template <typename T, require_rev_matrix_t<T>* = nullptr>
23inline auto trace(const T& m) {
24 arena_t<T> arena_m = m;
25
26 return make_callback_var(arena_m.val_op().trace(),
27 [arena_m](const auto& vi) mutable {
28 arena_m.adj().diagonal().array() += vi.adj();
29 });
30}
31
32} // namespace math
33} // namespace stan
34
35#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 ...