1 #ifndef STAN_MATH_REV_FUN_INV_SQUARE_HPP
2 #define STAN_MATH_REV_FUN_INV_SQUARE_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
6 #include <stan/math/prim/fun/inv_square.hpp>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  *
13    \f[
14    \mbox{inv\_square}(x) =
15    \begin{cases}
16      \frac{1}{x^2} & \mbox{if } -\infty\leq x \leq \infty \\[6pt]
17      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
18    \end{cases}
19    \f]
20 
21    \f[
22    \frac{\partial\, \mbox{inv\_square}(x)}{\partial x} =
23    \begin{cases}
24      -\frac{2}{x^3} & \mbox{if } -\infty\leq x\leq \infty \\[6pt]
25      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
26    \end{cases}
27    \f]
28  *
29  */
inv_square(const var & a)30 inline var inv_square(const var& a) {
31   auto a_cube = a.val() * a.val() * a.val();
32   return make_callback_var(inv_square(a.val()), [a, a_cube](auto& vi) mutable {
33     a.adj() -= 2 * vi.adj() / a_cube;
34   });
35 }
36 
37 }  // namespace math
38 }  // namespace stan
39 #endif
40