1 #ifndef STAN_MATH_REV_FUN_CBRT_HPP
2 #define STAN_MATH_REV_FUN_CBRT_HPP
3 
4 #include <stan/math/rev/meta.hpp>
5 #include <stan/math/rev/core.hpp>
6 #include <stan/math/prim/fun/cbrt.hpp>
7 
8 namespace stan {
9 namespace math {
10 
11 /**
12  * Returns the cube root of the specified variable (C99).
13  *
14  * The derivative is
15  *
16  * \f$\frac{d}{dx} x^{1/3} = \frac{1}{3 x^{2/3}}\f$.
17  *
18    \f[
19    \mbox{cbrt}(x) =
20    \begin{cases}
21      \sqrt[3]{x} & \mbox{if } -\infty\leq x \leq \infty \\[6pt]
22      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
23    \end{cases}
24    \f]
25 
26    \f[
27    \frac{\partial\, \mbox{cbrt}(x)}{\partial x} =
28    \begin{cases}
29      \frac{1}{3x^{2/3}} & \mbox{if } -\infty\leq x\leq \infty \\[6pt]
30      \textrm{NaN} & \mbox{if } x = \textrm{NaN}
31    \end{cases}
32    \f]
33  *
34  * @param a Specified variable.
35  * @return Cube root of the variable.
36  */
cbrt(const var & a)37 inline var cbrt(const var& a) {
38   return make_callback_var(cbrt(a.val()), [a](const auto& vi) mutable {
39     a.adj() += vi.adj() / (3.0 * vi.val() * vi.val());
40   });
41 }
42 
43 /**
44  * Returns the cube root of the specified variable (C99).
45  * @tparam Varmat a `var_value` with inner Eigen type
46  * @param a Specified variable.
47  * @return Cube root of the variable.
48  */
49 template <typename VarMat, require_var_matrix_t<VarMat>* = nullptr>
cbrt(const VarMat & a)50 inline auto cbrt(const VarMat& a) {
51   return make_callback_var(
52       a.val().unaryExpr([](const auto x) { return cbrt(x); }),
53       [a](const auto& vi) mutable {
54         a.adj().array() += vi.adj().array() / (3.0 * vi.val().array().square());
55       });
56 }
57 
58 }  // namespace math
59 }  // namespace stan
60 #endif
61