1 #ifndef STAN_MATH_PRIM_FUN_LOG2_HPP
2 #define STAN_MATH_PRIM_FUN_LOG2_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/constants.hpp>
6 #include <stan/math/prim/functor/apply_scalar_unary.hpp>
7 #include <cmath>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  * Return natural logarithm of two.
14  *
15  * @return Natural logarithm of two.
16  */
log2()17 inline double log2() { return LOG_TWO; }
18 
19 /**
20  * Structure to wrap `log2()` so it can be vectorized.
21  */
22 struct log2_fun {
23   /**
24    * Return the base two logarithm of the specified argument.
25    *
26    * @tparam T type of argument
27    * @param x argument
28    * @return base two log of the argument
29    */
30   template <typename T>
funstan::math::log2_fun31   static inline T fun(const T& x) {
32     using std::log2;
33     return log2(x);
34   }
35 };
36 
37 /**
38  * Return the elementwise application of `log2()` to
39  * specified argument container.  The return type promotes the
40  * underlying scalar argument type to double if it is an integer,
41  * and otherwise is the argument type.
42  *
43  * @tparam T type of container
44  * @param x container
45  * @return elementwise log2 of container elements
46  */
47 template <typename T, require_not_var_matrix_t<T>* = nullptr,
48           require_not_nonscalar_prim_or_rev_kernel_expression_t<T>* = nullptr>
log2(const T & x)49 inline auto log2(const T& x) {
50   return apply_scalar_unary<log2_fun, T>::apply(x);
51 }
52 
53 }  // namespace math
54 }  // namespace stan
55 
56 #endif
57