1 #ifndef STAN_MATH_PRIM_FUN_MULTIPLY_LOG_HPP
2 #define STAN_MATH_PRIM_FUN_MULTIPLY_LOG_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/fun/log.hpp>
6 #include <stan/math/prim/functor/apply_scalar_binary.hpp>
7 #include <cmath>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  * Calculate the value of the first argument
14  * times log of the second argument while behaving
15  * properly with 0 inputs.
16  *
17  * \f$ a * \log b \f$.
18  *
19    \f[
20    \mbox{multiply\_log}(x, y) =
21    \begin{cases}
22      0 & \mbox{if } x=y=0\\
23      x\ln y & \mbox{if } x, y\neq 0 \\[6pt]
24    \end{cases}
25    \f]
26 
27    \f[
28    \frac{\partial\, \mbox{multiply\_log}(x, y)}{\partial x} =
29    \begin{cases}
30      \ln y \\[6pt]
31    \end{cases}
32    \f]
33 
34    \f[
35    \frac{\partial\, \mbox{multiply\_log}(x, y)}{\partial y} =
36    \begin{cases}
37      \frac{x}{y} \\[6pt]
38    \end{cases}
39    \f]
40  *
41  * @tparam T_a type of the first variable
42  * @tparam T_b type of the second variable
43  * @param a the first variable
44  * @param b the second variable
45  * @return a * log(b)
46  */
47 template <typename T_a, typename T_b,
48           require_all_arithmetic_t<T_a, T_b>* = nullptr>
multiply_log(const T_a a,const T_b b)49 inline return_type_t<T_a, T_b> multiply_log(const T_a a, const T_b b) {
50   using std::log;
51   if (b == 0.0 && a == 0.0) {
52     return 0.0;
53   }
54 
55   return a * log(b);
56 }
57 
58 /**
59  * Enables the vectorised application of the multiply_log
60  * function, when the first and/or second arguments are containers.
61  *
62  * @tparam T1 type of first input
63  * @tparam T2 type of second input
64  * @param a First input
65  * @param b Second input
66  * @return multiply_log function applied to the two inputs.
67  */
68 template <typename T1, typename T2, require_any_container_t<T1, T2>* = nullptr,
69           require_all_not_var_matrix_t<T1, T2>* = nullptr>
multiply_log(const T1 & a,const T2 & b)70 inline auto multiply_log(const T1& a, const T2& b) {
71   return apply_scalar_binary(
72       a, b, [&](const auto& c, const auto& d) { return multiply_log(c, d); });
73 }
74 
75 }  // namespace math
76 }  // namespace stan
77 
78 #endif
79