From 69b171d25c3668d730f9ce9f4917da504931040e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Barv=C3=ADnek?= Date: Fri, 23 Oct 2026 22:17:47 +0200 Subject: [PATCH] Use lmultiply throughout and make multiply_log an alias of it lmultiply and multiply_log had equivalent implementations in prim, rev, fwd and OpenCL. Library code, the tests and the expression-test signatures now call lmultiply. multiply_log stays as an inline forwarder for code generated by stanc3; its rev, fwd and OpenCL copies and its duplicate tests are removed. Unused includes in the touched files are removed. --- doxygen/parallelism_support/opencl_support.md | 2 +- stan/math/fwd/fun.hpp | 1 - stan/math/fwd/fun/multiply_log.hpp | 30 --- .../kernel_generator/elt_function_cl.hpp | 6 - .../kernels/device_functions/multiply_log.hpp | 72 ------ .../prim/neg_binomial_2_log_glm_lpmf.hpp | 10 +- stan/math/opencl/prim/neg_binomial_2_lpmf.hpp | 6 +- stan/math/opencl/prim/poisson_lpmf.hpp | 3 +- stan/math/opencl/rev.hpp | 1 - stan/math/opencl/rev/multiply_log.hpp | 46 ---- .../offset_multiplier_constrain.hpp | 6 +- stan/math/prim/fun/grad_reg_inc_gamma.hpp | 8 +- .../fun/log_modified_bessel_first_kind.hpp | 14 +- stan/math/prim/fun/multiply_log.hpp | 78 ++---- stan/math/prim/prob/beta_lpdf.hpp | 4 - stan/math/prim/prob/binomial_lpmf.hpp | 2 +- stan/math/prim/prob/frechet_cdf.hpp | 7 - stan/math/prim/prob/frechet_lccdf.hpp | 7 - stan/math/prim/prob/frechet_lcdf.hpp | 7 - stan/math/prim/prob/frechet_lpdf.hpp | 8 - stan/math/prim/prob/gamma_cdf.hpp | 5 - stan/math/prim/prob/loglogistic_cdf.hpp | 11 +- stan/math/prim/prob/multinomial_lpmf.hpp | 4 +- .../prim/prob/neg_binomial_2_log_glm_lpmf.hpp | 9 +- stan/math/prim/prob/neg_binomial_2_lpmf.hpp | 10 +- stan/math/prim/prob/neg_binomial_lpmf.hpp | 5 - stan/math/prim/prob/poisson_binomial_cdf.hpp | 7 - .../math/prim/prob/poisson_binomial_lccdf.hpp | 6 - stan/math/prim/prob/poisson_binomial_lcdf.hpp | 7 - stan/math/prim/prob/poisson_lpmf.hpp | 8 +- stan/math/rev/fun.hpp | 1 - stan/math/rev/fun/lmultiply.hpp | 4 - stan/math/rev/fun/multiply_log.hpp | 234 ------------------ test/prob/binomial/binomial_logit_test.hpp | 4 +- test/prob/binomial/binomial_test.hpp | 4 +- test/prob/chi_square/chi_square_test.hpp | 8 +- test/prob/frechet/frechet_test.hpp | 6 +- test/prob/gamma/gamma_test.hpp | 8 +- .../inv_chi_square/inv_chi_square_test.hpp | 8 +- test/prob/inv_gamma/inv_gamma_test.hpp | 8 +- test/prob/neg_binomial/neg_binomial_test.hpp | 1 - .../neg_binomial_2_ccdf_log_test.hpp | 7 +- .../neg_binomial_2_cdf_log_test.hpp | 7 +- .../neg_binomial_2_cdf_test.hpp | 7 +- .../neg_binomial_2_log_test.hpp | 6 +- .../neg_binomial_2/neg_binomial_2_test.hpp | 6 +- test/prob/pareto/pareto_test.hpp | 7 +- test/prob/poisson/poisson_log_test.hpp | 1 - test/prob/poisson/poisson_test.hpp | 6 +- .../scaled_inv_chi_square_test.hpp | 8 +- test/prob/weibull/weibull_test.hpp | 8 +- test/sig_utils.py | 1 - test/unit/math/mix/fun/multiply_log2_test.cpp | 45 ---- test/unit/math/mix/fun/multiply_log3_test.cpp | 45 ---- .../device_functions/lmultiply_test.cpp | 2 +- .../device_functions/multiply_log_test.cpp | 53 ---- .../kernel_generator/elt_function_cl_test.cpp | 1 - .../math/opencl/rev/multiply_log_test.cpp | 53 ---- 58 files changed, 97 insertions(+), 842 deletions(-) delete mode 100644 stan/math/fwd/fun/multiply_log.hpp delete mode 100644 stan/math/opencl/kernels/device_functions/multiply_log.hpp delete mode 100644 stan/math/opencl/rev/multiply_log.hpp delete mode 100644 stan/math/rev/fun/multiply_log.hpp delete mode 100644 test/unit/math/mix/fun/multiply_log2_test.cpp delete mode 100644 test/unit/math/mix/fun/multiply_log3_test.cpp delete mode 100644 test/unit/math/opencl/device_functions/multiply_log_test.cpp delete mode 100644 test/unit/math/opencl/rev/multiply_log_test.cpp diff --git a/doxygen/parallelism_support/opencl_support.md b/doxygen/parallelism_support/opencl_support.md index 9d3adface36..ac30669f34a 100644 --- a/doxygen/parallelism_support/opencl_support.md +++ b/doxygen/parallelism_support/opencl_support.md @@ -141,6 +141,7 @@ C_sum.grad(); - inv, inv_cloglog, inv_logit, inv_sqrt, inv_square, inv_Phi - ldexp - lgamma +- lmultiply - log, log10, log1m, log1p, log2, logit - log1m_exp, log1p_exp, log1m_inv_logit, log_inv_logit - log_diff_exp, log_inv_logit_diff @@ -149,7 +150,6 @@ C_sum.grad(); - mean - mdivide_left_tri_low, mdivide_right_tri_low - multiply, operator* -- multiply_log, lmultiply - Phi, Phi_approx - pow - segment diff --git a/stan/math/fwd/fun.hpp b/stan/math/fwd/fun.hpp index ffd56a9297e..61ead3b855d 100644 --- a/stan/math/fwd/fun.hpp +++ b/stan/math/fwd/fun.hpp @@ -90,7 +90,6 @@ #include #include #include -#include #include #include #include diff --git a/stan/math/fwd/fun/multiply_log.hpp b/stan/math/fwd/fun/multiply_log.hpp deleted file mode 100644 index 3df40b33268..00000000000 --- a/stan/math/fwd/fun/multiply_log.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef STAN_MATH_FWD_FUN_MULTIPLY_LOG_HPP -#define STAN_MATH_FWD_FUN_MULTIPLY_LOG_HPP - -#include -#include -#include -#include -#include - -namespace stan { -namespace math { - -template -inline fvar multiply_log(const fvar& x1, const fvar& x2) { - return fvar(multiply_log(x1.val_, x2.val_), - x1.d_ * log(x2.val_) + x1.val_ * x2.d_ / x2.val_); -} - -template -inline fvar multiply_log(double x1, const fvar& x2) { - return fvar(multiply_log(x1, x2.val_), x1 * x2.d_ / x2.val_); -} - -template -inline fvar multiply_log(const fvar& x1, double x2) { - return fvar(multiply_log(x1.val_, x2), x1.d_ * log(x2)); -} -} // namespace math -} // namespace stan -#endif diff --git a/stan/math/opencl/kernel_generator/elt_function_cl.hpp b/stan/math/opencl/kernel_generator/elt_function_cl.hpp index df349a7b483..e691182d154 100644 --- a/stan/math/opencl/kernel_generator/elt_function_cl.hpp +++ b/stan/math/opencl/kernel_generator/elt_function_cl.hpp @@ -22,15 +22,11 @@ #include #include #include -#include #include #include #include #include -#include -#include #include -#include #include #include #include @@ -417,8 +413,6 @@ ADD_BINARY_FUNCTION_WITH_INCLUDES( ADD_BINARY_FUNCTION_WITH_INCLUDES(log_diff_exp, opencl_kernels::log1m_exp_device_function, opencl_kernels::log_diff_exp_device_function) -ADD_BINARY_FUNCTION_WITH_INCLUDES( - multiply_log, stan::math::opencl_kernels::multiply_log_device_function) ADD_BINARY_FUNCTION_WITH_INCLUDES( lmultiply, stan::math::opencl_kernels::lmultiply_device_function) diff --git a/stan/math/opencl/kernels/device_functions/multiply_log.hpp b/stan/math/opencl/kernels/device_functions/multiply_log.hpp deleted file mode 100644 index b1f5071b070..00000000000 --- a/stan/math/opencl/kernels/device_functions/multiply_log.hpp +++ /dev/null @@ -1,72 +0,0 @@ -#ifndef STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_MULTIPLY_LOG_HPP -#define STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_MULTIPLY_LOG_HPP -#ifdef STAN_OPENCL - -#include -#include - -namespace stan { -namespace math { -namespace opencl_kernels { - -// \cond -static constexpr const char* multiply_log_device_function - = "\n" - "#ifndef STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_MULTIPLY_LOG\n" - "#define " - "STAN_MATH_OPENCL_KERNELS_DEVICE_FUNCTIONS_MULTIPLY_LOG\n" STRINGIFY( - // \endcond - /** - * Calculate the value of the first argument - * times log of the second argument while behaving - * properly with 0 inputs. - * - * \f$ a * \log b \f$. - * - \f[ - \mbox{multiply\_log}(x, y) = - \begin{cases} - 0 & \mbox{if } x=y=0\\ - x\ln y & \mbox{if } x, y\neq0 \\[6pt] - \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} - \end{cases} - \f] - - \f[ - \frac{\partial\, \mbox{multiply\_log}(x, y)}{\partial x} = - \begin{cases} - \infty & \mbox{if } x=y=0\\ - \ln y & \mbox{if } x, y\neq 0 \\[6pt] - \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} - \end{cases} - \f] - - \f[ - \frac{\partial\, \mbox{multiply\_log}(x, y)}{\partial y} = - \begin{cases} - \infty & \mbox{if } x=y=0\\ - \frac{x}{y} & \mbox{if } x, y\neq 0 \\[6pt] - \textrm{NaN} & \mbox{if } x = \textrm{NaN or } y = \textrm{NaN} - \end{cases} - \f] - * - * @param a the first variable - * @param b the second variable - * @return a * log(b) - */ - double multiply_log(double a, double b) { - if (b == 0.0 && a == 0.0) { - return 0.0; - } - return a * log(b); - } - // \cond - ) "\n#endif\n"; // NOLINT -// \endcond - -} // namespace opencl_kernels -} // namespace math -} // namespace stan - -#endif -#endif diff --git a/stan/math/opencl/prim/neg_binomial_2_log_glm_lpmf.hpp b/stan/math/opencl/prim/neg_binomial_2_log_glm_lpmf.hpp index 13541b3440b..15934c620e2 100644 --- a/stan/math/opencl/prim/neg_binomial_2_log_glm_lpmf.hpp +++ b/stan/math/opencl/prim/neg_binomial_2_log_glm_lpmf.hpp @@ -6,21 +6,15 @@ #include #include #include -#include #include #include #include #include -#include #include -#include #include -#include +#include #include #include -#include -#include -#include #include namespace stan { @@ -167,7 +161,7 @@ neg_binomial_2_log_glm_lpmf(const T_y_cl& y, const T_x_cl& x, } if constexpr (include_summand::value && !is_phi_vector) { - logp += N * (multiply_log(phi_val, phi_val) - lgamma(phi_val)); + logp += N * (lmultiply(phi_val, phi_val) - lgamma(phi_val)); } if constexpr (include_summand::value && !is_y_vector && !is_phi_vector) { diff --git a/stan/math/opencl/prim/neg_binomial_2_lpmf.hpp b/stan/math/opencl/prim/neg_binomial_2_lpmf.hpp index 9dc4eb9a774..9eedce46940 100644 --- a/stan/math/opencl/prim/neg_binomial_2_lpmf.hpp +++ b/stan/math/opencl/prim/neg_binomial_2_lpmf.hpp @@ -4,13 +4,11 @@ #include #include -#include #include #include -#include #include #include -#include +#include namespace stan { namespace math { @@ -85,7 +83,7 @@ inline return_type_t neg_binomial_2_lpmf( logp1 + binomial_coefficient_log(n_plus_phi - 1, n), logp1); auto logp_expr = colwise_sum( static_select::value>( - logp2 + multiply_log(n, mu_val), logp2)); + logp2 + lmultiply(n, mu_val), logp2)); auto mu_deriv = elt_divide(n, mu_val) - elt_divide(n + phi_val, mu_plus_phi); auto log_term diff --git a/stan/math/opencl/prim/poisson_lpmf.hpp b/stan/math/opencl/prim/poisson_lpmf.hpp index 4d1644f1204..be47cf39820 100644 --- a/stan/math/opencl/prim/poisson_lpmf.hpp +++ b/stan/math/opencl/prim/poisson_lpmf.hpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -61,7 +60,7 @@ inline return_type_t poisson_lpmf(const T_n_cl& n, auto return_log_zero = colwise_max( cast(isinf(lambda_val) || (lambda_val == 0 && n != 0))); - auto logp1 = multiply_log(n, lambda_val); + auto logp1 = lmultiply(n, lambda_val); auto logp2 = static_select::value>( logp1 - lambda_val, logp1); auto logp_expr = colwise_sum(static_select::value>( diff --git a/stan/math/opencl/rev.hpp b/stan/math/opencl/rev.hpp index 4d8d8e53124..91bff1b125d 100644 --- a/stan/math/opencl/rev.hpp +++ b/stan/math/opencl/rev.hpp @@ -83,7 +83,6 @@ #include #include #include -#include #include #include #include diff --git a/stan/math/opencl/rev/multiply_log.hpp b/stan/math/opencl/rev/multiply_log.hpp deleted file mode 100644 index 535129b3e77..00000000000 --- a/stan/math/opencl/rev/multiply_log.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef STAN_MATH_OPENCL_REV_MULTIPLY_LOG_HPP -#define STAN_MATH_OPENCL_REV_MULTIPLY_LOG_HPP -#ifdef STAN_OPENCL - -#include -#include -#include -#include -#include - -namespace stan { -namespace math { - -/** - * Returns the elementwise `multiply_log()` of the input. - * - * @tparam T_a type of first expression - * @tparam T_b type of second expression - * @param a first expression - * @param b second expression - * - * @return Elementwise `multiply_log()` of the input. - */ -template * = nullptr, - require_any_var_t* = nullptr, - require_any_not_stan_scalar_t* = nullptr> -inline var_value> multiply_log(T_a&& a, T_b&& b) { - arena_t a_arena = std::forward(a); - arena_t b_arena = std::forward(b); - - return make_callback_var( - multiply_log(value_of(a_arena), value_of(b_arena)), - [a_arena, b_arena](const vari_value>& res) mutable { - adjoint_results(a_arena, b_arena) += expressions( - elt_multiply(res.adj(), log(value_of(b_arena))), - elt_multiply(res.adj(), - elt_divide(value_of(a_arena), value_of(b_arena)))); - }); -} - -} // namespace math -} // namespace stan - -#endif -#endif diff --git a/stan/math/prim/constraint/offset_multiplier_constrain.hpp b/stan/math/prim/constraint/offset_multiplier_constrain.hpp index 9e01e8303dc..85292ff9af1 100644 --- a/stan/math/prim/constraint/offset_multiplier_constrain.hpp +++ b/stan/math/prim/constraint/offset_multiplier_constrain.hpp @@ -4,13 +4,11 @@ #include #include #include -#include -#include +#include #include #include #include #include -#include #include namespace stan { @@ -111,7 +109,7 @@ inline auto offset_multiplier_constrain(const T& x, const M& mu, const S& sigma, check_positive_finite("offset_multiplier_constrain", "multiplier", value_of_rec(sigma_ref)); if (math::size(sigma_ref) == 1) { - lp += sum(multiply_log(math::size(x), sigma_ref)); + lp += sum(lmultiply(math::size(x), sigma_ref)); } else { lp += sum(log(sigma_ref)); } diff --git a/stan/math/prim/fun/grad_reg_inc_gamma.hpp b/stan/math/prim/fun/grad_reg_inc_gamma.hpp index 79f47def83e..630a4b38e5e 100644 --- a/stan/math/prim/fun/grad_reg_inc_gamma.hpp +++ b/stan/math/prim/fun/grad_reg_inc_gamma.hpp @@ -4,13 +4,11 @@ #include #include #include -#include #include #include #include #include -#include -#include +#include #include #include @@ -93,12 +91,12 @@ inline return_type_t grad_reg_inc_gamma(T1 a, T2 z, T1 g, T1 dig, TP log_s = 0.0; double s_sign = 1.0; T2 log_z = log(z); - TP log_delta = log_s - multiply_log(2, a); + TP log_delta = log_s - lmultiply(2, a); for (int k = 1; k <= max_steps; ++k) { S += s_sign >= 0.0 ? exp(log_delta) : -exp(log_delta); log_s += log_z - log(k); s_sign = -s_sign; - log_delta = log_s - multiply_log(2, k + a); + log_delta = log_s - lmultiply(2, k + a); if (is_inf(log_delta)) { throw_domain_error("grad_reg_inc_gamma", "is not converging", "", ""); } diff --git a/stan/math/prim/fun/log_modified_bessel_first_kind.hpp b/stan/math/prim/fun/log_modified_bessel_first_kind.hpp index 49a17ac36bf..edd7948e2bd 100644 --- a/stan/math/prim/fun/log_modified_bessel_first_kind.hpp +++ b/stan/math/prim/fun/log_modified_bessel_first_kind.hpp @@ -14,12 +14,10 @@ #include #include #include -#include #include #include #include -#include -#include +#include #include #include #include @@ -100,7 +98,7 @@ inline return_type_t log_modified_bessel_first_kind( 6.27767773636292611e-16, 4.34709704153272287e-18, 2.63417742690109154e-20, 1.13943037744822825e-22, 9.07926920085624812e-25}; - return log1p_exp(multiply_log(2, z) - log(4.0) + return log1p_exp(lmultiply(2, z) - log(4.0) + log(evaluate_polynomial(P, 0.25 * square(z)))); } if (z < 500) { @@ -119,7 +117,7 @@ inline return_type_t log_modified_bessel_first_kind( -4.49034849696138065e+13, 2.24155239966958995e+14, -8.13426467865659318e+14, 2.02391097391687777e+15, -3.08675715295370878e+15, 2.17587543863819074e+15}; - return z + log(evaluate_polynomial(P, inv(z))) - multiply_log(0.5, z); + return z + log(evaluate_polynomial(P, inv(z))) - lmultiply(0.5, z); } // Max error in interpolated form : 2.437e-18 // Max Error found at double precision = Poly : 1.216719e-16 @@ -127,7 +125,7 @@ inline return_type_t log_modified_bessel_first_kind( = {3.98942280401432905e-01, 4.98677850491434560e-02, 2.80506308916506102e-02, 2.92179096853915176e-02, 4.53371208762579442e-02}; - return z + log(evaluate_polynomial(P, inv(z))) - multiply_log(0.5, z); + return z + log(evaluate_polynomial(P, inv(z))) - lmultiply(0.5, z); } if (v == 1) { // WARNING: will not autodiff for v = 1 correctly // modified from Boost's bessel_i1_imp in the double precision case @@ -165,7 +163,7 @@ inline return_type_t log_modified_bessel_first_kind( 4.614040809616582764e+13, -2.298849639457172489e+14, 8.325554073334618015e+14, -2.067285045778906105e+15, 3.146401654361325073e+15, -2.213318202179221945e+15}; - return z + log(evaluate_polynomial(P, inv(z))) - multiply_log(0.5, z); + return z + log(evaluate_polynomial(P, inv(z))) - lmultiply(0.5, z); } // Max error in interpolated form: 1.320e-19 // Max Error found at double precision = Poly: 7.065357e-17 @@ -173,7 +171,7 @@ inline return_type_t log_modified_bessel_first_kind( = {3.989422804014314820e-01, -1.496033551467584157e-01, -4.675105322571775911e-02, -4.090421597376992892e-02, -5.843630344778927582e-02}; - return z + log(evaluate_polynomial(P, inv(z))) - multiply_log(0.5, z); + return z + log(evaluate_polynomial(P, inv(z))) - lmultiply(0.5, z); } if (z > 100) { // Boost does something like this in asymptotic_bessel_i_large_x diff --git a/stan/math/prim/fun/multiply_log.hpp b/stan/math/prim/fun/multiply_log.hpp index fb5010d9e27..f9c70d180b5 100644 --- a/stan/math/prim/fun/multiply_log.hpp +++ b/stan/math/prim/fun/multiply_log.hpp @@ -2,78 +2,28 @@ #define STAN_MATH_PRIM_FUN_MULTIPLY_LOG_HPP #include -#include -#include -#include +#include +#include namespace stan { namespace math { /** - * Calculate the value of the first argument - * times log of the second argument while behaving - * properly with 0 inputs. + * Return the first argument times the log of the second argument. + * This is the deprecated name of `lmultiply`, kept because code generated + * by released stanc3 versions calls it; it can be removed only in a major + * release. Library code calls `lmultiply`, which has the definition and + * the derivatives. * - * \f$ a * \log b \f$. - * - \f[ - \mbox{multiply\_log}(x, y) = - \begin{cases} - 0 & \mbox{if } x=y=0\\ - x\ln y & \mbox{if } x, y\neq 0 \\[6pt] - \end{cases} - \f] - - \f[ - \frac{\partial\, \mbox{multiply\_log}(x, y)}{\partial x} = - \begin{cases} - \ln y \\[6pt] - \end{cases} - \f] - - \f[ - \frac{\partial\, \mbox{multiply\_log}(x, y)}{\partial y} = - \begin{cases} - \frac{x}{y} \\[6pt] - \end{cases} - \f] - * - * @tparam T_a type of the first variable - * @tparam T_b type of the second variable - * @param a the first variable - * @param b the second variable - * @return a * log(b) - */ -template * = nullptr> -inline return_type_t multiply_log(const T_a a, const T_b b) { - using std::log; - if (b == 0.0 && a == 0.0) { - return 0.0; - } - - return a * log(b); -} - -/** - * Enables the vectorized application of the multiply_log - * function, when the first and/or second arguments are containers. - * - * @tparam T1 type of first input - * @tparam T2 type of second input - * @param a First input - * @param b Second input - * @return multiply_log function applied to the two inputs. + * @tparam T1 type of the first argument + * @tparam T2 type of the second argument + * @param a first argument + * @param b second argument + * @return `lmultiply(a, b)` */ -template * = nullptr, - require_all_not_var_matrix_t* = nullptr> +template inline auto multiply_log(T1&& a, T2&& b) { - return apply_scalar_binary( - [](auto&& c, auto&& d) { - return multiply_log(std::forward(c), - std::forward(d)); - }, - std::forward(a), std::forward(b)); + return lmultiply(std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/prob/beta_lpdf.hpp b/stan/math/prim/prob/beta_lpdf.hpp index 1424d7f5703..8d514a700ad 100644 --- a/stan/math/prim/prob/beta_lpdf.hpp +++ b/stan/math/prim/prob/beta_lpdf.hpp @@ -3,16 +3,12 @@ #include #include -#include #include -#include #include #include #include #include #include -#include -#include #include #include #include diff --git a/stan/math/prim/prob/binomial_lpmf.hpp b/stan/math/prim/prob/binomial_lpmf.hpp index c694df2abe4..79a0f3b054a 100644 --- a/stan/math/prim/prob/binomial_lpmf.hpp +++ b/stan/math/prim/prob/binomial_lpmf.hpp @@ -4,9 +4,9 @@ #include #include #include +#include #include #include -#include #include #include #include diff --git a/stan/math/prim/prob/frechet_cdf.hpp b/stan/math/prim/prob/frechet_cdf.hpp index e57a90f0bb0..17940d21ee6 100644 --- a/stan/math/prim/prob/frechet_cdf.hpp +++ b/stan/math/prim/prob/frechet_cdf.hpp @@ -3,19 +3,12 @@ #include #include -#include #include #include -#include #include -#include #include #include #include -#include -#include -#include -#include #include #include diff --git a/stan/math/prim/prob/frechet_lccdf.hpp b/stan/math/prim/prob/frechet_lccdf.hpp index 6226d64c92d..9083145f4a0 100644 --- a/stan/math/prim/prob/frechet_lccdf.hpp +++ b/stan/math/prim/prob/frechet_lccdf.hpp @@ -3,19 +3,12 @@ #include #include -#include #include #include #include #include -#include -#include #include #include -#include -#include -#include -#include #include #include diff --git a/stan/math/prim/prob/frechet_lcdf.hpp b/stan/math/prim/prob/frechet_lcdf.hpp index 4770deaa202..498465a64b0 100644 --- a/stan/math/prim/prob/frechet_lcdf.hpp +++ b/stan/math/prim/prob/frechet_lcdf.hpp @@ -3,17 +3,10 @@ #include #include -#include #include -#include #include -#include -#include #include #include -#include -#include -#include #include #include diff --git a/stan/math/prim/prob/frechet_lpdf.hpp b/stan/math/prim/prob/frechet_lpdf.hpp index 3dba3f3ffd9..7c997a166a0 100644 --- a/stan/math/prim/prob/frechet_lpdf.hpp +++ b/stan/math/prim/prob/frechet_lpdf.hpp @@ -3,20 +3,12 @@ #include #include -#include -#include #include -#include #include -#include #include -#include #include #include #include -#include -#include -#include #include #include diff --git a/stan/math/prim/prob/gamma_cdf.hpp b/stan/math/prim/prob/gamma_cdf.hpp index 4112a61ac39..9ab79689c4f 100644 --- a/stan/math/prim/prob/gamma_cdf.hpp +++ b/stan/math/prim/prob/gamma_cdf.hpp @@ -5,18 +5,13 @@ #include #include #include -#include #include #include #include -#include #include #include #include #include -#include -#include -#include #include #include diff --git a/stan/math/prim/prob/loglogistic_cdf.hpp b/stan/math/prim/prob/loglogistic_cdf.hpp index da7f8a950a0..da4998220e8 100644 --- a/stan/math/prim/prob/loglogistic_cdf.hpp +++ b/stan/math/prim/prob/loglogistic_cdf.hpp @@ -3,20 +3,13 @@ #include #include -#include -#include #include -#include -#include -#include -#include +#include #include #include -#include #include #include #include -#include namespace stan { namespace math { @@ -110,7 +103,7 @@ inline return_type_t loglogistic_cdf( } if constexpr (is_autodiff_v) { const auto& beta_deriv - = -multiply_log(alpha_div_y_pow_beta, alpha_div_y) * prod_all_sq; + = -lmultiply(alpha_div_y_pow_beta, alpha_div_y) * prod_all_sq; partials<2>(ops_partials) = beta_deriv * cdf_div_elt; } } diff --git a/stan/math/prim/prob/multinomial_lpmf.hpp b/stan/math/prim/prob/multinomial_lpmf.hpp index c941118f60f..8b01e932ef8 100644 --- a/stan/math/prim/prob/multinomial_lpmf.hpp +++ b/stan/math/prim/prob/multinomial_lpmf.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include #include #include #include @@ -35,7 +35,7 @@ inline return_type_t multinomial_lpmf(const std::vector& ns, } if constexpr (include_summand::value) { for (unsigned int i = 0; i < ns.size(); ++i) { - lp += multiply_log(ns[i], theta_ref.coeff(i)); + lp += lmultiply(ns[i], theta_ref.coeff(i)); } } return lp; diff --git a/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp b/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp index 52db7124971..ae93ef1e81e 100644 --- a/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp @@ -6,21 +6,18 @@ #include #include #include -#include #include #include -#include #include #include #include -#include +#include #include #include #include #include #include #include -#include #include namespace stan { @@ -169,10 +166,10 @@ neg_binomial_2_log_glm_lpmf(const T_y& y, const T_x& x, const T_alpha& alpha, if constexpr (is_vector::value) { scalar_seq_view phi_vec(phi_val_vec); for (size_t n = 0; n < N_instances; ++n) { - logp += multiply_log(phi_vec[n], phi_vec[n]) - lgamma(phi_vec[n]); + logp += lmultiply(phi_vec[n], phi_vec[n]) - lgamma(phi_vec[n]); } } else { - logp += N_instances * (multiply_log(phi_val, phi_val) - lgamma(phi_val)); + logp += N_instances * (lmultiply(phi_val, phi_val) - lgamma(phi_val)); } } logp -= sum(y_plus_phi * logsumexp_theta_logphi); diff --git a/stan/math/prim/prob/neg_binomial_2_lpmf.hpp b/stan/math/prim/prob/neg_binomial_2_lpmf.hpp index b708d4ce3a9..a7ba7273a70 100644 --- a/stan/math/prim/prob/neg_binomial_2_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_lpmf.hpp @@ -5,11 +5,7 @@ #include #include #include -#include -#include -#include -#include -#include +#include #include #include #include @@ -71,12 +67,12 @@ inline return_type_t neg_binomial_2_lpmf( if constexpr (include_precision || include_location) { if constexpr (include_precision && include_location) { logp += sum(binomial_coefficient_log(n_plus_phi - 1, n_vec) - + multiply_log(n_vec, mu_val) + logp_calc()); + + lmultiply(n_vec, mu_val) + logp_calc()); } else if constexpr (include_precision) { logp += sum(binomial_coefficient_log(n_plus_phi - 1, n_vec) + logp_calc()); } else if constexpr (include_location) { - logp += sum(multiply_log(n_vec, mu_val) + logp_calc()); + logp += sum(lmultiply(n_vec, mu_val) + logp_calc()); } } if constexpr (is_autodiff_v) { diff --git a/stan/math/prim/prob/neg_binomial_lpmf.hpp b/stan/math/prim/prob/neg_binomial_lpmf.hpp index 61c88160c6b..846e137d05d 100644 --- a/stan/math/prim/prob/neg_binomial_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_lpmf.hpp @@ -4,16 +4,11 @@ #include #include #include -#include #include -#include -#include #include -#include #include #include #include -#include #include #include diff --git a/stan/math/prim/prob/poisson_binomial_cdf.hpp b/stan/math/prim/prob/poisson_binomial_cdf.hpp index e11c9d57bd7..d520792d75a 100644 --- a/stan/math/prim/prob/poisson_binomial_cdf.hpp +++ b/stan/math/prim/prob/poisson_binomial_cdf.hpp @@ -3,16 +3,9 @@ #include #include -#include -#include -#include #include -#include -#include #include #include -#include -#include #include #include diff --git a/stan/math/prim/prob/poisson_binomial_lccdf.hpp b/stan/math/prim/prob/poisson_binomial_lccdf.hpp index aa2787951a3..d63f5d2869e 100644 --- a/stan/math/prim/prob/poisson_binomial_lccdf.hpp +++ b/stan/math/prim/prob/poisson_binomial_lccdf.hpp @@ -3,17 +3,11 @@ #include #include -#include #include -#include #include #include -#include -#include #include #include -#include -#include #include #include diff --git a/stan/math/prim/prob/poisson_binomial_lcdf.hpp b/stan/math/prim/prob/poisson_binomial_lcdf.hpp index 116c01144c1..6ae587cff95 100644 --- a/stan/math/prim/prob/poisson_binomial_lcdf.hpp +++ b/stan/math/prim/prob/poisson_binomial_lcdf.hpp @@ -3,16 +3,9 @@ #include #include -#include -#include -#include #include -#include -#include #include #include -#include -#include #include #include diff --git a/stan/math/prim/prob/poisson_lpmf.hpp b/stan/math/prim/prob/poisson_lpmf.hpp index a235c16be5a..caf4082035c 100644 --- a/stan/math/prim/prob/poisson_lpmf.hpp +++ b/stan/math/prim/prob/poisson_lpmf.hpp @@ -3,20 +3,16 @@ #include #include -#include -#include #include #include -#include #include +#include #include -#include #include #include #include #include #include -#include #include namespace stan { @@ -66,7 +62,7 @@ inline return_type_t poisson_lpmf(const T_n& n, const T_rate& lambda) { auto ops_partials = make_partials_propagator(lambda_ref); - T_partials_return logp = stan::math::sum(multiply_log(n_val, lambda_val)); + T_partials_return logp = stan::math::sum(lmultiply(n_val, lambda_val)); if constexpr (include_summand::value) { logp -= sum(lambda_val) * N / math::size(lambda); } diff --git a/stan/math/rev/fun.hpp b/stan/math/rev/fun.hpp index e5ef938a302..6ae1c14885c 100644 --- a/stan/math/rev/fun.hpp +++ b/stan/math/rev/fun.hpp @@ -126,7 +126,6 @@ #include #include #include -#include #include #include #include diff --git a/stan/math/rev/fun/lmultiply.hpp b/stan/math/rev/fun/lmultiply.hpp index 912040e7f30..17401100449 100644 --- a/stan/math/rev/fun/lmultiply.hpp +++ b/stan/math/rev/fun/lmultiply.hpp @@ -5,10 +5,6 @@ #include #include #include -#include -#include -#include -#include #include #include diff --git a/stan/math/rev/fun/multiply_log.hpp b/stan/math/rev/fun/multiply_log.hpp deleted file mode 100644 index 397d952d804..00000000000 --- a/stan/math/rev/fun/multiply_log.hpp +++ /dev/null @@ -1,234 +0,0 @@ -#ifndef STAN_MATH_REV_FUN_MULTIPLY_LOG_HPP -#define STAN_MATH_REV_FUN_MULTIPLY_LOG_HPP - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace stan { -namespace math { - -namespace internal { -class multiply_log_vv_vari : public op_vv_vari { - public: - multiply_log_vv_vari(vari* avi, vari* bvi) - : op_vv_vari(multiply_log(avi->val_, bvi->val_), avi, bvi) {} - void chain() { - using std::log; - avi_->adj_ += adj_ * log(bvi_->val_); - bvi_->adj_ += adj_ * avi_->val_ / bvi_->val_; - } -}; -class multiply_log_vd_vari : public op_vd_vari { - public: - multiply_log_vd_vari(vari* avi, double b) - : op_vd_vari(multiply_log(avi->val_, b), avi, b) {} - void chain() { - using std::log; - avi_->adj_ += adj_ * log(bd_); - } -}; -class multiply_log_dv_vari : public op_dv_vari { - public: - multiply_log_dv_vari(double a, vari* bvi) - : op_dv_vari(multiply_log(a, bvi->val_), a, bvi) {} - void chain() { bvi_->adj_ += adj_ * ad_ / bvi_->val_; } -}; -} // namespace internal - -/** - * Return the value of a*log(b). - * - * When both a and b are 0, the value returned is 0. - * The partial derivative with respect to a is log(b). - * The partial derivative with respect to b is a/b. - * - * @param a First variable. - * @param b Second variable. - * @return Value of a*log(b) - */ -inline var multiply_log(const var& a, const var& b) { - return var(new internal::multiply_log_vv_vari(a.vi_, b.vi_)); -} -/** - * Return the value of a*log(b). - * - * When both a and b are 0, the value returned is 0. - * The partial derivative with respect to a is log(b). - * - * @param a First variable. - * @param b Second scalar. - * @return Value of a*log(b) - */ -inline var multiply_log(const var& a, double b) { - return var(new internal::multiply_log_vd_vari(a.vi_, b)); -} -/** - * Return the value of a*log(b). - * - * When both a and b are 0, the value returned is 0. - * The partial derivative with respect to b is a/b. - * - * @param a First scalar. - * @param b Second variable. - * @return Value of a*log(b) - */ -inline var multiply_log(double a, const var& b) { - if (a == 1.0) { - return log(b); - } - return var(new internal::multiply_log_dv_vari(a, b.vi_)); -} - -/** - * Return the elementwise product `a * log(b)`. - * - * Both `T1` and `T2` are matrices, and one of `T1` or `T2` must be a - * `var_value` - * - * @tparam T1 Type of first argument - * @tparam T2 Type of second argument - * @param a First argument - * @param b Second argument - * @return elementwise product of `a` and `log(b)` - */ -template * = nullptr, - require_any_var_matrix_t* = nullptr> -inline auto multiply_log(const T1& a, const T2& b) { - check_matching_dims("multiply_log", "a", a, "b", b); - if constexpr (is_autodiff_v && is_autodiff_v) { - arena_t> arena_a = a; - arena_t> arena_b = b; - - return make_callback_var( - multiply_log(arena_a.val(), arena_b.val()), - [arena_a, arena_b](const auto& res) mutable { - arena_a.adj().array() - += res.adj().array() * arena_b.val().array().log(); - arena_b.adj().array() += res.adj().array() * arena_a.val().array() - / arena_b.val().array(); - }); - } else if constexpr (is_autodiff_v) { - arena_t> arena_a = a; - arena_t> arena_b = value_of(b); - - return make_callback_var(multiply_log(arena_a.val(), arena_b), - [arena_a, arena_b](const auto& res) mutable { - arena_a.adj().array() - += res.adj().array() - * arena_b.val().array().log(); - }); - } else { - arena_t> arena_a = value_of(a); - arena_t> arena_b = b; - - return make_callback_var(multiply_log(arena_a, arena_b.val()), - [arena_a, arena_b](const auto& res) mutable { - arena_b.adj().array() += res.adj().array() - * arena_a.val().array() - / arena_b.val().array(); - }); - } -} - -/** - * Return the product `a * log(b)`. - * - * @tparam T1 Type of matrix argument - * @tparam T2 Type of scalar argument - * @param a Matrix argument - * @param b Scalar argument - * @return Product of `a` and `log(b)` - */ -template * = nullptr, - require_stan_scalar_t* = nullptr> -inline auto multiply_log(const T1& a, const T2& b) { - using std::log; - - if constexpr (is_autodiff_v && is_autodiff_v) { - arena_t> arena_a = a; - var arena_b = b; - - return make_callback_var( - multiply_log(arena_a.val(), arena_b.val()), - [arena_a, arena_b](const auto& res) mutable { - arena_a.adj().array() += res.adj().array() * log(arena_b.val()); - arena_b.adj() += (res.adj().array() * arena_a.val().array()).sum() - / arena_b.val(); - }); - } else if constexpr (is_autodiff_v) { - arena_t> arena_a = a; - - return make_callback_var(multiply_log(arena_a.val(), value_of(b)), - [arena_a, b](const auto& res) mutable { - arena_a.adj().array() - += res.adj().array() * log(value_of(b)); - }); - } else { - arena_t> arena_a = value_of(a); - var arena_b = b; - - return make_callback_var( - multiply_log(arena_a, arena_b.val()), - [arena_a, arena_b](const auto& res) mutable { - arena_b.adj() - += (res.adj().array() * arena_a.array()).sum() / arena_b.val(); - }); - } -} - -/** - * Return the product `a * log(b)`. - * - * @tparam T1 Type of scalar argument - * @tparam T2 Type of matrix argument - * @param a Scalar argument - * @param b Matrix argument - * @return Product of `a` and `log(b)` - */ -template * = nullptr, - require_var_matrix_t* = nullptr> -inline auto multiply_log(const T1& a, const T2& b) { - if constexpr (is_autodiff_v && is_autodiff_v) { - var arena_a = a; - arena_t> arena_b = b; - - return make_callback_var( - multiply_log(arena_a.val(), arena_b.val()), - [arena_a, arena_b](const auto& res) mutable { - arena_a.adj() - += (res.adj().array() * arena_b.val().array().log()).sum(); - arena_b.adj().array() - += arena_a.val() * res.adj().array() / arena_b.val().array(); - }); - } else if constexpr (is_autodiff_v) { - var arena_a = a; - arena_t> arena_b = value_of(b); - - return make_callback_var( - multiply_log(arena_a.val(), arena_b), - [arena_a, arena_b](const auto& res) mutable { - arena_a.adj() - += (res.adj().array() * arena_b.val().array().log()).sum(); - }); - } else { - arena_t> arena_b = b; - - return make_callback_var(multiply_log(value_of(a), arena_b.val()), - [a, arena_b](const auto& res) mutable { - arena_b.adj().array() += value_of(a) - * res.adj().array() - / arena_b.val().array(); - }); - } -} - -} // namespace math -} // namespace stan -#endif diff --git a/test/prob/binomial/binomial_logit_test.hpp b/test/prob/binomial/binomial_logit_test.hpp index 30a3a925c85..279b9bd38e5 100644 --- a/test/prob/binomial/binomial_logit_test.hpp +++ b/test/prob/binomial/binomial_logit_test.hpp @@ -62,11 +62,11 @@ class AgradDistributionsBinomialLogit : public AgradDistributionTest { const T4&, const T5&) { using stan::math::binomial_coefficient_log; using stan::math::inv_logit; + using stan::math::lmultiply; using stan::math::log1m; - using stan::math::multiply_log; using std::log; - return binomial_coefficient_log(N, n) + multiply_log(n, inv_logit(alpha)) + return binomial_coefficient_log(N, n) + lmultiply(n, inv_logit(alpha)) + (N - n) * log1m(inv_logit(alpha)); } }; diff --git a/test/prob/binomial/binomial_test.hpp b/test/prob/binomial/binomial_test.hpp index 673dea79f77..d8a674b1b8b 100644 --- a/test/prob/binomial/binomial_test.hpp +++ b/test/prob/binomial/binomial_test.hpp @@ -63,11 +63,11 @@ class AgradDistributionsBinomial : public AgradDistributionTest { const T_prob& theta, const T3&, const T4&, const T5&) { using stan::math::binomial_coefficient_log; + using stan::math::lmultiply; using stan::math::log1m; - using stan::math::multiply_log; using std::log; - return binomial_coefficient_log(N, n) + multiply_log(n, theta) + return binomial_coefficient_log(N, n) + lmultiply(n, theta) + (N - n) * log1m(theta); } }; diff --git a/test/prob/chi_square/chi_square_test.hpp b/test/prob/chi_square/chi_square_test.hpp index 8fe5126c8d7..fb36e3aac27 100644 --- a/test/prob/chi_square/chi_square_test.hpp +++ b/test/prob/chi_square/chi_square_test.hpp @@ -1,6 +1,6 @@ // Arguments: Doubles, Doubles #include -#include +#include #include using stan::math::var; @@ -62,8 +62,8 @@ class AgradDistributionsChiSquare : public AgradDistributionTest { const T5&) { using stan::math::HALF_LOG_TWO; using stan::math::lgamma; - using stan::math::multiply_log; - return -nu * HALF_LOG_TWO - lgamma(0.5 * nu) - + multiply_log(0.5 * nu - 1.0, y) - 0.5 * y; + using stan::math::lmultiply; + return -nu * HALF_LOG_TWO - lgamma(0.5 * nu) + lmultiply(0.5 * nu - 1.0, y) + - 0.5 * y; } }; diff --git a/test/prob/frechet/frechet_test.hpp b/test/prob/frechet/frechet_test.hpp index 48dcda6f154..2bd2b5ec92d 100644 --- a/test/prob/frechet/frechet_test.hpp +++ b/test/prob/frechet/frechet_test.hpp @@ -1,7 +1,7 @@ // Arguments: Doubles, Doubles, Doubles #include #include -#include +#include #include using stan::math::var; @@ -77,12 +77,12 @@ class AgradDistributionsFrechet : public AgradDistributionTest { const T_y& y, const T_shape& alpha, const T_scale& sigma, const T3&, const T4&, const T5&) { using stan::math::include_summand; + using stan::math::lmultiply; using stan::math::log; - using stan::math::multiply_log; using stan::math::pow; using stan::math::value_of; - return log(alpha) + multiply_log(alpha, sigma) - multiply_log(alpha + 1, y) + return log(alpha) + lmultiply(alpha, sigma) - lmultiply(alpha + 1, y) - pow(sigma / y, alpha); } }; diff --git a/test/prob/gamma/gamma_test.hpp b/test/prob/gamma/gamma_test.hpp index 123b0aa30a2..8f87e7f93b2 100644 --- a/test/prob/gamma/gamma_test.hpp +++ b/test/prob/gamma/gamma_test.hpp @@ -1,7 +1,7 @@ // Arguments: Doubles, Doubles, Doubles #include #include -#include +#include using stan::math::var; using std::numeric_limits; @@ -83,9 +83,9 @@ class AgradDistributionsGamma : public AgradDistributionTest { stan::return_type_t log_prob_function( const T_y& y, const T_shape& alpha, const T_inv_scale& beta, const T3&, const T4&, const T5&) { - using stan::math::multiply_log; + using stan::math::lmultiply; - return -lgamma(alpha) + multiply_log(alpha, beta) - + multiply_log(alpha - 1.0, y) - beta * y; + return -lgamma(alpha) + lmultiply(alpha, beta) + lmultiply(alpha - 1.0, y) + - beta * y; } }; diff --git a/test/prob/inv_chi_square/inv_chi_square_test.hpp b/test/prob/inv_chi_square/inv_chi_square_test.hpp index ebabb40da1d..595c3995a3b 100644 --- a/test/prob/inv_chi_square/inv_chi_square_test.hpp +++ b/test/prob/inv_chi_square/inv_chi_square_test.hpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include using stan::math::var; using std::numeric_limits; @@ -63,8 +63,8 @@ class AgradDistributionsInvChiSquare : public AgradDistributionTest { const T5&) { using stan::math::HALF_LOG_TWO; using stan::math::lgamma; - using stan::math::multiply_log; - return -nu * HALF_LOG_TWO - lgamma(0.5 * nu) - - multiply_log(0.5 * nu + 1.0, y) - 0.5 / y; + using stan::math::lmultiply; + return -nu * HALF_LOG_TWO - lgamma(0.5 * nu) - lmultiply(0.5 * nu + 1.0, y) + - 0.5 / y; } }; diff --git a/test/prob/inv_gamma/inv_gamma_test.hpp b/test/prob/inv_gamma/inv_gamma_test.hpp index 6e48567f406..2cf8b00160a 100644 --- a/test/prob/inv_gamma/inv_gamma_test.hpp +++ b/test/prob/inv_gamma/inv_gamma_test.hpp @@ -1,7 +1,7 @@ // Arguments: Doubles, Doubles, Doubles #include #include -#include +#include using stan::math::var; using std::numeric_limits; @@ -85,9 +85,9 @@ class AgradDistributionsInvGamma : public AgradDistributionTest { return stan::math::LOG_ZERO; using stan::math::lgamma; - using stan::math::multiply_log; - return -lgamma(alpha) + multiply_log(alpha, beta) - - multiply_log(alpha + 1.0, y) - beta / y; + using stan::math::lmultiply; + return -lgamma(alpha) + lmultiply(alpha, beta) - lmultiply(alpha + 1.0, y) + - beta / y; } }; diff --git a/test/prob/neg_binomial/neg_binomial_test.hpp b/test/prob/neg_binomial/neg_binomial_test.hpp index 0871be96065..a89010ebbcd 100644 --- a/test/prob/neg_binomial/neg_binomial_test.hpp +++ b/test/prob/neg_binomial/neg_binomial_test.hpp @@ -75,7 +75,6 @@ class AgradDistributionsNegBinomial : public AgradDistributionTest { const T_n& n, const T_shape& alpha, const T_inv_scale& beta, const T3&, const T4&, const T5&) { using stan::math::binomial_coefficient_log; - using stan::math::multiply_log; using std::log; if (n != 0) diff --git a/test/prob/neg_binomial_2/neg_binomial_2_ccdf_log_test.hpp b/test/prob/neg_binomial_2/neg_binomial_2_ccdf_log_test.hpp index 35fa7499438..d46026074ae 100644 --- a/test/prob/neg_binomial_2/neg_binomial_2_ccdf_log_test.hpp +++ b/test/prob/neg_binomial_2/neg_binomial_2_ccdf_log_test.hpp @@ -1,6 +1,6 @@ // Arguments: Ints, Doubles, Doubles #include -#include +#include #include #include #include @@ -81,7 +81,7 @@ class AgradCdfLogNegBinomial2 : public AgradCcdfLogTest { const T_n& nn, const T_location& mu, const T_precision& phi, const T3&, const T4&, const T5&) { using stan::math::binomial_coefficient_log; - using stan::math::multiply_log; + using stan::math::lmultiply; using std::exp; using std::log; @@ -92,8 +92,7 @@ class AgradCdfLogNegBinomial2 : public AgradCcdfLogTest { if (n != 0) lp += binomial_coefficient_log< typename stan::scalar_type::type>(n + phi - 1.0, n); - lp += multiply_log(n, mu) + multiply_log(phi, phi) - - (n + phi) * log(mu + phi); + lp += lmultiply(n, mu) + lmultiply(phi, phi) - (n + phi) * log(mu + phi); ccdf += exp(lp); } diff --git a/test/prob/neg_binomial_2/neg_binomial_2_cdf_log_test.hpp b/test/prob/neg_binomial_2/neg_binomial_2_cdf_log_test.hpp index c012b0a91bf..8e356a07e04 100644 --- a/test/prob/neg_binomial_2/neg_binomial_2_cdf_log_test.hpp +++ b/test/prob/neg_binomial_2/neg_binomial_2_cdf_log_test.hpp @@ -1,6 +1,6 @@ // Arguments: Ints, Doubles, Doubles #include -#include +#include #include #include #include @@ -76,7 +76,7 @@ class AgradCdfLogNegBinomial2 : public AgradCdfLogTest { const T_n& nn, const T_location& mu, const T_precision& phi, const T3&, const T4&, const T5&) { using stan::math::binomial_coefficient_log; - using stan::math::multiply_log; + using stan::math::lmultiply; using std::exp; using std::log; @@ -87,8 +87,7 @@ class AgradCdfLogNegBinomial2 : public AgradCdfLogTest { if (n != 0) lp += binomial_coefficient_log< typename stan::scalar_type::type>(n + phi - 1.0, n); - lp += multiply_log(n, mu) + multiply_log(phi, phi) - - (n + phi) * log(mu + phi); + lp += lmultiply(n, mu) + lmultiply(phi, phi) - (n + phi) * log(mu + phi); cdf += exp(lp); } diff --git a/test/prob/neg_binomial_2/neg_binomial_2_cdf_test.hpp b/test/prob/neg_binomial_2/neg_binomial_2_cdf_test.hpp index cfbb54f83fa..178175e8963 100644 --- a/test/prob/neg_binomial_2/neg_binomial_2_cdf_test.hpp +++ b/test/prob/neg_binomial_2/neg_binomial_2_cdf_test.hpp @@ -1,6 +1,6 @@ // Arguments: Ints, Doubles, Doubles #include -#include +#include #include #include #include @@ -87,7 +87,7 @@ class AgradCdfNegBinomial2 : public AgradCdfTest { const T_n& nn, const T_location& mu, const T_precision& phi, const T3&, const T4&, const T5&) { using stan::math::binomial_coefficient_log; - using stan::math::multiply_log; + using stan::math::lmultiply; using std::exp; using std::log; @@ -98,8 +98,7 @@ class AgradCdfNegBinomial2 : public AgradCdfTest { if (n != 0) lp += binomial_coefficient_log< typename stan::scalar_type::type>(n + phi - 1.0, n); - lp += multiply_log(n, mu) + multiply_log(phi, phi) - - (n + phi) * log(mu + phi); + lp += lmultiply(n, mu) + lmultiply(phi, phi) - (n + phi) * log(mu + phi); cdf += exp(lp); } diff --git a/test/prob/neg_binomial_2/neg_binomial_2_log_test.hpp b/test/prob/neg_binomial_2/neg_binomial_2_log_test.hpp index 35e6367241b..0e546affcae 100644 --- a/test/prob/neg_binomial_2/neg_binomial_2_log_test.hpp +++ b/test/prob/neg_binomial_2/neg_binomial_2_log_test.hpp @@ -1,6 +1,6 @@ // Arguments: Ints, Doubles, Doubles #include -#include +#include #include #include @@ -71,15 +71,15 @@ class AgradDistributionsNegBinomial2Log : public AgradDistributionTest { const T_n& n, const T_log_location& eta, const T_inv_scale& phi, const T3&, const T4&, const T5&) { using stan::math::binomial_coefficient_log; + using stan::math::lmultiply; using stan::math::log_sum_exp; - using stan::math::multiply_log; using std::log; if (n != 0) return binomial_coefficient_log< typename stan::scalar_type::type>(n + phi - 1.0, n) - + n * eta + multiply_log(phi, phi) + + n * eta + lmultiply(phi, phi) - (n + phi) * log_sum_exp(eta, log(phi)); else return 0; diff --git a/test/prob/neg_binomial_2/neg_binomial_2_test.hpp b/test/prob/neg_binomial_2/neg_binomial_2_test.hpp index d7d16011505..41f9ee0d8a2 100644 --- a/test/prob/neg_binomial_2/neg_binomial_2_test.hpp +++ b/test/prob/neg_binomial_2/neg_binomial_2_test.hpp @@ -1,7 +1,7 @@ // Arguments: Ints, Doubles, Doubles #include #include -#include +#include #include using stan::math::var; @@ -73,14 +73,14 @@ class AgradDistributionsNegBinomial2 : public AgradDistributionTest { const T_n& n, const T_location& mu, const T_inv_scale& phi, const T3&, const T4&, const T5&) { using stan::math::binomial_coefficient_log; - using stan::math::multiply_log; + using stan::math::lmultiply; using std::log; if (n != 0) return binomial_coefficient_log< typename stan::scalar_type::type>(n + phi - 1.0, n) - + multiply_log(n, mu) + multiply_log(phi, phi) + + lmultiply(n, mu) + lmultiply(phi, phi) - (n + phi) * log(mu + phi); return log(0.0); } diff --git a/test/prob/pareto/pareto_test.hpp b/test/prob/pareto/pareto_test.hpp index bf49136d330..2147bf254dc 100644 --- a/test/prob/pareto/pareto_test.hpp +++ b/test/prob/pareto/pareto_test.hpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include using stan::math::var; using std::numeric_limits; @@ -82,13 +82,12 @@ class AgradDistributionsPareto : public AgradDistributionTest { stan::return_type_t log_prob_function( const T_y& y, const T_scale& y_min, const T_shape& alpha, const T3&, const T4&, const T5&) { + using stan::math::lmultiply; using stan::math::LOG_ZERO; - using stan::math::multiply_log; if (y < y_min) return LOG_ZERO; - return log(alpha) + multiply_log(alpha, y_min) - - multiply_log(alpha + 1.0, y); + return log(alpha) + lmultiply(alpha, y_min) - lmultiply(alpha + 1.0, y); } }; diff --git a/test/prob/poisson/poisson_log_test.hpp b/test/prob/poisson/poisson_log_test.hpp index 9e83b97fe11..2fb6cf9fccb 100644 --- a/test/prob/poisson/poisson_log_test.hpp +++ b/test/prob/poisson/poisson_log_test.hpp @@ -65,7 +65,6 @@ class AgradDistributionsPoisson : public AgradDistributionTest { const T4&, const T5&) { using stan::math::lgamma; using stan::math::LOG_ZERO; - using stan::math::multiply_log; using std::exp; if (alpha == -std::numeric_limits::infinity()) diff --git a/test/prob/poisson/poisson_test.hpp b/test/prob/poisson/poisson_test.hpp index 2b25bff9aa0..3311cc3cfd6 100644 --- a/test/prob/poisson/poisson_test.hpp +++ b/test/prob/poisson/poisson_test.hpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include using stan::math::var; using std::numeric_limits; @@ -66,14 +66,14 @@ class AgradDistributionsPoisson : public AgradDistributionTest { const T2&, const T3&, const T4&, const T5&) { using stan::math::lgamma; + using stan::math::lmultiply; using stan::math::LOG_ZERO; - using stan::math::multiply_log; if (lambda == 0) return n == 0 ? 0 : LOG_ZERO; if (stan::math::is_inf(lambda)) return LOG_ZERO; - return -lgamma(n + 1.0) + multiply_log(n, lambda) - lambda; + return -lgamma(n + 1.0) + lmultiply(n, lambda) - lambda; } }; diff --git a/test/prob/scaled_inv_chi_square/scaled_inv_chi_square_test.hpp b/test/prob/scaled_inv_chi_square/scaled_inv_chi_square_test.hpp index 7839a5d9bd8..0342bd624a9 100644 --- a/test/prob/scaled_inv_chi_square/scaled_inv_chi_square_test.hpp +++ b/test/prob/scaled_inv_chi_square/scaled_inv_chi_square_test.hpp @@ -1,6 +1,6 @@ // Arguments: Doubles, Doubles, Doubles #include -#include +#include #include #include @@ -77,14 +77,14 @@ class AgradDistributionsScaledInvChiSquare : public AgradDistributionTest { stan::return_type_t log_prob_function( const T_y& y, const T_dof& nu, const T_scale& s, const T3&, const T4&, const T5&) { - using stan::math::multiply_log; + using stan::math::lmultiply; using stan::math::square; using std::log; if (y <= 0) return stan::math::LOG_ZERO; - return multiply_log(0.5 * nu, 0.5 * nu) - lgamma(0.5 * nu) + nu * log(s) - - multiply_log(nu * 0.5 + 1.0, y) - nu * 0.5 * square(s) / y; + return lmultiply(0.5 * nu, 0.5 * nu) - lgamma(0.5 * nu) + nu * log(s) + - lmultiply(nu * 0.5 + 1.0, y) - nu * 0.5 * square(s) / y; } }; diff --git a/test/prob/weibull/weibull_test.hpp b/test/prob/weibull/weibull_test.hpp index 3deaba1b2c1..54e4660c47e 100644 --- a/test/prob/weibull/weibull_test.hpp +++ b/test/prob/weibull/weibull_test.hpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include using stan::math::var; using std::numeric_limits; @@ -76,12 +76,12 @@ class AgradDistributionsWeibull : public AgradDistributionTest { stan::return_type_t log_prob_function( const T_y& y, const T_shape& alpha, const T_scale& sigma, const T3&, const T4&, const T5&) { + using stan::math::lmultiply; using stan::math::log; - using stan::math::multiply_log; using stan::math::pow; - return log(alpha) + multiply_log(alpha - 1.0, y) - - multiply_log(alpha, sigma) - pow(y / sigma, alpha); + return log(alpha) + lmultiply(alpha - 1.0, y) - lmultiply(alpha, sigma) + - pow(y / sigma, alpha); } }; diff --git a/test/sig_utils.py b/test/sig_utils.py index 69d91b2cf13..c19073598ca 100644 --- a/test/sig_utils.py +++ b/test/sig_utils.py @@ -162,7 +162,6 @@ def get_cpp_type(stan_type): # these are all slight renames compared to stan math renames = { "lchoose": "binomial_coefficient_log", - "lmultiply": "multiply_log", "cholesky_factor_corr_constrain": "cholesky_corr_constrain", "cholesky_factor_corr_unconstrain": "cholesky_corr_free", "cholesky_factor_cov_constrain": "cholesky_factor_constrain", diff --git a/test/unit/math/mix/fun/multiply_log2_test.cpp b/test/unit/math/mix/fun/multiply_log2_test.cpp deleted file mode 100644 index a124be21bdb..00000000000 --- a/test/unit/math/mix/fun/multiply_log2_test.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include - -TEST(mathMixScalFun, multiplyLog2_vec) { - auto f = [](const auto& x1, const auto& x2) { - using stan::math::multiply_log; - return multiply_log(x1, x2); - }; - - Eigen::VectorXd in1(2); - in1 << 3, 1; - Eigen::VectorXd in2(2); - in2 << 0.5, 3.4; - - Eigen::VectorXd x1(3); - x1 << 1.0, 2.0, 3.0; - Eigen::RowVectorXd x2(3); - x2 << 1.0, 2.0, 3.0; - Eigen::MatrixXd x3(2, 3); - x3 << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0; - - stan::test::expect_ad_matvar(f, x1, x1); - stan::test::expect_ad_matvar(f, x1, 2.0); - stan::test::expect_ad_matvar(f, 3.0, x1); - stan::test::expect_ad_matvar(f, x2, x2); - stan::test::expect_ad_matvar(f, x2, 2.5); - stan::test::expect_ad_matvar(f, 3.5, x2); - stan::test::expect_ad_matvar(f, x3, x3); - stan::test::expect_ad_matvar(f, x3, 4.0); - stan::test::expect_ad_matvar(f, 5.0, x3); - - Eigen::VectorXd x4(0); - Eigen::RowVectorXd x5(0); - Eigen::MatrixXd x6(0, 0); - - stan::test::expect_ad_matvar(f, x4, x4); - stan::test::expect_ad_matvar(f, x4, 2.0); - stan::test::expect_ad_matvar(f, 3.0, x4); - stan::test::expect_ad_matvar(f, x5, x5); - stan::test::expect_ad_matvar(f, x5, 2.5); - stan::test::expect_ad_matvar(f, 3.5, x5); - stan::test::expect_ad_matvar(f, x6, x6); - stan::test::expect_ad_matvar(f, x6, 4.0); - stan::test::expect_ad_matvar(f, 5.0, x6); -} diff --git a/test/unit/math/mix/fun/multiply_log3_test.cpp b/test/unit/math/mix/fun/multiply_log3_test.cpp deleted file mode 100644 index 3e4f29b073f..00000000000 --- a/test/unit/math/mix/fun/multiply_log3_test.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include - -TEST(mathMixScalFun, multiplyLog3_vec) { - auto f = [](const auto& x1, const auto& x2) { - using stan::math::multiply_log; - return multiply_log(x1, x2); - }; - - Eigen::VectorXd in1(2); - in1 << 3, 1; - Eigen::VectorXd in2(2); - in2 << 0.5, 3.4; - - Eigen::VectorXd x1(3); - x1 << 1.0, 2.0, 3.0; - Eigen::RowVectorXd x2(3); - x2 << 1.0, 2.0, 3.0; - Eigen::MatrixXd x3(2, 3); - x3 << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0; - - stan::test::expect_ad_matvar(f, x1, x1); - stan::test::expect_ad_matvar(f, x1, 2.0); - stan::test::expect_ad_matvar(f, 3.0, x1); - stan::test::expect_ad_matvar(f, x2, x2); - stan::test::expect_ad_matvar(f, x2, 2.5); - stan::test::expect_ad_matvar(f, 3.5, x2); - stan::test::expect_ad_matvar(f, x3, x3); - stan::test::expect_ad_matvar(f, x3, 4.0); - stan::test::expect_ad_matvar(f, 5.0, x3); - - Eigen::VectorXd x4(0); - Eigen::RowVectorXd x5(0); - Eigen::MatrixXd x6(0, 0); - - stan::test::expect_ad_matvar(f, x4, x4); - stan::test::expect_ad_matvar(f, x4, 2.0); - stan::test::expect_ad_matvar(f, 3.0, x4); - stan::test::expect_ad_matvar(f, x5, x5); - stan::test::expect_ad_matvar(f, x5, 2.5); - stan::test::expect_ad_matvar(f, 3.5, x5); - stan::test::expect_ad_matvar(f, x6, x6); - stan::test::expect_ad_matvar(f, x6, 4.0); - stan::test::expect_ad_matvar(f, 5.0, x6); -} diff --git a/test/unit/math/opencl/device_functions/lmultiply_test.cpp b/test/unit/math/opencl/device_functions/lmultiply_test.cpp index 6ec9bb82d88..67a0981c6d6 100644 --- a/test/unit/math/opencl/device_functions/lmultiply_test.cpp +++ b/test/unit/math/opencl/device_functions/lmultiply_test.cpp @@ -1,6 +1,6 @@ #ifdef STAN_OPENCL #include -#include +#include #include #include #include diff --git a/test/unit/math/opencl/device_functions/multiply_log_test.cpp b/test/unit/math/opencl/device_functions/multiply_log_test.cpp deleted file mode 100644 index 43c7a21ad4f..00000000000 --- a/test/unit/math/opencl/device_functions/multiply_log_test.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#ifdef STAN_OPENCL -#include -#include -#include -#include -#include -#include - -static const std::string test_multiply_log_kernel_code - = STRINGIFY(__kernel void test(__global double *C, __global double *A, - __global double *B) { - const int i = get_global_id(0); - C[i] = multiply_log(A[i], B[i]); - }); - -const stan::math::opencl_kernels::kernel_cl< - stan::math::opencl_kernels::out_buffer, - stan::math::opencl_kernels::in_buffer, - stan::math::opencl_kernels::in_buffer> - multiply_log("test", - {stan::math::opencl_kernels::multiply_log_device_function, - test_multiply_log_kernel_code}); - -TEST(MathMatrixCL, multiply_log) { - Eigen::VectorXd a = Eigen::VectorXd::Random(1000).array() * 5; - Eigen::VectorXd b = Eigen::VectorXd::Random(1000).array().abs(); - - stan::math::matrix_cl a_cl(a); - stan::math::matrix_cl b_cl(b); - stan::math::matrix_cl res_cl(1000, 1); - multiply_log(cl::NDRange(1000), res_cl, a_cl, b_cl); - Eigen::VectorXd res = stan::math::from_matrix_cl(res_cl); - - EXPECT_NEAR_REL(res, stan::math::multiply_log(a, b)); -} - -TEST(MathMatrixCL, multiply_log_edge_cases) { - Eigen::VectorXd a(7); - a << NAN, INFINITY, 1.0E50, 0, 1, 1, 1; - - Eigen::VectorXd b(7); - b << 1, 1, 1, 0, NAN, INFINITY, 1.0E50; - - stan::math::matrix_cl a_cl(a); - stan::math::matrix_cl b_cl(b); - stan::math::matrix_cl res_cl(7, 1); - multiply_log(cl::NDRange(7), res_cl, a_cl, b_cl); - Eigen::VectorXd res = stan::math::from_matrix_cl(res_cl); - - EXPECT_NEAR_REL(res, stan::math::multiply_log(a, b)); -} - -#endif diff --git a/test/unit/math/opencl/kernel_generator/elt_function_cl_test.cpp b/test/unit/math/opencl/kernel_generator/elt_function_cl_test.cpp index f23bce2e8d7..0ed632524fc 100644 --- a/test/unit/math/opencl/kernel_generator/elt_function_cl_test.cpp +++ b/test/unit/math/opencl/kernel_generator/elt_function_cl_test.cpp @@ -266,7 +266,6 @@ TEST_BINARY_FUNCTION(fmin) TEST_BINARY_FUNCTION(fmod) TEST_BINARY_FUNCTION(lbeta) TEST_BINARY_FUNCTION(lmultiply) -TEST_BINARY_FUNCTION(multiply_log) TEST_BINARY_FUNCTION(pow) #endif diff --git a/test/unit/math/opencl/rev/multiply_log_test.cpp b/test/unit/math/opencl/rev/multiply_log_test.cpp deleted file mode 100644 index c5b9c25734f..00000000000 --- a/test/unit/math/opencl/rev/multiply_log_test.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#ifdef STAN_OPENCL -#include -#include -#include -#include - -auto multiply_log_functor = [](const auto& a, const auto& b) { - return stan::math::multiply_log(a, b); -}; - -TEST(OpenCLMatrix_multiply_log, prim_rev_values_small) { - int N = 2; - int M = 3; - - Eigen::MatrixXd a(N, M); - a << 1, 2, 3, 4, 5, 6; - Eigen::MatrixXd b(N, M); - b << 12, 11, 10, 9, 8, 7; - stan::math::test::compare_cpu_opencl_prim_rev(multiply_log_functor, a, b); -} - -TEST(OpenCLMatrixmultiply_log, prim_rev_values_M_0) { - int N = 2; - int M = 0; - - Eigen::MatrixXd a(N, M); - Eigen::MatrixXd b(N, M); - stan::math::test::compare_cpu_opencl_prim_rev(multiply_log_functor, a, b); - - Eigen::MatrixXd c(M, N); - Eigen::MatrixXd d(M, N); - stan::math::test::compare_cpu_opencl_prim_rev(multiply_log_functor, c, d); -} - -TEST(OpenCLMatrixmultiply_log, prim_rev_values_large) { - int N = 71; - int M = 83; - - Eigen::MatrixXd a = Eigen::MatrixXd::Random(N, M); - Eigen::MatrixXd b = Eigen::MatrixXd::Random(N, M); - stan::math::test::compare_cpu_opencl_prim_rev(multiply_log_functor, a, b); -} - -TEST(OpenCLMatrixmultiply_log, prim_rev_scalar_values_large) { - int N = 71; - int M = 83; - - Eigen::MatrixXd a = Eigen::MatrixXd::Random(N, M); - double b = 0.3; - stan::math::test::compare_cpu_opencl_prim_rev(multiply_log_functor, a, b); - stan::math::test::compare_cpu_opencl_prim_rev(multiply_log_functor, b, a); -} -#endif