Automatic Differentiation
 
Loading...
Searching...
No Matches
inv_sqrt.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_INV_SQRT_HPP
2#define STAN_MATH_REV_FUN_INV_SQRT_HPP
3
10#include <cmath>
11
12namespace stan {
13namespace math {
14
35template <typename T, require_stan_scalar_or_eigen_t<T>* = nullptr>
36inline auto inv_sqrt(const var_value<T>& a) {
37 auto denom = to_arena(as_array_or_scalar(a.val())
38 * as_array_or_scalar(sqrt(a.val())));
39 return make_callback_var(inv_sqrt(a.val()), [a, denom](auto& vi) mutable {
40 as_array_or_scalar(a.adj()) -= 0.5 * as_array_or_scalar(vi.adj()) / denom;
41 });
42}
43
44} // namespace math
45} // namespace stan
46#endif
T as_array_or_scalar(T &&v)
Returns specified input value.
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...
arena_t< T > to_arena(const T &a)
Converts given argument into a type that either has any dynamic allocation on AD stack or schedules i...
Definition to_arena.hpp:25
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:18
fvar< T > inv_sqrt(const fvar< T > &x)
Definition inv_sqrt.hpp:14
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...