1 #ifndef STAN_MATH_REV_FUN_LOG2_HPP 2 #define STAN_MATH_REV_FUN_LOG2_HPP 3 4 #include <stan/math/rev/meta.hpp> 5 #include <stan/math/rev/core.hpp> 6 #include <stan/math/prim/fun/log2.hpp> 7 #include <stan/math/prim/fun/constants.hpp> 8 9 namespace stan { 10 namespace math { 11 12 /** 13 * Returns the base 2 logarithm of the specified variable (C99). 14 * 15 * See log2() for the double-based version. 16 * 17 * The derivative is 18 * 19 * \f$\frac{d}{dx} \log_2 x = \frac{1}{x \log 2}\f$. 20 * 21 \f[ 22 \mbox{log2}(x) = 23 \begin{cases} 24 \textrm{NaN} & \mbox{if } x < 0 \\ 25 \log_2(x) & \mbox{if } x\geq 0 \\[6pt] 26 \textrm{NaN} & \mbox{if } x = \textrm{NaN} 27 \end{cases} 28 \f] 29 30 \f[ 31 \frac{\partial\, \mbox{log2}(x)}{\partial x} = 32 \begin{cases} 33 \textrm{NaN} & \mbox{if } x < 0 \\ 34 \frac{1}{x\ln2} & \mbox{if } x\geq 0 \\[6pt] 35 \textrm{NaN} & \mbox{if } x = \textrm{NaN} 36 \end{cases} 37 \f] 38 * 39 * @tparam T Arithmetic or a type inheriting from `EigenBase`. 40 * @param a The variable. 41 * @return Base 2 logarithm of the variable. 42 */ 43 template <typename T, require_stan_scalar_or_eigen_t<T>* = nullptr> log2(const var_value<T> & a)44inline auto log2(const var_value<T>& a) { 45 return make_callback_var(log2(a.val()), [a](auto& vi) mutable { 46 as_array_or_scalar(a.adj()) += as_array_or_scalar(vi.adj()) 47 / (LOG_TWO * as_array_or_scalar(a.val())); 48 }); 49 } 50 51 } // namespace math 52 } // namespace stan 53 #endif 54