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
10
11namespace stan {
12namespace math {
13
22template <typename Container,
23 require_eigen_vector_vt<is_var, Container>* = nullptr>
24inline var norm2(Container&& x) {
25 arena_t<Container> arena_x = std::forward<Container>(x);
26 var res = norm2(arena_x.val());
27 reverse_pass_callback([res, arena_x]() mutable {
28 arena_x.adj().array() += res.adj() * (arena_x.val().array() / res.val());
29 });
30 return res;
31}
32
41template <typename Container, require_var_matrix_t<Container>* = nullptr>
42inline var norm2(const Container& x) {
43 return make_callback_vari(norm2(x.val()), [x](const auto& res) mutable {
44 x.adj().array() += res.adj() * (x.val().array() / res.val());
45 });
46}
47
48} // namespace math
49} // namespace stan
50#endif
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
auto norm2(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 ...