Automatic Differentiation
 
Loading...
Searching...
No Matches
square.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_SQUARE_HPP
2#define STAN_MATH_REV_FUN_SQUARE_HPP
3
6
7namespace stan {
8namespace math {
9
35inline var square(const var& x) {
36 return make_callback_var(square(x.val()), [x](auto& vi) mutable {
37 x.adj() += vi.adj() * 2.0 * x.val();
38 });
39}
40
48template <typename T, require_var_matrix_t<T>* = nullptr>
49inline auto square(const T& x) {
50 return make_callback_var(
51 (x.val().array().square()).matrix(), [x](const auto& vi) mutable {
52 x.adj() += (2.0 * x.val().array() * vi.adj().array()).matrix();
53 });
54}
55
56} // namespace math
57} // namespace stan
58#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...
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...