Automatic Differentiation
 
Loading...
Searching...
No Matches
norm2.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_NORM2_HPP
2#define STAN_MATH_REV_FUN_NORM2_HPP
3
9
10namespace stan {
11namespace math {
12
21template <typename T, require_eigen_vector_vt<is_var, T>* = nullptr>
22inline var norm2(const T& v) {
23 arena_t<T> arena_v = v;
24 var res = norm2(arena_v.val());
25 reverse_pass_callback([res, arena_v]() mutable {
26 arena_v.adj().array() += res.adj() * (arena_v.val().array() / res.val());
27 });
28 return res;
29}
30
38template <typename T, require_var_matrix_t<T>* = nullptr>
39inline var norm2(const T& v) {
40 return make_callback_vari(norm2(v.val()), [v](const auto& res) mutable {
41 v.adj().array() += res.adj() * (v.val().array() / res.val());
42 });
43}
44
45} // namespace math
46} // namespace stan
47#endif
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
var_value< double > var
Definition var.hpp:1187
auto norm2(const Container &x)
Compute the L2 norm of the specified vector of values.
Definition norm2.hpp:22
internal::callback_vari< plain_type_t< T >, F > * make_callback_vari(T &&value, F &&functor)
Creates a new vari with given value and a callback that implements the reverse pass (chain).
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 ...