From 4eb5681999cb8402269f5345d3a301b29ab69006 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Fri, 13 Jun 2025 16:33:01 -0400 Subject: [PATCH 01/31] perfect forward container math overloads --- stan/math/prim/fun/Phi.hpp | 4 ++-- stan/math/prim/fun/Phi_approx.hpp | 4 ++-- stan/math/prim/fun/abs.hpp | 14 ++++++++----- stan/math/prim/fun/acos.hpp | 14 ++++++++----- stan/math/prim/fun/acosh.hpp | 8 +++---- stan/math/prim/fun/asin.hpp | 14 ++++++++----- stan/math/prim/fun/asinh.hpp | 8 +++---- stan/math/prim/fun/atan.hpp | 14 ++++++++----- stan/math/prim/fun/atan2.hpp | 8 +++++-- stan/math/prim/fun/atanh.hpp | 8 +++---- stan/math/prim/fun/bessel_first_kind.hpp | 9 +++++--- stan/math/prim/fun/bessel_second_kind.hpp | 9 +++++--- stan/math/prim/fun/beta.hpp | 8 +++++-- stan/math/prim/fun/binary_log_loss.hpp | 8 +++++-- .../prim/fun/binomial_coefficient_log.hpp | 9 ++++---- stan/math/prim/fun/cbrt.hpp | 4 ++-- stan/math/prim/fun/ceil.hpp | 14 ++++++++----- stan/math/prim/fun/choose.hpp | 8 +++++-- stan/math/prim/fun/cos.hpp | 14 ++++++++----- stan/math/prim/fun/cosh.hpp | 11 +++++----- stan/math/prim/fun/digamma.hpp | 4 ++-- stan/math/prim/fun/erf.hpp | 4 ++-- stan/math/prim/fun/erfc.hpp | 4 ++-- stan/math/prim/fun/exp.hpp | 12 ++++++----- stan/math/prim/fun/exp2.hpp | 4 ++-- stan/math/prim/fun/expm1.hpp | 4 ++-- stan/math/prim/fun/fabs.hpp | 11 +++++----- stan/math/prim/fun/falling_factorial.hpp | 9 +++++--- stan/math/prim/fun/fdim.hpp | 8 +++++-- stan/math/prim/fun/floor.hpp | 11 +++++----- stan/math/prim/fun/fmax.hpp | 8 +++++-- stan/math/prim/fun/fmin.hpp | 8 +++++-- stan/math/prim/fun/fmod.hpp | 9 ++++---- stan/math/prim/fun/gamma_p.hpp | 7 +++++-- stan/math/prim/fun/gamma_q.hpp | 8 +++++-- stan/math/prim/fun/hypot.hpp | 8 +++++-- stan/math/prim/fun/inv.hpp | 11 +++++----- stan/math/prim/fun/inv_Phi.hpp | 4 ++-- stan/math/prim/fun/inv_cloglog.hpp | 13 +++++++----- stan/math/prim/fun/inv_erfc.hpp | 4 ++-- stan/math/prim/fun/inv_sqrt.hpp | 14 +++++++------ stan/math/prim/fun/inv_square.hpp | 9 +++++--- stan/math/prim/fun/lambert_w.hpp | 8 +++---- stan/math/prim/fun/lbeta.hpp | 8 +++++-- stan/math/prim/fun/ldexp.hpp | 8 +++++-- stan/math/prim/fun/lgamma.hpp | 4 ++-- stan/math/prim/fun/lmgamma.hpp | 8 +++++-- stan/math/prim/fun/lmultiply.hpp | 8 +++++-- stan/math/prim/fun/log.hpp | 12 ++++++----- stan/math/prim/fun/log10.hpp | 11 +++++----- stan/math/prim/fun/log1m.hpp | 4 ++-- stan/math/prim/fun/log1m_exp.hpp | 4 ++-- stan/math/prim/fun/log1m_inv_logit.hpp | 7 ++++--- stan/math/prim/fun/log1p.hpp | 4 ++-- stan/math/prim/fun/log1p_exp.hpp | 4 ++-- stan/math/prim/fun/log2.hpp | 4 ++-- stan/math/prim/fun/log_diff_exp.hpp | 8 +++++-- stan/math/prim/fun/log_falling_factorial.hpp | 9 +++++--- stan/math/prim/fun/log_inv_logit.hpp | 4 ++-- stan/math/prim/fun/log_inv_logit_diff.hpp | 9 +++++--- .../fun/log_modified_bessel_first_kind.hpp | 9 ++++---- stan/math/prim/fun/log_rising_factorial.hpp | 9 +++++--- stan/math/prim/fun/log_softmax.hpp | 13 +++++++----- stan/math/prim/fun/log_sum_exp.hpp | 13 ++++++++---- stan/math/prim/fun/logit.hpp | 21 ++++++++++++------- stan/math/prim/fun/minus.hpp | 5 +++-- .../prim/fun/modified_bessel_first_kind.hpp | 9 ++++---- .../prim/fun/modified_bessel_second_kind.hpp | 9 ++++---- stan/math/prim/fun/multiply_log.hpp | 8 +++++-- stan/math/prim/fun/norm1.hpp | 7 ++++--- stan/math/prim/fun/norm2.hpp | 7 ++++--- stan/math/prim/fun/owens_t.hpp | 8 +++++-- stan/math/prim/fun/pow.hpp | 5 +++-- stan/math/prim/fun/rising_factorial.hpp | 9 +++++--- stan/math/prim/fun/round.hpp | 11 +++++----- stan/math/prim/fun/sd.hpp | 5 +++-- stan/math/prim/fun/select.hpp | 20 +++++++++--------- stan/math/prim/fun/sign.hpp | 4 ++-- stan/math/prim/fun/sin.hpp | 12 ++++++----- stan/math/prim/fun/sinh.hpp | 11 +++++----- stan/math/prim/fun/sqrt.hpp | 11 +++++----- stan/math/prim/fun/square.hpp | 11 +++++----- stan/math/prim/fun/step.hpp | 4 ++-- stan/math/prim/fun/tan.hpp | 14 ++++++++----- stan/math/prim/fun/tanh.hpp | 11 +++++----- stan/math/prim/fun/tgamma.hpp | 4 ++-- stan/math/prim/fun/to_complex.hpp | 9 +++++--- stan/math/prim/fun/to_int.hpp | 4 ++-- stan/math/prim/fun/trigamma.hpp | 4 ++-- stan/math/prim/fun/trunc.hpp | 4 ++-- 90 files changed, 463 insertions(+), 296 deletions(-) diff --git a/stan/math/prim/fun/Phi.hpp b/stan/math/prim/fun/Phi.hpp index 152ab151bcf..6c21789468c 100644 --- a/stan/math/prim/fun/Phi.hpp +++ b/stan/math/prim/fun/Phi.hpp @@ -67,8 +67,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto Phi(const T& x) { - return apply_scalar_unary::apply(x); +inline auto Phi(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/Phi_approx.hpp b/stan/math/prim/fun/Phi_approx.hpp index 555a06a2c41..a806c765153 100644 --- a/stan/math/prim/fun/Phi_approx.hpp +++ b/stan/math/prim/fun/Phi_approx.hpp @@ -66,8 +66,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto Phi_approx(const T& x) { - return apply_scalar_unary::apply(x); +inline auto Phi_approx(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/abs.hpp b/stan/math/prim/fun/abs.hpp index fca073133ec..7ef05d9d23f 100644 --- a/stan/math/prim/fun/abs.hpp +++ b/stan/math/prim/fun/abs.hpp @@ -63,8 +63,9 @@ struct abs_fun { * @return Absolute value of each variable in the container. */ template * = nullptr> -inline auto abs(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto abs(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -77,9 +78,12 @@ inline auto abs(const Container& x) { */ template * = nullptr> -inline auto abs(const Container& x) { - return apply_vector_unary::apply( - x, [&](const auto& v) { return v.array().abs(); }); +inline auto abs(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [&](const auto& v) { + return std::forward(v).array().abs(); + }); } namespace internal { diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index 1efa76b5174..8d35f36c994 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -65,8 +65,9 @@ struct acos_fun { * @return Arc cosine of each variable in the container, in radians. */ template * = nullptr> -inline auto acos(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto acos(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -79,9 +80,12 @@ inline auto acos(const Container& x) { */ template * = nullptr> -inline auto acos(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().acos(); }); +inline auto acos(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { + return std::forward(v).array().acos(); + }); } namespace internal { diff --git a/stan/math/prim/fun/acosh.hpp b/stan/math/prim/fun/acosh.hpp index 5580e9e1e73..c3821b69fca 100644 --- a/stan/math/prim/fun/acosh.hpp +++ b/stan/math/prim/fun/acosh.hpp @@ -78,8 +78,8 @@ struct acosh_fun { * @return Elementwise acosh of members of container. */ template * = nullptr> -inline auto acosh(const T& x) { - return apply_scalar_unary::apply(x); +inline auto acosh(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -94,8 +94,8 @@ inline auto acosh(const T& x) { */ template * = nullptr> -inline auto acosh(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto acosh(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } namespace internal { diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index f620a24d2af..287cddf467b 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -63,8 +63,9 @@ struct asin_fun { * @return Arcsine of each variable in the container, in radians. */ template * = nullptr> -inline auto asin(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto asin(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -77,9 +78,12 @@ inline auto asin(const Container& x) { */ template * = nullptr> -inline auto asin(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().asin(); }); +inline auto asin(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { + return std::forward(v).array().asin(); + }); } namespace internal { diff --git a/stan/math/prim/fun/asinh.hpp b/stan/math/prim/fun/asinh.hpp index ab72065efae..63a53caf255 100644 --- a/stan/math/prim/fun/asinh.hpp +++ b/stan/math/prim/fun/asinh.hpp @@ -66,8 +66,8 @@ struct asinh_fun { * @return Inverse hyperbolic sine of each value in the container. */ template * = nullptr> -inline auto asinh(const T& x) { - return apply_scalar_unary::apply(x); +inline auto asinh(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -80,8 +80,8 @@ inline auto asinh(const T& x) { */ template * = nullptr> -inline auto asinh(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto asinh(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } namespace internal { diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 9e7099b7d43..ac505f0cd2b 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -61,8 +61,9 @@ struct atan_fun { * @return Arctan of each value in x, in radians. */ template * = nullptr> -inline auto atan(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto atan(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -75,9 +76,12 @@ inline auto atan(const Container& x) { */ template * = nullptr> -inline auto atan(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().atan(); }); +inline auto atan(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { + return std::forward(v).array().atan(); + }); } namespace internal { diff --git a/stan/math/prim/fun/atan2.hpp b/stan/math/prim/fun/atan2.hpp index 7909afdf0d4..060c99ea314 100644 --- a/stan/math/prim/fun/atan2.hpp +++ b/stan/math/prim/fun/atan2.hpp @@ -36,9 +36,13 @@ double atan2(T1 y, T2 x) { */ template * = nullptr, require_all_not_var_matrix_t* = nullptr> -inline auto atan2(const T1& a, const T2& b) { +inline auto atan2(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return atan2(c, d); }, a, b); + [](auto&& c, auto&& d) { + return atan2(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/atanh.hpp b/stan/math/prim/fun/atanh.hpp index fdf6c4df9b5..4d1802b1ab6 100644 --- a/stan/math/prim/fun/atanh.hpp +++ b/stan/math/prim/fun/atanh.hpp @@ -77,8 +77,8 @@ struct atanh_fun { * @return Elementwise atanh of members of container. */ template * = nullptr> -inline auto atanh(const T& x) { - return apply_scalar_unary::apply(x); +inline auto atanh(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -93,8 +93,8 @@ inline auto atanh(const T& x) { */ template * = nullptr> -inline auto atanh(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto atanh(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } namespace internal { diff --git a/stan/math/prim/fun/bessel_first_kind.hpp b/stan/math/prim/fun/bessel_first_kind.hpp index e28cd8e3e0c..ba76708d233 100644 --- a/stan/math/prim/fun/bessel_first_kind.hpp +++ b/stan/math/prim/fun/bessel_first_kind.hpp @@ -53,10 +53,13 @@ inline T2 bessel_first_kind(int v, const T2 z) { */ template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto bessel_first_kind(const T1& a, const T2& b) { +inline auto bessel_first_kind(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return bessel_first_kind(c, d); }, a, - b); + [](auto&& c, auto&& d) { + return bessel_first_kind(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/bessel_second_kind.hpp b/stan/math/prim/fun/bessel_second_kind.hpp index e7a711c24e4..377badca7f8 100644 --- a/stan/math/prim/fun/bessel_second_kind.hpp +++ b/stan/math/prim/fun/bessel_second_kind.hpp @@ -53,10 +53,13 @@ inline T2 bessel_second_kind(int v, const T2 z) { * @return Bessel second kind function applied to the two inputs. */ template * = nullptr> -inline auto bessel_second_kind(const T1& a, const T2& b) { +inline auto bessel_second_kind(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return bessel_second_kind(c, d); }, a, - b); + [](auto&& c, auto&& d) { + return bessel_second_kind(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/beta.hpp b/stan/math/prim/fun/beta.hpp index 969ce5e0efc..58512cb631d 100644 --- a/stan/math/prim/fun/beta.hpp +++ b/stan/math/prim/fun/beta.hpp @@ -67,9 +67,13 @@ inline return_type_t beta(const T1 a, const T2 b) { */ template * = nullptr, require_all_not_var_matrix_t* = nullptr> -inline auto beta(const T1& a, const T2& b) { +inline auto beta(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return beta(c, d); }, a, b); + [](auto&& c, auto&& d) { + return beta(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/binary_log_loss.hpp b/stan/math/prim/fun/binary_log_loss.hpp index 2fba8a37b8d..ba097e56574 100644 --- a/stan/math/prim/fun/binary_log_loss.hpp +++ b/stan/math/prim/fun/binary_log_loss.hpp @@ -44,9 +44,13 @@ inline T binary_log_loss(int y, const T& y_hat) { */ template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto binary_log_loss(const T1& a, const T2& b) { +inline auto binary_log_loss(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return binary_log_loss(c, d); }, a, b); + [](auto&& c, auto&& d) { + return binary_log_loss(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/binomial_coefficient_log.hpp b/stan/math/prim/fun/binomial_coefficient_log.hpp index 986807a55f8..f9c70facedc 100644 --- a/stan/math/prim/fun/binomial_coefficient_log.hpp +++ b/stan/math/prim/fun/binomial_coefficient_log.hpp @@ -161,12 +161,13 @@ inline return_type_t binomial_coefficient_log(const T_n n, * @return Binomial coefficient log function applied to the two inputs. */ template * = nullptr> -inline auto binomial_coefficient_log(const T1& a, const T2& b) { +inline auto binomial_coefficient_log(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { - return binomial_coefficient_log(c, d); + [](auto&& c, auto&& d) { + return binomial_coefficient_log(std::forward(c), + std::forward(d)); }, - a, b); + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/cbrt.hpp b/stan/math/prim/fun/cbrt.hpp index 090105f25aa..0a5d1ae3a71 100644 --- a/stan/math/prim/fun/cbrt.hpp +++ b/stan/math/prim/fun/cbrt.hpp @@ -34,8 +34,8 @@ struct cbrt_fun { template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline auto cbrt(const T& x) { - return apply_scalar_unary::apply(x); +inline auto cbrt(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index c257837250e..fc25d99955a 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -37,8 +37,9 @@ template * = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t< Container>* = nullptr> -inline auto ceil(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto ceil(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -52,9 +53,12 @@ inline auto ceil(const Container& x) { template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto ceil(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().ceil(); }); +inline auto ceil(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { + return std::forward(v).array().ceil(); + }); } } // namespace math diff --git a/stan/math/prim/fun/choose.hpp b/stan/math/prim/fun/choose.hpp index 92528cc676b..05ac3911b13 100644 --- a/stan/math/prim/fun/choose.hpp +++ b/stan/math/prim/fun/choose.hpp @@ -49,9 +49,13 @@ inline int choose(int n, int k) { * @return Binomial coefficient function applied to the two inputs. */ template * = nullptr> -inline auto choose(const T1& a, const T2& b) { +inline auto choose(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return choose(c, d); }, a, b); + [](auto&& c, auto&& d) { + return choose(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index 0a3b044ccdc..5f111a9c7b0 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -60,8 +60,9 @@ struct cos_fun { * @return Cosine of each value in x. */ template * = nullptr> -inline auto cos(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto cos(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -74,9 +75,12 @@ inline auto cos(const Container& x) { */ template * = nullptr> -inline auto cos(const Container& x) { - return apply_vector_unary::apply( - x, [&](const auto& v) { return v.array().cos(); }); +inline auto cos(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [&](const auto& v) { + return std::forward(v).array().cos(); + }); } namespace internal { diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index ae60c233a68..96734fe39d3 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -59,8 +59,8 @@ struct cosh_fun { * @return Hyberbolic cosine of x. */ template * = nullptr> -inline auto cosh(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto cosh(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -73,9 +73,10 @@ inline auto cosh(const Container& x) { */ template * = nullptr> -inline auto cosh(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().cosh(); }); +inline auto cosh(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().cosh(); }); } namespace internal { diff --git a/stan/math/prim/fun/digamma.hpp b/stan/math/prim/fun/digamma.hpp index 05032782e8c..71ccd014b4d 100644 --- a/stan/math/prim/fun/digamma.hpp +++ b/stan/math/prim/fun/digamma.hpp @@ -74,8 +74,8 @@ struct digamma_fun { template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto digamma(const T& x) { - return apply_scalar_unary::apply(x); +inline auto digamma(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/erf.hpp b/stan/math/prim/fun/erf.hpp index 9f3526bcafa..683205ded17 100644 --- a/stan/math/prim/fun/erf.hpp +++ b/stan/math/prim/fun/erf.hpp @@ -35,8 +35,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto erf(const T& x) { - return apply_scalar_unary::apply(x); +inline auto erf(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/erfc.hpp b/stan/math/prim/fun/erfc.hpp index a9599b42b2c..92f3c904cf4 100644 --- a/stan/math/prim/fun/erfc.hpp +++ b/stan/math/prim/fun/erfc.hpp @@ -36,8 +36,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto erfc(const T& x) { - return apply_scalar_unary::apply(x); +inline auto erfc(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index d12d82cb556..1524bfca246 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -68,8 +68,9 @@ struct exp_fun { * @return Elementwise application of exponentiation to the argument. */ template * = nullptr> -inline auto exp(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto exp(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -82,9 +83,10 @@ inline auto exp(const Container& x) { */ template * = nullptr> -inline auto exp(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().exp(); }); +inline auto exp(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().exp(); }); } namespace internal { diff --git a/stan/math/prim/fun/exp2.hpp b/stan/math/prim/fun/exp2.hpp index f5f491cb926..6bea1ff00fb 100644 --- a/stan/math/prim/fun/exp2.hpp +++ b/stan/math/prim/fun/exp2.hpp @@ -39,8 +39,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto exp2(const T& x) { - return apply_scalar_unary::apply(x); +inline auto exp2(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/expm1.hpp b/stan/math/prim/fun/expm1.hpp index 62d6cab97f7..da737a6539e 100644 --- a/stan/math/prim/fun/expm1.hpp +++ b/stan/math/prim/fun/expm1.hpp @@ -36,8 +36,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto expm1(const T& x) { - return apply_scalar_unary::apply(x); +inline auto expm1(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 71a1d521656..4b9e5651e4f 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -48,8 +48,8 @@ template * = nullptr, require_not_stan_scalar_t* = nullptr> -inline auto fabs(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto fabs(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -62,9 +62,10 @@ inline auto fabs(const Container& x) { */ template * = nullptr> -inline auto fabs(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().abs(); }); +inline auto fabs(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().abs(); }); } } // namespace math diff --git a/stan/math/prim/fun/falling_factorial.hpp b/stan/math/prim/fun/falling_factorial.hpp index 0a79f0c4a7c..4040a53150a 100644 --- a/stan/math/prim/fun/falling_factorial.hpp +++ b/stan/math/prim/fun/falling_factorial.hpp @@ -80,10 +80,13 @@ inline return_type_t falling_factorial(const T& x, int n) { */ template * = nullptr, require_all_not_var_matrix_t* = nullptr> -inline auto falling_factorial(const T1& a, const T2& b) { +inline auto falling_factorial(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return falling_factorial(c, d); }, a, - b); + [](auto&& c, auto&& d) { + return falling_factorial(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/fdim.hpp b/stan/math/prim/fun/fdim.hpp index 4ed2d8231c7..4e602bfcc7e 100644 --- a/stan/math/prim/fun/fdim.hpp +++ b/stan/math/prim/fun/fdim.hpp @@ -35,9 +35,13 @@ inline double fdim(T1 x, T2 y) { * @return Fdim function applied to the two inputs. */ template * = nullptr> -inline auto fdim(const T1& a, const T2& b) { +inline auto fdim(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return fdim(c, d); }, a, b); + [](auto&& c, auto&& d) { + return fdim(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index 3845bee34b2..8f5b1a952c2 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -42,8 +42,8 @@ struct floor_fun { * @return Greatest integer <= each value in x. */ template * = nullptr> -inline auto floor(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto floor(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -57,9 +57,10 @@ inline auto floor(const Container& x) { template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto floor(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().floor(); }); +inline auto floor(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().floor(); }); } } // namespace math diff --git a/stan/math/prim/fun/fmax.hpp b/stan/math/prim/fun/fmax.hpp index 43790df1272..620a7f5f4f7 100644 --- a/stan/math/prim/fun/fmax.hpp +++ b/stan/math/prim/fun/fmax.hpp @@ -33,9 +33,13 @@ inline double fmax(T1 x, T2 y) { * @return fmax function applied to the two inputs. */ template * = nullptr> -inline auto fmax(const T1& a, const T2& b) { +inline auto fmax(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return fmax(c, d); }, a, b); + [](auto&& c, auto&& d) { + return fmax(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/fmin.hpp b/stan/math/prim/fun/fmin.hpp index c7011c00a2c..7bf25f93be9 100644 --- a/stan/math/prim/fun/fmin.hpp +++ b/stan/math/prim/fun/fmin.hpp @@ -33,9 +33,13 @@ inline double fmin(T1 x, T2 y) { * @return fmin function applied to the two inputs. */ template * = nullptr> -inline auto fmin(const T1& a, const T2& b) { +inline auto fmin(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return fmin(c, d); }, a, b); + [](auto&& c, auto&& d) { + return fmin(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/fmod.hpp b/stan/math/prim/fun/fmod.hpp index 53c575c12d8..26f916df7b5 100644 --- a/stan/math/prim/fun/fmod.hpp +++ b/stan/math/prim/fun/fmod.hpp @@ -34,13 +34,14 @@ inline double fmod(const T1& a, const T2& b) { * @return fmod function applied to the two inputs. */ template * = nullptr> -inline auto fmod(const T1& a, const T2& b) { +inline auto fmod(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { + [](auto&& c, auto&& d) { using std::fmod; - return fmod(c, d); + return fmod(std::forward(c), + std::forward(d)); }, - a, b); + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/gamma_p.hpp b/stan/math/prim/fun/gamma_p.hpp index de21249ca37..512d3c48526 100644 --- a/stan/math/prim/fun/gamma_p.hpp +++ b/stan/math/prim/fun/gamma_p.hpp @@ -87,9 +87,12 @@ inline double gamma_p(double z, double a) { * @return gamma_p function applied to the two inputs. */ template * = nullptr> -inline auto gamma_p(const T1& a, const T2& b) { +inline auto gamma_p(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return gamma_p(c, d); }, a, b); + [](auto&& c, auto&& d) { + return gamma_p(std::forward(c), std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/gamma_q.hpp b/stan/math/prim/fun/gamma_q.hpp index 2b4e2494305..17b9ceb92da 100644 --- a/stan/math/prim/fun/gamma_q.hpp +++ b/stan/math/prim/fun/gamma_q.hpp @@ -65,9 +65,13 @@ inline double gamma_q(double x, double a) { return boost::math::gamma_q(x, a); } * @return gamma_q function applied to the two inputs. */ template * = nullptr> -inline auto gamma_q(const T1& a, const T2& b) { +inline auto gamma_q(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return gamma_q(c, d); }, a, b); + [](auto&& c, auto&& d) { + return gamma_q(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/hypot.hpp b/stan/math/prim/fun/hypot.hpp index 07fe51d1857..c074b730f17 100644 --- a/stan/math/prim/fun/hypot.hpp +++ b/stan/math/prim/fun/hypot.hpp @@ -39,9 +39,13 @@ inline double hypot(T1 x, T2 y) { template * = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t< T1, T2>* = nullptr> -inline auto hypot(const T1& a, const T2& b) { +inline auto hypot(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return hypot(c, d); }, a, b); + [](auto&& c, auto&& d) { + return hypot(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index fe09a75e6df..c798ea20b1c 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -34,8 +34,8 @@ struct inv_fun { template < typename T, require_not_container_st* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline auto inv(const T& x) { - return apply_scalar_unary::apply(x); +inline auto inv(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -50,9 +50,10 @@ template * = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t< Container>* = nullptr> -inline auto inv(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().inverse(); }); +inline auto inv(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().inverse(); }); } } // namespace math diff --git a/stan/math/prim/fun/inv_Phi.hpp b/stan/math/prim/fun/inv_Phi.hpp index 0ec72de8062..15b11207edc 100644 --- a/stan/math/prim/fun/inv_Phi.hpp +++ b/stan/math/prim/fun/inv_Phi.hpp @@ -177,8 +177,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto inv_Phi(const T& x) { - return apply_scalar_unary::apply(x); +inline auto inv_Phi(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index f197d49c24e..05d057f0386 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -90,8 +90,8 @@ struct inv_cloglog_fun { * @return 1 - exp(-exp()) applied to each value in x. */ template * = nullptr> -inline auto inv_cloglog(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto inv_cloglog(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -104,9 +104,12 @@ inline auto inv_cloglog(const Container& x) { */ template * = nullptr> -inline auto inv_cloglog(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return 1 - (-v.array().exp()).exp(); }); +inline auto inv_cloglog(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { + return 1 - (-std::forward(v).array().exp()).exp(); + }); } } // namespace math diff --git a/stan/math/prim/fun/inv_erfc.hpp b/stan/math/prim/fun/inv_erfc.hpp index 4eca9e1b8cd..1afba170442 100644 --- a/stan/math/prim/fun/inv_erfc.hpp +++ b/stan/math/prim/fun/inv_erfc.hpp @@ -48,8 +48,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr, require_not_arithmetic_t* = nullptr> -inline auto inv_erfc(const T& x) { - return apply_scalar_unary::apply(x); +inline auto inv_erfc(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index 00fa616a45f..80fc7431880 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -45,8 +45,8 @@ struct inv_sqrt_fun { * @return inverse square root of each value in x. */ template * = nullptr> -inline auto inv_sqrt(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto inv_sqrt(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -59,14 +59,16 @@ inline auto inv_sqrt(const Container& x) { */ template * = nullptr, require_container_bt* = nullptr> -inline auto inv_sqrt(const Container& x) { +inline auto inv_sqrt(Container&& x) { // Eigen 3.4.0 has precision issues on ARM64 with vectorised rsqrt // Resolved in current master branch, below can be removed on next release #ifdef __aarch64__ - return apply_scalar_unary::apply(x); + return apply_scalar_unary>::apply( + std::forward(x)); #else - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().rsqrt(); }); + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().rsqrt(); }); #endif } diff --git a/stan/math/prim/fun/inv_square.hpp b/stan/math/prim/fun/inv_square.hpp index 0793e054c3d..074afbde47d 100644 --- a/stan/math/prim/fun/inv_square.hpp +++ b/stan/math/prim/fun/inv_square.hpp @@ -34,9 +34,12 @@ inline auto inv_square(const Container& x) { */ template * = nullptr> -inline auto inv_square(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().square().inverse(); }); +inline auto inv_square(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { + return std::forward(v).array().square().inverse(); + }); } } // namespace math diff --git a/stan/math/prim/fun/lambert_w.hpp b/stan/math/prim/fun/lambert_w.hpp index 2af6cf9e55f..9fedeee69d2 100644 --- a/stan/math/prim/fun/lambert_w.hpp +++ b/stan/math/prim/fun/lambert_w.hpp @@ -80,8 +80,8 @@ struct lambert_wm1_fun { */ template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto lambert_w0(const T& x) { - return apply_scalar_unary::apply(x); +inline auto lambert_w0(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -95,8 +95,8 @@ inline auto lambert_w0(const T& x) { */ template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto lambert_wm1(const T& x) { - return apply_scalar_unary::apply(x); +inline auto lambert_wm1(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/lbeta.hpp b/stan/math/prim/fun/lbeta.hpp index 82b4a507bea..5716c290b8c 100644 --- a/stan/math/prim/fun/lbeta.hpp +++ b/stan/math/prim/fun/lbeta.hpp @@ -127,9 +127,13 @@ return_type_t lbeta(const T1 a, const T2 b) { * @return lbeta function applied to the two inputs. */ template * = nullptr> -inline auto lbeta(const T1& a, const T2& b) { +inline auto lbeta(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return lbeta(c, d); }, a, b); + [](auto&& c, auto&& d) { + return lbeta(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/ldexp.hpp b/stan/math/prim/fun/ldexp.hpp index 01f6856032a..4430559ec5b 100644 --- a/stan/math/prim/fun/ldexp.hpp +++ b/stan/math/prim/fun/ldexp.hpp @@ -36,9 +36,13 @@ inline double ldexp(T1 a, int b) { template * = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t< T1, T2>* = nullptr> -inline auto ldexp(const T1& a, const T2& b) { +inline auto ldexp(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return ldexp(c, d); }, a, b); + [](auto&& c, auto&& d) { + return ldexp(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/lgamma.hpp b/stan/math/prim/fun/lgamma.hpp index 8c5fec86e79..6cfb99d20ec 100644 --- a/stan/math/prim/fun/lgamma.hpp +++ b/stan/math/prim/fun/lgamma.hpp @@ -116,8 +116,8 @@ struct lgamma_fun { */ template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline auto lgamma(const T& x) { - return apply_scalar_unary::apply(x); +inline auto lgamma(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/lmgamma.hpp b/stan/math/prim/fun/lmgamma.hpp index 5d1b946f7e8..9f4381deaf0 100644 --- a/stan/math/prim/fun/lmgamma.hpp +++ b/stan/math/prim/fun/lmgamma.hpp @@ -70,9 +70,13 @@ inline return_type_t lmgamma(int k, T x) { * inputs. */ template * = nullptr> -inline auto lmgamma(const T1& a, const T2& b) { +inline auto lmgamma(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return lmgamma(c, d); }, a, b); + [](auto&& c, auto&& d) { + return lmgamma(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/lmultiply.hpp b/stan/math/prim/fun/lmultiply.hpp index e7de4468f81..8e38dc0695d 100644 --- a/stan/math/prim/fun/lmultiply.hpp +++ b/stan/math/prim/fun/lmultiply.hpp @@ -43,9 +43,13 @@ inline return_type_t lmultiply(const T1 a, const T2 b) { */ template * = nullptr, require_all_not_var_matrix_t* = nullptr> -inline auto lmultiply(const T1& a, const T2& b) { +inline auto lmultiply(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return lmultiply(c, d); }, a, b); + [](auto&& c, auto&& d) { + return lmultiply(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index 8216cee2aa6..1402ecd3fea 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -67,8 +67,9 @@ struct log_fun { * @return Elementwise application of natural log to the argument. */ template * = nullptr> -inline auto log(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto log(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -81,9 +82,10 @@ inline auto log(const Container& x) { */ template * = nullptr> -inline auto log(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().log(); }); +inline auto log(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().log(); }); } namespace internal { diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index f7d81343ea5..59e07b9cf30 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -58,8 +58,8 @@ struct log10_fun { * @return Log base-10 applied to each value in x. */ template * = nullptr> -inline auto log10(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto log10(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -72,9 +72,10 @@ inline auto log10(const Container& x) { */ template * = nullptr> -inline auto log10(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().log10(); }); +inline auto log10(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().log10(); }); } namespace internal { diff --git a/stan/math/prim/fun/log1m.hpp b/stan/math/prim/fun/log1m.hpp index 702ee7f20fb..c092ea8873a 100644 --- a/stan/math/prim/fun/log1m.hpp +++ b/stan/math/prim/fun/log1m.hpp @@ -70,8 +70,8 @@ struct log1m_fun { template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline auto log1m(const T& x) { - return apply_scalar_unary::apply(x); +inline auto log1m(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1m_exp.hpp b/stan/math/prim/fun/log1m_exp.hpp index 89d1a4d6af6..c79aed1b725 100644 --- a/stan/math/prim/fun/log1m_exp.hpp +++ b/stan/math/prim/fun/log1m_exp.hpp @@ -80,8 +80,8 @@ struct log1m_exp_fun { template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline auto log1m_exp(const T& x) { - return apply_scalar_unary::apply(x); +inline auto log1m_exp(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1m_inv_logit.hpp b/stan/math/prim/fun/log1m_inv_logit.hpp index 0128aa7804e..d6717a431af 100644 --- a/stan/math/prim/fun/log1m_inv_logit.hpp +++ b/stan/math/prim/fun/log1m_inv_logit.hpp @@ -82,9 +82,10 @@ struct log1m_inv_logit_fun { */ template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline typename apply_scalar_unary::return_t -log1m_inv_logit(const T& x) { - return apply_scalar_unary::apply(x); +inline typename apply_scalar_unary>::return_t +log1m_inv_logit(T&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1p.hpp b/stan/math/prim/fun/log1p.hpp index 3bcbc205a54..e4d15264ec7 100644 --- a/stan/math/prim/fun/log1p.hpp +++ b/stan/math/prim/fun/log1p.hpp @@ -79,8 +79,8 @@ struct log1p_fun { template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto log1p(const T& x) { - return apply_scalar_unary::apply(x); +inline auto log1p(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1p_exp.hpp b/stan/math/prim/fun/log1p_exp.hpp index de658ae3885..02df5805859 100644 --- a/stan/math/prim/fun/log1p_exp.hpp +++ b/stan/math/prim/fun/log1p_exp.hpp @@ -75,8 +75,8 @@ struct log1p_exp_fun { template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto log1p_exp(const T& x) { - return apply_scalar_unary::apply(x); +inline auto log1p_exp(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log2.hpp b/stan/math/prim/fun/log2.hpp index 52a0674b8f1..ecd11406c56 100644 --- a/stan/math/prim/fun/log2.hpp +++ b/stan/math/prim/fun/log2.hpp @@ -46,8 +46,8 @@ struct log2_fun { */ template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline auto log2(const T& x) { - return apply_scalar_unary::apply(x); +inline auto log2(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log_diff_exp.hpp b/stan/math/prim/fun/log_diff_exp.hpp index a393015582e..463220d5ee7 100644 --- a/stan/math/prim/fun/log_diff_exp.hpp +++ b/stan/math/prim/fun/log_diff_exp.hpp @@ -66,9 +66,13 @@ inline return_type_t log_diff_exp(const T1 x, const T2 y) { * @return log_diff_exp function applied to the two inputs. */ template * = nullptr> -inline auto log_diff_exp(const T1& a, const T2& b) { +inline auto log_diff_exp(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return log_diff_exp(c, d); }, a, b); + [](auto&& c, auto&& d) { + return log_diff_exp(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/log_falling_factorial.hpp b/stan/math/prim/fun/log_falling_factorial.hpp index 7180e42ab6f..f1770d66e70 100644 --- a/stan/math/prim/fun/log_falling_factorial.hpp +++ b/stan/math/prim/fun/log_falling_factorial.hpp @@ -72,10 +72,13 @@ inline return_type_t log_falling_factorial(const T1 x, const T2 n) { * @return log_falling_factorial function applied to the two inputs. */ template * = nullptr> -inline auto log_falling_factorial(const T1& a, const T2& b) { +inline auto log_falling_factorial(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return log_falling_factorial(c, d); }, - a, b); + [](auto&& c, auto&& d) { + return log_falling_factorial(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/log_inv_logit.hpp b/stan/math/prim/fun/log_inv_logit.hpp index d63d8ccf36a..a02d4f376ae 100644 --- a/stan/math/prim/fun/log_inv_logit.hpp +++ b/stan/math/prim/fun/log_inv_logit.hpp @@ -81,8 +81,8 @@ struct log_inv_logit_fun { template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto log_inv_logit(const T& x) { - return apply_scalar_unary::apply(x); +inline auto log_inv_logit(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log_inv_logit_diff.hpp b/stan/math/prim/fun/log_inv_logit_diff.hpp index 9843583d3fc..96bce37601b 100644 --- a/stan/math/prim/fun/log_inv_logit_diff.hpp +++ b/stan/math/prim/fun/log_inv_logit_diff.hpp @@ -52,10 +52,13 @@ inline return_type_t log_inv_logit_diff(const T1& x, const T2& y) { * @return log_inv_logit_diff function applied to the two inputs. */ template * = nullptr> -inline auto log_inv_logit_diff(const T1& a, const T2& b) { +inline auto log_inv_logit_diff(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return log_inv_logit_diff(c, d); }, a, - b); + [](auto&& c, auto&& d) { + return log_inv_logit_diff(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math 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 7607560a08d..49a17ac36bf 100644 --- a/stan/math/prim/fun/log_modified_bessel_first_kind.hpp +++ b/stan/math/prim/fun/log_modified_bessel_first_kind.hpp @@ -232,12 +232,13 @@ inline return_type_t log_modified_bessel_first_kind( * @return log_modified_bessel_first_kind function applied to the two inputs. */ template * = nullptr> -inline auto log_modified_bessel_first_kind(const T1& a, const T2& b) { +inline auto log_modified_bessel_first_kind(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { - return log_modified_bessel_first_kind(c, d); + [](auto&& c, auto&& d) { + return log_modified_bessel_first_kind(std::forward(c), + std::forward(d)); }, - a, b); + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/log_rising_factorial.hpp b/stan/math/prim/fun/log_rising_factorial.hpp index bff1df3d9e1..24bc73533ea 100644 --- a/stan/math/prim/fun/log_rising_factorial.hpp +++ b/stan/math/prim/fun/log_rising_factorial.hpp @@ -70,10 +70,13 @@ inline return_type_t log_rising_factorial(const T1& x, const T2& n) { * @return log_rising_factorial function applied to the two inputs. */ template * = nullptr> -inline auto log_rising_factorial(const T1& a, const T2& b) { +inline auto log_rising_factorial(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return log_rising_factorial(c, d); }, - a, b); + [](auto&& c, auto&& d) { + return log_rising_factorial(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/log_softmax.hpp b/stan/math/prim/fun/log_softmax.hpp index 62d753293f6..8471c2646ae 100644 --- a/stan/math/prim/fun/log_softmax.hpp +++ b/stan/math/prim/fun/log_softmax.hpp @@ -41,14 +41,17 @@ namespace math { */ template * = nullptr, require_container_t* = nullptr> -inline auto log_softmax(const Container& x) { +inline auto log_softmax(Container&& x) { check_nonzero_size("log_softmax", "v", x); return make_holder( - [](const auto& a) { - return apply_vector_unary>::apply( - a, [](const auto& v) { return v.array() - log_sum_exp(v); }); + [](auto&& a) { + return apply_vector_unary>>::apply( + std::forward(a), + [](const auto& v) { + return std::forward(v).array() - log_sum_exp(v); + }); }, - to_ref(x)); + to_ref(std::forward(x))); } } // namespace math diff --git a/stan/math/prim/fun/log_sum_exp.hpp b/stan/math/prim/fun/log_sum_exp.hpp index 128b9cd8189..7d732b47c26 100644 --- a/stan/math/prim/fun/log_sum_exp.hpp +++ b/stan/math/prim/fun/log_sum_exp.hpp @@ -79,8 +79,9 @@ inline return_type_t log_sum_exp(const T2& a, const T1& b) { * @return The log of the sum of the exponentiated vector values. */ template * = nullptr> -inline auto log_sum_exp(const T& x) { - return apply_vector_unary::reduce(x, [&](const auto& v) { +inline auto log_sum_exp(T&& x) { + return apply_vector_unary>::reduce( + std::forward(x), [&](auto&& v) { if (v.size() == 0) { return NEGATIVE_INFTY; } @@ -104,9 +105,13 @@ inline auto log_sum_exp(const T& x) { * @return log_sum_exp function applied to the two inputs. */ template * = nullptr> -inline auto log_sum_exp(const T1& a, const T2& b) { +inline auto log_sum_exp(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return log_sum_exp(c, d); }, a, b); + [](auto&& c, auto&& d) { + return log_sum_exp(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/logit.hpp b/stan/math/prim/fun/logit.hpp index 3f6f7ce7a8c..82498e42135 100644 --- a/stan/math/prim/fun/logit.hpp +++ b/stan/math/prim/fun/logit.hpp @@ -87,8 +87,8 @@ struct logit_fun { * @return elementwise logit of container elements */ template * = nullptr> -inline auto logit(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto logit(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -104,14 +104,19 @@ inline auto logit(const Container& x) { */ template * = nullptr> -inline auto logit(const Container& x) { +inline auto logit(Container&& x) { return make_holder( - [](const auto& v_ref) { - return apply_vector_unary>::apply( - v_ref, - [](const auto& v) { return (v.array() / (1 - v.array())).log(); }); + [](auto&& v_ref) { + return apply_vector_unary>>::apply( + std::forward(v_ref), + [](const auto& v) { + return ( + std::forward(v).array() + / (1 - std::forward(v).array())) + .log(); + }); }, - to_ref(x)); + to_ref(std::forward(x))); } } // namespace math diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index b2830183ad3..02e8ddd5bc3 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -27,9 +27,10 @@ inline auto minus(const T& x) { * @return Container where each element is negated. */ template -inline auto minus(const std::vector& x) { +inline auto minus(std::vector&& x) { return apply_vector_unary>::apply( - x, [](const auto& v) { return -v; }); + std::forward>(x), + [](auto&& v) { return -std::forward(v); }); } } // namespace math diff --git a/stan/math/prim/fun/modified_bessel_first_kind.hpp b/stan/math/prim/fun/modified_bessel_first_kind.hpp index 25a6f214726..448787662a7 100644 --- a/stan/math/prim/fun/modified_bessel_first_kind.hpp +++ b/stan/math/prim/fun/modified_bessel_first_kind.hpp @@ -63,12 +63,13 @@ inline double modified_bessel_first_kind(int v, int z) { * @return modified_bessel_first_kind function applied to the two inputs. */ template * = nullptr> -inline auto modified_bessel_first_kind(const T1& a, const T2& b) { +inline auto modified_bessel_first_kind(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { - return modified_bessel_first_kind(c, d); + [](auto&& c, auto&& d) { + return modified_bessel_first_kind(std::forward(c), + std::forward(d)); }, - a, b); + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/modified_bessel_second_kind.hpp b/stan/math/prim/fun/modified_bessel_second_kind.hpp index cfb00030ae8..1f4bf7edfe0 100644 --- a/stan/math/prim/fun/modified_bessel_second_kind.hpp +++ b/stan/math/prim/fun/modified_bessel_second_kind.hpp @@ -55,12 +55,13 @@ inline T2 modified_bessel_second_kind(int v, const T2 z) { * @return modified_bessel_second_kind function applied to the two inputs. */ template * = nullptr> -inline auto modified_bessel_second_kind(const T1& a, const T2& b) { +inline auto modified_bessel_second_kind(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { - return modified_bessel_second_kind(c, d); + [](auto&& c, auto&& d) { + return modified_bessel_second_kind(std::forward(c), + std::forward(d)); }, - a, b); + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/multiply_log.hpp b/stan/math/prim/fun/multiply_log.hpp index 6dcd8909999..fb5010d9e27 100644 --- a/stan/math/prim/fun/multiply_log.hpp +++ b/stan/math/prim/fun/multiply_log.hpp @@ -67,9 +67,13 @@ inline return_type_t multiply_log(const T_a a, const T_b b) { */ template * = nullptr, require_all_not_var_matrix_t* = nullptr> -inline auto multiply_log(const T1& a, const T2& b) { +inline auto multiply_log(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return multiply_log(c, d); }, a, b); + [](auto&& c, auto&& d) { + return multiply_log(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/norm1.hpp b/stan/math/prim/fun/norm1.hpp index c842ff27917..a6dd7e08507 100644 --- a/stan/math/prim/fun/norm1.hpp +++ b/stan/math/prim/fun/norm1.hpp @@ -31,9 +31,10 @@ inline double norm1(const T& v) { * @return L1 norm of v. */ template * = nullptr> -inline auto norm1(const Container& x) { - return apply_vector_unary::reduce( - x, [](const auto& v) { return norm1(v); }); +inline auto norm1(Container&& x) { + return apply_vector_unary>::reduce( + std::forward(x), + [](auto&& v) { return norm1(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/fun/norm2.hpp b/stan/math/prim/fun/norm2.hpp index 07040003943..235782f9ad5 100644 --- a/stan/math/prim/fun/norm2.hpp +++ b/stan/math/prim/fun/norm2.hpp @@ -31,9 +31,10 @@ inline double norm2(const T& v) { * @return L2 norm of v. */ template * = nullptr> -inline auto norm2(const Container& x) { - return apply_vector_unary::reduce( - x, [](const auto& v) { return norm2(v); }); +inline auto norm2(Container&& x) { + return apply_vector_unary>::reduce( + std::forward(x), + [](auto&& v) { return norm2(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/fun/owens_t.hpp b/stan/math/prim/fun/owens_t.hpp index 88f4302e8aa..8ddd9fabf56 100644 --- a/stan/math/prim/fun/owens_t.hpp +++ b/stan/math/prim/fun/owens_t.hpp @@ -74,9 +74,13 @@ inline double owens_t(double h, double a) { */ template * = nullptr, require_all_not_var_and_matrix_types* = nullptr> -inline auto owens_t(const T1& a, const T2& b) { +inline auto owens_t(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return owens_t(c, d); }, a, b); + [](auto&& c, auto&& d) { + return owens_t(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/pow.hpp b/stan/math/prim/fun/pow.hpp index b93957891c6..d0ea2470718 100644 --- a/stan/math/prim/fun/pow.hpp +++ b/stan/math/prim/fun/pow.hpp @@ -108,10 +108,11 @@ inline auto pow(const T1& a, const T2& b) { template * = nullptr, require_all_not_matrix_st* = nullptr, require_all_arithmetic_t, base_type_t>* = nullptr> -inline auto pow(const T1& a, const T2& b) { +inline auto pow(T1&& a, T2&& b) { return apply_scalar_binary( // Qualified pow since only Arithmetic types are accepted here - [](const auto& c, const auto& d) { return stan::math::pow(c, d); }, a, b); + [](const auto& c, const auto& d) { return stan::math::pow(c, d); }, + std::forward(a), std::forward(b)); } } // namespace math } // namespace stan diff --git a/stan/math/prim/fun/rising_factorial.hpp b/stan/math/prim/fun/rising_factorial.hpp index 5a93b9f079f..c3288b9e7b0 100644 --- a/stan/math/prim/fun/rising_factorial.hpp +++ b/stan/math/prim/fun/rising_factorial.hpp @@ -78,10 +78,13 @@ inline return_type_t rising_factorial(const T& x, int n) { * @return rising_factorial function applied to the two inputs. */ template * = nullptr> -inline auto rising_factorial(const T1& a, const T2& b) { +inline auto rising_factorial(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return rising_factorial(c, d); }, a, - b); + [](auto&& c, auto&& d) { + return rising_factorial(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index 5ed3649bc47..1b196cceee3 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -36,8 +36,8 @@ template * = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t< Container>* = nullptr> -inline auto round(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto round(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -50,9 +50,10 @@ inline auto round(const Container& x) { */ template * = nullptr> -inline auto round(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().round(); }); +inline auto round(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().round(); }); } } // namespace math diff --git a/stan/math/prim/fun/sd.hpp b/stan/math/prim/fun/sd.hpp index 39223a6135b..b2b928403af 100644 --- a/stan/math/prim/fun/sd.hpp +++ b/stan/math/prim/fun/sd.hpp @@ -23,10 +23,11 @@ namespace math { */ template * = nullptr, require_not_st_var* = nullptr> -inline auto sd(const T& m) { +inline auto sd(T&& m) { using std::sqrt; - return apply_vector_unary::reduce(m, [](const auto& x) { + return apply_vector_unary>::reduce( + std::forward(m), [](auto&& x) { check_nonzero_size("sd", "x", x); if (x.size() == 1) { diff --git a/stan/math/prim/fun/select.hpp b/stan/math/prim/fun/select.hpp index def8bd3d67c..7968d65a12b 100644 --- a/stan/math/prim/fun/select.hpp +++ b/stan/math/prim/fun/select.hpp @@ -23,8 +23,8 @@ namespace math { template , require_all_stan_scalar_t* = nullptr> -inline ReturnT select(const bool c, const T_true y_true, - const T_false y_false) { +inline ReturnT select(const bool c, T_true&& y_true, + T_false&& y_false) { return c ? ReturnT(y_true) : ReturnT(y_false); } @@ -80,16 +80,16 @@ template >, require_container_t* = nullptr, require_stan_scalar_t* = nullptr> -inline ReturnT select(const bool c, const T_true& y_true, - const T_false& y_false) { +inline ReturnT select(const bool c, T_true&& y_true, + T_false&& y_false) { if (c) { return y_true; } else { return apply_scalar_binary( - [](const auto& y_true_inner, const auto& y_false_inner) { - return y_false_inner; + [](auto&& y_true_inner, auto&& y_false_inner) { + return std::forward(y_false_inner); }, - y_true, y_false); + std::forward(y_true), std::forward(y_false)); } } @@ -119,10 +119,10 @@ inline ReturnT select(const bool c, const T_true y_true, const T_false y_false) { if (c) { return apply_scalar_binary( - [](const auto& y_true_inner, const auto& y_false_inner) { - return y_true_inner; + [](auto&& y_true_inner, auto&& y_false_inner) { + return std::forward(y_true_inner); }, - y_true, y_false); + std::forward(y_true), std::forward(y_false)); } else { return y_false; } diff --git a/stan/math/prim/fun/sign.hpp b/stan/math/prim/fun/sign.hpp index 0651030f0eb..50449bd0e2e 100644 --- a/stan/math/prim/fun/sign.hpp +++ b/stan/math/prim/fun/sign.hpp @@ -39,8 +39,8 @@ struct sign_fun { * @return Elementwise sign of members of container. */ template * = nullptr> -inline auto sign(const T& x) { - return apply_scalar_unary::apply(x); +inline auto sign(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index a0145dd4c6b..0a1678a38bf 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -59,8 +59,9 @@ struct sin_fun { * @return Sine of each value in x. */ template * = nullptr> -inline auto sin(const T& x) { - return apply_scalar_unary::apply(x); +inline auto sin(T&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -73,9 +74,10 @@ inline auto sin(const T& x) { */ template * = nullptr> -inline auto sin(const Container& x) { - return apply_vector_unary::apply( - x, [&](const auto& v) { return v.array().sin(); }); +inline auto sin(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [&](const auto& v) { return std::forward(v).array().sin(); }); } namespace internal { diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index e3e55fa7477..dd34a48145a 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -57,8 +57,8 @@ struct sinh_fun { * @return Hyperbolic sine of each variable in x. */ template * = nullptr> -inline auto sinh(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto sinh(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -71,9 +71,10 @@ inline auto sinh(const Container& x) { */ template * = nullptr> -inline auto sinh(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().sinh(); }); +inline auto sinh(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().sinh(); }); } namespace internal { diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index 3e1620d2a61..d70307bbed4 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -57,8 +57,8 @@ struct sqrt_fun { * @return Square root of each value in x. */ template * = nullptr> -inline auto sqrt(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto sqrt(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -72,9 +72,10 @@ inline auto sqrt(const Container& x) { template * = nullptr, require_not_var_matrix_t* = nullptr> -inline auto sqrt(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().sqrt(); }); +inline auto sqrt(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().sqrt(); }); } namespace internal { diff --git a/stan/math/prim/fun/square.hpp b/stan/math/prim/fun/square.hpp index cb14bd77c8b..7462934ef65 100644 --- a/stan/math/prim/fun/square.hpp +++ b/stan/math/prim/fun/square.hpp @@ -50,8 +50,8 @@ struct square_fun { * @return Each value in x squared. */ template * = nullptr> -inline auto square(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto square(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -64,9 +64,10 @@ inline auto square(const Container& x) { */ template * = nullptr> -inline auto square(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().square(); }); +inline auto square(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().square(); }); } } // namespace math diff --git a/stan/math/prim/fun/step.hpp b/stan/math/prim/fun/step.hpp index c75d164fd2b..7398b4ba707 100644 --- a/stan/math/prim/fun/step.hpp +++ b/stan/math/prim/fun/step.hpp @@ -58,8 +58,8 @@ struct step_fun { * @return Elementwise step of members of container. */ template * = nullptr> -inline auto step(const T& x) { - return apply_scalar_unary::apply(x); +inline auto step(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index 373672eb0c8..e11f500010b 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -59,8 +59,9 @@ struct tan_fun { * @return Tangent of each value in x. */ template * = nullptr> -inline auto tan(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto tan(Container&& x) { + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -73,9 +74,12 @@ inline auto tan(const Container& x) { */ template * = nullptr> -inline auto tan(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().tan(); }); +inline auto tan(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { + return std::forward(v).array().tan(); + }); } namespace internal { diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index 1ae3faff087..3a26a16797a 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -59,8 +59,8 @@ struct tanh_fun { * @return Hyperbolic tangent of each value in x. */ template * = nullptr> -inline auto tanh(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto tanh(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } /** @@ -73,9 +73,10 @@ inline auto tanh(const Container& x) { */ template * = nullptr> -inline auto tanh(const Container& x) { - return apply_vector_unary::apply( - x, [](const auto& v) { return v.array().tanh(); }); +inline auto tanh(Container&& x) { + return apply_vector_unary>::apply( + std::forward(x), + [](const auto& v) { return std::forward(v).array().tanh(); }); } namespace internal { diff --git a/stan/math/prim/fun/tgamma.hpp b/stan/math/prim/fun/tgamma.hpp index c038b699ab7..40d9249615e 100644 --- a/stan/math/prim/fun/tgamma.hpp +++ b/stan/math/prim/fun/tgamma.hpp @@ -50,8 +50,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto tgamma(const T& x) { - return apply_scalar_unary::apply(x); +inline auto tgamma(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/to_complex.hpp b/stan/math/prim/fun/to_complex.hpp index ea0122595c1..f5777bfc17c 100644 --- a/stan/math/prim/fun/to_complex.hpp +++ b/stan/math/prim/fun/to_complex.hpp @@ -37,10 +37,13 @@ constexpr inline std::complex> to_complex( */ template * = nullptr, require_all_st_stan_scalar* = nullptr> -inline auto to_complex(const T1& re, const T2& im) { +inline auto to_complex(T1&& re, T2&& im) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return stan::math::to_complex(c, d); }, - re, im); + [](auto&& c, auto&& d) { + return stan::math::to_complex(std::forward(c), + std::forward(d)); + }, + std::forward(re), std::forward(im)); } } // namespace math diff --git a/stan/math/prim/fun/to_int.hpp b/stan/math/prim/fun/to_int.hpp index ea286f55925..0a1d54a4efc 100644 --- a/stan/math/prim/fun/to_int.hpp +++ b/stan/math/prim/fun/to_int.hpp @@ -71,8 +71,8 @@ struct to_int_fun { */ template * = nullptr> -inline auto to_int(const Container& x) { - return apply_scalar_unary::apply(x); +inline auto to_int(Container&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/trigamma.hpp b/stan/math/prim/fun/trigamma.hpp index 9a30daab99b..fd62dfaab91 100644 --- a/stan/math/prim/fun/trigamma.hpp +++ b/stan/math/prim/fun/trigamma.hpp @@ -155,8 +155,8 @@ struct trigamma_fun { */ template * = nullptr> -inline auto trigamma(const T& x) { - return apply_scalar_unary::apply(x); +inline auto trigamma(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/trunc.hpp b/stan/math/prim/fun/trunc.hpp index 4a5b1c1d79b..941461ea8e2 100644 --- a/stan/math/prim/fun/trunc.hpp +++ b/stan/math/prim/fun/trunc.hpp @@ -39,8 +39,8 @@ struct trunc_fun { */ template * = nullptr> -inline auto trunc(const T& x) { - return apply_scalar_unary::apply(x); +inline auto trunc(T&& x) { + return apply_scalar_unary>::apply(std::forward(x)); } } // namespace math From 81bd2d0a74cd94573e9bca928a3d66d8229f2710 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 30 Jun 2025 16:27:27 -0400 Subject: [PATCH 02/31] use perfect forwarding for functions that use apply --- stan/math/fwd/fun/log_softmax.hpp | 4 +- stan/math/fwd/fun/log_sum_exp.hpp | 4 +- stan/math/fwd/fun/norm1.hpp | 4 +- stan/math/fwd/fun/norm2.hpp | 4 +- stan/math/fwd/fun/pow.hpp | 4 +- .../constraint/cholesky_corr_constrain.hpp | 18 +++---- .../prim/constraint/cholesky_corr_free.hpp | 6 +-- .../constraint/cholesky_factor_constrain.hpp | 18 +++---- .../prim/constraint/cholesky_factor_free.hpp | 6 +-- stan/math/prim/constraint/corr_constrain.hpp | 12 ++--- stan/math/prim/constraint/corr_free.hpp | 4 +- .../prim/constraint/corr_matrix_constrain.hpp | 30 ++++++------ .../math/prim/constraint/corr_matrix_free.hpp | 6 +-- .../prim/constraint/cov_matrix_constrain.hpp | 30 ++++++------ .../constraint/cov_matrix_constrain_lkj.hpp | 20 ++++---- stan/math/prim/constraint/cov_matrix_free.hpp | 10 ++-- .../prim/constraint/cov_matrix_free_lkj.hpp | 6 +-- .../prim/constraint/identity_constrain.hpp | 2 +- stan/math/prim/constraint/identity_free.hpp | 2 +- stan/math/prim/constraint/lb_constrain.hpp | 2 +- stan/math/prim/constraint/lb_free.hpp | 2 +- stan/math/prim/constraint/lub_free.hpp | 6 +-- .../offset_multiplier_constrain.hpp | 6 +-- .../constraint/offset_multiplier_free.hpp | 39 +++++++-------- .../prim/constraint/ordered_constrain.hpp | 24 ++++----- stan/math/prim/constraint/ordered_free.hpp | 6 +-- .../prim/constraint/positive_constrain.hpp | 20 ++++---- stan/math/prim/constraint/positive_free.hpp | 4 +- .../constraint/positive_ordered_constrain.hpp | 25 +++++----- .../prim/constraint/positive_ordered_free.hpp | 6 +-- stan/math/prim/constraint/prob_constrain.hpp | 20 ++++---- stan/math/prim/constraint/prob_free.hpp | 4 +- .../prim/constraint/simplex_constrain.hpp | 18 +++---- stan/math/prim/constraint/simplex_free.hpp | 6 +-- .../stochastic_column_constrain.hpp | 22 ++++----- .../constraint/stochastic_column_free.hpp | 10 ++-- .../constraint/stochastic_row_constrain.hpp | 26 +++++----- .../prim/constraint/stochastic_row_free.hpp | 10 ++-- .../prim/constraint/sum_to_zero_constrain.hpp | 22 ++++----- .../math/prim/constraint/sum_to_zero_free.hpp | 6 +-- stan/math/prim/constraint/ub_constrain.hpp | 22 ++++----- .../prim/constraint/unit_vector_constrain.hpp | 18 +++---- .../math/prim/constraint/unit_vector_free.hpp | 6 +-- stan/math/prim/fun/abs.hpp | 2 +- stan/math/prim/fun/acos.hpp | 2 +- stan/math/prim/fun/asin.hpp | 2 +- stan/math/prim/fun/atan.hpp | 2 +- stan/math/prim/fun/ceil.hpp | 2 +- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/fun/cosh.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/fabs.hpp | 2 +- stan/math/prim/fun/floor.hpp | 2 +- stan/math/prim/fun/inv.hpp | 2 +- stan/math/prim/fun/inv_cloglog.hpp | 2 +- stan/math/prim/fun/inv_logit.hpp | 2 +- stan/math/prim/fun/inv_sqrt.hpp | 2 +- stan/math/prim/fun/inv_square.hpp | 2 +- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/log1m_inv_logit.hpp | 7 ++- stan/math/prim/fun/max.hpp | 6 +-- stan/math/prim/fun/mean.hpp | 4 +- stan/math/prim/fun/min.hpp | 6 +-- stan/math/prim/fun/minus.hpp | 19 ++++--- stan/math/prim/fun/pow.hpp | 2 +- stan/math/prim/fun/rank.hpp | 7 ++- stan/math/prim/fun/round.hpp | 2 +- stan/math/prim/fun/select.hpp | 12 ++--- stan/math/prim/fun/sin.hpp | 2 +- stan/math/prim/fun/sinh.hpp | 2 +- stan/math/prim/fun/sqrt.hpp | 2 +- stan/math/prim/fun/square.hpp | 2 +- stan/math/prim/fun/tan.hpp | 2 +- stan/math/prim/fun/tanh.hpp | 2 +- .../math/prim/functor/apply_scalar_binary.hpp | 4 +- .../prim/functor/apply_scalar_ternary.hpp | 21 ++++---- stan/math/prim/functor/apply_scalar_unary.hpp | 20 +++++--- stan/math/prim/functor/apply_vector_unary.hpp | 49 ++++++++++++------- stan/math/prim/prob/std_normal_log_qf.hpp | 4 +- stan/math/rev/fun/floor.hpp | 2 +- stan/math/rev/fun/log_softmax.hpp | 6 +-- stan/math/rev/fun/log_sum_exp.hpp | 6 +-- stan/math/rev/fun/pow.hpp | 4 +- stan/math/rev/fun/sd.hpp | 4 +- stan/math/rev/functor/apply_scalar_unary.hpp | 3 +- stan/math/rev/functor/apply_vector_unary.hpp | 18 +++---- 87 files changed, 396 insertions(+), 376 deletions(-) diff --git a/stan/math/fwd/fun/log_softmax.hpp b/stan/math/fwd/fun/log_softmax.hpp index 82d1f7b0204..48e7be01f82 100644 --- a/stan/math/fwd/fun/log_softmax.hpp +++ b/stan/math/fwd/fun/log_softmax.hpp @@ -19,8 +19,8 @@ namespace math { * @throw std::domain_error If the input vector is size 0. */ template * = nullptr> -inline auto log_softmax(const T& x) { - return apply_vector_unary::apply(x, [&](const auto& alpha) { +inline auto log_softmax(T&& x) { + return apply_vector_unary::apply(std::forward(x), [&](const auto& alpha) { using T_alpha = decltype(alpha); using T_fvar = value_type_t; using T_fvar_inner = typename T_fvar::Scalar; diff --git a/stan/math/fwd/fun/log_sum_exp.hpp b/stan/math/fwd/fun/log_sum_exp.hpp index 30795b8f99b..cf61b472cc8 100644 --- a/stan/math/fwd/fun/log_sum_exp.hpp +++ b/stan/math/fwd/fun/log_sum_exp.hpp @@ -50,9 +50,9 @@ inline fvar log_sum_exp(const fvar& x1, double x2) { * @return The log of the sum of the exponentiated vector values. */ template * = nullptr> -inline auto log_sum_exp(const T& x) { +inline auto log_sum_exp(T&& x) { return apply_vector_unary>::reduce( - to_ref(x), [&](const auto& v) { + to_ref(std::forward(x)), [&](const auto& v) { using T_fvar_inner = typename value_type_t::Scalar; using mat_type = Eigen::Matrix; mat_type vals = v.val(); diff --git a/stan/math/fwd/fun/norm1.hpp b/stan/math/fwd/fun/norm1.hpp index 151216f2f12..a4026988dee 100644 --- a/stan/math/fwd/fun/norm1.hpp +++ b/stan/math/fwd/fun/norm1.hpp @@ -20,9 +20,9 @@ namespace math { * @return L1 norm of x. */ template * = nullptr> -inline auto norm1(const Container& x) { +inline auto norm1(Container&& x) { return apply_vector_unary>::reduce( - to_ref(x), [&](const auto& v) { + to_ref(std::forward(x)), [&](const auto& v) { using T_fvar_inner = typename value_type_t::Scalar; return fvar(norm1(v.val()), v.d().cwiseProduct(sign(v.val())).sum()); diff --git a/stan/math/fwd/fun/norm2.hpp b/stan/math/fwd/fun/norm2.hpp index 3ba8200363a..61129a3709b 100644 --- a/stan/math/fwd/fun/norm2.hpp +++ b/stan/math/fwd/fun/norm2.hpp @@ -19,9 +19,9 @@ namespace math { * @return L2 norm of x. */ template * = nullptr> -inline auto norm2(const Container& x) { +inline auto norm2(Container&& x) { return apply_vector_unary>::reduce( - to_ref(x), [&](const auto& v) { + to_ref(std::forward(x)), [&](const auto& v) { using T_fvar_inner = typename value_type_t::Scalar; T_fvar_inner res = norm2(v.val()); return fvar(res, diff --git a/stan/math/fwd/fun/pow.hpp b/stan/math/fwd/fun/pow.hpp index 1a23514727a..2a29557dad5 100644 --- a/stan/math/fwd/fun/pow.hpp +++ b/stan/math/fwd/fun/pow.hpp @@ -73,9 +73,9 @@ inline auto pow(const T1& x1, const T2& x2) { template * = nullptr, require_all_not_matrix_st* = nullptr, require_any_fvar_t, base_type_t>* = nullptr> -inline auto pow(const T1& a, const T2& b) { +inline auto pow(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return stan::math::pow(c, d); }, a, b); + [](auto&& c, auto&& d) { return stan::math::pow(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index 3a55e04b3f2..7e868080ebd 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -87,9 +87,9 @@ cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) { * @param K The size of the matrix to return */ template * = nullptr> -inline auto cholesky_corr_constrain(const T& y, int K) { - return apply_vector_unary::apply( - y, [K](auto&& v) { return cholesky_corr_constrain(v, K); }); +inline auto cholesky_corr_constrain(T&& y, int K) { + return apply_vector_unary>::apply( + std::forward(y), [K](auto&& v) { return cholesky_corr_constrain(std::forward(v), K); }); } /** @@ -107,9 +107,9 @@ inline auto cholesky_corr_constrain(const T& y, int K) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto cholesky_corr_constrain(const T& y, int K, Lp& lp) { - return apply_vector_unary::apply( - y, [&lp, K](auto&& v) { return cholesky_corr_constrain(v, K, lp); }); +inline auto cholesky_corr_constrain(T&& y, int K, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(y), [&lp, K](auto&& v) { return cholesky_corr_constrain(std::forward(v), K, lp); }); } /** @@ -132,11 +132,11 @@ inline auto cholesky_corr_constrain(const T& y, int K, Lp& lp) { */ template , Lp>* = nullptr> -inline auto cholesky_corr_constrain(const T& y, int K, Lp& lp) { +inline auto cholesky_corr_constrain(T&& y, int K, Lp& lp) { if constexpr (Jacobian) { - return cholesky_corr_constrain(y, K, lp); + return cholesky_corr_constrain(std::forward(y), K, lp); } else { - return cholesky_corr_constrain(y, K); + return cholesky_corr_constrain(std::forward(y), K); } } diff --git a/stan/math/prim/constraint/cholesky_corr_free.hpp b/stan/math/prim/constraint/cholesky_corr_free.hpp index b359f677313..40fde2cd559 100644 --- a/stan/math/prim/constraint/cholesky_corr_free.hpp +++ b/stan/math/prim/constraint/cholesky_corr_free.hpp @@ -42,9 +42,9 @@ inline auto cholesky_corr_free(const T& x) { * @param x The standard vector to untransform. */ template * = nullptr> -inline auto cholesky_corr_free(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return cholesky_corr_free(v); }); +inline auto cholesky_corr_free(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return cholesky_corr_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/cholesky_factor_constrain.hpp b/stan/math/prim/constraint/cholesky_factor_constrain.hpp index 61356d2a897..1ec88c90c25 100644 --- a/stan/math/prim/constraint/cholesky_factor_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_factor_constrain.hpp @@ -101,9 +101,9 @@ cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) { * @return Cholesky factor */ template * = nullptr> -inline auto cholesky_factor_constrain(const T& x, int M, int N) { - return apply_vector_unary::apply( - x, [M, N](auto&& v) { return cholesky_factor_constrain(v, M, N); }); +inline auto cholesky_factor_constrain(T&& x, int M, int N) { + return apply_vector_unary>::apply( + std::forward(x), [M, N](auto&& v) { return cholesky_factor_constrain(std::forward(v), M, N); }); } /** @@ -125,9 +125,9 @@ inline auto cholesky_factor_constrain(const T& x, int M, int N) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) { - return apply_vector_unary::apply(x, [&lp, M, N](auto&& v) { - return cholesky_factor_constrain(v, M, N, lp); +inline auto cholesky_factor_constrain(T&& x, int M, int N, Lp& lp) { + return apply_vector_unary>::apply(std::forward(x), [&lp, M, N](auto&& v) { + return cholesky_factor_constrain(std::forward(v), M, N, lp); }); } @@ -155,11 +155,11 @@ inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) { */ template , Lp>* = nullptr> -inline auto cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) { +inline auto cholesky_factor_constrain(T&& x, int M, int N, Lp& lp) { if constexpr (Jacobian) { - return cholesky_factor_constrain(x, M, N, lp); + return cholesky_factor_constrain(std::forward(x), M, N, lp); } else { - return cholesky_factor_constrain(x, M, N); + return cholesky_factor_constrain(std::forward(x), M, N); } } diff --git a/stan/math/prim/constraint/cholesky_factor_free.hpp b/stan/math/prim/constraint/cholesky_factor_free.hpp index e3be04e2589..b4827a8131a 100644 --- a/stan/math/prim/constraint/cholesky_factor_free.hpp +++ b/stan/math/prim/constraint/cholesky_factor_free.hpp @@ -56,9 +56,9 @@ Eigen::Matrix, Eigen::Dynamic, 1> cholesky_factor_free( * @param x The standard vector to untransform. */ template * = nullptr> -inline auto cholesky_factor_free(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return cholesky_factor_free(v); }); +inline auto cholesky_factor_free(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return cholesky_factor_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/corr_constrain.hpp b/stan/math/prim/constraint/corr_constrain.hpp index 8366132f9f9..f19f2ea8669 100644 --- a/stan/math/prim/constraint/corr_constrain.hpp +++ b/stan/math/prim/constraint/corr_constrain.hpp @@ -24,8 +24,8 @@ namespace math { * @return tanh transform */ template -inline plain_type_t corr_constrain(const T& x) { - return tanh(x); +inline plain_type_t corr_constrain(T&& x) { + return tanh(std::forward(x)); } /** @@ -43,7 +43,7 @@ inline plain_type_t corr_constrain(const T& x) { * @param[in,out] lp log density accumulator */ template -inline auto corr_constrain(const T_x& x, T_lp& lp) { +inline auto corr_constrain(T_x&& x, T_lp& lp) { plain_type_t tanh_x = tanh(x); lp += sum(log1m(square(tanh_x))); return tanh_x; @@ -65,11 +65,11 @@ inline auto corr_constrain(const T_x& x, T_lp& lp) { * @param[in,out] lp log density accumulator */ template -inline auto corr_constrain(const T_x& x, T_lp& lp) { +inline auto corr_constrain(T_x&& x, T_lp& lp) { if constexpr (Jacobian) { - return corr_constrain(x, lp); + return corr_constrain(std::forward(x), lp); } else { - return corr_constrain(x); + return corr_constrain(std::forward(x)); } } diff --git a/stan/math/prim/constraint/corr_free.hpp b/stan/math/prim/constraint/corr_free.hpp index 3714b26f4f1..5e0b542c758 100644 --- a/stan/math/prim/constraint/corr_free.hpp +++ b/stan/math/prim/constraint/corr_free.hpp @@ -25,9 +25,9 @@ namespace math { * @return free scalar that transforms to the specified input */ template -inline plain_type_t corr_free(const T& y) { +inline plain_type_t corr_free(T&& y) { check_bounded("lub_free", "Correlation variable", y, -1.0, 1.0); - return atanh(y); + return atanh(std::forward(y)); } } // namespace math diff --git a/stan/math/prim/constraint/corr_matrix_constrain.hpp b/stan/math/prim/constraint/corr_matrix_constrain.hpp index 060b1d5f6a3..4c44f60b2cd 100644 --- a/stan/math/prim/constraint/corr_matrix_constrain.hpp +++ b/stan/math/prim/constraint/corr_matrix_constrain.hpp @@ -38,11 +38,11 @@ namespace math { */ template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -corr_matrix_constrain(const T& x, Eigen::Index k) { - Eigen::Index k_choose_2 = (k * (k - 1)) / 2; +corr_matrix_constrain(T&& x, Eigen::Index k) { + const Eigen::Index k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); - return read_corr_matrix(corr_constrain(x), k); + return read_corr_matrix(corr_constrain(std::forward(x)), k); } /** @@ -70,11 +70,11 @@ corr_matrix_constrain(const T& x, Eigen::Index k) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -corr_matrix_constrain(const T& x, Eigen::Index k, Lp& lp) { - Eigen::Index k_choose_2 = (k * (k - 1)) / 2; +corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) { + const Eigen::Index k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); - return read_corr_matrix(corr_constrain(x, lp), k, lp); + return read_corr_matrix(corr_constrain(std::forward(x), lp), k, lp); } /** @@ -91,9 +91,9 @@ corr_matrix_constrain(const T& x, Eigen::Index k, Lp& lp) { * @param K Dimensionality of returned correlation matrix */ template * = nullptr> -inline auto corr_matrix_constrain(const T& y, int K) { - return apply_vector_unary::apply( - y, [K](auto&& v) { return corr_matrix_constrain(v, K); }); +inline auto corr_matrix_constrain(T&& y, int K) { + return apply_vector_unary>::apply( + std::forward(y), [K](auto&& v) { return corr_matrix_constrain(std::forward(v), K); }); } /** @@ -114,9 +114,9 @@ inline auto corr_matrix_constrain(const T& y, int K) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto corr_matrix_constrain(const T& y, int K, Lp& lp) { - return apply_vector_unary::apply( - y, [&lp, K](auto&& v) { return corr_matrix_constrain(v, K, lp); }); +inline auto corr_matrix_constrain(T&& y, int K, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(y), [&lp, K](auto&& v) { return corr_matrix_constrain(std::forward(v), K, lp); }); } /** @@ -142,11 +142,11 @@ inline auto corr_matrix_constrain(const T& y, int K, Lp& lp) { */ template , Lp>* = nullptr> -inline auto corr_matrix_constrain(const T& x, Eigen::Index k, Lp& lp) { +inline auto corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) { if constexpr (Jacobian) { - return corr_matrix_constrain(x, k, lp); + return corr_matrix_constrain(std::forward(x), k, lp); } else { - return corr_matrix_constrain(x, k); + return corr_matrix_constrain(std::forward(x), k); } } diff --git a/stan/math/prim/constraint/corr_matrix_free.hpp b/stan/math/prim/constraint/corr_matrix_free.hpp index 7ddb1230e62..76f46f11c7e 100644 --- a/stan/math/prim/constraint/corr_matrix_free.hpp +++ b/stan/math/prim/constraint/corr_matrix_free.hpp @@ -60,9 +60,9 @@ Eigen::Matrix, Eigen::Dynamic, 1> corr_matrix_free(const T& y) { * @param x The standard vector to untransform. */ template * = nullptr> -inline auto corr_matrix_free(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return corr_matrix_free(v); }); +inline auto corr_matrix_free(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return corr_matrix_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/cov_matrix_constrain.hpp b/stan/math/prim/constraint/cov_matrix_constrain.hpp index 3f2c67b3741..26bc919e29f 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain.hpp @@ -29,7 +29,7 @@ namespace math { */ template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cov_matrix_constrain(const T& x, Eigen::Index K) { +cov_matrix_constrain(T&& x, Eigen::Index K) { using Eigen::Dynamic; using Eigen::Matrix; using std::exp; @@ -37,7 +37,7 @@ cov_matrix_constrain(const T& x, Eigen::Index K) { Matrix, Dynamic, Dynamic> L(K, K); check_size_match("cov_matrix_constrain", "x.size()", x.size(), "K + (K choose 2)", (K * (K + 1)) / 2); - const auto& x_ref = to_ref(x); + auto&& x_ref = to_ref(std::forward(x)); int i = 0; for (Eigen::Index m = 0; m < K; ++m) { L.row(m).head(m) = x_ref.segment(i, m); @@ -45,7 +45,7 @@ cov_matrix_constrain(const T& x, Eigen::Index K) { L.coeffRef(m, m) = exp(x_ref.coeff(i++)); L.row(m).tail(K - m - 1).setZero(); } - return multiply_lower_tri_self_transpose(L); + return multiply_lower_tri_self_transpose(std::move(L)); } /** @@ -65,7 +65,7 @@ cov_matrix_constrain(const T& x, Eigen::Index K) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) { +cov_matrix_constrain(T&& x, Eigen::Index K, Lp& lp) { using Eigen::Dynamic; using Eigen::Matrix; using std::exp; @@ -73,7 +73,7 @@ cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) { check_size_match("cov_matrix_constrain", "x.size()", x.size(), "K + (K choose 2)", (K * (K + 1)) / 2); Matrix, Dynamic, Dynamic> L(K, K); - const auto& x_ref = to_ref(x); + auto&& x_ref = to_ref(std::forward(x)); int i = 0; for (Eigen::Index m = 0; m < K; ++m) { L.row(m).head(m) = x_ref.segment(i, m); @@ -86,7 +86,7 @@ cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) { for (Eigen::Index k = 0; k < K; ++k) { lp += (K - k + 1) * log(L.coeff(k, k)); // only +1 because index from 0 } - return multiply_lower_tri_self_transpose(L); + return multiply_lower_tri_self_transpose(std::move(L)); } /** @@ -102,9 +102,9 @@ cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) { * @throws std::domain_error if (x.size() != K + (K choose 2)). */ template * = nullptr> -inline auto cov_matrix_constrain(const T& x, Eigen::Index K) { - return apply_vector_unary::apply( - x, [K](auto&& v) { return cov_matrix_constrain(v, K); }); +inline auto cov_matrix_constrain(T&& x, Eigen::Index K) { + return apply_vector_unary>::apply( + std::forward(x), [K](auto&& v) { return cov_matrix_constrain(std::forward(v), K); }); } /** @@ -124,9 +124,9 @@ inline auto cov_matrix_constrain(const T& x, Eigen::Index K) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) { - return apply_vector_unary::apply( - x, [&lp, K](auto&& v) { return cov_matrix_constrain(v, K, lp); }); +inline auto cov_matrix_constrain(T&& x, Eigen::Index K, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(x), [&lp, K](auto&& v) { return cov_matrix_constrain(std::forward(v), K, lp); }); } /** @@ -151,11 +151,11 @@ inline auto cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) { */ template , Lp>* = nullptr> -inline auto cov_matrix_constrain(const T& x, Eigen::Index K, Lp& lp) { +inline auto cov_matrix_constrain(T&& x, Eigen::Index K, Lp& lp) { if constexpr (Jacobian) { - return cov_matrix_constrain(x, K, lp); + return cov_matrix_constrain(std::forward(x), K, lp); } else { - return cov_matrix_constrain(x, K); + return cov_matrix_constrain(std::forward(x), K); } } diff --git a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp index 7f30b7c11c0..825a98ddc3c 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp @@ -32,9 +32,9 @@ namespace math { */ template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cov_matrix_constrain_lkj(const T& x, size_t k) { +cov_matrix_constrain_lkj(T&& x, size_t k) { size_t k_choose_2 = (k * (k - 1)) / 2; - const auto& x_ref = to_ref(x); + auto&& x_ref = to_ref(std::forward(x)); return read_cov_matrix(corr_constrain(x_ref.head(k_choose_2)), positive_constrain(x_ref.tail(k))); } @@ -56,9 +56,9 @@ cov_matrix_constrain_lkj(const T& x, size_t k) { */ template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cov_matrix_constrain_lkj(const T& x, size_t k, return_type_t& lp) { +cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { size_t k_choose_2 = (k * (k - 1)) / 2; - const auto& x_ref = x; + auto&& x_ref = to_ref(std::forward(x)); return read_cov_matrix(corr_constrain(x_ref.head(k_choose_2)), positive_constrain(x_ref.tail(k)), lp); } @@ -84,12 +84,12 @@ cov_matrix_constrain_lkj(const T& x, size_t k, return_type_t& lp) { * correlations and deviations. */ template * = nullptr> -inline auto cov_matrix_constrain_lkj(const T& x, size_t k, +inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { if constexpr (Jacobian) { - return cov_matrix_constrain_lkj(x, k, lp); + return cov_matrix_constrain_lkj(std::forward(x), k, lp); } else { - return cov_matrix_constrain_lkj(x, k); + return cov_matrix_constrain_lkj(std::forward(x), k); } } @@ -114,10 +114,10 @@ inline auto cov_matrix_constrain_lkj(const T& x, size_t k, * correlations and deviations. */ template * = nullptr> -inline auto cov_matrix_constrain_lkj(const T& x, size_t k, +inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { - return apply_vector_unary::apply(x, [&lp, k](auto&& v) { - return cov_matrix_constrain_lkj(v, k, lp); + return apply_vector_unary>::apply(std::forward(x), [&lp, k](auto&& v) { + return cov_matrix_constrain_lkj(std::forward(v), k, lp); }); } diff --git a/stan/math/prim/constraint/cov_matrix_free.hpp b/stan/math/prim/constraint/cov_matrix_free.hpp index 4fcff40b647..04c5f388bec 100644 --- a/stan/math/prim/constraint/cov_matrix_free.hpp +++ b/stan/math/prim/constraint/cov_matrix_free.hpp @@ -35,8 +35,8 @@ namespace math { * has zero dimensionality, or has a non-positive diagonal element. */ template * = nullptr> -Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free(const T& y) { - const auto& y_ref = to_ref(y); +Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free(T&& y) { + auto&& y_ref = to_ref(std::forward(y)); check_square("cov_matrix_free", "y", y_ref); check_nonzero_size("cov_matrix_free", "y", y_ref); @@ -66,9 +66,9 @@ Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free(const T& y) { * @param x The standard vector to untransform. */ template * = nullptr> -inline auto cov_matrix_free(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return cov_matrix_free(v); }); +inline auto cov_matrix_free(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return cov_matrix_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/cov_matrix_free_lkj.hpp b/stan/math/prim/constraint/cov_matrix_free_lkj.hpp index b0d6c94f7a8..f77e05bf1c8 100644 --- a/stan/math/prim/constraint/cov_matrix_free_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_free_lkj.hpp @@ -57,9 +57,9 @@ Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free_lkj( * @param x The standard vector to untransform. */ template * = nullptr> -auto cov_matrix_free_lkj(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return cov_matrix_free_lkj(v); }); +auto cov_matrix_free_lkj(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return cov_matrix_free_lkj(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/identity_constrain.hpp b/stan/math/prim/constraint/identity_constrain.hpp index 00590438700..4d5ae105b4d 100644 --- a/stan/math/prim/constraint/identity_constrain.hpp +++ b/stan/math/prim/constraint/identity_constrain.hpp @@ -21,7 +21,7 @@ namespace math { template * = nullptr> inline auto identity_constrain(T&& x, Types&&... /* args */) { - return promote_scalar_t, T>(x); + return promote_scalar_t, T>(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/constraint/identity_free.hpp b/stan/math/prim/constraint/identity_free.hpp index ef5925c2355..2686d8fae5c 100644 --- a/stan/math/prim/constraint/identity_free.hpp +++ b/stan/math/prim/constraint/identity_free.hpp @@ -20,7 +20,7 @@ namespace math { template * = nullptr> inline auto identity_free(T&& x, Types&&... /* args */) { - return promote_scalar_t, T>(x); + return promote_scalar_t, T>(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/constraint/lb_constrain.hpp b/stan/math/prim/constraint/lb_constrain.hpp index 8f09c7733aa..f77c4056033 100644 --- a/stan/math/prim/constraint/lb_constrain.hpp +++ b/stan/math/prim/constraint/lb_constrain.hpp @@ -34,7 +34,7 @@ namespace math { */ template * = nullptr, require_all_not_st_var* = nullptr> -inline auto lb_constrain(const T& x, const L& lb) { +inline auto lb_constrain(T&& x, const L& lb) { if (unlikely(value_of_rec(lb) == NEGATIVE_INFTY)) { return identity_constrain(x, lb); } else { diff --git a/stan/math/prim/constraint/lb_free.hpp b/stan/math/prim/constraint/lb_free.hpp index f5e80d700e6..5542be2505c 100644 --- a/stan/math/prim/constraint/lb_free.hpp +++ b/stan/math/prim/constraint/lb_free.hpp @@ -29,7 +29,7 @@ template * = nullptr, require_stan_scalar_t* = nullptr> inline auto lb_free(T&& y, L&& lb) { if (value_of_rec(lb) == NEGATIVE_INFTY) { - return identity_free(y, lb); + return identity_free(std::forward(y), lb); } else { auto&& y_ref = to_ref(std::forward(y)); auto&& lb_ref = to_ref(std::forward(lb)); diff --git a/stan/math/prim/constraint/lub_free.hpp b/stan/math/prim/constraint/lub_free.hpp index 15cb46beb7e..15323e1eac2 100644 --- a/stan/math/prim/constraint/lub_free.hpp +++ b/stan/math/prim/constraint/lub_free.hpp @@ -47,11 +47,11 @@ inline auto lub_free(T&& y, L&& lb, U&& ub) { const bool is_lb_inf = value_of(lb) == NEGATIVE_INFTY; const bool is_ub_inf = value_of(ub) == INFTY; if (unlikely(is_ub_inf && is_lb_inf)) { - return identity_free(y, lb, ub); + return identity_free(std::forward(y), lb, ub); } else if (unlikely(is_ub_inf)) { - return lb_free(identity_free(y, ub), lb); + return lb_free(identity_free(std::forward(y), ub), lb); } else if (unlikely(is_lb_inf)) { - return ub_free(identity_free(y, lb), ub); + return ub_free(identity_free(std::forward(y), lb), ub); } else { auto&& y_ref = to_ref(std::forward(y)); check_bounded("lub_free", "Bounded variable", value_of(y_ref), value_of(lb), diff --git a/stan/math/prim/constraint/offset_multiplier_constrain.hpp b/stan/math/prim/constraint/offset_multiplier_constrain.hpp index 186c8f38988..ca781b76e25 100644 --- a/stan/math/prim/constraint/offset_multiplier_constrain.hpp +++ b/stan/math/prim/constraint/offset_multiplier_constrain.hpp @@ -303,12 +303,12 @@ inline auto offset_multiplier_constrain(const std::vector& x, */ template , Lp>* = nullptr> -inline auto offset_multiplier_constrain(const T& x, const M& mu, const S& sigma, +inline auto offset_multiplier_constrain(T&& x, M&& mu, S&& sigma, Lp& lp) { if constexpr (Jacobian) { - return offset_multiplier_constrain(x, mu, sigma, lp); + return offset_multiplier_constrain(std::forward(x), std::forward(mu), std::forward(sigma), lp); } else { - return offset_multiplier_constrain(x, mu, sigma); + return offset_multiplier_constrain(std::forward(x), std::forward(mu), std::forward(sigma)); } } diff --git a/stan/math/prim/constraint/offset_multiplier_free.hpp b/stan/math/prim/constraint/offset_multiplier_free.hpp index 372d5095d23..f4cb5153330 100644 --- a/stan/math/prim/constraint/offset_multiplier_free.hpp +++ b/stan/math/prim/constraint/offset_multiplier_free.hpp @@ -39,10 +39,11 @@ namespace math { * @throw std::domain_error if mu is not finite * @throw std::invalid_argument if non-scalar arguments don't match in size */ -template -inline auto offset_multiplier_free(const T& y, const M& mu, const S& sigma) { - auto&& mu_ref = to_ref(mu); - auto&& sigma_ref = to_ref(sigma); +template * = nullptr, + require_all_not_st_var* = nullptr> +inline auto offset_multiplier_free(T&& y, M&& mu, S&& sigma) { + auto&& mu_ref = to_ref(std::forward(mu)); + auto&& sigma_ref = to_ref(std::forward(sigma)); if (is_matrix::value && is_matrix::value) { check_matching_dims("offset_multiplier_constrain", "y", y, "mu", mu); } @@ -56,21 +57,21 @@ inline auto offset_multiplier_free(const T& y, const M& mu, const S& sigma) { check_finite("offset_multiplier_constrain", "offset", value_of(mu_ref)); check_positive_finite("offset_multiplier_constrain", "multiplier", value_of(sigma_ref)); - return stan::math::eval(divide(subtract(y, mu_ref), sigma_ref)); + return stan::math::eval(divide(subtract(std::forward(y), std::forward(mu_ref)), std::forward(sigma_ref))); } /** * Overload for array of x and non-array mu and sigma */ -template * = nullptr, require_all_not_std_vector_t* = nullptr> -inline auto offset_multiplier_free(const std::vector& x, const M& mu, - const S& sigma) { +inline auto offset_multiplier_free(T&& x, M&& mu, + S&& sigma) { std::vector> ret; ret.reserve(x.size()); - const auto& mu_ref = to_ref(mu); - const auto& sigma_ref = to_ref(sigma); + auto&& mu_ref = to_ref(std::forward(mu)); + auto&& sigma_ref = to_ref(std::forward(sigma)); for (size_t i = 0; i < x.size(); ++i) { ret.emplace_back(offset_multiplier_free(x[i], mu_ref, sigma_ref)); } @@ -80,10 +81,10 @@ inline auto offset_multiplier_free(const std::vector& x, const M& mu, /** * Overload for array of x and sigma and non-array mu */ -template * = nullptr, require_not_std_vector_t* = nullptr> -inline auto offset_multiplier_free(const std::vector& x, const M& mu, - const std::vector& sigma) { +inline auto offset_multiplier_free(T&& x, const M& mu, + S&& sigma) { check_matching_dims("offset_multiplier_free", "x", x, "sigma", sigma); std::vector< plain_type_t> @@ -99,10 +100,10 @@ inline auto offset_multiplier_free(const std::vector& x, const M& mu, /** * Overload for array of x and mu and non-array sigma */ -template * = nullptr, require_not_std_vector_t* = nullptr> -inline auto offset_multiplier_free(const std::vector& x, - const std::vector& mu, const S& sigma) { +inline auto offset_multiplier_free(T&& x, + M&& mu, const S& sigma) { check_matching_dims("offset_multiplier_free", "x", x, "mu", mu); std::vector< plain_type_t> @@ -118,10 +119,8 @@ inline auto offset_multiplier_free(const std::vector& x, /** * Overload for array of x, mu, and sigma */ -template -inline auto offset_multiplier_free(const std::vector& x, - const std::vector& mu, - const std::vector& sigma) { +template * = nullptr> +inline auto offset_multiplier_free(T&& x, M&& mu, S&& sigma) { check_matching_dims("offset_multiplier_free", "x", x, "mu", mu); check_matching_dims("offset_multiplier_free", "x", x, "sigma", sigma); std::vector< diff --git a/stan/math/prim/constraint/ordered_constrain.hpp b/stan/math/prim/constraint/ordered_constrain.hpp index 9fab8f2225d..4f10137a766 100644 --- a/stan/math/prim/constraint/ordered_constrain.hpp +++ b/stan/math/prim/constraint/ordered_constrain.hpp @@ -55,12 +55,12 @@ inline plain_type_t ordered_constrain(const EigVec& x) { template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto ordered_constrain(const EigVec& x, Lp& lp) { - const auto& x_ref = to_ref(x); +inline auto ordered_constrain(EigVec&& x, Lp& lp) { + auto&& x_ref = to_ref(std::forward(x)); if (likely(x.size() > 1)) { lp += sum(x_ref.tail(x.size() - 1)); } - return ordered_constrain(x_ref); + return ordered_constrain(std::forward(x_ref)); } /** @@ -76,9 +76,9 @@ inline auto ordered_constrain(const EigVec& x, Lp& lp) { * @return Positive, increasing ordered vector. */ template * = nullptr> -inline auto ordered_constrain(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return ordered_constrain(v); }); +inline auto ordered_constrain(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return ordered_constrain(std::forward(v)); }); } /** @@ -98,9 +98,9 @@ inline auto ordered_constrain(const T& x) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto ordered_constrain(const T& x, Lp& lp) { - return apply_vector_unary::apply( - x, [&lp](auto&& v) { return ordered_constrain(v, lp); }); +inline auto ordered_constrain(T&& x, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(x), [&lp](auto&& v) { return ordered_constrain(std::forward(v), lp); }); } /** @@ -125,11 +125,11 @@ inline auto ordered_constrain(const T& x, Lp& lp) { */ template , Lp>* = nullptr> -inline auto ordered_constrain(const T& x, Lp& lp) { +inline auto ordered_constrain(T&& x, Lp& lp) { if constexpr (Jacobian) { - return ordered_constrain(x, lp); + return ordered_constrain(std::forward(x), lp); } else { - return ordered_constrain(x); + return ordered_constrain(std::forward(x)); } } diff --git a/stan/math/prim/constraint/ordered_free.hpp b/stan/math/prim/constraint/ordered_free.hpp index 297bae0b062..43354174e39 100644 --- a/stan/math/prim/constraint/ordered_free.hpp +++ b/stan/math/prim/constraint/ordered_free.hpp @@ -49,9 +49,9 @@ plain_type_t ordered_free(const EigVec& y) { * @param x The standard vector to untransform. */ template * = nullptr> -inline auto ordered_free(const T& x) { - return apply_vector_unary::apply(x, - [](auto&& v) { return ordered_free(v); }); +inline auto ordered_free(T&& x) { + return apply_vector_unary>::apply(std::forward(x), + [](auto&& v) { return ordered_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/positive_constrain.hpp b/stan/math/prim/constraint/positive_constrain.hpp index cd8147ec78f..35602b449ee 100644 --- a/stan/math/prim/constraint/positive_constrain.hpp +++ b/stan/math/prim/constraint/positive_constrain.hpp @@ -20,8 +20,8 @@ namespace math { * @return Input transformed to be positive. */ template -inline auto positive_constrain(const T& x) { - return exp(x); +inline auto positive_constrain(T&& x) { + return exp(std::forward(x)); } /** @@ -41,9 +41,9 @@ inline auto positive_constrain(const T& x) { * @return positive constrained version of unconstrained value(s) */ template -inline auto positive_constrain(const T& x, S& lp) { +inline auto positive_constrain(T&& x, S& lp) { lp += sum(x); - return exp(x); + return exp(std::forward(x)); } /** @@ -66,11 +66,11 @@ inline auto positive_constrain(const T& x, S& lp) { template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto positive_constrain(const T& x, Lp& lp) { +inline auto positive_constrain(T&& x, Lp& lp) { if constexpr (Jacobian) { - return positive_constrain(x, lp); + return positive_constrain(std::forward(x), lp); } else { - return positive_constrain(x); + return positive_constrain(std::forward(x)); } } @@ -95,9 +95,9 @@ inline auto positive_constrain(const T& x, Lp& lp) { template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto positive_constrain(const T& x, Lp& lp) { - return apply_vector_unary::apply( - x, [&lp](auto&& v) { return positive_constrain(v, lp); }); +inline auto positive_constrain(T&& x, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(x), [&lp](auto&& v) { return positive_constrain(std::forward(v), lp); }); } } // namespace math diff --git a/stan/math/prim/constraint/positive_free.hpp b/stan/math/prim/constraint/positive_free.hpp index ba76d16123f..55e184c8254 100644 --- a/stan/math/prim/constraint/positive_free.hpp +++ b/stan/math/prim/constraint/positive_free.hpp @@ -26,9 +26,9 @@ namespace math { * @throw std::domain_error if the variable is negative */ template -inline plain_type_t positive_free(const T& y) { +inline plain_type_t positive_free(T&& y) { check_positive("positive_free", "Positive variable", y); - return log(y); + return log(std::forward(y)); } } // namespace math diff --git a/stan/math/prim/constraint/positive_ordered_constrain.hpp b/stan/math/prim/constraint/positive_ordered_constrain.hpp index 4d1f228814f..172bd0ed8e0 100644 --- a/stan/math/prim/constraint/positive_ordered_constrain.hpp +++ b/stan/math/prim/constraint/positive_ordered_constrain.hpp @@ -52,11 +52,10 @@ inline auto positive_ordered_constrain(const EigVec& x) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> - -inline auto positive_ordered_constrain(const Vec& x, Lp& lp) { - const auto& x_ref = to_ref(x); +inline auto positive_ordered_constrain(Vec&& x, Lp& lp) { + auto&& x_ref = to_ref(std::forward(x)); lp += sum(x_ref); - return positive_ordered_constrain(x_ref); + return positive_ordered_constrain(std::forward(x_ref)); } /** @@ -71,9 +70,9 @@ inline auto positive_ordered_constrain(const Vec& x, Lp& lp) { * @return Positive, increasing ordered vector */ template * = nullptr> -inline auto positive_ordered_constrain(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return positive_ordered_constrain(v); }); +inline auto positive_ordered_constrain(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return positive_ordered_constrain(std::forward(v)); }); } /** @@ -92,9 +91,9 @@ inline auto positive_ordered_constrain(const T& x) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto positive_ordered_constrain(const T& x, Lp& lp) { - return apply_vector_unary::apply( - x, [&lp](auto&& v) { return positive_ordered_constrain(v, lp); }); +inline auto positive_ordered_constrain(T&& x, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(x), [&lp](auto&& v) { return positive_ordered_constrain(std::forward(v), lp); }); } /** @@ -118,11 +117,11 @@ inline auto positive_ordered_constrain(const T& x, Lp& lp) { */ template , Lp>* = nullptr> -inline auto positive_ordered_constrain(const Vec& x, Lp& lp) { +inline auto positive_ordered_constrain(Vec&& x, Lp& lp) { if constexpr (Jacobian) { - return positive_ordered_constrain(x, lp); + return positive_ordered_constrain(std::forward(x), lp); } else { - return positive_ordered_constrain(x); + return positive_ordered_constrain(std::forward(x)); } } diff --git a/stan/math/prim/constraint/positive_ordered_free.hpp b/stan/math/prim/constraint/positive_ordered_free.hpp index a28052ec89e..b3adacf604f 100644 --- a/stan/math/prim/constraint/positive_ordered_free.hpp +++ b/stan/math/prim/constraint/positive_ordered_free.hpp @@ -49,9 +49,9 @@ inline auto positive_ordered_free(const EigVec& y) { * @param x The standard vector to untransform. */ template * = nullptr> -inline auto positive_ordered_free(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return positive_ordered_free(v); }); +inline auto positive_ordered_free(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return positive_ordered_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/prob_constrain.hpp b/stan/math/prim/constraint/prob_constrain.hpp index d05193c8648..7bc61dd7a87 100644 --- a/stan/math/prim/constraint/prob_constrain.hpp +++ b/stan/math/prim/constraint/prob_constrain.hpp @@ -23,8 +23,8 @@ namespace math { * @return result constrained to fall in (0, 1) */ template -inline T prob_constrain(const T& x) { - return inv_logit(x); +inline auto prob_constrain(T&& x) { + return inv_logit(std::forward(x)); } /** @@ -48,10 +48,10 @@ inline T prob_constrain(const T& x) { * @return result constrained to fall in (0, 1) */ template -inline T prob_constrain(const T& x, T& lp) { - T log_inv_logit_x = log_inv_logit(x); - lp += log_inv_logit_x + log1m_inv_logit(x); - return exp(log_inv_logit_x); +inline auto prob_constrain(T&& x, return_type_t& lp) { + plain_type_t log_inv_logit_x = log_inv_logit(x); + lp += log_inv_logit_x + log1m_inv_logit(std::forward(x)); + return exp(std::move(log_inv_logit_x)); } /** @@ -69,11 +69,11 @@ inline T prob_constrain(const T& x, T& lp) { * @return result constrained to fall in (0, 1) */ template -inline auto prob_constrain(const T& x, T& lp) { - if (Jacobian) { - return prob_constrain(x, lp); +inline auto prob_constrain(T&& x, return_type_t& lp) { + if constexpr (Jacobian) { + return prob_constrain(std::forward(x), lp); } else { - return prob_constrain(x); + return prob_constrain(std::forward(x)); } } } // namespace math diff --git a/stan/math/prim/constraint/prob_free.hpp b/stan/math/prim/constraint/prob_free.hpp index 0f980ab87cd..adb2b5d6c61 100644 --- a/stan/math/prim/constraint/prob_free.hpp +++ b/stan/math/prim/constraint/prob_free.hpp @@ -24,10 +24,10 @@ namespace math { * @throw std::domain_error if y is not in (0, 1) */ template -inline T prob_free(const T& y) { +inline auto prob_free(T&& y) { check_bounded("prob_free", "Probability variable", y, 0, 1); - return logit(y); + return logit(std::forward(y)); } } // namespace math diff --git a/stan/math/prim/constraint/simplex_constrain.hpp b/stan/math/prim/constraint/simplex_constrain.hpp index cdae665c1e8..0bcdd1afda9 100644 --- a/stan/math/prim/constraint/simplex_constrain.hpp +++ b/stan/math/prim/constraint/simplex_constrain.hpp @@ -145,9 +145,9 @@ inline plain_type_t simplex_constrain(const Vec& y, Lp& lp) { * @return simplex of dimensionality one greater than `y` */ template * = nullptr> -inline auto simplex_constrain(const T& y) { - return apply_vector_unary::apply( - y, [](auto&& v) { return simplex_constrain(v); }); +inline auto simplex_constrain(T&& y) { + return apply_vector_unary>::apply( + std::forward(y), [](auto&& v) { return simplex_constrain(std::forward(v)); }); } /** @@ -165,9 +165,9 @@ inline auto simplex_constrain(const T& y) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto simplex_constrain(const T& y, Lp& lp) { - return apply_vector_unary::apply( - y, [&lp](auto&& v) { return simplex_constrain(v, lp); }); +inline auto simplex_constrain(T&& y, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(y), [&lp](auto&& v) { return simplex_constrain(std::forward(v), lp); }); } /** @@ -190,11 +190,11 @@ inline auto simplex_constrain(const T& y, Lp& lp) { */ template , Lp>* = nullptr> -inline plain_type_t simplex_constrain(const Vec& y, Lp& lp) { +inline plain_type_t simplex_constrain(Vec&& y, Lp& lp) { if constexpr (Jacobian) { - return simplex_constrain(y, lp); + return simplex_constrain(std::forward(y), lp); } else { - return simplex_constrain(y); + return simplex_constrain(std::forward(y)); } } diff --git a/stan/math/prim/constraint/simplex_free.hpp b/stan/math/prim/constraint/simplex_free.hpp index 4c4a27c33bd..3b1d242b5cd 100644 --- a/stan/math/prim/constraint/simplex_free.hpp +++ b/stan/math/prim/constraint/simplex_free.hpp @@ -53,9 +53,9 @@ inline plain_type_t simplex_free(const Vec& x) { * @param x The standard vector to untransform. */ template * = nullptr> -inline auto simplex_free(const T& x) { - return apply_vector_unary::apply(x, - [](auto&& v) { return simplex_free(v); }); +inline auto simplex_free(T&& x) { + return apply_vector_unary>::apply(std::forward(x), + [](auto&& v) { return simplex_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/stochastic_column_constrain.hpp b/stan/math/prim/constraint/stochastic_column_constrain.hpp index 58ef35ea0f6..84a442834b2 100644 --- a/stan/math/prim/constraint/stochastic_column_constrain.hpp +++ b/stan/math/prim/constraint/stochastic_column_constrain.hpp @@ -21,8 +21,8 @@ namespace math { */ template * = nullptr, require_not_st_var* = nullptr> -inline plain_type_t stochastic_column_constrain(const Mat& y) { - auto&& y_ref = to_ref(y); +inline plain_type_t stochastic_column_constrain(Mat&& y) { + auto&& y_ref = to_ref(std::forward(y)); const Eigen::Index M = y_ref.cols(); plain_type_t ret(y_ref.rows() + 1, M); for (Eigen::Index i = 0; i < M; ++i) { @@ -71,9 +71,9 @@ inline plain_type_t stochastic_column_constrain(const Mat& y, Lp& lp) { * dimensionality (K, M). */ template * = nullptr> -inline auto stochastic_column_constrain(const T& y) { - return apply_vector_unary::apply( - y, [](auto&& v) { return stochastic_column_constrain(v); }); +inline auto stochastic_column_constrain(T&& y) { + return apply_vector_unary>::apply( + std::forward(y), [](auto&& v) { return stochastic_column_constrain(std::forward(v)); }); } /** @@ -92,9 +92,9 @@ inline auto stochastic_column_constrain(const T& y) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto stochastic_column_constrain(const T& y, Lp& lp) { - return apply_vector_unary::apply( - y, [&lp](auto&& v) { return stochastic_column_constrain(v, lp); }); +inline auto stochastic_column_constrain(T&& y, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(y), [&lp](auto&& v) { return stochastic_column_constrain(std::forward(v), lp); }); } /** @@ -117,11 +117,11 @@ inline auto stochastic_column_constrain(const T& y, Lp& lp) { */ template , Lp>* = nullptr> -inline plain_type_t stochastic_column_constrain(const Mat& y, Lp& lp) { +inline plain_type_t stochastic_column_constrain(Mat&& y, Lp& lp) { if constexpr (Jacobian) { - return stochastic_column_constrain(y, lp); + return stochastic_column_constrain(std::forward(y), lp); } else { - return stochastic_column_constrain(y); + return stochastic_column_constrain(std::forward(y)); } } diff --git a/stan/math/prim/constraint/stochastic_column_free.hpp b/stan/math/prim/constraint/stochastic_column_free.hpp index 988a579d2d3..f7e64aacd77 100644 --- a/stan/math/prim/constraint/stochastic_column_free.hpp +++ b/stan/math/prim/constraint/stochastic_column_free.hpp @@ -18,8 +18,8 @@ namespace math { * @param y Columnwise stochastic matrix input of dimensionality (N, K) */ template * = nullptr> -inline plain_type_t stochastic_column_free(const Mat& y) { - auto&& y_ref = to_ref(y); +inline plain_type_t stochastic_column_free(Mat&& y) { + auto&& y_ref = to_ref(std::forward(y)); const Eigen::Index M = y_ref.cols(); plain_type_t ret(y_ref.rows() - 1, M); for (Eigen::Index i = 0; i < M; ++i) { @@ -36,9 +36,9 @@ inline plain_type_t stochastic_column_free(const Mat& y) { * @param[in] y vector of columnwise stochastic matrix of size (N, K) */ template * = nullptr> -inline auto stochastic_column_free(const T& y) { - return apply_vector_unary::apply( - y, [](auto&& v) { return stochastic_column_free(v); }); +inline auto stochastic_column_free(T&& y) { + return apply_vector_unary>::apply( + std::forward(y), [](auto&& v) { return stochastic_column_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/stochastic_row_constrain.hpp b/stan/math/prim/constraint/stochastic_row_constrain.hpp index 290f3d3b72f..e14a23121d9 100644 --- a/stan/math/prim/constraint/stochastic_row_constrain.hpp +++ b/stan/math/prim/constraint/stochastic_row_constrain.hpp @@ -21,8 +21,8 @@ namespace math { */ template * = nullptr, require_not_st_var* = nullptr> -inline plain_type_t stochastic_row_constrain(const Mat& y) { - auto&& y_ref = to_ref(y); +inline plain_type_t stochastic_row_constrain(Mat&& y) { + auto&& y_ref = to_ref(std::forward(y)); const Eigen::Index N = y_ref.rows(); plain_type_t ret(N, y_ref.cols() + 1); for (Eigen::Index i = 0; i < N; ++i) { @@ -47,8 +47,8 @@ template * = nullptr, require_not_st_var* = nullptr, require_convertible_t, Lp>* = nullptr> -inline plain_type_t stochastic_row_constrain(const Mat& y, Lp& lp) { - auto&& y_ref = to_ref(y); +inline plain_type_t stochastic_row_constrain(Mat&& y, Lp& lp) { + auto&& y_ref = to_ref(std::forward(y)); const Eigen::Index N = y_ref.rows(); plain_type_t ret(N, y_ref.cols() + 1); for (Eigen::Index i = 0; i < N; ++i) { @@ -68,9 +68,9 @@ inline plain_type_t stochastic_row_constrain(const Mat& y, Lp& lp) { * @return vector of matrices with simplex rows of dimensionality (N, K) */ template * = nullptr> -inline auto stochastic_row_constrain(const T& y) { - return apply_vector_unary::apply( - y, [](auto&& v) { return stochastic_row_constrain(v); }); +inline auto stochastic_row_constrain(T&& y) { + return apply_vector_unary>::apply( + std::forward(y), [](auto&& v) { return stochastic_row_constrain(std::forward(v)); }); } /** @@ -88,9 +88,9 @@ inline auto stochastic_row_constrain(const T& y) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto stochastic_row_constrain(const T& y, Lp& lp) { - return apply_vector_unary::apply( - y, [&lp](auto&& v) { return stochastic_row_constrain(v, lp); }); +inline auto stochastic_row_constrain(T&& y, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(y), [&lp](auto&& v) { return stochastic_row_constrain(std::forward(v), lp); }); } /** @@ -113,11 +113,11 @@ inline auto stochastic_row_constrain(const T& y, Lp& lp) { */ template , Lp>* = nullptr> -inline plain_type_t stochastic_row_constrain(const Mat& y, Lp& lp) { +inline plain_type_t stochastic_row_constrain(Mat&& y, Lp& lp) { if constexpr (Jacobian) { - return stochastic_row_constrain(y, lp); + return stochastic_row_constrain(std::forward(y), lp); } else { - return stochastic_row_constrain(y); + return stochastic_row_constrain(std::forward(y)); } } diff --git a/stan/math/prim/constraint/stochastic_row_free.hpp b/stan/math/prim/constraint/stochastic_row_free.hpp index 069db772d66..c9122aba00f 100644 --- a/stan/math/prim/constraint/stochastic_row_free.hpp +++ b/stan/math/prim/constraint/stochastic_row_free.hpp @@ -17,8 +17,8 @@ namespace math { * @param y Rowwise simplex Matrix input of dimensionality (N, K) */ template * = nullptr> -inline plain_type_t stochastic_row_free(const Mat& y) { - auto&& y_ref = to_ref(y); +inline plain_type_t stochastic_row_free(Mat&& y) { + auto&& y_ref = to_ref(std::forward(y)); const Eigen::Index N = y_ref.rows(); plain_type_t ret(N, y_ref.cols() - 1); for (Eigen::Index i = 0; i < N; ++i) { @@ -35,9 +35,9 @@ inline plain_type_t stochastic_row_free(const Mat& y) { * @param[in] y vector of rowwise simplex matrices each of size (N, K) */ template * = nullptr> -inline auto stochastic_row_free(const T& y) { - return apply_vector_unary::apply( - y, [](auto&& v) { return stochastic_row_free(v); }); +inline auto stochastic_row_free(T&& y) { + return apply_vector_unary>::apply( + std::forward(y), [](auto&& v) { return stochastic_row_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/sum_to_zero_constrain.hpp b/stan/math/prim/constraint/sum_to_zero_constrain.hpp index 19f330c5692..8b1c4798d91 100644 --- a/stan/math/prim/constraint/sum_to_zero_constrain.hpp +++ b/stan/math/prim/constraint/sum_to_zero_constrain.hpp @@ -122,9 +122,9 @@ inline plain_type_t sum_to_zero_constrain(const Mat& x) { * @param lp unused * @return Zero-sum vector or matrix which is one larger in each dimension */ -template * = nullptr> -inline plain_type_t sum_to_zero_constrain(const T& y, Lp& lp) { - return sum_to_zero_constrain(y); +template * = nullptr> +inline plain_type_t sum_to_zero_constrain(T&& y, Lp& lp) { + return sum_to_zero_constrain(std::forward(y)); } /** @@ -138,9 +138,9 @@ inline plain_type_t sum_to_zero_constrain(const T& y, Lp& lp) { * @return Zero-sum vectors or matrices which are one larger in each dimension */ template * = nullptr> -inline auto sum_to_zero_constrain(const T& y) { - return apply_vector_unary::apply( - y, [](auto&& v) { return sum_to_zero_constrain(v); }); +inline auto sum_to_zero_constrain(T&& y) { + return apply_vector_unary>::apply( + std::forward(y), [](auto&& v) { return sum_to_zero_constrain(std::forward(v)); }); } /** @@ -157,9 +157,9 @@ inline auto sum_to_zero_constrain(const T& y) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto sum_to_zero_constrain(const T& y, Lp& lp) { - return apply_vector_unary::apply( - y, [](auto&& v) { return sum_to_zero_constrain(v); }); +inline auto sum_to_zero_constrain(T&& y, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(y), [&lp](auto&& v) { return sum_to_zero_constrain(std::forward(v), lp); }); } /** @@ -175,8 +175,8 @@ inline auto sum_to_zero_constrain(const T& y, Lp& lp) { * @return Zero-sum vector or matrix which is one larger in each dimension */ template -inline plain_type_t sum_to_zero_constrain(const T& y, Lp& lp) { - return sum_to_zero_constrain(y); +inline plain_type_t sum_to_zero_constrain(T&& y, Lp& lp) { + return sum_to_zero_constrain(std::forward(y), lp); } } // namespace math diff --git a/stan/math/prim/constraint/sum_to_zero_free.hpp b/stan/math/prim/constraint/sum_to_zero_free.hpp index 4acbbdfacdd..936a6f37204 100644 --- a/stan/math/prim/constraint/sum_to_zero_free.hpp +++ b/stan/math/prim/constraint/sum_to_zero_free.hpp @@ -115,9 +115,9 @@ inline plain_type_t sum_to_zero_free(const Mat& z) { * @param z The standard vector to untransform. */ template * = nullptr> -inline auto sum_to_zero_free(const T& z) { - return apply_vector_unary::apply( - z, [](auto&& v) { return sum_to_zero_free(v); }); +inline auto sum_to_zero_free(T&& z) { + return apply_vector_unary>::apply( + std::forward(z), [](auto&& v) { return sum_to_zero_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/ub_constrain.hpp b/stan/math/prim/constraint/ub_constrain.hpp index fe836471456..749104b164f 100644 --- a/stan/math/prim/constraint/ub_constrain.hpp +++ b/stan/math/prim/constraint/ub_constrain.hpp @@ -88,8 +88,8 @@ inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { template * = nullptr, require_stan_scalar_t* = nullptr, require_all_not_st_var* = nullptr> -inline auto ub_constrain(const T& x, const U& ub) { - return eval(x.unaryExpr([ub](auto&& xx) { return ub_constrain(xx, ub); })); +inline auto ub_constrain(T&& x, const U& ub) { + return eval(std::forward(x).unaryExpr([ub](auto&& xx) { return ub_constrain(xx, ub); })); } /** @@ -108,9 +108,9 @@ template * = nullptr, require_stan_scalar_t* = nullptr, require_all_not_st_var* = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { +inline auto ub_constrain(T&& x, const U& ub, Lp& lp) { return eval( - x.unaryExpr([ub, &lp](auto&& xx) { return ub_constrain(xx, ub, lp); })); + std::forward(x).unaryExpr([ub, &lp](auto&& xx) { return ub_constrain(xx, ub, lp); })); } /** @@ -125,9 +125,9 @@ inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { */ template * = nullptr, require_all_not_st_var* = nullptr> -inline auto ub_constrain(const T& x, const U& ub) { +inline auto ub_constrain(T&& x, const U& ub) { check_matching_dims("ub_constrain", "x", x, "ub", ub); - return eval(x.binaryExpr( + return eval(std::forward(x).binaryExpr( ub, [](auto&& xx, auto&& ubb) { return ub_constrain(xx, ubb); })); } @@ -147,9 +147,9 @@ template * = nullptr, require_all_not_st_var* = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { +inline auto ub_constrain(T&& x, const U& ub, Lp& lp) { check_matching_dims("ub_constrain", "x", x, "ub", ub); - return eval(x.binaryExpr( + return eval(std::forward(x).binaryExpr( ub, [&lp](auto&& xx, auto&& ubb) { return ub_constrain(xx, ubb, lp); })); } @@ -261,11 +261,11 @@ inline auto ub_constrain(const std::vector& x, const std::vector& ub, */ template , Lp>* = nullptr> -inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { +inline auto ub_constrain(T&& x, U&& ub, Lp& lp) { if constexpr (Jacobian) { - return ub_constrain(x, ub, lp); + return ub_constrain(std::forward(x), std::forward(ub), lp); } else { - return ub_constrain(x, ub); + return ub_constrain(std::forward(x), std::forward(ub)); } } diff --git a/stan/math/prim/constraint/unit_vector_constrain.hpp b/stan/math/prim/constraint/unit_vector_constrain.hpp index ae72f6277a1..5a18366200c 100644 --- a/stan/math/prim/constraint/unit_vector_constrain.hpp +++ b/stan/math/prim/constraint/unit_vector_constrain.hpp @@ -66,9 +66,9 @@ inline plain_type_t unit_vector_constrain(const T1& y, T2& lp) { * @return Unit length vector of dimension K */ template * = nullptr> -inline auto unit_vector_constrain(const T& y) { - return apply_vector_unary::apply( - y, [](auto&& v) { return unit_vector_constrain(v); }); +inline auto unit_vector_constrain(T&& y) { + return apply_vector_unary>::apply( + std::forward(y), [](auto&& v) { return unit_vector_constrain(std::forward(v)); }); } /** @@ -86,9 +86,9 @@ inline auto unit_vector_constrain(const T& y) { */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto unit_vector_constrain(const T& y, Lp& lp) { - return apply_vector_unary::apply( - y, [&lp](auto&& v) { return unit_vector_constrain(v, lp); }); +inline auto unit_vector_constrain(T&& y, Lp& lp) { + return apply_vector_unary>::apply( + std::forward(y), [&lp](auto&& v) { return unit_vector_constrain(std::forward(v), lp); }); } /** @@ -111,11 +111,11 @@ inline auto unit_vector_constrain(const T& y, Lp& lp) { */ template , Lp>* = nullptr> -inline auto unit_vector_constrain(const T& y, Lp& lp) { +inline auto unit_vector_constrain(T&& y, Lp& lp) { if constexpr (Jacobian) { - return unit_vector_constrain(y, lp); + return unit_vector_constrain(std::forward(y), lp); } else { - return unit_vector_constrain(y); + return unit_vector_constrain(std::forward(y)); } } diff --git a/stan/math/prim/constraint/unit_vector_free.hpp b/stan/math/prim/constraint/unit_vector_free.hpp index c0debd3429e..a1c5d52e01c 100644 --- a/stan/math/prim/constraint/unit_vector_free.hpp +++ b/stan/math/prim/constraint/unit_vector_free.hpp @@ -36,9 +36,9 @@ inline auto unit_vector_free(EigVec&& x) { * @param x The standard vector to untransform. */ template * = nullptr> -inline auto unit_vector_free(const T& x) { - return apply_vector_unary::apply( - x, [](auto&& v) { return unit_vector_free(v); }); +inline auto unit_vector_free(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return unit_vector_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/fun/abs.hpp b/stan/math/prim/fun/abs.hpp index 7ef05d9d23f..5d7571167be 100644 --- a/stan/math/prim/fun/abs.hpp +++ b/stan/math/prim/fun/abs.hpp @@ -81,7 +81,7 @@ template >::apply( std::forward(x), - [&](const auto& v) { + [&](auto&& v) { return std::forward(v).array().abs(); }); } diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index 8d35f36c994..1b22d4299bf 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -83,7 +83,7 @@ template >::apply( std::forward(x), - [](const auto& v) { + [](auto&& v) { return std::forward(v).array().acos(); }); } diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index 287cddf467b..8dfd7aca987 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -81,7 +81,7 @@ template >::apply( std::forward(x), - [](const auto& v) { + [](auto&& v) { return std::forward(v).array().asin(); }); } diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index ac505f0cd2b..08b2da5cb78 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -79,7 +79,7 @@ template >::apply( std::forward(x), - [](const auto& v) { + [](auto&& v) { return std::forward(v).array().atan(); }); } diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index fc25d99955a..ac9a59e8541 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -56,7 +56,7 @@ template >::apply( std::forward(x), - [](const auto& v) { + [](auto&& v) { return std::forward(v).array().ceil(); }); } diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index 5f111a9c7b0..8bdeffd12c3 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -78,7 +78,7 @@ template >::apply( std::forward(x), - [&](const auto& v) { + [&](auto&& v) { return std::forward(v).array().cos(); }); } diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index 96734fe39d3..dda708c9f9e 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -76,7 +76,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().cosh(); }); + [](auto&& v) { return std::forward(v).array().cosh(); }); } namespace internal { diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 1524bfca246..51e37f9e559 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -86,7 +86,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().exp(); }); + [](auto&& v) { return std::forward(v).array().exp(); }); } namespace internal { diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 4b9e5651e4f..9db7205c1b6 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -65,7 +65,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().abs(); }); + [](auto&& v) { return std::forward(v).array().abs(); }); } } // namespace math diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index 8f5b1a952c2..11529d21df6 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -60,7 +60,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().floor(); }); + [](auto&& v) { return std::forward(v).array().floor(); }); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index c798ea20b1c..1c05385df37 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -53,7 +53,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().inverse(); }); + [](auto&& v) { return std::forward(v).array().inverse(); }); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index 05d057f0386..cb31db2e6c2 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -108,7 +108,7 @@ inline auto inv_cloglog(Container&& x) { return apply_vector_unary>::apply( std::forward(x), [](const auto& v) { - return 1 - (-std::forward(v).array().exp()).exp(); + return (1 - (-std::forward(v).array().exp()).exp()); }); } diff --git a/stan/math/prim/fun/inv_logit.hpp b/stan/math/prim/fun/inv_logit.hpp index 08f2a940a7f..1db746d7f8a 100644 --- a/stan/math/prim/fun/inv_logit.hpp +++ b/stan/math/prim/fun/inv_logit.hpp @@ -106,7 +106,7 @@ template ::apply( std::forward(x), - [](const auto& v) { return v.array().logistic(); }); + [](auto&& v) { return std::forward(v).array().logistic(); }); } } // namespace math } // namespace stan diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index 80fc7431880..bd8cc17904f 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -68,7 +68,7 @@ inline auto inv_sqrt(Container&& x) { #else return apply_vector_unary>::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().rsqrt(); }); + [](auto&& v) { return std::forward(v).array().rsqrt(); }); #endif } diff --git a/stan/math/prim/fun/inv_square.hpp b/stan/math/prim/fun/inv_square.hpp index 074afbde47d..96ecdb3b877 100644 --- a/stan/math/prim/fun/inv_square.hpp +++ b/stan/math/prim/fun/inv_square.hpp @@ -37,7 +37,7 @@ template >::apply( std::forward(x), - [](const auto& v) { + [](auto&& v) { return std::forward(v).array().square().inverse(); }); } diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index 1402ecd3fea..45621a07594 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -85,7 +85,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().log(); }); + [](auto&& v) { return std::forward(v).array().log(); }); } namespace internal { diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index 59e07b9cf30..fecc2368759 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -75,7 +75,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().log10(); }); + [](auto&& v) { return std::forward(v).array().log10(); }); } namespace internal { diff --git a/stan/math/prim/fun/log1m_inv_logit.hpp b/stan/math/prim/fun/log1m_inv_logit.hpp index d6717a431af..3554fe33f7d 100644 --- a/stan/math/prim/fun/log1m_inv_logit.hpp +++ b/stan/math/prim/fun/log1m_inv_logit.hpp @@ -65,8 +65,8 @@ struct log1m_inv_logit_fun { * @return natural log of one minus inverse logit of argument */ template - static inline auto fun(const T& x) { - return log1m_inv_logit(x); + static inline auto fun(T&& x) { + return log1m_inv_logit(std::forward(x)); } }; @@ -82,8 +82,7 @@ struct log1m_inv_logit_fun { */ template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline typename apply_scalar_unary>::return_t -log1m_inv_logit(T&& x) { +inline auto log1m_inv_logit(T&& x) { return apply_scalar_unary>::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/max.hpp b/stan/math/prim/fun/max.hpp index 7ca81daa8f6..0fa8ec8419f 100644 --- a/stan/math/prim/fun/max.hpp +++ b/stan/math/prim/fun/max.hpp @@ -39,14 +39,14 @@ auto max(T1 x, T2 y) { * scalar type in the container is integer */ template * = nullptr> -inline value_type_t max(const T& m) { +inline value_type_t max(T&& m) { if (std::is_integral>::value) { check_nonzero_size("max", "int vector", m); } else if (m.size() == 0) { return NEGATIVE_INFTY; } - return apply_vector_unary::reduce( - m, [](const auto& x) { return x.maxCoeff(); }); + return apply_vector_unary>::reduce( + std::forward(m), [](const auto& x) { return x.maxCoeff(); }); } } // namespace math diff --git a/stan/math/prim/fun/mean.hpp b/stan/math/prim/fun/mean.hpp index aff32358d41..d9568fce905 100644 --- a/stan/math/prim/fun/mean.hpp +++ b/stan/math/prim/fun/mean.hpp @@ -19,9 +19,9 @@ namespace math { * @return Sample mean of container coefficients. */ template * = nullptr> -inline return_type_t mean(const T& m) { +inline return_type_t mean(T&& m) { check_nonzero_size("mean", "m", m); - return apply_vector_unary::reduce(m, + return apply_vector_unary>::reduce(std::forward(m), [](const auto& a) { return a.mean(); }); } diff --git a/stan/math/prim/fun/min.hpp b/stan/math/prim/fun/min.hpp index 9d84a38ac93..ae1e14b44f5 100644 --- a/stan/math/prim/fun/min.hpp +++ b/stan/math/prim/fun/min.hpp @@ -38,14 +38,14 @@ auto min(T1 x, T2 y) { * scalar type in the container is integer */ template * = nullptr> -inline value_type_t min(const T& m) { +inline value_type_t min(T&& m) { if (std::is_integral>::value) { check_nonzero_size("min", "int vector", m); } else if (m.size() == 0) { return INFTY; } - return apply_vector_unary::reduce( - m, [](const auto& x) { return x.minCoeff(); }); + return apply_vector_unary>::reduce( + std::forward(m), [](const auto& x) { return x.minCoeff(); }); } } // namespace math diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index 02e8ddd5bc3..4feae5426a2 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -15,8 +15,14 @@ namespace math { * @return Negation of subtrahend. */ template * = nullptr> -inline auto minus(const T& x) { - return -x; +inline auto minus(T&& x) { + if constexpr (is_eigen_v) { + return make_holder([](auto&& xx) { + return -std::forward(xx); + }, std::forward(x)); + } else { + return -x; + } } /** @@ -26,13 +32,12 @@ inline auto minus(const T& x) { * @param x Container. * @return Container where each element is negated. */ -template -inline auto minus(std::vector&& x) { - return apply_vector_unary>::apply( - std::forward>(x), +template * = nullptr> +inline auto minus(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](auto&& v) { return -std::forward(v); }); } - } // namespace math } // namespace stan diff --git a/stan/math/prim/fun/pow.hpp b/stan/math/prim/fun/pow.hpp index d0ea2470718..46d60cb00ef 100644 --- a/stan/math/prim/fun/pow.hpp +++ b/stan/math/prim/fun/pow.hpp @@ -111,7 +111,7 @@ template * = nullptr, inline auto pow(T1&& a, T2&& b) { return apply_scalar_binary( // Qualified pow since only Arithmetic types are accepted here - [](const auto& c, const auto& d) { return stan::math::pow(c, d); }, + [](auto&& c, auto&& d) { return stan::math::pow(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/rank.hpp b/stan/math/prim/fun/rank.hpp index 9378a5c120b..f3357d0424b 100644 --- a/stan/math/prim/fun/rank.hpp +++ b/stan/math/prim/fun/rank.hpp @@ -17,12 +17,11 @@ namespace math { * @throw std::out_of_range if s is out of range. */ template * = nullptr> -inline int rank(const C& v, int s) { +inline int rank(C&& v, int s) { check_range("rank", "v", v.size(), s); --s; // adjust for indexing by one - return apply_vector_unary::reduce(v, [s](const auto& vec) { - const auto& vec_ref = to_ref(vec); - + return apply_vector_unary>::reduce(std::forward(v), [s](const auto& vec) { + auto&& vec_ref = to_ref(std::forward(vec)); return (vec_ref.array() < vec_ref.coeff(s)).template cast().sum(); }); } diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index 1b196cceee3..c91673aea36 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -53,7 +53,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().round(); }); + [](auto&& v) { return std::forward(v).array().round(); }); } } // namespace math diff --git a/stan/math/prim/fun/select.hpp b/stan/math/prim/fun/select.hpp index 7968d65a12b..0b551a1da6e 100644 --- a/stan/math/prim/fun/select.hpp +++ b/stan/math/prim/fun/select.hpp @@ -116,13 +116,13 @@ template * = nullptr, require_container_t* = nullptr> inline ReturnT select(const bool c, const T_true y_true, - const T_false y_false) { + T_false&& y_false) { if (c) { return apply_scalar_binary( [](auto&& y_true_inner, auto&& y_false_inner) { return std::forward(y_true_inner); }, - std::forward(y_true), std::forward(y_false)); + T_true(y_true), std::forward(y_false)); } else { return y_false; } @@ -146,9 +146,9 @@ inline ReturnT select(const bool c, const T_true y_true, template * = nullptr, require_all_stan_scalar_t* = nullptr> -inline auto select(const T_bool c, const T_true y_true, const T_false y_false) { +inline auto select(T_bool&& c, const T_true y_true, const T_false y_false) { using ret_t = return_type_t; - return c + return std::forward(c) .unaryExpr([y_true, y_false](bool cond) { return cond ? ret_t(y_true) : ret_t(y_false); }) @@ -170,11 +170,11 @@ inline auto select(const T_bool c, const T_true y_true, const T_false y_false) { template * = nullptr, require_any_eigen_array_t* = nullptr> -inline auto select(const T_bool c, const T_true y_true, const T_false y_false) { +inline auto select(T_bool&& c, T_true&& y_true, T_false&& y_false) { check_consistent_sizes("select", "boolean", c, "left hand side", y_true, "right hand side", y_false); using ret_t = return_type_t; - return c.select(y_true, y_false).template cast().eval(); + return std::forward(c).select(std::forward(y_true), std::forward(y_false)).template cast().eval(); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index 0a1678a38bf..f9b05bdc91d 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -77,7 +77,7 @@ template >::apply( std::forward(x), - [&](const auto& v) { return std::forward(v).array().sin(); }); + [&](auto&& v) { return std::forward(v).array().sin(); }); } namespace internal { diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index dd34a48145a..c5bcb3c2f09 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -74,7 +74,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().sinh(); }); + [](auto&& v) { return std::forward(v).array().sinh(); }); } namespace internal { diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index d70307bbed4..b62a9f18aad 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -75,7 +75,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().sqrt(); }); + [](auto&& v) { return std::forward(v).array().sqrt(); }); } namespace internal { diff --git a/stan/math/prim/fun/square.hpp b/stan/math/prim/fun/square.hpp index 7462934ef65..a958b83b18a 100644 --- a/stan/math/prim/fun/square.hpp +++ b/stan/math/prim/fun/square.hpp @@ -67,7 +67,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().square(); }); + [](auto&& v) { return std::forward(v).array().square(); }); } } // namespace math diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index e11f500010b..ec0387365dd 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -77,7 +77,7 @@ template >::apply( std::forward(x), - [](const auto& v) { + [](auto&& v) { return std::forward(v).array().tan(); }); } diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index 3a26a16797a..cc5ee6b9cd3 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -76,7 +76,7 @@ template >::apply( std::forward(x), - [](const auto& v) { return std::forward(v).array().tanh(); }); + [](auto&& v) { return std::forward(v).array().tanh(); }); } namespace internal { diff --git a/stan/math/prim/functor/apply_scalar_binary.hpp b/stan/math/prim/functor/apply_scalar_binary.hpp index a896df33914..77ea8369d27 100644 --- a/stan/math/prim/functor/apply_scalar_binary.hpp +++ b/stan/math/prim/functor/apply_scalar_binary.hpp @@ -203,7 +203,7 @@ inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { return make_holder( [](auto&& f_inner, auto&& x_inner, auto&& y_inner) { return x_inner.unaryExpr( - [f_inner, y_inner](const auto& v) { return f_inner(v, y_inner); }); + [f_inner_ = std::forward(f_inner), y_inner](const auto& v) { return f_inner_(v, y_inner); }); }, std::forward(f), std::forward(x), std::forward(y)); } @@ -229,7 +229,7 @@ inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { return make_holder( [](auto&& f_inner, auto&& x_inner, auto&& y_inner) { return y_inner.unaryExpr( - [f_inner, x_inner](const auto& v) { return f_inner(x_inner, v); }); + [f_inner_ = std::forward(f_inner), x_inner](auto&& v) { return f_inner_(x_inner, std::forward(v)); }); }, std::forward(f), std::forward(x), std::forward(y)); } diff --git a/stan/math/prim/functor/apply_scalar_ternary.hpp b/stan/math/prim/functor/apply_scalar_ternary.hpp index 7876c72895d..e9dfcdab834 100644 --- a/stan/math/prim/functor/apply_scalar_ternary.hpp +++ b/stan/math/prim/functor/apply_scalar_ternary.hpp @@ -126,8 +126,7 @@ inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, template * = nullptr> -inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, - const T3& z) { +inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { check_matching_sizes("Ternary function", "x", x, "y", y); check_matching_sizes("Ternary function", "y", y, "z", z); using T_return @@ -159,10 +158,10 @@ inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, - const T3& z) { +inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, + T3&& z) { return apply_scalar_binary( - [f, z](const auto& a, const auto& b) { return f(a, b, z); }, x, y); + [f_ = std::forward(f), z](auto&& a, auto&& b) { return f_(std::forward(a), std::forward(b), z); }, std::forward(x), std::forward(y)); } /** @@ -184,10 +183,10 @@ inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, - const T3& z) { +inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, + T3&& z) { return apply_scalar_binary( - [f, y](const auto& a, const auto& c) { return f(a, y, c); }, x, z); + [f, y](auto&& a, auto&& c) { return f(std::forward(a), y, std::forward(c)); }, std::forward(x), std::forward(z)); } /** @@ -209,10 +208,10 @@ inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, - const T3& z) { +inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, + T3&& z) { return apply_scalar_binary( - [f, x](const auto& b, const auto& c) { return f(x, b, c); }, y, z); + [f, x](auto&& b, auto&& c) { return f(x, std::forward(b), std::forward(c)); }, std::forward(y), std::forward(z)); } } // namespace math diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 2cbcae179aa..2fae2be79f1 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -58,10 +58,13 @@ struct apply_scalar_unary> { * @return Componentwise application of the function specified * by F to the specified matrix. */ - static inline auto apply(const std::decay_t& x) { - return x.unaryExpr([](auto&& x) { - return apply_scalar_unary>::apply(x); - }); + template + static inline auto apply(TT&& x) { + return make_holder([](auto&& xx) { + return std::forward(xx).unaryExpr([](auto&& xxx) { + return apply_scalar_unary>::apply(xxx); + }); + }, std::forward(x)); } /** @@ -176,10 +179,15 @@ struct apply_scalar_unary> { * @return Elementwise application of F to the elements of the * container. */ - static inline auto apply(const std::decay_t& x) { + template + static inline auto apply(TT&& x) { return_t fx(x.size()); for (size_t i = 0; i < x.size(); ++i) { - fx[i] = apply_scalar_unary>::apply(x[i]); + if constexpr (std::is_rvalue_reference_v) { + fx[i] = apply_scalar_unary>::apply(std::move(x[i])); + } else { + fx[i] = apply_scalar_unary>::apply(x[i]); + } } return fx; } diff --git a/stan/math/prim/functor/apply_vector_unary.hpp b/stan/math/prim/functor/apply_vector_unary.hpp index 5787043a385..2472e77c7d7 100644 --- a/stan/math/prim/functor/apply_vector_unary.hpp +++ b/stan/math/prim/functor/apply_vector_unary.hpp @@ -42,15 +42,26 @@ struct apply_vector_unary> { */ template >>* = nullptr> - static inline auto apply(const T& x, const F& f) { - return make_holder([](const auto& a) { return a.matrix().derived(); }, - f(x)); + static inline auto apply(T2&& x, F&& f) { + if constexpr (is_eigen_array::value) { + return make_holder([](auto&& xx) { + return std::forward(xx).matrix(); + }, make_holder(std::forward(f), std::forward(x))); + } else { + return make_holder(std::forward(f), std::forward(x)); + } } template >>* = nullptr> - static inline auto apply(const T& x, const F& f) { - return make_holder([](const auto& a) { return a.array().derived(); }, f(x)); + static inline auto apply(T2&& x, F&& f) { + if constexpr (is_eigen_array::value) { + return make_holder(std::forward(f), std::forward(x)); + } else { + return make_holder([](auto&& xx) { + return std::forward(xx).array(); + }, make_holder(std::forward(f), std::forward(x))); + } } /** @@ -67,14 +78,14 @@ struct apply_vector_unary> { */ template >>* = nullptr> - static inline auto apply_no_holder(const T& x, const F& f) { - return f(x).matrix().derived(); + static inline auto apply_no_holder(T2& x, F&& f) { + return std::forward(f)(std::forward(x)); } template >>* = nullptr> - static inline auto apply_no_holder(const T& x, const F& f) { - return f(x).array().derived(); + static inline auto apply_no_holder(T2&& x, F&& f) { + return std::forward(f)(std::forward(x)); } /** @@ -88,9 +99,9 @@ struct apply_vector_unary> { * @param f functor to apply to Eigen input. * @return scalar result of applying functor to input. */ - template - static inline auto reduce(const T& x, const F& f) { - return f(x); + template + static inline auto reduce(T2&& x, F&& f) { + return make_holder(std::forward(f), std::forward(x)); } }; @@ -138,9 +149,9 @@ struct apply_vector_unary> { * @return std::vector of containers with result of applying functor to * input. */ - template - static inline auto apply_no_holder(const T& x, const F& f) { - return apply(x, f); + template + static inline auto apply_no_holder(T2&& x, F&& f) { + return apply(std::forward(x), std::forward(f)); } /** @@ -153,9 +164,9 @@ struct apply_vector_unary> { * @param f functor to apply to std::vector input. * @return scalar result of applying functor to input vector. */ - template - static inline auto reduce(const T& x, const F& f) { - return apply_vector_unary::reduce(as_column_vector_or_scalar(x), f); + template + static inline auto reduce(T2&& x, F&& f) { + return apply_vector_unary::reduce(as_column_vector_or_scalar(std::forward(x)), std::forward(f)); } }; @@ -191,7 +202,7 @@ struct apply_vector_unary< = plain_type_t::apply(x[0], f))>; std::vector result(x.size()); std::transform(x.begin(), x.end(), result.begin(), [&f](auto&& xx) { - return apply_vector_unary::apply_no_holder(xx, f); + return apply_vector_unary::apply(xx, f); }); return result; } diff --git a/stan/math/prim/prob/std_normal_log_qf.hpp b/stan/math/prim/prob/std_normal_log_qf.hpp index 895b4997a0a..62c881674d1 100644 --- a/stan/math/prim/prob/std_normal_log_qf.hpp +++ b/stan/math/prim/prob/std_normal_log_qf.hpp @@ -150,8 +150,8 @@ template < typename T, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> -inline auto std_normal_log_qf(const T& x) { - return apply_scalar_unary::apply(x); +inline auto std_normal_log_qf(T&& x) { + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/rev/fun/floor.hpp b/stan/math/rev/fun/floor.hpp index bac9ab778ed..89a5fe76e4d 100644 --- a/stan/math/rev/fun/floor.hpp +++ b/stan/math/rev/fun/floor.hpp @@ -49,7 +49,7 @@ inline var floor(const var& a) { return var(std::floor(a.val())); } template * = nullptr> inline auto floor(const var_value& a) { - return var_value(a.val().array().floor().matrix()); + return var_value(a.val().array().floor()); } } // namespace math diff --git a/stan/math/rev/fun/log_softmax.hpp b/stan/math/rev/fun/log_softmax.hpp index d41a567156b..3015bb12b3e 100644 --- a/stan/math/rev/fun/log_softmax.hpp +++ b/stan/math/rev/fun/log_softmax.hpp @@ -120,9 +120,9 @@ inline auto log_softmax(const T& x) { * @throw std::domain_error if the input size is 0 */ template * = nullptr> -inline auto log_softmax(const T& x) { - return apply_vector_unary::apply( - x, [](const auto& alpha) { return log_softmax(alpha); }); +inline auto log_softmax(T&& x) { + return apply_vector_unary>::apply( + std::forward(x), [](const auto& alpha) { return log_softmax(alpha); }); } } // namespace math diff --git a/stan/math/rev/fun/log_sum_exp.hpp b/stan/math/rev/fun/log_sum_exp.hpp index 69299560616..b61e06be30e 100644 --- a/stan/math/rev/fun/log_sum_exp.hpp +++ b/stan/math/rev/fun/log_sum_exp.hpp @@ -99,9 +99,9 @@ inline var log_sum_exp(const T& x) { * @param x matrix */ template * = nullptr> -inline auto log_sum_exp(const T& x) { - return apply_vector_unary::reduce( - x, [](const auto& v) { return log_sum_exp(v); }); +inline auto log_sum_exp(T&& x) { + return apply_vector_unary>::reduce( + std::forward(x), [](const auto& v) { return log_sum_exp(v); }); } } // namespace math diff --git a/stan/math/rev/fun/pow.hpp b/stan/math/rev/fun/pow.hpp index d525e00a04b..a72b5f04226 100644 --- a/stan/math/rev/fun/pow.hpp +++ b/stan/math/rev/fun/pow.hpp @@ -287,9 +287,9 @@ inline auto pow(Scal1 base, const Mat1& exponent) { template * = nullptr, require_all_not_matrix_st* = nullptr, require_any_var_t, base_type_t>* = nullptr> -inline auto pow(const T1& a, const T2& b) { +inline auto pow(T1&& a, T2&& b) { return apply_scalar_binary( - [](const auto& c, const auto& d) { return stan::math::pow(c, d); }, a, b); + [](auto&& c, auto&& d) { return stan::math::pow(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/rev/fun/sd.hpp b/stan/math/rev/fun/sd.hpp index e3db2d1651f..f143930802e 100644 --- a/stan/math/rev/fun/sd.hpp +++ b/stan/math/rev/fun/sd.hpp @@ -94,8 +94,8 @@ var sd(const T& x) { * @throw domain error size is not greater than zero. */ template * = nullptr> -auto sd(const T& m) { - return apply_vector_unary::reduce(m, [](const auto& x) { return sd(x); }); +auto sd(T&& m) { + return apply_vector_unary>::reduce(std::forward(m), [](const auto& x) { return sd(x); }); } } // namespace math diff --git a/stan/math/rev/functor/apply_scalar_unary.hpp b/stan/math/rev/functor/apply_scalar_unary.hpp index d68bd344335..4213559b5db 100644 --- a/stan/math/rev/functor/apply_scalar_unary.hpp +++ b/stan/math/rev/functor/apply_scalar_unary.hpp @@ -44,7 +44,8 @@ struct apply_scalar_unary> { * @param x Argument variable. * @return Function applied to the variable. */ - static inline return_t apply(const T& x) { return F::fun(x); } + template + static inline return_t apply(T2&& x) { return F::fun(std::forward(x)); } }; } // namespace math diff --git a/stan/math/rev/functor/apply_vector_unary.hpp b/stan/math/rev/functor/apply_vector_unary.hpp index 01cad885a70..8c74c3dec5e 100644 --- a/stan/math/rev/functor/apply_vector_unary.hpp +++ b/stan/math/rev/functor/apply_vector_unary.hpp @@ -24,9 +24,9 @@ struct apply_vector_unary> { * @param f functor to apply to Eigen input. * @return object with result of applying functor to input */ - template - static inline plain_type_t apply(const T& x, const F& f) { - return f(x); + template + static inline plain_type_t apply(T2&& x, F&& f) { + return std::forward(f)(std::forward(x)); } /** @@ -39,9 +39,9 @@ struct apply_vector_unary> { * @param f functor to apply to Eigen input. * @return object with result of applying functor to input */ - template - static inline plain_type_t apply_no_holder(const T& x, const F& f) { - return f(x); + template + static inline plain_type_t apply_no_holder(T2&& x, F&& f) { + return std::forward(f)(std::forward(x)); } /** @@ -55,9 +55,9 @@ struct apply_vector_unary> { * @param f functor to apply to input. * @return scalar result of applying functor to input. */ - template - static inline var reduce(const T& x, const F& f) { - return f(x); + template + static inline var reduce(T2& x, F&& f) { + return std::forward(f)(std::forward(x)); } }; From e40bd1eb54d709e95b9e31ffbd5dfa1f74a82d42 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 30 Jun 2025 17:12:21 -0400 Subject: [PATCH 03/31] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- stan/math/fwd/fun/log_softmax.hpp | 56 ++++++++++--------- stan/math/fwd/fun/pow.hpp | 6 +- .../constraint/cholesky_corr_constrain.hpp | 8 ++- .../prim/constraint/cholesky_corr_free.hpp | 4 +- .../constraint/cholesky_factor_constrain.hpp | 12 ++-- .../prim/constraint/cholesky_factor_free.hpp | 4 +- .../prim/constraint/corr_matrix_constrain.hpp | 8 ++- .../math/prim/constraint/corr_matrix_free.hpp | 3 +- .../prim/constraint/cov_matrix_constrain.hpp | 8 ++- .../constraint/cov_matrix_constrain_lkj.hpp | 14 ++--- stan/math/prim/constraint/cov_matrix_free.hpp | 3 +- .../prim/constraint/cov_matrix_free_lkj.hpp | 4 +- .../offset_multiplier_constrain.hpp | 9 +-- .../constraint/offset_multiplier_free.hpp | 28 ++++++---- .../prim/constraint/ordered_constrain.hpp | 7 ++- stan/math/prim/constraint/ordered_free.hpp | 5 +- .../prim/constraint/positive_constrain.hpp | 4 +- .../constraint/positive_ordered_constrain.hpp | 8 ++- .../prim/constraint/positive_ordered_free.hpp | 4 +- .../prim/constraint/simplex_constrain.hpp | 7 ++- stan/math/prim/constraint/simplex_free.hpp | 5 +- .../stochastic_column_constrain.hpp | 8 ++- .../constraint/stochastic_column_free.hpp | 4 +- .../constraint/stochastic_row_constrain.hpp | 8 ++- .../prim/constraint/stochastic_row_free.hpp | 4 +- .../prim/constraint/sum_to_zero_constrain.hpp | 8 ++- .../math/prim/constraint/sum_to_zero_free.hpp | 3 +- stan/math/prim/constraint/ub_constrain.hpp | 7 ++- .../prim/constraint/unit_vector_constrain.hpp | 8 ++- .../math/prim/constraint/unit_vector_free.hpp | 3 +- stan/math/prim/fun/Phi.hpp | 3 +- stan/math/prim/fun/Phi_approx.hpp | 3 +- stan/math/prim/fun/abs.hpp | 4 +- stan/math/prim/fun/acos.hpp | 4 +- stan/math/prim/fun/acosh.hpp | 6 +- stan/math/prim/fun/asin.hpp | 4 +- stan/math/prim/fun/asinh.hpp | 6 +- stan/math/prim/fun/atan.hpp | 4 +- stan/math/prim/fun/atan2.hpp | 2 +- stan/math/prim/fun/atanh.hpp | 6 +- stan/math/prim/fun/beta.hpp | 3 +- stan/math/prim/fun/cbrt.hpp | 3 +- stan/math/prim/fun/ceil.hpp | 4 +- stan/math/prim/fun/cos.hpp | 4 +- stan/math/prim/fun/cosh.hpp | 3 +- stan/math/prim/fun/digamma.hpp | 3 +- stan/math/prim/fun/erf.hpp | 3 +- stan/math/prim/fun/erfc.hpp | 3 +- stan/math/prim/fun/exp2.hpp | 3 +- stan/math/prim/fun/expm1.hpp | 3 +- stan/math/prim/fun/fabs.hpp | 3 +- stan/math/prim/fun/fdim.hpp | 3 +- stan/math/prim/fun/floor.hpp | 3 +- stan/math/prim/fun/fmax.hpp | 3 +- stan/math/prim/fun/fmin.hpp | 3 +- stan/math/prim/fun/fmod.hpp | 3 +- stan/math/prim/fun/gamma_p.hpp | 3 +- stan/math/prim/fun/inv.hpp | 3 +- stan/math/prim/fun/inv_Phi.hpp | 3 +- stan/math/prim/fun/inv_cloglog.hpp | 6 +- stan/math/prim/fun/inv_erfc.hpp | 3 +- stan/math/prim/fun/inv_sqrt.hpp | 3 +- stan/math/prim/fun/inv_square.hpp | 3 +- stan/math/prim/fun/lambert_w.hpp | 6 +- stan/math/prim/fun/lgamma.hpp | 3 +- stan/math/prim/fun/log10.hpp | 3 +- stan/math/prim/fun/log1m.hpp | 3 +- stan/math/prim/fun/log1m_exp.hpp | 3 +- stan/math/prim/fun/log1p.hpp | 3 +- stan/math/prim/fun/log1p_exp.hpp | 3 +- stan/math/prim/fun/log2.hpp | 3 +- stan/math/prim/fun/log_inv_logit.hpp | 3 +- stan/math/prim/fun/log_softmax.hpp | 3 +- stan/math/prim/fun/log_sum_exp.hpp | 20 +++---- stan/math/prim/fun/logit.hpp | 11 ++-- stan/math/prim/fun/mean.hpp | 4 +- stan/math/prim/fun/minus.hpp | 6 +- stan/math/prim/fun/pow.hpp | 5 +- stan/math/prim/fun/rank.hpp | 9 +-- stan/math/prim/fun/round.hpp | 3 +- stan/math/prim/fun/sd.hpp | 12 ++-- stan/math/prim/fun/select.hpp | 14 ++--- stan/math/prim/fun/sign.hpp | 3 +- stan/math/prim/fun/sinh.hpp | 3 +- stan/math/prim/fun/sqrt.hpp | 3 +- stan/math/prim/fun/square.hpp | 3 +- stan/math/prim/fun/step.hpp | 3 +- stan/math/prim/fun/tan.hpp | 4 +- stan/math/prim/fun/tanh.hpp | 3 +- stan/math/prim/fun/tgamma.hpp | 3 +- stan/math/prim/fun/to_int.hpp | 3 +- stan/math/prim/fun/trigamma.hpp | 3 +- stan/math/prim/fun/trunc.hpp | 3 +- .../math/prim/functor/apply_scalar_binary.hpp | 8 ++- .../prim/functor/apply_scalar_ternary.hpp | 25 ++++++--- stan/math/prim/functor/apply_scalar_unary.hpp | 13 +++-- stan/math/prim/functor/apply_vector_unary.hpp | 15 ++--- stan/math/prim/prob/std_normal_log_qf.hpp | 3 +- stan/math/rev/fun/pow.hpp | 6 +- stan/math/rev/fun/sd.hpp | 3 +- stan/math/rev/functor/apply_scalar_unary.hpp | 4 +- 101 files changed, 363 insertions(+), 241 deletions(-) diff --git a/stan/math/fwd/fun/log_softmax.hpp b/stan/math/fwd/fun/log_softmax.hpp index 48e7be01f82..8e52ebb0fdd 100644 --- a/stan/math/fwd/fun/log_softmax.hpp +++ b/stan/math/fwd/fun/log_softmax.hpp @@ -20,35 +20,37 @@ namespace math { */ template * = nullptr> inline auto log_softmax(T&& x) { - return apply_vector_unary::apply(std::forward(x), [&](const auto& alpha) { - using T_alpha = decltype(alpha); - using T_fvar = value_type_t; - using T_fvar_inner = typename T_fvar::Scalar; - - const Eigen::Ref>& alpha_ref = alpha; - Eigen::Matrix alpha_t = alpha_ref.val(); - Eigen::Matrix softmax_alpha_t = softmax(alpha_t); - - Eigen::Matrix log_softmax_alpha(alpha.size()); - log_softmax_alpha.val() = log_softmax(alpha_t); - log_softmax_alpha.d().setZero(); - - for (int m = 0; m < alpha.size(); ++m) { - T_fvar_inner negative_alpha_m_d_times_softmax_alpha_t_m - = -alpha_ref.coeff(m).d_ * softmax_alpha_t(m); - for (int k = 0; k < alpha.size(); ++k) { - if (m == k) { - log_softmax_alpha(k).d_ - += alpha_ref.coeff(m).d_ - + negative_alpha_m_d_times_softmax_alpha_t_m; - } else { - log_softmax_alpha(k).d_ += negative_alpha_m_d_times_softmax_alpha_t_m; + return apply_vector_unary::apply( + std::forward(x), [&](const auto& alpha) { + using T_alpha = decltype(alpha); + using T_fvar = value_type_t; + using T_fvar_inner = typename T_fvar::Scalar; + + const Eigen::Ref>& alpha_ref = alpha; + Eigen::Matrix alpha_t = alpha_ref.val(); + Eigen::Matrix softmax_alpha_t = softmax(alpha_t); + + Eigen::Matrix log_softmax_alpha(alpha.size()); + log_softmax_alpha.val() = log_softmax(alpha_t); + log_softmax_alpha.d().setZero(); + + for (int m = 0; m < alpha.size(); ++m) { + T_fvar_inner negative_alpha_m_d_times_softmax_alpha_t_m + = -alpha_ref.coeff(m).d_ * softmax_alpha_t(m); + for (int k = 0; k < alpha.size(); ++k) { + if (m == k) { + log_softmax_alpha(k).d_ + += alpha_ref.coeff(m).d_ + + negative_alpha_m_d_times_softmax_alpha_t_m; + } else { + log_softmax_alpha(k).d_ + += negative_alpha_m_d_times_softmax_alpha_t_m; + } + } } - } - } - return log_softmax_alpha; - }); + return log_softmax_alpha; + }); } } // namespace math diff --git a/stan/math/fwd/fun/pow.hpp b/stan/math/fwd/fun/pow.hpp index 2a29557dad5..21a0bf36380 100644 --- a/stan/math/fwd/fun/pow.hpp +++ b/stan/math/fwd/fun/pow.hpp @@ -75,7 +75,11 @@ template * = nullptr, require_any_fvar_t, base_type_t>* = nullptr> inline auto pow(T1&& a, T2&& b) { return apply_scalar_binary( - [](auto&& c, auto&& d) { return stan::math::pow(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); + [](auto&& c, auto&& d) { + return stan::math::pow(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index 7e868080ebd..63cd1b22271 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -89,7 +89,9 @@ cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) { template * = nullptr> inline auto cholesky_corr_constrain(T&& y, int K) { return apply_vector_unary>::apply( - std::forward(y), [K](auto&& v) { return cholesky_corr_constrain(std::forward(v), K); }); + std::forward(y), [K](auto&& v) { + return cholesky_corr_constrain(std::forward(v), K); + }); } /** @@ -109,7 +111,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto cholesky_corr_constrain(T&& y, int K, Lp& lp) { return apply_vector_unary>::apply( - std::forward(y), [&lp, K](auto&& v) { return cholesky_corr_constrain(std::forward(v), K, lp); }); + std::forward(y), [&lp, K](auto&& v) { + return cholesky_corr_constrain(std::forward(v), K, lp); + }); } /** diff --git a/stan/math/prim/constraint/cholesky_corr_free.hpp b/stan/math/prim/constraint/cholesky_corr_free.hpp index 40fde2cd559..daba71ac14d 100644 --- a/stan/math/prim/constraint/cholesky_corr_free.hpp +++ b/stan/math/prim/constraint/cholesky_corr_free.hpp @@ -44,7 +44,9 @@ inline auto cholesky_corr_free(const T& x) { template * = nullptr> inline auto cholesky_corr_free(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return cholesky_corr_free(std::forward(v)); }); + std::forward(x), [](auto&& v) { + return cholesky_corr_free(std::forward(v)); + }); } } // namespace math diff --git a/stan/math/prim/constraint/cholesky_factor_constrain.hpp b/stan/math/prim/constraint/cholesky_factor_constrain.hpp index 1ec88c90c25..6a273e88d94 100644 --- a/stan/math/prim/constraint/cholesky_factor_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_factor_constrain.hpp @@ -103,7 +103,9 @@ cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) { template * = nullptr> inline auto cholesky_factor_constrain(T&& x, int M, int N) { return apply_vector_unary>::apply( - std::forward(x), [M, N](auto&& v) { return cholesky_factor_constrain(std::forward(v), M, N); }); + std::forward(x), [M, N](auto&& v) { + return cholesky_factor_constrain(std::forward(v), M, N); + }); } /** @@ -126,9 +128,11 @@ inline auto cholesky_factor_constrain(T&& x, int M, int N) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto cholesky_factor_constrain(T&& x, int M, int N, Lp& lp) { - return apply_vector_unary>::apply(std::forward(x), [&lp, M, N](auto&& v) { - return cholesky_factor_constrain(std::forward(v), M, N, lp); - }); + return apply_vector_unary>::apply( + std::forward(x), [&lp, M, N](auto&& v) { + return cholesky_factor_constrain(std::forward(v), M, N, + lp); + }); } /** diff --git a/stan/math/prim/constraint/cholesky_factor_free.hpp b/stan/math/prim/constraint/cholesky_factor_free.hpp index b4827a8131a..affe6d32d04 100644 --- a/stan/math/prim/constraint/cholesky_factor_free.hpp +++ b/stan/math/prim/constraint/cholesky_factor_free.hpp @@ -58,7 +58,9 @@ Eigen::Matrix, Eigen::Dynamic, 1> cholesky_factor_free( template * = nullptr> inline auto cholesky_factor_free(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return cholesky_factor_free(std::forward(v)); }); + std::forward(x), [](auto&& v) { + return cholesky_factor_free(std::forward(v)); + }); } } // namespace math diff --git a/stan/math/prim/constraint/corr_matrix_constrain.hpp b/stan/math/prim/constraint/corr_matrix_constrain.hpp index 4c44f60b2cd..1df645f7ff3 100644 --- a/stan/math/prim/constraint/corr_matrix_constrain.hpp +++ b/stan/math/prim/constraint/corr_matrix_constrain.hpp @@ -93,7 +93,9 @@ corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) { template * = nullptr> inline auto corr_matrix_constrain(T&& y, int K) { return apply_vector_unary>::apply( - std::forward(y), [K](auto&& v) { return corr_matrix_constrain(std::forward(v), K); }); + std::forward(y), [K](auto&& v) { + return corr_matrix_constrain(std::forward(v), K); + }); } /** @@ -116,7 +118,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto corr_matrix_constrain(T&& y, int K, Lp& lp) { return apply_vector_unary>::apply( - std::forward(y), [&lp, K](auto&& v) { return corr_matrix_constrain(std::forward(v), K, lp); }); + std::forward(y), [&lp, K](auto&& v) { + return corr_matrix_constrain(std::forward(v), K, lp); + }); } /** diff --git a/stan/math/prim/constraint/corr_matrix_free.hpp b/stan/math/prim/constraint/corr_matrix_free.hpp index 76f46f11c7e..b482e88d9f8 100644 --- a/stan/math/prim/constraint/corr_matrix_free.hpp +++ b/stan/math/prim/constraint/corr_matrix_free.hpp @@ -62,7 +62,8 @@ Eigen::Matrix, Eigen::Dynamic, 1> corr_matrix_free(const T& y) { template * = nullptr> inline auto corr_matrix_free(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return corr_matrix_free(std::forward(v)); }); + std::forward(x), + [](auto&& v) { return corr_matrix_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/cov_matrix_constrain.hpp b/stan/math/prim/constraint/cov_matrix_constrain.hpp index 26bc919e29f..785016872ef 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain.hpp @@ -104,7 +104,9 @@ cov_matrix_constrain(T&& x, Eigen::Index K, Lp& lp) { template * = nullptr> inline auto cov_matrix_constrain(T&& x, Eigen::Index K) { return apply_vector_unary>::apply( - std::forward(x), [K](auto&& v) { return cov_matrix_constrain(std::forward(v), K); }); + std::forward(x), [K](auto&& v) { + return cov_matrix_constrain(std::forward(v), K); + }); } /** @@ -126,7 +128,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto cov_matrix_constrain(T&& x, Eigen::Index K, Lp& lp) { return apply_vector_unary>::apply( - std::forward(x), [&lp, K](auto&& v) { return cov_matrix_constrain(std::forward(v), K, lp); }); + std::forward(x), [&lp, K](auto&& v) { + return cov_matrix_constrain(std::forward(v), K, lp); + }); } /** diff --git a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp index 825a98ddc3c..9e630a91b39 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp @@ -84,8 +84,7 @@ cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { * correlations and deviations. */ template * = nullptr> -inline auto cov_matrix_constrain_lkj(T&& x, size_t k, - return_type_t& lp) { +inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { if constexpr (Jacobian) { return cov_matrix_constrain_lkj(std::forward(x), k, lp); } else { @@ -114,11 +113,12 @@ inline auto cov_matrix_constrain_lkj(T&& x, size_t k, * correlations and deviations. */ template * = nullptr> -inline auto cov_matrix_constrain_lkj(T&& x, size_t k, - return_type_t& lp) { - return apply_vector_unary>::apply(std::forward(x), [&lp, k](auto&& v) { - return cov_matrix_constrain_lkj(std::forward(v), k, lp); - }); +inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { + return apply_vector_unary>::apply( + std::forward(x), [&lp, k](auto&& v) { + return cov_matrix_constrain_lkj(std::forward(v), + k, lp); + }); } } // namespace math diff --git a/stan/math/prim/constraint/cov_matrix_free.hpp b/stan/math/prim/constraint/cov_matrix_free.hpp index 04c5f388bec..310bf897b14 100644 --- a/stan/math/prim/constraint/cov_matrix_free.hpp +++ b/stan/math/prim/constraint/cov_matrix_free.hpp @@ -68,7 +68,8 @@ Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free(T&& y) { template * = nullptr> inline auto cov_matrix_free(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return cov_matrix_free(std::forward(v)); }); + std::forward(x), + [](auto&& v) { return cov_matrix_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/cov_matrix_free_lkj.hpp b/stan/math/prim/constraint/cov_matrix_free_lkj.hpp index f77e05bf1c8..d547261b814 100644 --- a/stan/math/prim/constraint/cov_matrix_free_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_free_lkj.hpp @@ -59,7 +59,9 @@ Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free_lkj( template * = nullptr> auto cov_matrix_free_lkj(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return cov_matrix_free_lkj(std::forward(v)); }); + std::forward(x), [](auto&& v) { + return cov_matrix_free_lkj(std::forward(v)); + }); } } // namespace math diff --git a/stan/math/prim/constraint/offset_multiplier_constrain.hpp b/stan/math/prim/constraint/offset_multiplier_constrain.hpp index ca781b76e25..640110f56d3 100644 --- a/stan/math/prim/constraint/offset_multiplier_constrain.hpp +++ b/stan/math/prim/constraint/offset_multiplier_constrain.hpp @@ -303,12 +303,13 @@ inline auto offset_multiplier_constrain(const std::vector& x, */ template , Lp>* = nullptr> -inline auto offset_multiplier_constrain(T&& x, M&& mu, S&& sigma, - Lp& lp) { +inline auto offset_multiplier_constrain(T&& x, M&& mu, S&& sigma, Lp& lp) { if constexpr (Jacobian) { - return offset_multiplier_constrain(std::forward(x), std::forward(mu), std::forward(sigma), lp); + return offset_multiplier_constrain(std::forward(x), std::forward(mu), + std::forward(sigma), lp); } else { - return offset_multiplier_constrain(std::forward(x), std::forward(mu), std::forward(sigma)); + return offset_multiplier_constrain(std::forward(x), std::forward(mu), + std::forward(sigma)); } } diff --git a/stan/math/prim/constraint/offset_multiplier_free.hpp b/stan/math/prim/constraint/offset_multiplier_free.hpp index f4cb5153330..e61c1046f4a 100644 --- a/stan/math/prim/constraint/offset_multiplier_free.hpp +++ b/stan/math/prim/constraint/offset_multiplier_free.hpp @@ -39,7 +39,8 @@ namespace math { * @throw std::domain_error if mu is not finite * @throw std::invalid_argument if non-scalar arguments don't match in size */ -template * = nullptr, +template * = nullptr, require_all_not_st_var* = nullptr> inline auto offset_multiplier_free(T&& y, M&& mu, S&& sigma) { auto&& mu_ref = to_ref(std::forward(mu)); @@ -57,16 +58,18 @@ inline auto offset_multiplier_free(T&& y, M&& mu, S&& sigma) { check_finite("offset_multiplier_constrain", "offset", value_of(mu_ref)); check_positive_finite("offset_multiplier_constrain", "multiplier", value_of(sigma_ref)); - return stan::math::eval(divide(subtract(std::forward(y), std::forward(mu_ref)), std::forward(sigma_ref))); + return stan::math::eval( + divide(subtract(std::forward(y), std::forward(mu_ref)), + std::forward(sigma_ref))); } /** * Overload for array of x and non-array mu and sigma */ -template * = nullptr, +template * = nullptr, require_all_not_std_vector_t* = nullptr> -inline auto offset_multiplier_free(T&& x, M&& mu, - S&& sigma) { +inline auto offset_multiplier_free(T&& x, M&& mu, S&& sigma) { std::vector> ret; ret.reserve(x.size()); @@ -81,10 +84,10 @@ inline auto offset_multiplier_free(T&& x, M&& mu, /** * Overload for array of x and sigma and non-array mu */ -template * = nullptr, +template * = nullptr, require_not_std_vector_t* = nullptr> -inline auto offset_multiplier_free(T&& x, const M& mu, - S&& sigma) { +inline auto offset_multiplier_free(T&& x, const M& mu, S&& sigma) { check_matching_dims("offset_multiplier_free", "x", x, "sigma", sigma); std::vector< plain_type_t> @@ -100,10 +103,10 @@ inline auto offset_multiplier_free(T&& x, const M& mu, /** * Overload for array of x and mu and non-array sigma */ -template * = nullptr, +template * = nullptr, require_not_std_vector_t* = nullptr> -inline auto offset_multiplier_free(T&& x, - M&& mu, const S& sigma) { +inline auto offset_multiplier_free(T&& x, M&& mu, const S& sigma) { check_matching_dims("offset_multiplier_free", "x", x, "mu", mu); std::vector< plain_type_t> @@ -119,7 +122,8 @@ inline auto offset_multiplier_free(T&& x, /** * Overload for array of x, mu, and sigma */ -template * = nullptr> +template * = nullptr> inline auto offset_multiplier_free(T&& x, M&& mu, S&& sigma) { check_matching_dims("offset_multiplier_free", "x", x, "mu", mu); check_matching_dims("offset_multiplier_free", "x", x, "sigma", sigma); diff --git a/stan/math/prim/constraint/ordered_constrain.hpp b/stan/math/prim/constraint/ordered_constrain.hpp index 4f10137a766..e2a2280d842 100644 --- a/stan/math/prim/constraint/ordered_constrain.hpp +++ b/stan/math/prim/constraint/ordered_constrain.hpp @@ -78,7 +78,8 @@ inline auto ordered_constrain(EigVec&& x, Lp& lp) { template * = nullptr> inline auto ordered_constrain(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return ordered_constrain(std::forward(v)); }); + std::forward(x), + [](auto&& v) { return ordered_constrain(std::forward(v)); }); } /** @@ -100,7 +101,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto ordered_constrain(T&& x, Lp& lp) { return apply_vector_unary>::apply( - std::forward(x), [&lp](auto&& v) { return ordered_constrain(std::forward(v), lp); }); + std::forward(x), [&lp](auto&& v) { + return ordered_constrain(std::forward(v), lp); + }); } /** diff --git a/stan/math/prim/constraint/ordered_free.hpp b/stan/math/prim/constraint/ordered_free.hpp index 43354174e39..609d8a8f9f9 100644 --- a/stan/math/prim/constraint/ordered_free.hpp +++ b/stan/math/prim/constraint/ordered_free.hpp @@ -50,8 +50,9 @@ plain_type_t ordered_free(const EigVec& y) { */ template * = nullptr> inline auto ordered_free(T&& x) { - return apply_vector_unary>::apply(std::forward(x), - [](auto&& v) { return ordered_free(std::forward(v)); }); + return apply_vector_unary>::apply( + std::forward(x), + [](auto&& v) { return ordered_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/positive_constrain.hpp b/stan/math/prim/constraint/positive_constrain.hpp index 35602b449ee..193b9721890 100644 --- a/stan/math/prim/constraint/positive_constrain.hpp +++ b/stan/math/prim/constraint/positive_constrain.hpp @@ -97,7 +97,9 @@ template , Lp>* = nullptr> inline auto positive_constrain(T&& x, Lp& lp) { return apply_vector_unary>::apply( - std::forward(x), [&lp](auto&& v) { return positive_constrain(std::forward(v), lp); }); + std::forward(x), [&lp](auto&& v) { + return positive_constrain(std::forward(v), lp); + }); } } // namespace math diff --git a/stan/math/prim/constraint/positive_ordered_constrain.hpp b/stan/math/prim/constraint/positive_ordered_constrain.hpp index 172bd0ed8e0..608da90af22 100644 --- a/stan/math/prim/constraint/positive_ordered_constrain.hpp +++ b/stan/math/prim/constraint/positive_ordered_constrain.hpp @@ -72,7 +72,9 @@ inline auto positive_ordered_constrain(Vec&& x, Lp& lp) { template * = nullptr> inline auto positive_ordered_constrain(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return positive_ordered_constrain(std::forward(v)); }); + std::forward(x), [](auto&& v) { + return positive_ordered_constrain(std::forward(v)); + }); } /** @@ -93,7 +95,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto positive_ordered_constrain(T&& x, Lp& lp) { return apply_vector_unary>::apply( - std::forward(x), [&lp](auto&& v) { return positive_ordered_constrain(std::forward(v), lp); }); + std::forward(x), [&lp](auto&& v) { + return positive_ordered_constrain(std::forward(v), lp); + }); } /** diff --git a/stan/math/prim/constraint/positive_ordered_free.hpp b/stan/math/prim/constraint/positive_ordered_free.hpp index b3adacf604f..3602f405810 100644 --- a/stan/math/prim/constraint/positive_ordered_free.hpp +++ b/stan/math/prim/constraint/positive_ordered_free.hpp @@ -51,7 +51,9 @@ inline auto positive_ordered_free(const EigVec& y) { template * = nullptr> inline auto positive_ordered_free(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return positive_ordered_free(std::forward(v)); }); + std::forward(x), [](auto&& v) { + return positive_ordered_free(std::forward(v)); + }); } } // namespace math diff --git a/stan/math/prim/constraint/simplex_constrain.hpp b/stan/math/prim/constraint/simplex_constrain.hpp index 0bcdd1afda9..0f9c9fc4af4 100644 --- a/stan/math/prim/constraint/simplex_constrain.hpp +++ b/stan/math/prim/constraint/simplex_constrain.hpp @@ -147,7 +147,8 @@ inline plain_type_t simplex_constrain(const Vec& y, Lp& lp) { template * = nullptr> inline auto simplex_constrain(T&& y) { return apply_vector_unary>::apply( - std::forward(y), [](auto&& v) { return simplex_constrain(std::forward(v)); }); + std::forward(y), + [](auto&& v) { return simplex_constrain(std::forward(v)); }); } /** @@ -167,7 +168,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto simplex_constrain(T&& y, Lp& lp) { return apply_vector_unary>::apply( - std::forward(y), [&lp](auto&& v) { return simplex_constrain(std::forward(v), lp); }); + std::forward(y), [&lp](auto&& v) { + return simplex_constrain(std::forward(v), lp); + }); } /** diff --git a/stan/math/prim/constraint/simplex_free.hpp b/stan/math/prim/constraint/simplex_free.hpp index 3b1d242b5cd..2b2bd9ae68c 100644 --- a/stan/math/prim/constraint/simplex_free.hpp +++ b/stan/math/prim/constraint/simplex_free.hpp @@ -54,8 +54,9 @@ inline plain_type_t simplex_free(const Vec& x) { */ template * = nullptr> inline auto simplex_free(T&& x) { - return apply_vector_unary>::apply(std::forward(x), - [](auto&& v) { return simplex_free(std::forward(v)); }); + return apply_vector_unary>::apply( + std::forward(x), + [](auto&& v) { return simplex_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/stochastic_column_constrain.hpp b/stan/math/prim/constraint/stochastic_column_constrain.hpp index 84a442834b2..104877f0dbf 100644 --- a/stan/math/prim/constraint/stochastic_column_constrain.hpp +++ b/stan/math/prim/constraint/stochastic_column_constrain.hpp @@ -73,7 +73,9 @@ inline plain_type_t stochastic_column_constrain(const Mat& y, Lp& lp) { template * = nullptr> inline auto stochastic_column_constrain(T&& y) { return apply_vector_unary>::apply( - std::forward(y), [](auto&& v) { return stochastic_column_constrain(std::forward(v)); }); + std::forward(y), [](auto&& v) { + return stochastic_column_constrain(std::forward(v)); + }); } /** @@ -94,7 +96,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto stochastic_column_constrain(T&& y, Lp& lp) { return apply_vector_unary>::apply( - std::forward(y), [&lp](auto&& v) { return stochastic_column_constrain(std::forward(v), lp); }); + std::forward(y), [&lp](auto&& v) { + return stochastic_column_constrain(std::forward(v), lp); + }); } /** diff --git a/stan/math/prim/constraint/stochastic_column_free.hpp b/stan/math/prim/constraint/stochastic_column_free.hpp index f7e64aacd77..c1cd90ece06 100644 --- a/stan/math/prim/constraint/stochastic_column_free.hpp +++ b/stan/math/prim/constraint/stochastic_column_free.hpp @@ -38,7 +38,9 @@ inline plain_type_t stochastic_column_free(Mat&& y) { template * = nullptr> inline auto stochastic_column_free(T&& y) { return apply_vector_unary>::apply( - std::forward(y), [](auto&& v) { return stochastic_column_free(std::forward(v)); }); + std::forward(y), [](auto&& v) { + return stochastic_column_free(std::forward(v)); + }); } } // namespace math diff --git a/stan/math/prim/constraint/stochastic_row_constrain.hpp b/stan/math/prim/constraint/stochastic_row_constrain.hpp index e14a23121d9..0b0c1fa2723 100644 --- a/stan/math/prim/constraint/stochastic_row_constrain.hpp +++ b/stan/math/prim/constraint/stochastic_row_constrain.hpp @@ -70,7 +70,9 @@ inline plain_type_t stochastic_row_constrain(Mat&& y, Lp& lp) { template * = nullptr> inline auto stochastic_row_constrain(T&& y) { return apply_vector_unary>::apply( - std::forward(y), [](auto&& v) { return stochastic_row_constrain(std::forward(v)); }); + std::forward(y), [](auto&& v) { + return stochastic_row_constrain(std::forward(v)); + }); } /** @@ -90,7 +92,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto stochastic_row_constrain(T&& y, Lp& lp) { return apply_vector_unary>::apply( - std::forward(y), [&lp](auto&& v) { return stochastic_row_constrain(std::forward(v), lp); }); + std::forward(y), [&lp](auto&& v) { + return stochastic_row_constrain(std::forward(v), lp); + }); } /** diff --git a/stan/math/prim/constraint/stochastic_row_free.hpp b/stan/math/prim/constraint/stochastic_row_free.hpp index c9122aba00f..068d287435d 100644 --- a/stan/math/prim/constraint/stochastic_row_free.hpp +++ b/stan/math/prim/constraint/stochastic_row_free.hpp @@ -37,7 +37,9 @@ inline plain_type_t stochastic_row_free(Mat&& y) { template * = nullptr> inline auto stochastic_row_free(T&& y) { return apply_vector_unary>::apply( - std::forward(y), [](auto&& v) { return stochastic_row_free(std::forward(v)); }); + std::forward(y), [](auto&& v) { + return stochastic_row_free(std::forward(v)); + }); } } // namespace math diff --git a/stan/math/prim/constraint/sum_to_zero_constrain.hpp b/stan/math/prim/constraint/sum_to_zero_constrain.hpp index 8b1c4798d91..f928f14e67e 100644 --- a/stan/math/prim/constraint/sum_to_zero_constrain.hpp +++ b/stan/math/prim/constraint/sum_to_zero_constrain.hpp @@ -140,7 +140,9 @@ inline plain_type_t sum_to_zero_constrain(T&& y, Lp& lp) { template * = nullptr> inline auto sum_to_zero_constrain(T&& y) { return apply_vector_unary>::apply( - std::forward(y), [](auto&& v) { return sum_to_zero_constrain(std::forward(v)); }); + std::forward(y), [](auto&& v) { + return sum_to_zero_constrain(std::forward(v)); + }); } /** @@ -159,7 +161,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto sum_to_zero_constrain(T&& y, Lp& lp) { return apply_vector_unary>::apply( - std::forward(y), [&lp](auto&& v) { return sum_to_zero_constrain(std::forward(v), lp); }); + std::forward(y), [&lp](auto&& v) { + return sum_to_zero_constrain(std::forward(v), lp); + }); } /** diff --git a/stan/math/prim/constraint/sum_to_zero_free.hpp b/stan/math/prim/constraint/sum_to_zero_free.hpp index 936a6f37204..85a679cad2d 100644 --- a/stan/math/prim/constraint/sum_to_zero_free.hpp +++ b/stan/math/prim/constraint/sum_to_zero_free.hpp @@ -117,7 +117,8 @@ inline plain_type_t sum_to_zero_free(const Mat& z) { template * = nullptr> inline auto sum_to_zero_free(T&& z) { return apply_vector_unary>::apply( - std::forward(z), [](auto&& v) { return sum_to_zero_free(std::forward(v)); }); + std::forward(z), + [](auto&& v) { return sum_to_zero_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/constraint/ub_constrain.hpp b/stan/math/prim/constraint/ub_constrain.hpp index 749104b164f..bef8aee2324 100644 --- a/stan/math/prim/constraint/ub_constrain.hpp +++ b/stan/math/prim/constraint/ub_constrain.hpp @@ -89,7 +89,8 @@ template * = nullptr, require_stan_scalar_t* = nullptr, require_all_not_st_var* = nullptr> inline auto ub_constrain(T&& x, const U& ub) { - return eval(std::forward(x).unaryExpr([ub](auto&& xx) { return ub_constrain(xx, ub); })); + return eval(std::forward(x).unaryExpr( + [ub](auto&& xx) { return ub_constrain(xx, ub); })); } /** @@ -109,8 +110,8 @@ template * = nullptr, require_all_not_st_var* = nullptr, require_convertible_t, Lp>* = nullptr> inline auto ub_constrain(T&& x, const U& ub, Lp& lp) { - return eval( - std::forward(x).unaryExpr([ub, &lp](auto&& xx) { return ub_constrain(xx, ub, lp); })); + return eval(std::forward(x).unaryExpr( + [ub, &lp](auto&& xx) { return ub_constrain(xx, ub, lp); })); } /** diff --git a/stan/math/prim/constraint/unit_vector_constrain.hpp b/stan/math/prim/constraint/unit_vector_constrain.hpp index 5a18366200c..7b3f29e2d5e 100644 --- a/stan/math/prim/constraint/unit_vector_constrain.hpp +++ b/stan/math/prim/constraint/unit_vector_constrain.hpp @@ -68,7 +68,9 @@ inline plain_type_t unit_vector_constrain(const T1& y, T2& lp) { template * = nullptr> inline auto unit_vector_constrain(T&& y) { return apply_vector_unary>::apply( - std::forward(y), [](auto&& v) { return unit_vector_constrain(std::forward(v)); }); + std::forward(y), [](auto&& v) { + return unit_vector_constrain(std::forward(v)); + }); } /** @@ -88,7 +90,9 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto unit_vector_constrain(T&& y, Lp& lp) { return apply_vector_unary>::apply( - std::forward(y), [&lp](auto&& v) { return unit_vector_constrain(std::forward(v), lp); }); + std::forward(y), [&lp](auto&& v) { + return unit_vector_constrain(std::forward(v), lp); + }); } /** diff --git a/stan/math/prim/constraint/unit_vector_free.hpp b/stan/math/prim/constraint/unit_vector_free.hpp index a1c5d52e01c..6c8a9f19091 100644 --- a/stan/math/prim/constraint/unit_vector_free.hpp +++ b/stan/math/prim/constraint/unit_vector_free.hpp @@ -38,7 +38,8 @@ inline auto unit_vector_free(EigVec&& x) { template * = nullptr> inline auto unit_vector_free(T&& x) { return apply_vector_unary>::apply( - std::forward(x), [](auto&& v) { return unit_vector_free(std::forward(v)); }); + std::forward(x), + [](auto&& v) { return unit_vector_free(std::forward(v)); }); } } // namespace math diff --git a/stan/math/prim/fun/Phi.hpp b/stan/math/prim/fun/Phi.hpp index 6c21789468c..14454e98baf 100644 --- a/stan/math/prim/fun/Phi.hpp +++ b/stan/math/prim/fun/Phi.hpp @@ -68,7 +68,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto Phi(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/Phi_approx.hpp b/stan/math/prim/fun/Phi_approx.hpp index a806c765153..465b738abd1 100644 --- a/stan/math/prim/fun/Phi_approx.hpp +++ b/stan/math/prim/fun/Phi_approx.hpp @@ -67,7 +67,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto Phi_approx(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/abs.hpp b/stan/math/prim/fun/abs.hpp index 5d7571167be..113471b2367 100644 --- a/stan/math/prim/fun/abs.hpp +++ b/stan/math/prim/fun/abs.hpp @@ -81,9 +81,7 @@ template >::apply( std::forward(x), - [&](auto&& v) { - return std::forward(v).array().abs(); - }); + [&](auto&& v) { return std::forward(v).array().abs(); }); } namespace internal { diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index 1b22d4299bf..097350a292b 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -83,9 +83,7 @@ template >::apply( std::forward(x), - [](auto&& v) { - return std::forward(v).array().acos(); - }); + [](auto&& v) { return std::forward(v).array().acos(); }); } namespace internal { diff --git a/stan/math/prim/fun/acosh.hpp b/stan/math/prim/fun/acosh.hpp index c3821b69fca..4ff5f7a3df9 100644 --- a/stan/math/prim/fun/acosh.hpp +++ b/stan/math/prim/fun/acosh.hpp @@ -79,7 +79,8 @@ struct acosh_fun { */ template * = nullptr> inline auto acosh(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -95,7 +96,8 @@ inline auto acosh(T&& x) { template * = nullptr> inline auto acosh(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } namespace internal { diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index 8dfd7aca987..ad6b03285a3 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -81,9 +81,7 @@ template >::apply( std::forward(x), - [](auto&& v) { - return std::forward(v).array().asin(); - }); + [](auto&& v) { return std::forward(v).array().asin(); }); } namespace internal { diff --git a/stan/math/prim/fun/asinh.hpp b/stan/math/prim/fun/asinh.hpp index 63a53caf255..6db8b657b0a 100644 --- a/stan/math/prim/fun/asinh.hpp +++ b/stan/math/prim/fun/asinh.hpp @@ -67,7 +67,8 @@ struct asinh_fun { */ template * = nullptr> inline auto asinh(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -81,7 +82,8 @@ inline auto asinh(T&& x) { template * = nullptr> inline auto asinh(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } namespace internal { diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 08b2da5cb78..976298b1a62 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -79,9 +79,7 @@ template >::apply( std::forward(x), - [](auto&& v) { - return std::forward(v).array().atan(); - }); + [](auto&& v) { return std::forward(v).array().atan(); }); } namespace internal { diff --git a/stan/math/prim/fun/atan2.hpp b/stan/math/prim/fun/atan2.hpp index 060c99ea314..54b50d551cd 100644 --- a/stan/math/prim/fun/atan2.hpp +++ b/stan/math/prim/fun/atan2.hpp @@ -40,7 +40,7 @@ inline auto atan2(T1&& a, T2&& b) { return apply_scalar_binary( [](auto&& c, auto&& d) { return atan2(std::forward(c), - std::forward(d)); + std::forward(d)); }, std::forward(a), std::forward(b)); } diff --git a/stan/math/prim/fun/atanh.hpp b/stan/math/prim/fun/atanh.hpp index 4d1802b1ab6..0105fe66d02 100644 --- a/stan/math/prim/fun/atanh.hpp +++ b/stan/math/prim/fun/atanh.hpp @@ -78,7 +78,8 @@ struct atanh_fun { */ template * = nullptr> inline auto atanh(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -94,7 +95,8 @@ inline auto atanh(T&& x) { template * = nullptr> inline auto atanh(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } namespace internal { diff --git a/stan/math/prim/fun/beta.hpp b/stan/math/prim/fun/beta.hpp index 58512cb631d..fefcf44b479 100644 --- a/stan/math/prim/fun/beta.hpp +++ b/stan/math/prim/fun/beta.hpp @@ -70,8 +70,7 @@ template * = nullptr, inline auto beta(T1&& a, T2&& b) { return apply_scalar_binary( [](auto&& c, auto&& d) { - return beta(std::forward(c), - std::forward(d)); + return beta(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); } diff --git a/stan/math/prim/fun/cbrt.hpp b/stan/math/prim/fun/cbrt.hpp index 0a5d1ae3a71..900ddb1178a 100644 --- a/stan/math/prim/fun/cbrt.hpp +++ b/stan/math/prim/fun/cbrt.hpp @@ -35,7 +35,8 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto cbrt(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index ac9a59e8541..74fb7f4d097 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -56,9 +56,7 @@ template >::apply( std::forward(x), - [](auto&& v) { - return std::forward(v).array().ceil(); - }); + [](auto&& v) { return std::forward(v).array().ceil(); }); } } // namespace math diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index 8bdeffd12c3..d35764ebcfb 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -78,9 +78,7 @@ template >::apply( std::forward(x), - [&](auto&& v) { - return std::forward(v).array().cos(); - }); + [&](auto&& v) { return std::forward(v).array().cos(); }); } namespace internal { diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index dda708c9f9e..f909bbf859e 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -60,7 +60,8 @@ struct cosh_fun { */ template * = nullptr> inline auto cosh(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/digamma.hpp b/stan/math/prim/fun/digamma.hpp index 71ccd014b4d..7fd91478025 100644 --- a/stan/math/prim/fun/digamma.hpp +++ b/stan/math/prim/fun/digamma.hpp @@ -75,7 +75,8 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto digamma(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/erf.hpp b/stan/math/prim/fun/erf.hpp index 683205ded17..1a91648a42d 100644 --- a/stan/math/prim/fun/erf.hpp +++ b/stan/math/prim/fun/erf.hpp @@ -36,7 +36,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto erf(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/erfc.hpp b/stan/math/prim/fun/erfc.hpp index 92f3c904cf4..9195b970e96 100644 --- a/stan/math/prim/fun/erfc.hpp +++ b/stan/math/prim/fun/erfc.hpp @@ -37,7 +37,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto erfc(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/exp2.hpp b/stan/math/prim/fun/exp2.hpp index 6bea1ff00fb..ce5fd964e92 100644 --- a/stan/math/prim/fun/exp2.hpp +++ b/stan/math/prim/fun/exp2.hpp @@ -40,7 +40,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto exp2(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/expm1.hpp b/stan/math/prim/fun/expm1.hpp index da737a6539e..1cdbdde4ed7 100644 --- a/stan/math/prim/fun/expm1.hpp +++ b/stan/math/prim/fun/expm1.hpp @@ -37,7 +37,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto expm1(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 9db7205c1b6..476fb1e7ed7 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -49,7 +49,8 @@ template * = nullptr, require_not_stan_scalar_t* = nullptr> inline auto fabs(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/fdim.hpp b/stan/math/prim/fun/fdim.hpp index 4e602bfcc7e..c940a79ffbf 100644 --- a/stan/math/prim/fun/fdim.hpp +++ b/stan/math/prim/fun/fdim.hpp @@ -38,8 +38,7 @@ template * = nullptr> inline auto fdim(T1&& a, T2&& b) { return apply_scalar_binary( [](auto&& c, auto&& d) { - return fdim(std::forward(c), - std::forward(d)); + return fdim(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); } diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index 11529d21df6..f683c5e5130 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -43,7 +43,8 @@ struct floor_fun { */ template * = nullptr> inline auto floor(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/fmax.hpp b/stan/math/prim/fun/fmax.hpp index 620a7f5f4f7..77c0319ede5 100644 --- a/stan/math/prim/fun/fmax.hpp +++ b/stan/math/prim/fun/fmax.hpp @@ -36,8 +36,7 @@ template * = nullptr> inline auto fmax(T1&& a, T2&& b) { return apply_scalar_binary( [](auto&& c, auto&& d) { - return fmax(std::forward(c), - std::forward(d)); + return fmax(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); } diff --git a/stan/math/prim/fun/fmin.hpp b/stan/math/prim/fun/fmin.hpp index 7bf25f93be9..5b2ffe63643 100644 --- a/stan/math/prim/fun/fmin.hpp +++ b/stan/math/prim/fun/fmin.hpp @@ -36,8 +36,7 @@ template * = nullptr> inline auto fmin(T1&& a, T2&& b) { return apply_scalar_binary( [](auto&& c, auto&& d) { - return fmin(std::forward(c), - std::forward(d)); + return fmin(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); } diff --git a/stan/math/prim/fun/fmod.hpp b/stan/math/prim/fun/fmod.hpp index 26f916df7b5..b3cc51bb65d 100644 --- a/stan/math/prim/fun/fmod.hpp +++ b/stan/math/prim/fun/fmod.hpp @@ -38,8 +38,7 @@ inline auto fmod(T1&& a, T2&& b) { return apply_scalar_binary( [](auto&& c, auto&& d) { using std::fmod; - return fmod(std::forward(c), - std::forward(d)); + return fmod(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); } diff --git a/stan/math/prim/fun/gamma_p.hpp b/stan/math/prim/fun/gamma_p.hpp index 512d3c48526..59ca71a2e42 100644 --- a/stan/math/prim/fun/gamma_p.hpp +++ b/stan/math/prim/fun/gamma_p.hpp @@ -90,7 +90,8 @@ template * = nullptr> inline auto gamma_p(T1&& a, T2&& b) { return apply_scalar_binary( [](auto&& c, auto&& d) { - return gamma_p(std::forward(c), std::forward(d)); + return gamma_p(std::forward(c), + std::forward(d)); }, std::forward(a), std::forward(b)); } diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index 1c05385df37..0d710bf32bb 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -35,7 +35,8 @@ template < typename T, require_not_container_st* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto inv(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/inv_Phi.hpp b/stan/math/prim/fun/inv_Phi.hpp index 15b11207edc..4664c3e4944 100644 --- a/stan/math/prim/fun/inv_Phi.hpp +++ b/stan/math/prim/fun/inv_Phi.hpp @@ -178,7 +178,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto inv_Phi(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index cb31db2e6c2..33176f5181d 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -91,7 +91,8 @@ struct inv_cloglog_fun { */ template * = nullptr> inline auto inv_cloglog(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -106,8 +107,7 @@ template * = nullptr> inline auto inv_cloglog(Container&& x) { return apply_vector_unary>::apply( - std::forward(x), - [](const auto& v) { + std::forward(x), [](const auto& v) { return (1 - (-std::forward(v).array().exp()).exp()); }); } diff --git a/stan/math/prim/fun/inv_erfc.hpp b/stan/math/prim/fun/inv_erfc.hpp index 1afba170442..ffc560cf543 100644 --- a/stan/math/prim/fun/inv_erfc.hpp +++ b/stan/math/prim/fun/inv_erfc.hpp @@ -49,7 +49,8 @@ template < require_not_var_matrix_t* = nullptr, require_not_arithmetic_t* = nullptr> inline auto inv_erfc(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index bd8cc17904f..fe7163a4040 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -46,7 +46,8 @@ struct inv_sqrt_fun { */ template * = nullptr> inline auto inv_sqrt(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/inv_square.hpp b/stan/math/prim/fun/inv_square.hpp index 96ecdb3b877..79a2c755f35 100644 --- a/stan/math/prim/fun/inv_square.hpp +++ b/stan/math/prim/fun/inv_square.hpp @@ -36,8 +36,7 @@ template * = nullptr> inline auto inv_square(Container&& x) { return apply_vector_unary>::apply( - std::forward(x), - [](auto&& v) { + std::forward(x), [](auto&& v) { return std::forward(v).array().square().inverse(); }); } diff --git a/stan/math/prim/fun/lambert_w.hpp b/stan/math/prim/fun/lambert_w.hpp index 9fedeee69d2..7353a265c2e 100644 --- a/stan/math/prim/fun/lambert_w.hpp +++ b/stan/math/prim/fun/lambert_w.hpp @@ -81,7 +81,8 @@ struct lambert_wm1_fun { template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto lambert_w0(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -96,7 +97,8 @@ inline auto lambert_w0(T&& x) { template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto lambert_wm1(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/lgamma.hpp b/stan/math/prim/fun/lgamma.hpp index 6cfb99d20ec..6ce3826e504 100644 --- a/stan/math/prim/fun/lgamma.hpp +++ b/stan/math/prim/fun/lgamma.hpp @@ -117,7 +117,8 @@ struct lgamma_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto lgamma(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index fecc2368759..5d1dc4e4f12 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -59,7 +59,8 @@ struct log10_fun { */ template * = nullptr> inline auto log10(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/log1m.hpp b/stan/math/prim/fun/log1m.hpp index c092ea8873a..92d49c20118 100644 --- a/stan/math/prim/fun/log1m.hpp +++ b/stan/math/prim/fun/log1m.hpp @@ -71,7 +71,8 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1m_exp.hpp b/stan/math/prim/fun/log1m_exp.hpp index c79aed1b725..9771ed9734f 100644 --- a/stan/math/prim/fun/log1m_exp.hpp +++ b/stan/math/prim/fun/log1m_exp.hpp @@ -81,7 +81,8 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m_exp(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1p.hpp b/stan/math/prim/fun/log1p.hpp index e4d15264ec7..4f337a0dedc 100644 --- a/stan/math/prim/fun/log1p.hpp +++ b/stan/math/prim/fun/log1p.hpp @@ -80,7 +80,8 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log1p(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1p_exp.hpp b/stan/math/prim/fun/log1p_exp.hpp index 02df5805859..2180f24e2ea 100644 --- a/stan/math/prim/fun/log1p_exp.hpp +++ b/stan/math/prim/fun/log1p_exp.hpp @@ -76,7 +76,8 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log1p_exp(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log2.hpp b/stan/math/prim/fun/log2.hpp index ecd11406c56..d77bec26820 100644 --- a/stan/math/prim/fun/log2.hpp +++ b/stan/math/prim/fun/log2.hpp @@ -47,7 +47,8 @@ struct log2_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log2(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log_inv_logit.hpp b/stan/math/prim/fun/log_inv_logit.hpp index a02d4f376ae..11d0fa215fc 100644 --- a/stan/math/prim/fun/log_inv_logit.hpp +++ b/stan/math/prim/fun/log_inv_logit.hpp @@ -82,7 +82,8 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log_inv_logit(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log_softmax.hpp b/stan/math/prim/fun/log_softmax.hpp index 8471c2646ae..61ada1bb62c 100644 --- a/stan/math/prim/fun/log_softmax.hpp +++ b/stan/math/prim/fun/log_softmax.hpp @@ -46,8 +46,7 @@ inline auto log_softmax(Container&& x) { return make_holder( [](auto&& a) { return apply_vector_unary>>::apply( - std::forward(a), - [](const auto& v) { + std::forward(a), [](const auto& v) { return std::forward(v).array() - log_sum_exp(v); }); }, diff --git a/stan/math/prim/fun/log_sum_exp.hpp b/stan/math/prim/fun/log_sum_exp.hpp index 7d732b47c26..e58ab40a4ed 100644 --- a/stan/math/prim/fun/log_sum_exp.hpp +++ b/stan/math/prim/fun/log_sum_exp.hpp @@ -82,16 +82,16 @@ template * = nullptr> inline auto log_sum_exp(T&& x) { return apply_vector_unary>::reduce( std::forward(x), [&](auto&& v) { - if (v.size() == 0) { - return NEGATIVE_INFTY; - } - const auto& v_ref = to_ref(v); - const double max = v_ref.maxCoeff(); - if (!std::isfinite(max)) { - return max; - } - return max + std::log((v_ref.array() - max).exp().sum()); - }); + if (v.size() == 0) { + return NEGATIVE_INFTY; + } + const auto& v_ref = to_ref(v); + const double max = v_ref.maxCoeff(); + if (!std::isfinite(max)) { + return max; + } + return max + std::log((v_ref.array() - max).exp().sum()); + }); } /** diff --git a/stan/math/prim/fun/logit.hpp b/stan/math/prim/fun/logit.hpp index 82498e42135..f9e6bc3fb30 100644 --- a/stan/math/prim/fun/logit.hpp +++ b/stan/math/prim/fun/logit.hpp @@ -88,7 +88,8 @@ struct logit_fun { */ template * = nullptr> inline auto logit(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** @@ -108,11 +109,9 @@ inline auto logit(Container&& x) { return make_holder( [](auto&& v_ref) { return apply_vector_unary>>::apply( - std::forward(v_ref), - [](const auto& v) { - return ( - std::forward(v).array() - / (1 - std::forward(v).array())) + std::forward(v_ref), [](const auto& v) { + return (std::forward(v).array() + / (1 - std::forward(v).array())) .log(); }); }, diff --git a/stan/math/prim/fun/mean.hpp b/stan/math/prim/fun/mean.hpp index d9568fce905..6101263c91b 100644 --- a/stan/math/prim/fun/mean.hpp +++ b/stan/math/prim/fun/mean.hpp @@ -21,8 +21,8 @@ namespace math { template * = nullptr> inline return_type_t mean(T&& m) { check_nonzero_size("mean", "m", m); - return apply_vector_unary>::reduce(std::forward(m), - [](const auto& a) { return a.mean(); }); + return apply_vector_unary>::reduce( + std::forward(m), [](const auto& a) { return a.mean(); }); } } // namespace math diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index 4feae5426a2..58c8c682677 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -17,9 +17,9 @@ namespace math { template * = nullptr> inline auto minus(T&& x) { if constexpr (is_eigen_v) { - return make_holder([](auto&& xx) { - return -std::forward(xx); - }, std::forward(x)); + return make_holder( + [](auto&& xx) { return -std::forward(xx); }, + std::forward(x)); } else { return -x; } diff --git a/stan/math/prim/fun/pow.hpp b/stan/math/prim/fun/pow.hpp index 46d60cb00ef..90db4a4fc85 100644 --- a/stan/math/prim/fun/pow.hpp +++ b/stan/math/prim/fun/pow.hpp @@ -111,7 +111,10 @@ template * = nullptr, inline auto pow(T1&& a, T2&& b) { return apply_scalar_binary( // Qualified pow since only Arithmetic types are accepted here - [](auto&& c, auto&& d) { return stan::math::pow(std::forward(c), std::forward(d)); }, + [](auto&& c, auto&& d) { + return stan::math::pow(std::forward(c), + std::forward(d)); + }, std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/prim/fun/rank.hpp b/stan/math/prim/fun/rank.hpp index f3357d0424b..2b8fd90544d 100644 --- a/stan/math/prim/fun/rank.hpp +++ b/stan/math/prim/fun/rank.hpp @@ -20,10 +20,11 @@ template * = nullptr> inline int rank(C&& v, int s) { check_range("rank", "v", v.size(), s); --s; // adjust for indexing by one - return apply_vector_unary>::reduce(std::forward(v), [s](const auto& vec) { - auto&& vec_ref = to_ref(std::forward(vec)); - return (vec_ref.array() < vec_ref.coeff(s)).template cast().sum(); - }); + return apply_vector_unary>::reduce( + std::forward(v), [s](const auto& vec) { + auto&& vec_ref = to_ref(std::forward(vec)); + return (vec_ref.array() < vec_ref.coeff(s)).template cast().sum(); + }); } } // namespace math diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index c91673aea36..120e2fa8aec 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -37,7 +37,8 @@ template * = nullptr> inline auto round(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/sd.hpp b/stan/math/prim/fun/sd.hpp index b2b928403af..bee70a57c32 100644 --- a/stan/math/prim/fun/sd.hpp +++ b/stan/math/prim/fun/sd.hpp @@ -28,14 +28,14 @@ inline auto sd(T&& m) { return apply_vector_unary>::reduce( std::forward(m), [](auto&& x) { - check_nonzero_size("sd", "x", x); + check_nonzero_size("sd", "x", x); - if (x.size() == 1) { - return scalar_type_t(0.0); - } + if (x.size() == 1) { + return scalar_type_t(0.0); + } - return sqrt(variance(x)); - }); + return sqrt(variance(x)); + }); } } // namespace math diff --git a/stan/math/prim/fun/select.hpp b/stan/math/prim/fun/select.hpp index 0b551a1da6e..99f3a461061 100644 --- a/stan/math/prim/fun/select.hpp +++ b/stan/math/prim/fun/select.hpp @@ -23,8 +23,7 @@ namespace math { template , require_all_stan_scalar_t* = nullptr> -inline ReturnT select(const bool c, T_true&& y_true, - T_false&& y_false) { +inline ReturnT select(const bool c, T_true&& y_true, T_false&& y_false) { return c ? ReturnT(y_true) : ReturnT(y_false); } @@ -80,8 +79,7 @@ template >, require_container_t* = nullptr, require_stan_scalar_t* = nullptr> -inline ReturnT select(const bool c, T_true&& y_true, - T_false&& y_false) { +inline ReturnT select(const bool c, T_true&& y_true, T_false&& y_false) { if (c) { return y_true; } else { @@ -115,8 +113,7 @@ template >, require_stan_scalar_t* = nullptr, require_container_t* = nullptr> -inline ReturnT select(const bool c, const T_true y_true, - T_false&& y_false) { +inline ReturnT select(const bool c, const T_true y_true, T_false&& y_false) { if (c) { return apply_scalar_binary( [](auto&& y_true_inner, auto&& y_false_inner) { @@ -174,7 +171,10 @@ inline auto select(T_bool&& c, T_true&& y_true, T_false&& y_false) { check_consistent_sizes("select", "boolean", c, "left hand side", y_true, "right hand side", y_false); using ret_t = return_type_t; - return std::forward(c).select(std::forward(y_true), std::forward(y_false)).template cast().eval(); + return std::forward(c) + .select(std::forward(y_true), std::forward(y_false)) + .template cast() + .eval(); } } // namespace math diff --git a/stan/math/prim/fun/sign.hpp b/stan/math/prim/fun/sign.hpp index 50449bd0e2e..24e2f07b126 100644 --- a/stan/math/prim/fun/sign.hpp +++ b/stan/math/prim/fun/sign.hpp @@ -40,7 +40,8 @@ struct sign_fun { */ template * = nullptr> inline auto sign(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index c5bcb3c2f09..b7258a0622a 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -58,7 +58,8 @@ struct sinh_fun { */ template * = nullptr> inline auto sinh(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index b62a9f18aad..f68dd39d2a8 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -58,7 +58,8 @@ struct sqrt_fun { */ template * = nullptr> inline auto sqrt(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/square.hpp b/stan/math/prim/fun/square.hpp index a958b83b18a..c16f8bd7ad0 100644 --- a/stan/math/prim/fun/square.hpp +++ b/stan/math/prim/fun/square.hpp @@ -51,7 +51,8 @@ struct square_fun { */ template * = nullptr> inline auto square(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/step.hpp b/stan/math/prim/fun/step.hpp index 7398b4ba707..dcc7a8263bc 100644 --- a/stan/math/prim/fun/step.hpp +++ b/stan/math/prim/fun/step.hpp @@ -59,7 +59,8 @@ struct step_fun { */ template * = nullptr> inline auto step(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index ec0387365dd..9e73c8ffcfe 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -77,9 +77,7 @@ template >::apply( std::forward(x), - [](auto&& v) { - return std::forward(v).array().tan(); - }); + [](auto&& v) { return std::forward(v).array().tan(); }); } namespace internal { diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index cc5ee6b9cd3..c5fe4930240 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -60,7 +60,8 @@ struct tanh_fun { */ template * = nullptr> inline auto tanh(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } /** diff --git a/stan/math/prim/fun/tgamma.hpp b/stan/math/prim/fun/tgamma.hpp index 40d9249615e..354eb55de21 100644 --- a/stan/math/prim/fun/tgamma.hpp +++ b/stan/math/prim/fun/tgamma.hpp @@ -51,7 +51,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto tgamma(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/to_int.hpp b/stan/math/prim/fun/to_int.hpp index 0a1d54a4efc..a08474faa8a 100644 --- a/stan/math/prim/fun/to_int.hpp +++ b/stan/math/prim/fun/to_int.hpp @@ -72,7 +72,8 @@ struct to_int_fun { template * = nullptr> inline auto to_int(Container&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/trigamma.hpp b/stan/math/prim/fun/trigamma.hpp index fd62dfaab91..07dae79ef85 100644 --- a/stan/math/prim/fun/trigamma.hpp +++ b/stan/math/prim/fun/trigamma.hpp @@ -156,7 +156,8 @@ struct trigamma_fun { template * = nullptr> inline auto trigamma(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/trunc.hpp b/stan/math/prim/fun/trunc.hpp index 941461ea8e2..71c417a91fd 100644 --- a/stan/math/prim/fun/trunc.hpp +++ b/stan/math/prim/fun/trunc.hpp @@ -40,7 +40,8 @@ struct trunc_fun { template * = nullptr> inline auto trunc(T&& x) { - return apply_scalar_unary>::apply(std::forward(x)); + return apply_scalar_unary>::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/prim/functor/apply_scalar_binary.hpp b/stan/math/prim/functor/apply_scalar_binary.hpp index 77ea8369d27..b0f60807031 100644 --- a/stan/math/prim/functor/apply_scalar_binary.hpp +++ b/stan/math/prim/functor/apply_scalar_binary.hpp @@ -203,7 +203,8 @@ inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { return make_holder( [](auto&& f_inner, auto&& x_inner, auto&& y_inner) { return x_inner.unaryExpr( - [f_inner_ = std::forward(f_inner), y_inner](const auto& v) { return f_inner_(v, y_inner); }); + [f_inner_ = std::forward(f_inner), + y_inner](const auto& v) { return f_inner_(v, y_inner); }); }, std::forward(f), std::forward(x), std::forward(y)); } @@ -229,7 +230,10 @@ inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { return make_holder( [](auto&& f_inner, auto&& x_inner, auto&& y_inner) { return y_inner.unaryExpr( - [f_inner_ = std::forward(f_inner), x_inner](auto&& v) { return f_inner_(x_inner, std::forward(v)); }); + [f_inner_ = std::forward(f_inner), + x_inner](auto&& v) { + return f_inner_(x_inner, std::forward(v)); + }); }, std::forward(f), std::forward(x), std::forward(y)); } diff --git a/stan/math/prim/functor/apply_scalar_ternary.hpp b/stan/math/prim/functor/apply_scalar_ternary.hpp index e9dfcdab834..81dc4bf081e 100644 --- a/stan/math/prim/functor/apply_scalar_ternary.hpp +++ b/stan/math/prim/functor/apply_scalar_ternary.hpp @@ -158,10 +158,13 @@ inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, - T3&& z) { +inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { return apply_scalar_binary( - [f_ = std::forward(f), z](auto&& a, auto&& b) { return f_(std::forward(a), std::forward(b), z); }, std::forward(x), std::forward(y)); + [f_ = std::forward(f), z](auto&& a, auto&& b) { + return f_(std::forward(a), std::forward(b), + z); + }, + std::forward(x), std::forward(y)); } /** @@ -183,10 +186,12 @@ inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, - T3&& z) { +inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, T3&& z) { return apply_scalar_binary( - [f, y](auto&& a, auto&& c) { return f(std::forward(a), y, std::forward(c)); }, std::forward(x), std::forward(z)); + [f, y](auto&& a, auto&& c) { + return f(std::forward(a), y, std::forward(c)); + }, + std::forward(x), std::forward(z)); } /** @@ -208,10 +213,12 @@ inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, - T3&& z) { +inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, T3&& z) { return apply_scalar_binary( - [f, x](auto&& b, auto&& c) { return f(x, std::forward(b), std::forward(c)); }, std::forward(y), std::forward(z)); + [f, x](auto&& b, auto&& c) { + return f(x, std::forward(b), std::forward(c)); + }, + std::forward(y), std::forward(z)); } } // namespace math diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 2fae2be79f1..73deb13b607 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -60,11 +60,14 @@ struct apply_scalar_unary> { */ template static inline auto apply(TT&& x) { - return make_holder([](auto&& xx) { - return std::forward(xx).unaryExpr([](auto&& xxx) { - return apply_scalar_unary>::apply(xxx); - }); - }, std::forward(x)); + return make_holder( + [](auto&& xx) { + return std::forward(xx).unaryExpr([](auto&& xxx) { + return apply_scalar_unary>::apply( + xxx); + }); + }, + std::forward(x)); } /** diff --git a/stan/math/prim/functor/apply_vector_unary.hpp b/stan/math/prim/functor/apply_vector_unary.hpp index 2472e77c7d7..f4c1b2f00b4 100644 --- a/stan/math/prim/functor/apply_vector_unary.hpp +++ b/stan/math/prim/functor/apply_vector_unary.hpp @@ -44,9 +44,9 @@ struct apply_vector_unary> { require_t>>* = nullptr> static inline auto apply(T2&& x, F&& f) { if constexpr (is_eigen_array::value) { - return make_holder([](auto&& xx) { - return std::forward(xx).matrix(); - }, make_holder(std::forward(f), std::forward(x))); + return make_holder( + [](auto&& xx) { return std::forward(xx).matrix(); }, + make_holder(std::forward(f), std::forward(x))); } else { return make_holder(std::forward(f), std::forward(x)); } @@ -58,9 +58,9 @@ struct apply_vector_unary> { if constexpr (is_eigen_array::value) { return make_holder(std::forward(f), std::forward(x)); } else { - return make_holder([](auto&& xx) { - return std::forward(xx).array(); - }, make_holder(std::forward(f), std::forward(x))); + return make_holder( + [](auto&& xx) { return std::forward(xx).array(); }, + make_holder(std::forward(f), std::forward(x))); } } @@ -166,7 +166,8 @@ struct apply_vector_unary> { */ template static inline auto reduce(T2&& x, F&& f) { - return apply_vector_unary::reduce(as_column_vector_or_scalar(std::forward(x)), std::forward(f)); + return apply_vector_unary::reduce( + as_column_vector_or_scalar(std::forward(x)), std::forward(f)); } }; diff --git a/stan/math/prim/prob/std_normal_log_qf.hpp b/stan/math/prim/prob/std_normal_log_qf.hpp index 62c881674d1..d4815b38809 100644 --- a/stan/math/prim/prob/std_normal_log_qf.hpp +++ b/stan/math/prim/prob/std_normal_log_qf.hpp @@ -151,7 +151,8 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto std_normal_log_qf(T&& x) { - return apply_scalar_unary::apply(std::forward(x)); + return apply_scalar_unary::apply( + std::forward(x)); } } // namespace math diff --git a/stan/math/rev/fun/pow.hpp b/stan/math/rev/fun/pow.hpp index a72b5f04226..641272bd9cf 100644 --- a/stan/math/rev/fun/pow.hpp +++ b/stan/math/rev/fun/pow.hpp @@ -289,7 +289,11 @@ template * = nullptr, require_any_var_t, base_type_t>* = nullptr> inline auto pow(T1&& a, T2&& b) { return apply_scalar_binary( - [](auto&& c, auto&& d) { return stan::math::pow(std::forward(c), std::forward(d)); }, std::forward(a), std::forward(b)); + [](auto&& c, auto&& d) { + return stan::math::pow(std::forward(c), + std::forward(d)); + }, + std::forward(a), std::forward(b)); } } // namespace math diff --git a/stan/math/rev/fun/sd.hpp b/stan/math/rev/fun/sd.hpp index f143930802e..02c5bc7c609 100644 --- a/stan/math/rev/fun/sd.hpp +++ b/stan/math/rev/fun/sd.hpp @@ -95,7 +95,8 @@ var sd(const T& x) { */ template * = nullptr> auto sd(T&& m) { - return apply_vector_unary>::reduce(std::forward(m), [](const auto& x) { return sd(x); }); + return apply_vector_unary>::reduce( + std::forward(m), [](const auto& x) { return sd(x); }); } } // namespace math diff --git a/stan/math/rev/functor/apply_scalar_unary.hpp b/stan/math/rev/functor/apply_scalar_unary.hpp index 4213559b5db..eeba3e8cab9 100644 --- a/stan/math/rev/functor/apply_scalar_unary.hpp +++ b/stan/math/rev/functor/apply_scalar_unary.hpp @@ -45,7 +45,9 @@ struct apply_scalar_unary> { * @return Function applied to the variable. */ template - static inline return_t apply(T2&& x) { return F::fun(std::forward(x)); } + static inline return_t apply(T2&& x) { + return F::fun(std::forward(x)); + } }; } // namespace math From 3c9462e09848493ef348c683918861dd1bc83de7 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 30 Jun 2025 17:49:41 -0400 Subject: [PATCH 04/31] cleanup apply_scalar_unary --- stan/math/fwd/functor/apply_scalar_unary.hpp | 7 ++++--- stan/math/prim/fun/Phi.hpp | 2 +- stan/math/prim/fun/Phi_approx.hpp | 2 +- stan/math/prim/fun/abs.hpp | 2 +- stan/math/prim/fun/acos.hpp | 2 +- stan/math/prim/fun/acosh.hpp | 4 ++-- stan/math/prim/fun/asin.hpp | 2 +- stan/math/prim/fun/asinh.hpp | 4 ++-- stan/math/prim/fun/atan.hpp | 2 +- stan/math/prim/fun/atanh.hpp | 4 ++-- stan/math/prim/fun/cbrt.hpp | 2 +- stan/math/prim/fun/ceil.hpp | 2 +- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/fun/cosh.hpp | 2 +- stan/math/prim/fun/digamma.hpp | 2 +- stan/math/prim/fun/erf.hpp | 2 +- stan/math/prim/fun/erfc.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/exp2.hpp | 2 +- stan/math/prim/fun/expm1.hpp | 2 +- stan/math/prim/fun/fabs.hpp | 2 +- stan/math/prim/fun/floor.hpp | 2 +- stan/math/prim/fun/inv.hpp | 2 +- stan/math/prim/fun/inv_Phi.hpp | 2 +- stan/math/prim/fun/inv_cloglog.hpp | 2 +- stan/math/prim/fun/inv_erfc.hpp | 2 +- stan/math/prim/fun/inv_sqrt.hpp | 4 ++-- stan/math/prim/fun/lambert_w.hpp | 4 ++-- stan/math/prim/fun/lgamma.hpp | 2 +- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/log1m.hpp | 2 +- stan/math/prim/fun/log1m_exp.hpp | 2 +- stan/math/prim/fun/log1m_inv_logit.hpp | 2 +- stan/math/prim/fun/log1p.hpp | 2 +- stan/math/prim/fun/log1p_exp.hpp | 2 +- stan/math/prim/fun/log2.hpp | 2 +- stan/math/prim/fun/log_inv_logit.hpp | 2 +- stan/math/prim/fun/logit.hpp | 2 +- stan/math/prim/fun/round.hpp | 2 +- stan/math/prim/fun/sign.hpp | 2 +- stan/math/prim/fun/sin.hpp | 2 +- stan/math/prim/fun/sinh.hpp | 2 +- stan/math/prim/fun/sqrt.hpp | 2 +- stan/math/prim/fun/square.hpp | 2 +- stan/math/prim/fun/step.hpp | 2 +- stan/math/prim/fun/tan.hpp | 2 +- stan/math/prim/fun/tanh.hpp | 2 +- stan/math/prim/fun/tgamma.hpp | 2 +- stan/math/prim/fun/to_int.hpp | 2 +- stan/math/prim/fun/trigamma.hpp | 2 +- stan/math/prim/fun/trunc.hpp | 2 +- stan/math/prim/functor/apply_scalar_unary.hpp | 14 ++++++++------ stan/math/rev/functor/apply_scalar_unary.hpp | 8 ++++---- test/unit/math/test_ad_test.cpp | 5 ++--- 55 files changed, 74 insertions(+), 72 deletions(-) diff --git a/stan/math/fwd/functor/apply_scalar_unary.hpp b/stan/math/fwd/functor/apply_scalar_unary.hpp index 8f37e8f8a65..a26289f836d 100644 --- a/stan/math/fwd/functor/apply_scalar_unary.hpp +++ b/stan/math/fwd/functor/apply_scalar_unary.hpp @@ -18,12 +18,12 @@ namespace math { * autodiff variable. */ template -struct apply_scalar_unary > { +struct apply_scalar_unary> { /** * Function return type, which is same as the argument type for * the function, fvar<T>. */ - using return_t = fvar; + using return_t = std::decay_t; /** * Apply the function specified by F to the specified argument. @@ -31,7 +31,8 @@ struct apply_scalar_unary > { * @param x Argument variable. * @return Function applied to the variable. */ - static inline return_t apply(const fvar& x) { return F::fun(x); } + template + static inline auto apply(const T2& x) { return F::fun(x); } }; } // namespace math diff --git a/stan/math/prim/fun/Phi.hpp b/stan/math/prim/fun/Phi.hpp index 14454e98baf..c64523f232a 100644 --- a/stan/math/prim/fun/Phi.hpp +++ b/stan/math/prim/fun/Phi.hpp @@ -68,7 +68,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto Phi(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/Phi_approx.hpp b/stan/math/prim/fun/Phi_approx.hpp index 465b738abd1..51a7956f578 100644 --- a/stan/math/prim/fun/Phi_approx.hpp +++ b/stan/math/prim/fun/Phi_approx.hpp @@ -67,7 +67,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto Phi_approx(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/abs.hpp b/stan/math/prim/fun/abs.hpp index 113471b2367..fa13ff2b853 100644 --- a/stan/math/prim/fun/abs.hpp +++ b/stan/math/prim/fun/abs.hpp @@ -64,7 +64,7 @@ struct abs_fun { */ template * = nullptr> inline auto abs(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index 097350a292b..b78663a587a 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -66,7 +66,7 @@ struct acos_fun { */ template * = nullptr> inline auto acos(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/acosh.hpp b/stan/math/prim/fun/acosh.hpp index 4ff5f7a3df9..cc17dc4f8da 100644 --- a/stan/math/prim/fun/acosh.hpp +++ b/stan/math/prim/fun/acosh.hpp @@ -79,7 +79,7 @@ struct acosh_fun { */ template * = nullptr> inline auto acosh(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } @@ -96,7 +96,7 @@ inline auto acosh(T&& x) { template * = nullptr> inline auto acosh(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index ad6b03285a3..feb756fb4fd 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -64,7 +64,7 @@ struct asin_fun { */ template * = nullptr> inline auto asin(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/asinh.hpp b/stan/math/prim/fun/asinh.hpp index 6db8b657b0a..499b8a14d16 100644 --- a/stan/math/prim/fun/asinh.hpp +++ b/stan/math/prim/fun/asinh.hpp @@ -67,7 +67,7 @@ struct asinh_fun { */ template * = nullptr> inline auto asinh(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } @@ -82,7 +82,7 @@ inline auto asinh(T&& x) { template * = nullptr> inline auto asinh(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 976298b1a62..8366ba41428 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -62,7 +62,7 @@ struct atan_fun { */ template * = nullptr> inline auto atan(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/atanh.hpp b/stan/math/prim/fun/atanh.hpp index 0105fe66d02..1d36ed9b469 100644 --- a/stan/math/prim/fun/atanh.hpp +++ b/stan/math/prim/fun/atanh.hpp @@ -78,7 +78,7 @@ struct atanh_fun { */ template * = nullptr> inline auto atanh(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } @@ -95,7 +95,7 @@ inline auto atanh(T&& x) { template * = nullptr> inline auto atanh(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/cbrt.hpp b/stan/math/prim/fun/cbrt.hpp index 900ddb1178a..7282bd6bb33 100644 --- a/stan/math/prim/fun/cbrt.hpp +++ b/stan/math/prim/fun/cbrt.hpp @@ -35,7 +35,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto cbrt(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index 74fb7f4d097..2185a50b313 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -38,7 +38,7 @@ template * = nullptr> inline auto ceil(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index d35764ebcfb..ae8eb3b275e 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -61,7 +61,7 @@ struct cos_fun { */ template * = nullptr> inline auto cos(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index f909bbf859e..8a6bae2571b 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -60,7 +60,7 @@ struct cosh_fun { */ template * = nullptr> inline auto cosh(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/digamma.hpp b/stan/math/prim/fun/digamma.hpp index 7fd91478025..10247dcb178 100644 --- a/stan/math/prim/fun/digamma.hpp +++ b/stan/math/prim/fun/digamma.hpp @@ -75,7 +75,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto digamma(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/erf.hpp b/stan/math/prim/fun/erf.hpp index 1a91648a42d..3b123411da4 100644 --- a/stan/math/prim/fun/erf.hpp +++ b/stan/math/prim/fun/erf.hpp @@ -36,7 +36,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto erf(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/erfc.hpp b/stan/math/prim/fun/erfc.hpp index 9195b970e96..549ca8a94b5 100644 --- a/stan/math/prim/fun/erfc.hpp +++ b/stan/math/prim/fun/erfc.hpp @@ -37,7 +37,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto erfc(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 51e37f9e559..01ec45e5888 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -69,7 +69,7 @@ struct exp_fun { */ template * = nullptr> inline auto exp(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/exp2.hpp b/stan/math/prim/fun/exp2.hpp index ce5fd964e92..ec2ae69bcc4 100644 --- a/stan/math/prim/fun/exp2.hpp +++ b/stan/math/prim/fun/exp2.hpp @@ -40,7 +40,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto exp2(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/expm1.hpp b/stan/math/prim/fun/expm1.hpp index 1cdbdde4ed7..c37ff036a8a 100644 --- a/stan/math/prim/fun/expm1.hpp +++ b/stan/math/prim/fun/expm1.hpp @@ -37,7 +37,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto expm1(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 476fb1e7ed7..51bb3f554bd 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -49,7 +49,7 @@ template * = nullptr, require_not_stan_scalar_t* = nullptr> inline auto fabs(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index f683c5e5130..09e7828acab 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -43,7 +43,7 @@ struct floor_fun { */ template * = nullptr> inline auto floor(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index 0d710bf32bb..5789f3792a8 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -35,7 +35,7 @@ template < typename T, require_not_container_st* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto inv(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/inv_Phi.hpp b/stan/math/prim/fun/inv_Phi.hpp index 4664c3e4944..b7a81c4d86f 100644 --- a/stan/math/prim/fun/inv_Phi.hpp +++ b/stan/math/prim/fun/inv_Phi.hpp @@ -178,7 +178,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto inv_Phi(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index 33176f5181d..714cef57b7c 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -91,7 +91,7 @@ struct inv_cloglog_fun { */ template * = nullptr> inline auto inv_cloglog(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/inv_erfc.hpp b/stan/math/prim/fun/inv_erfc.hpp index ffc560cf543..9dce6761221 100644 --- a/stan/math/prim/fun/inv_erfc.hpp +++ b/stan/math/prim/fun/inv_erfc.hpp @@ -49,7 +49,7 @@ template < require_not_var_matrix_t* = nullptr, require_not_arithmetic_t* = nullptr> inline auto inv_erfc(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index fe7163a4040..97602360827 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -46,7 +46,7 @@ struct inv_sqrt_fun { */ template * = nullptr> inline auto inv_sqrt(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } @@ -64,7 +64,7 @@ inline auto inv_sqrt(Container&& x) { // Eigen 3.4.0 has precision issues on ARM64 with vectorised rsqrt // Resolved in current master branch, below can be removed on next release #ifdef __aarch64__ - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); #else return apply_vector_unary>::apply( diff --git a/stan/math/prim/fun/lambert_w.hpp b/stan/math/prim/fun/lambert_w.hpp index 7353a265c2e..11be58201c9 100644 --- a/stan/math/prim/fun/lambert_w.hpp +++ b/stan/math/prim/fun/lambert_w.hpp @@ -81,7 +81,7 @@ struct lambert_wm1_fun { template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto lambert_w0(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } @@ -97,7 +97,7 @@ inline auto lambert_w0(T&& x) { template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto lambert_wm1(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/lgamma.hpp b/stan/math/prim/fun/lgamma.hpp index 6ce3826e504..bba400f83cd 100644 --- a/stan/math/prim/fun/lgamma.hpp +++ b/stan/math/prim/fun/lgamma.hpp @@ -117,7 +117,7 @@ struct lgamma_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto lgamma(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index 45621a07594..d3df5d3c82d 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -68,7 +68,7 @@ struct log_fun { */ template * = nullptr> inline auto log(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index 5d1dc4e4f12..6b928c39fd1 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -59,7 +59,7 @@ struct log10_fun { */ template * = nullptr> inline auto log10(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log1m.hpp b/stan/math/prim/fun/log1m.hpp index 92d49c20118..d2bb1d99d77 100644 --- a/stan/math/prim/fun/log1m.hpp +++ b/stan/math/prim/fun/log1m.hpp @@ -71,7 +71,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log1m_exp.hpp b/stan/math/prim/fun/log1m_exp.hpp index 9771ed9734f..ceb2ac42b98 100644 --- a/stan/math/prim/fun/log1m_exp.hpp +++ b/stan/math/prim/fun/log1m_exp.hpp @@ -81,7 +81,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m_exp(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log1m_inv_logit.hpp b/stan/math/prim/fun/log1m_inv_logit.hpp index 3554fe33f7d..c43590d646d 100644 --- a/stan/math/prim/fun/log1m_inv_logit.hpp +++ b/stan/math/prim/fun/log1m_inv_logit.hpp @@ -83,7 +83,7 @@ struct log1m_inv_logit_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m_inv_logit(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log1p.hpp b/stan/math/prim/fun/log1p.hpp index 4f337a0dedc..45318dac93b 100644 --- a/stan/math/prim/fun/log1p.hpp +++ b/stan/math/prim/fun/log1p.hpp @@ -80,7 +80,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log1p(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log1p_exp.hpp b/stan/math/prim/fun/log1p_exp.hpp index 2180f24e2ea..59809a69726 100644 --- a/stan/math/prim/fun/log1p_exp.hpp +++ b/stan/math/prim/fun/log1p_exp.hpp @@ -76,7 +76,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log1p_exp(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log2.hpp b/stan/math/prim/fun/log2.hpp index d77bec26820..d90012d6a68 100644 --- a/stan/math/prim/fun/log2.hpp +++ b/stan/math/prim/fun/log2.hpp @@ -47,7 +47,7 @@ struct log2_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log2(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/log_inv_logit.hpp b/stan/math/prim/fun/log_inv_logit.hpp index 11d0fa215fc..12f709d5081 100644 --- a/stan/math/prim/fun/log_inv_logit.hpp +++ b/stan/math/prim/fun/log_inv_logit.hpp @@ -82,7 +82,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log_inv_logit(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/logit.hpp b/stan/math/prim/fun/logit.hpp index f9e6bc3fb30..22fab56a3b1 100644 --- a/stan/math/prim/fun/logit.hpp +++ b/stan/math/prim/fun/logit.hpp @@ -88,7 +88,7 @@ struct logit_fun { */ template * = nullptr> inline auto logit(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index 120e2fa8aec..09e8a7d8ac6 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -37,7 +37,7 @@ template * = nullptr> inline auto round(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/sign.hpp b/stan/math/prim/fun/sign.hpp index 24e2f07b126..bebe18b6861 100644 --- a/stan/math/prim/fun/sign.hpp +++ b/stan/math/prim/fun/sign.hpp @@ -40,7 +40,7 @@ struct sign_fun { */ template * = nullptr> inline auto sign(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index f9b05bdc91d..185efbb18ee 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -60,7 +60,7 @@ struct sin_fun { */ template * = nullptr> inline auto sin(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index b7258a0622a..a6a8f93c09d 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -58,7 +58,7 @@ struct sinh_fun { */ template * = nullptr> inline auto sinh(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index f68dd39d2a8..75c680373a2 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -58,7 +58,7 @@ struct sqrt_fun { */ template * = nullptr> inline auto sqrt(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/square.hpp b/stan/math/prim/fun/square.hpp index c16f8bd7ad0..10859a54beb 100644 --- a/stan/math/prim/fun/square.hpp +++ b/stan/math/prim/fun/square.hpp @@ -51,7 +51,7 @@ struct square_fun { */ template * = nullptr> inline auto square(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/step.hpp b/stan/math/prim/fun/step.hpp index dcc7a8263bc..fe5f1ce2ca6 100644 --- a/stan/math/prim/fun/step.hpp +++ b/stan/math/prim/fun/step.hpp @@ -59,7 +59,7 @@ struct step_fun { */ template * = nullptr> inline auto step(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index 9e73c8ffcfe..9c1d758bc3a 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -60,7 +60,7 @@ struct tan_fun { */ template * = nullptr> inline auto tan(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index c5fe4930240..bcc25653ab3 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -60,7 +60,7 @@ struct tanh_fun { */ template * = nullptr> inline auto tanh(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/tgamma.hpp b/stan/math/prim/fun/tgamma.hpp index 354eb55de21..fcee0906258 100644 --- a/stan/math/prim/fun/tgamma.hpp +++ b/stan/math/prim/fun/tgamma.hpp @@ -51,7 +51,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto tgamma(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/to_int.hpp b/stan/math/prim/fun/to_int.hpp index a08474faa8a..29db2daa96b 100644 --- a/stan/math/prim/fun/to_int.hpp +++ b/stan/math/prim/fun/to_int.hpp @@ -72,7 +72,7 @@ struct to_int_fun { template * = nullptr> inline auto to_int(Container&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/trigamma.hpp b/stan/math/prim/fun/trigamma.hpp index 07dae79ef85..0f0b2e5a23c 100644 --- a/stan/math/prim/fun/trigamma.hpp +++ b/stan/math/prim/fun/trigamma.hpp @@ -156,7 +156,7 @@ struct trigamma_fun { template * = nullptr> inline auto trigamma(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/fun/trunc.hpp b/stan/math/prim/fun/trunc.hpp index 71c417a91fd..bce409943fa 100644 --- a/stan/math/prim/fun/trunc.hpp +++ b/stan/math/prim/fun/trunc.hpp @@ -40,7 +40,7 @@ struct trunc_fun { template * = nullptr> inline auto trunc(T&& x) { - return apply_scalar_unary>::apply( + return apply_scalar_unary::apply( std::forward(x)); } diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 73deb13b607..94d0ea6838a 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -63,8 +63,7 @@ struct apply_scalar_unary> { return make_holder( [](auto&& xx) { return std::forward(xx).unaryExpr([](auto&& xxx) { - return apply_scalar_unary>::apply( - xxx); + return apply_scalar_unary::apply(xxx); }); }, std::forward(x)); @@ -75,7 +74,7 @@ struct apply_scalar_unary> { * expression template of type T. */ using return_t = std::decay_t>::apply(std::declval()))>; + apply_scalar_unary::apply(std::declval()))>; }; /** @@ -101,7 +100,8 @@ struct apply_scalar_unary> { * @param x Argument scalar. * @return Result of applying F to the scalar. */ - static inline auto apply(T x) { return F::fun(x); } + template + static inline auto apply(T2 x) { return F::fun(x); } }; /** @@ -121,7 +121,8 @@ struct apply_scalar_unary> { * @param x Argument scalar. * @return Result of applying F to the scalar. */ - static inline auto apply(const std::decay_t& x) { return F::fun(x); } + template + static inline auto apply(const T2& x) { return F::fun(x); } /** * The return type */ @@ -148,7 +149,8 @@ struct apply_scalar_unary> { * @param x Argument scalar. * @return Result of applying F to the scalar. */ - static inline auto apply(T x) { return F::fun(x); } + template + static inline auto apply(T2 x) { return F::fun(x); } /** * The return type, double. */ diff --git a/stan/math/rev/functor/apply_scalar_unary.hpp b/stan/math/rev/functor/apply_scalar_unary.hpp index eeba3e8cab9..78b159e6549 100644 --- a/stan/math/rev/functor/apply_scalar_unary.hpp +++ b/stan/math/rev/functor/apply_scalar_unary.hpp @@ -15,8 +15,8 @@ namespace math { * * @tparam F Type of function to apply. */ -template -struct apply_scalar_unary { +template +struct apply_scalar_unary::value && is_stan_scalar::value>> { /** * Function return type, which is var. */ @@ -28,7 +28,7 @@ struct apply_scalar_unary { * @param x Argument variable. * @return Function applied to the variable. */ - static inline return_t apply(const var& x) { return F::fun(x); } + static inline auto apply(const var& x) { return F::fun(x); } }; template @@ -45,7 +45,7 @@ struct apply_scalar_unary> { * @return Function applied to the variable. */ template - static inline return_t apply(T2&& x) { + static inline auto apply(T2&& x) { return F::fun(std::forward(x)); } }; diff --git a/test/unit/math/test_ad_test.cpp b/test/unit/math/test_ad_test.cpp index 65d42abc3f1..d77fdaafd30 100644 --- a/test/unit/math/test_ad_test.cpp +++ b/test/unit/math/test_ad_test.cpp @@ -274,9 +274,8 @@ struct baz_fun { }; template -inline typename stan::math::apply_scalar_unary::return_t baz( - const T& x) { - return stan::math::apply_scalar_unary::apply(x); +inline auto baz(T&& x) { + return stan::math::apply_scalar_unary::apply(std::forward(x)); } TEST(testAd, integerGetsPassedVectorized) { From 5dd0f62fbdc02d5e21c757742bac67abd65dc174 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 30 Jun 2025 17:51:21 -0400 Subject: [PATCH 05/31] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- stan/math/fwd/functor/apply_scalar_unary.hpp | 4 +++- stan/math/prim/fun/Phi.hpp | 3 +-- stan/math/prim/fun/Phi_approx.hpp | 3 +-- stan/math/prim/fun/acosh.hpp | 3 +-- stan/math/prim/fun/asinh.hpp | 3 +-- stan/math/prim/fun/atanh.hpp | 3 +-- stan/math/prim/fun/cbrt.hpp | 3 +-- stan/math/prim/fun/digamma.hpp | 3 +-- stan/math/prim/fun/erf.hpp | 3 +-- stan/math/prim/fun/erfc.hpp | 3 +-- stan/math/prim/fun/exp2.hpp | 3 +-- stan/math/prim/fun/expm1.hpp | 3 +-- stan/math/prim/fun/inv.hpp | 3 +-- stan/math/prim/fun/inv_Phi.hpp | 3 +-- stan/math/prim/fun/inv_erfc.hpp | 3 +-- stan/math/prim/fun/inv_sqrt.hpp | 2 +- stan/math/prim/fun/lgamma.hpp | 3 +-- stan/math/prim/fun/log1m.hpp | 3 +-- stan/math/prim/fun/log1m_exp.hpp | 3 +-- stan/math/prim/fun/log1m_inv_logit.hpp | 3 +-- stan/math/prim/fun/log1p.hpp | 3 +-- stan/math/prim/fun/log1p_exp.hpp | 3 +-- stan/math/prim/fun/log2.hpp | 3 +-- stan/math/prim/fun/log_inv_logit.hpp | 3 +-- stan/math/prim/fun/sign.hpp | 3 +-- stan/math/prim/fun/sin.hpp | 3 +-- stan/math/prim/fun/step.hpp | 3 +-- stan/math/prim/fun/tgamma.hpp | 3 +-- stan/math/prim/fun/trigamma.hpp | 3 +-- stan/math/prim/fun/trunc.hpp | 3 +-- stan/math/prim/functor/apply_scalar_unary.hpp | 12 +++++++++--- stan/math/rev/functor/apply_scalar_unary.hpp | 3 ++- 32 files changed, 43 insertions(+), 62 deletions(-) diff --git a/stan/math/fwd/functor/apply_scalar_unary.hpp b/stan/math/fwd/functor/apply_scalar_unary.hpp index a26289f836d..ac9b067ee55 100644 --- a/stan/math/fwd/functor/apply_scalar_unary.hpp +++ b/stan/math/fwd/functor/apply_scalar_unary.hpp @@ -32,7 +32,9 @@ struct apply_scalar_unary> { * @return Function applied to the variable. */ template - static inline auto apply(const T2& x) { return F::fun(x); } + static inline auto apply(const T2& x) { + return F::fun(x); + } }; } // namespace math diff --git a/stan/math/prim/fun/Phi.hpp b/stan/math/prim/fun/Phi.hpp index c64523f232a..5c43bf30df1 100644 --- a/stan/math/prim/fun/Phi.hpp +++ b/stan/math/prim/fun/Phi.hpp @@ -68,8 +68,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto Phi(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/Phi_approx.hpp b/stan/math/prim/fun/Phi_approx.hpp index 51a7956f578..0fd1ce6a5ae 100644 --- a/stan/math/prim/fun/Phi_approx.hpp +++ b/stan/math/prim/fun/Phi_approx.hpp @@ -67,8 +67,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto Phi_approx(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/acosh.hpp b/stan/math/prim/fun/acosh.hpp index cc17dc4f8da..a85097c236b 100644 --- a/stan/math/prim/fun/acosh.hpp +++ b/stan/math/prim/fun/acosh.hpp @@ -79,8 +79,7 @@ struct acosh_fun { */ template * = nullptr> inline auto acosh(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } /** diff --git a/stan/math/prim/fun/asinh.hpp b/stan/math/prim/fun/asinh.hpp index 499b8a14d16..fda581b1c86 100644 --- a/stan/math/prim/fun/asinh.hpp +++ b/stan/math/prim/fun/asinh.hpp @@ -67,8 +67,7 @@ struct asinh_fun { */ template * = nullptr> inline auto asinh(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } /** diff --git a/stan/math/prim/fun/atanh.hpp b/stan/math/prim/fun/atanh.hpp index 1d36ed9b469..c879135575c 100644 --- a/stan/math/prim/fun/atanh.hpp +++ b/stan/math/prim/fun/atanh.hpp @@ -78,8 +78,7 @@ struct atanh_fun { */ template * = nullptr> inline auto atanh(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } /** diff --git a/stan/math/prim/fun/cbrt.hpp b/stan/math/prim/fun/cbrt.hpp index 7282bd6bb33..5cbe85b6276 100644 --- a/stan/math/prim/fun/cbrt.hpp +++ b/stan/math/prim/fun/cbrt.hpp @@ -35,8 +35,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto cbrt(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/digamma.hpp b/stan/math/prim/fun/digamma.hpp index 10247dcb178..dc9d3e57645 100644 --- a/stan/math/prim/fun/digamma.hpp +++ b/stan/math/prim/fun/digamma.hpp @@ -75,8 +75,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto digamma(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/erf.hpp b/stan/math/prim/fun/erf.hpp index 3b123411da4..51cab1c72f0 100644 --- a/stan/math/prim/fun/erf.hpp +++ b/stan/math/prim/fun/erf.hpp @@ -36,8 +36,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto erf(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/erfc.hpp b/stan/math/prim/fun/erfc.hpp index 549ca8a94b5..53955a11161 100644 --- a/stan/math/prim/fun/erfc.hpp +++ b/stan/math/prim/fun/erfc.hpp @@ -37,8 +37,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto erfc(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/exp2.hpp b/stan/math/prim/fun/exp2.hpp index ec2ae69bcc4..4f6dff50e9e 100644 --- a/stan/math/prim/fun/exp2.hpp +++ b/stan/math/prim/fun/exp2.hpp @@ -40,8 +40,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto exp2(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/expm1.hpp b/stan/math/prim/fun/expm1.hpp index c37ff036a8a..ce0fefa8b91 100644 --- a/stan/math/prim/fun/expm1.hpp +++ b/stan/math/prim/fun/expm1.hpp @@ -37,8 +37,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto expm1(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index 5789f3792a8..72d2cd26733 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -35,8 +35,7 @@ template < typename T, require_not_container_st* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto inv(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } /** diff --git a/stan/math/prim/fun/inv_Phi.hpp b/stan/math/prim/fun/inv_Phi.hpp index b7a81c4d86f..b4f295bd315 100644 --- a/stan/math/prim/fun/inv_Phi.hpp +++ b/stan/math/prim/fun/inv_Phi.hpp @@ -178,8 +178,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto inv_Phi(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/inv_erfc.hpp b/stan/math/prim/fun/inv_erfc.hpp index 9dce6761221..1b105c1ba87 100644 --- a/stan/math/prim/fun/inv_erfc.hpp +++ b/stan/math/prim/fun/inv_erfc.hpp @@ -49,8 +49,7 @@ template < require_not_var_matrix_t* = nullptr, require_not_arithmetic_t* = nullptr> inline auto inv_erfc(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/inv_sqrt.hpp b/stan/math/prim/fun/inv_sqrt.hpp index 97602360827..527751a709a 100644 --- a/stan/math/prim/fun/inv_sqrt.hpp +++ b/stan/math/prim/fun/inv_sqrt.hpp @@ -67,7 +67,7 @@ inline auto inv_sqrt(Container&& x) { return apply_scalar_unary::apply( std::forward(x)); #else - return apply_vector_unary>::apply( + return apply_vector_unary >::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().rsqrt(); }); #endif diff --git a/stan/math/prim/fun/lgamma.hpp b/stan/math/prim/fun/lgamma.hpp index bba400f83cd..4540a341e2c 100644 --- a/stan/math/prim/fun/lgamma.hpp +++ b/stan/math/prim/fun/lgamma.hpp @@ -117,8 +117,7 @@ struct lgamma_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto lgamma(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1m.hpp b/stan/math/prim/fun/log1m.hpp index d2bb1d99d77..3943c16dbb5 100644 --- a/stan/math/prim/fun/log1m.hpp +++ b/stan/math/prim/fun/log1m.hpp @@ -71,8 +71,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1m_exp.hpp b/stan/math/prim/fun/log1m_exp.hpp index ceb2ac42b98..737a0bc4080 100644 --- a/stan/math/prim/fun/log1m_exp.hpp +++ b/stan/math/prim/fun/log1m_exp.hpp @@ -81,8 +81,7 @@ template < typename T, require_not_var_matrix_t* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m_exp(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1m_inv_logit.hpp b/stan/math/prim/fun/log1m_inv_logit.hpp index c43590d646d..fed956ea3bd 100644 --- a/stan/math/prim/fun/log1m_inv_logit.hpp +++ b/stan/math/prim/fun/log1m_inv_logit.hpp @@ -83,8 +83,7 @@ struct log1m_inv_logit_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log1m_inv_logit(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1p.hpp b/stan/math/prim/fun/log1p.hpp index 45318dac93b..66752bfeca6 100644 --- a/stan/math/prim/fun/log1p.hpp +++ b/stan/math/prim/fun/log1p.hpp @@ -80,8 +80,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log1p(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log1p_exp.hpp b/stan/math/prim/fun/log1p_exp.hpp index 59809a69726..d69a1d1c059 100644 --- a/stan/math/prim/fun/log1p_exp.hpp +++ b/stan/math/prim/fun/log1p_exp.hpp @@ -76,8 +76,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log1p_exp(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log2.hpp b/stan/math/prim/fun/log2.hpp index d90012d6a68..ab294e80a5b 100644 --- a/stan/math/prim/fun/log2.hpp +++ b/stan/math/prim/fun/log2.hpp @@ -47,8 +47,7 @@ struct log2_fun { template * = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> inline auto log2(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/log_inv_logit.hpp b/stan/math/prim/fun/log_inv_logit.hpp index 12f709d5081..002a02a22c1 100644 --- a/stan/math/prim/fun/log_inv_logit.hpp +++ b/stan/math/prim/fun/log_inv_logit.hpp @@ -82,8 +82,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto log_inv_logit(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/sign.hpp b/stan/math/prim/fun/sign.hpp index bebe18b6861..3d525c1afef 100644 --- a/stan/math/prim/fun/sign.hpp +++ b/stan/math/prim/fun/sign.hpp @@ -40,8 +40,7 @@ struct sign_fun { */ template * = nullptr> inline auto sign(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index 185efbb18ee..850bbcabc9f 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -60,8 +60,7 @@ struct sin_fun { */ template * = nullptr> inline auto sin(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } /** diff --git a/stan/math/prim/fun/step.hpp b/stan/math/prim/fun/step.hpp index fe5f1ce2ca6..d2663f0b2ee 100644 --- a/stan/math/prim/fun/step.hpp +++ b/stan/math/prim/fun/step.hpp @@ -59,8 +59,7 @@ struct step_fun { */ template * = nullptr> inline auto step(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/tgamma.hpp b/stan/math/prim/fun/tgamma.hpp index fcee0906258..a37da4ef511 100644 --- a/stan/math/prim/fun/tgamma.hpp +++ b/stan/math/prim/fun/tgamma.hpp @@ -51,8 +51,7 @@ template < require_all_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr, require_not_var_matrix_t* = nullptr> inline auto tgamma(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/trigamma.hpp b/stan/math/prim/fun/trigamma.hpp index 0f0b2e5a23c..372eb6cbc6c 100644 --- a/stan/math/prim/fun/trigamma.hpp +++ b/stan/math/prim/fun/trigamma.hpp @@ -156,8 +156,7 @@ struct trigamma_fun { template * = nullptr> inline auto trigamma(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/trunc.hpp b/stan/math/prim/fun/trunc.hpp index bce409943fa..631e7e44deb 100644 --- a/stan/math/prim/fun/trunc.hpp +++ b/stan/math/prim/fun/trunc.hpp @@ -40,8 +40,7 @@ struct trunc_fun { template * = nullptr> inline auto trunc(T&& x) { - return apply_scalar_unary::apply( - std::forward(x)); + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index 94d0ea6838a..cad17ca8301 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -101,7 +101,9 @@ struct apply_scalar_unary> { * @return Result of applying F to the scalar. */ template - static inline auto apply(T2 x) { return F::fun(x); } + static inline auto apply(T2 x) { + return F::fun(x); + } }; /** @@ -122,7 +124,9 @@ struct apply_scalar_unary> { * @return Result of applying F to the scalar. */ template - static inline auto apply(const T2& x) { return F::fun(x); } + static inline auto apply(const T2& x) { + return F::fun(x); + } /** * The return type */ @@ -150,7 +154,9 @@ struct apply_scalar_unary> { * @return Result of applying F to the scalar. */ template - static inline auto apply(T2 x) { return F::fun(x); } + static inline auto apply(T2 x) { + return F::fun(x); + } /** * The return type, double. */ diff --git a/stan/math/rev/functor/apply_scalar_unary.hpp b/stan/math/rev/functor/apply_scalar_unary.hpp index 78b159e6549..d00f8092d9a 100644 --- a/stan/math/rev/functor/apply_scalar_unary.hpp +++ b/stan/math/rev/functor/apply_scalar_unary.hpp @@ -16,7 +16,8 @@ namespace math { * @tparam F Type of function to apply. */ template -struct apply_scalar_unary::value && is_stan_scalar::value>> { +struct apply_scalar_unary< + F, T, std::enable_if_t::value && is_stan_scalar::value>> { /** * Function return type, which is var. */ From 589895dff3d7a184d678ea62c86f485ba7e55ab0 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 30 Jun 2025 20:08:33 -0400 Subject: [PATCH 06/31] update sum_to_zero --- .../rev/constraint/sum_to_zero_constrain.hpp | 29 ------------------- 1 file changed, 29 deletions(-) diff --git a/stan/math/rev/constraint/sum_to_zero_constrain.hpp b/stan/math/rev/constraint/sum_to_zero_constrain.hpp index 5e62b560894..e0288e7370e 100644 --- a/stan/math/rev/constraint/sum_to_zero_constrain.hpp +++ b/stan/math/rev/constraint/sum_to_zero_constrain.hpp @@ -138,35 +138,6 @@ inline auto sum_to_zero_constrain(T&& x) { return arena_z; } -/** - * Return a vector with sum zero corresponding to the specified - * free vector. - * - * The sum-to-zero transform is defined using a modified version of - * the inverse of the isometric log ratio transform (ILR). - * See: - * Egozcue, Juan Jose; Pawlowsky-Glahn, Vera; Mateu-Figueras, Gloria; - * Barcelo-Vidal, Carles (2003), "Isometric logratio transformations for - * compositional data analysis", Mathematical Geology, 35 (3): 279–300, - * doi:10.1023/A:1023818214614, S2CID 122844634 - * - * This implementation is closer to the description of the same using "pivot - * coordinates" in - * Filzmoser, P., Hron, K., Templ, M. (2018). Geometrical Properties of - * Compositional Data. In: Applied Compositional Data Analysis. Springer Series - * in Statistics. Springer, Cham. https://doi.org/10.1007/978-3-319-96422-5_3 - * - * This is a linear transform, with no Jacobian. - * - * @tparam T type of the vector or matrix - * @param y Free vector or matrix. - * @param lp unused - * @return Zero-sum vector or matrix which is one larger in each dimension - */ -template * = nullptr> -inline auto sum_to_zero_constrain(T&& y, Lp& lp) { - return sum_to_zero_constrain(std::forward(y)); -} } // namespace math } // namespace stan From deec65ae52c3e50a2b3025e6583246cd804a09d6 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 30 Jun 2025 20:09:36 -0400 Subject: [PATCH 07/31] [Jenkins] auto-formatting by clang-format version 10.0.0-4ubuntu1 --- stan/math/rev/constraint/sum_to_zero_constrain.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/stan/math/rev/constraint/sum_to_zero_constrain.hpp b/stan/math/rev/constraint/sum_to_zero_constrain.hpp index e0288e7370e..6a06d215814 100644 --- a/stan/math/rev/constraint/sum_to_zero_constrain.hpp +++ b/stan/math/rev/constraint/sum_to_zero_constrain.hpp @@ -138,7 +138,6 @@ inline auto sum_to_zero_constrain(T&& x) { return arena_z; } - } // namespace math } // namespace stan #endif From 8a824c916032385caebc086eff329953a9f63dc9 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 1 Jul 2025 00:17:06 -0400 Subject: [PATCH 08/31] fix mu and sigma ref --- stan/math/prim/constraint/offset_multiplier_free.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stan/math/prim/constraint/offset_multiplier_free.hpp b/stan/math/prim/constraint/offset_multiplier_free.hpp index e61c1046f4a..d7e7161c953 100644 --- a/stan/math/prim/constraint/offset_multiplier_free.hpp +++ b/stan/math/prim/constraint/offset_multiplier_free.hpp @@ -59,8 +59,7 @@ inline auto offset_multiplier_free(T&& y, M&& mu, S&& sigma) { check_positive_finite("offset_multiplier_constrain", "multiplier", value_of(sigma_ref)); return stan::math::eval( - divide(subtract(std::forward(y), std::forward(mu_ref)), - std::forward(sigma_ref))); + divide(subtract(std::forward(y), mu_ref), sigma_ref)); } /** From 5fffe240de6db59900bd1379f0751b0e92c35bea Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 1 Jul 2025 09:06:42 -0400 Subject: [PATCH 09/31] update offset_multiplier_free requires --- stan/math/prim/constraint/offset_multiplier_free.hpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/stan/math/prim/constraint/offset_multiplier_free.hpp b/stan/math/prim/constraint/offset_multiplier_free.hpp index d7e7161c953..90d81a1745a 100644 --- a/stan/math/prim/constraint/offset_multiplier_free.hpp +++ b/stan/math/prim/constraint/offset_multiplier_free.hpp @@ -40,8 +40,7 @@ namespace math { * @throw std::invalid_argument if non-scalar arguments don't match in size */ template * = nullptr, - require_all_not_st_var* = nullptr> + require_all_not_std_vector_t* = nullptr> inline auto offset_multiplier_free(T&& y, M&& mu, S&& sigma) { auto&& mu_ref = to_ref(std::forward(mu)); auto&& sigma_ref = to_ref(std::forward(sigma)); @@ -86,13 +85,13 @@ inline auto offset_multiplier_free(T&& x, M&& mu, S&& sigma) { template * = nullptr, require_not_std_vector_t* = nullptr> -inline auto offset_multiplier_free(T&& x, const M& mu, S&& sigma) { +inline auto offset_multiplier_free(T&& x, M&& mu, S&& sigma) { check_matching_dims("offset_multiplier_free", "x", x, "sigma", sigma); std::vector< plain_type_t> ret; ret.reserve(x.size()); - const auto& mu_ref = to_ref(mu); + auto&& mu_ref = to_ref(std::forward(mu)); for (size_t i = 0; i < x.size(); ++i) { ret.emplace_back(offset_multiplier_free(x[i], mu_ref, sigma[i])); } From 80dbe6c35d5fe6aa5a840c30539f34301ede647a Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Tue, 1 Jul 2025 15:36:24 -0400 Subject: [PATCH 10/31] update functions with holder --- stan/math/fwd/fun/log_softmax.hpp | 19 +++---- stan/math/fwd/functor/apply_scalar_unary.hpp | 4 +- .../constraint/cholesky_corr_constrain.hpp | 12 ++--- .../prim/constraint/cholesky_corr_free.hpp | 10 ++-- .../constraint/cholesky_factor_constrain.hpp | 14 ++--- .../prim/constraint/cholesky_factor_free.hpp | 17 +++--- stan/math/prim/constraint/corr_constrain.hpp | 2 +- .../prim/constraint/corr_matrix_constrain.hpp | 4 +- .../math/prim/constraint/corr_matrix_free.hpp | 6 +-- .../prim/constraint/cov_matrix_constrain.hpp | 4 +- .../constraint/cov_matrix_constrain_lkj.hpp | 2 +- stan/math/prim/constraint/cov_matrix_free.hpp | 2 +- .../prim/constraint/cov_matrix_free_lkj.hpp | 7 ++- stan/math/prim/constraint/lb_constrain.hpp | 6 +-- .../offset_multiplier_constrain.hpp | 28 +++++----- .../constraint/offset_multiplier_free.hpp | 18 ++++--- .../prim/constraint/ordered_constrain.hpp | 4 +- stan/math/prim/constraint/ordered_free.hpp | 2 +- .../prim/constraint/positive_constrain.hpp | 2 +- .../constraint/positive_ordered_constrain.hpp | 4 +- .../prim/constraint/positive_ordered_free.hpp | 2 +- .../prim/constraint/simplex_constrain.hpp | 8 +-- stan/math/prim/constraint/simplex_free.hpp | 2 +- .../stochastic_column_constrain.hpp | 8 +-- .../constraint/stochastic_column_free.hpp | 2 +- .../constraint/stochastic_row_constrain.hpp | 8 +-- .../prim/constraint/stochastic_row_free.hpp | 2 +- .../prim/constraint/sum_to_zero_constrain.hpp | 4 +- .../math/prim/constraint/sum_to_zero_free.hpp | 2 +- stan/math/prim/constraint/ub_constrain.hpp | 25 +++++---- stan/math/prim/constraint/ub_free.hpp | 2 +- .../prim/constraint/unit_vector_constrain.hpp | 18 ++++--- .../math/prim/constraint/unit_vector_free.hpp | 4 +- stan/math/prim/fun/abs.hpp | 2 +- stan/math/prim/fun/acos.hpp | 2 +- stan/math/prim/fun/asin.hpp | 2 +- stan/math/prim/fun/atan.hpp | 2 +- stan/math/prim/fun/ceil.hpp | 2 +- stan/math/prim/fun/cos.hpp | 2 +- stan/math/prim/fun/cosh.hpp | 2 +- stan/math/prim/fun/exp.hpp | 2 +- stan/math/prim/fun/fabs.hpp | 2 +- stan/math/prim/fun/floor.hpp | 2 +- stan/math/prim/fun/inv.hpp | 2 +- stan/math/prim/fun/inv_cloglog.hpp | 2 +- stan/math/prim/fun/inv_square.hpp | 2 +- stan/math/prim/fun/log.hpp | 2 +- stan/math/prim/fun/log10.hpp | 2 +- stan/math/prim/fun/minus.hpp | 2 +- stan/math/prim/fun/quad_form.hpp | 8 +-- stan/math/prim/fun/quad_form_sym.hpp | 21 ++++---- stan/math/prim/fun/read_corr_L.hpp | 8 +-- stan/math/prim/fun/read_corr_matrix.hpp | 14 +++-- stan/math/prim/fun/read_cov_matrix.hpp | 4 +- stan/math/prim/fun/rep_matrix.hpp | 12 +++-- stan/math/prim/fun/reverse.hpp | 7 ++- stan/math/prim/fun/round.hpp | 2 +- stan/math/prim/fun/row.hpp | 7 +-- stan/math/prim/fun/rows.hpp | 6 ++- stan/math/prim/fun/rows_dot_product.hpp | 8 +-- stan/math/prim/fun/rows_dot_self.hpp | 8 +-- .../prim/fun/scale_matrix_exp_multiply.hpp | 9 ++-- stan/math/prim/fun/sd.hpp | 3 +- stan/math/prim/fun/segment.hpp | 6 ++- stan/math/prim/fun/select.hpp | 23 ++++---- stan/math/prim/fun/sin.hpp | 2 +- stan/math/prim/fun/sinh.hpp | 2 +- stan/math/prim/fun/sqrt.hpp | 2 +- stan/math/prim/fun/square.hpp | 6 +-- stan/math/prim/fun/tan.hpp | 2 +- stan/math/prim/fun/tanh.hpp | 2 +- .../math/prim/functor/apply_scalar_binary.hpp | 47 +++++++++-------- .../prim/functor/apply_scalar_ternary.hpp | 22 ++++---- stan/math/prim/functor/apply_scalar_unary.hpp | 4 +- stan/math/prim/functor/apply_vector_unary.hpp | 8 +-- stan/math/prim/meta/conjunction.hpp | 4 ++ stan/math/prim/meta/disjunction.hpp | 3 ++ stan/math/prim/meta/holder.hpp | 52 ++++++++++++++++++- stan/math/prim/prob/loglogistic_cdf.hpp | 28 +++++----- stan/math/rev/fun/log_softmax.hpp | 2 +- stan/math/rev/fun/multiply_log.hpp | 12 ++--- stan/math/rev/functor/apply_scalar_binary.hpp | 16 +++--- stan/math/rev/functor/apply_scalar_unary.hpp | 3 +- stan/math/rev/functor/apply_vector_unary.hpp | 6 +-- 84 files changed, 368 insertions(+), 287 deletions(-) diff --git a/stan/math/fwd/fun/log_softmax.hpp b/stan/math/fwd/fun/log_softmax.hpp index 8e52ebb0fdd..3e97183dca1 100644 --- a/stan/math/fwd/fun/log_softmax.hpp +++ b/stan/math/fwd/fun/log_softmax.hpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace stan { namespace math { @@ -21,23 +22,23 @@ namespace math { template * = nullptr> inline auto log_softmax(T&& x) { return apply_vector_unary::apply( - std::forward(x), [&](const auto& alpha) { + std::forward(x), [](auto&& alpha) { using T_alpha = decltype(alpha); using T_fvar = value_type_t; using T_fvar_inner = typename T_fvar::Scalar; - const Eigen::Ref>& alpha_ref = alpha; - Eigen::Matrix alpha_t = alpha_ref.val(); - Eigen::Matrix softmax_alpha_t = softmax(alpha_t); + auto&& alpha_ref = to_ref(std::forward(alpha)); + Eigen::Matrix alpha_v = alpha_ref.val(); + Eigen::Matrix softmax_alpha_v = softmax(alpha_v); - Eigen::Matrix log_softmax_alpha(alpha.size()); - log_softmax_alpha.val() = log_softmax(alpha_t); + Eigen::Matrix log_softmax_alpha(alpha_ref.size()); + log_softmax_alpha.val() = log_softmax(alpha_v); log_softmax_alpha.d().setZero(); - for (int m = 0; m < alpha.size(); ++m) { + for (int m = 0; m < alpha_ref.size(); ++m) { T_fvar_inner negative_alpha_m_d_times_softmax_alpha_t_m - = -alpha_ref.coeff(m).d_ * softmax_alpha_t(m); - for (int k = 0; k < alpha.size(); ++k) { + = -alpha_ref.coeff(m).d_ * softmax_alpha_v(m); + for (int k = 0; k < alpha_ref.size(); ++k) { if (m == k) { log_softmax_alpha(k).d_ += alpha_ref.coeff(m).d_ diff --git a/stan/math/fwd/functor/apply_scalar_unary.hpp b/stan/math/fwd/functor/apply_scalar_unary.hpp index ac9b067ee55..425eb2af15e 100644 --- a/stan/math/fwd/functor/apply_scalar_unary.hpp +++ b/stan/math/fwd/functor/apply_scalar_unary.hpp @@ -32,8 +32,8 @@ struct apply_scalar_unary> { * @return Function applied to the variable. */ template - static inline auto apply(const T2& x) { - return F::fun(x); + static inline auto apply(T2&& x) { + return F::fun(std::forward(x)); } }; diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index 63cd1b22271..061e514735a 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -14,7 +14,7 @@ namespace math { template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cholesky_corr_constrain(const EigVec& y, int K) { +cholesky_corr_constrain(EigVec&& y, int K) { using Eigen::Dynamic; using Eigen::Matrix; using std::sqrt; @@ -22,7 +22,7 @@ cholesky_corr_constrain(const EigVec& y, int K) { int k_choose_2 = (K * (K - 1)) / 2; check_size_match("cholesky_corr_constrain", "constrain size", y.size(), "k_choose_2", k_choose_2); - Matrix z = corr_constrain(y); + Matrix z = corr_constrain(std::forward(y)); Matrix x(K, K); if (K == 0) { return x; @@ -47,7 +47,7 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) { +cholesky_corr_constrain(EigVec&& y, int K, Lp& lp) { using Eigen::Dynamic; using Eigen::Matrix; using std::sqrt; @@ -55,7 +55,7 @@ cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) { int k_choose_2 = (K * (K - 1)) / 2; check_size_match("cholesky_corr_constrain", "y.size()", y.size(), "k_choose_2", k_choose_2); - Matrix z = corr_constrain(y, lp); + Matrix z = corr_constrain(std::forward(y), lp); Matrix x(K, K); if (K == 0) { return x; @@ -88,7 +88,7 @@ cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) { */ template * = nullptr> inline auto cholesky_corr_constrain(T&& y, int K) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [K](auto&& v) { return cholesky_corr_constrain(std::forward(v), K); }); @@ -110,7 +110,7 @@ inline auto cholesky_corr_constrain(T&& y, int K) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto cholesky_corr_constrain(T&& y, int K, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [&lp, K](auto&& v) { return cholesky_corr_constrain(std::forward(v), K, lp); }); diff --git a/stan/math/prim/constraint/cholesky_corr_free.hpp b/stan/math/prim/constraint/cholesky_corr_free.hpp index daba71ac14d..68cf1c63057 100644 --- a/stan/math/prim/constraint/cholesky_corr_free.hpp +++ b/stan/math/prim/constraint/cholesky_corr_free.hpp @@ -11,7 +11,7 @@ namespace stan { namespace math { template * = nullptr> -inline auto cholesky_corr_free(const T& x) { +inline auto cholesky_corr_free(T&& x) { using Eigen::Dynamic; using Eigen::Matrix; using std::sqrt; @@ -19,11 +19,11 @@ inline auto cholesky_corr_free(const T& x) { check_square("cholesky_corr_free", "x", x); // should validate lower-triangular, unit lengths - const auto& x_ref = to_ref(x); - int K = (x.rows() * (x.rows() - 1)) / 2; + auto&& x_ref = to_ref(std::forward(x)); + int K = (x_ref.rows() * (x_ref.rows() - 1)) / 2; Matrix, Dynamic, 1> z(K); int k = 0; - for (int i = 1; i < x.rows(); ++i) { + for (int i = 1; i < x_ref.rows(); ++i) { z.coeffRef(k++) = corr_free(x_ref.coeff(i, 0)); auto sum_sqs = square(x_ref.coeff(i, 0)); for (int j = 1; j < i; ++j) { @@ -43,7 +43,7 @@ inline auto cholesky_corr_free(const T& x) { */ template * = nullptr> inline auto cholesky_corr_free(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return cholesky_corr_free(std::forward(v)); }); diff --git a/stan/math/prim/constraint/cholesky_factor_constrain.hpp b/stan/math/prim/constraint/cholesky_factor_constrain.hpp index 6a273e88d94..ec3d2611871 100644 --- a/stan/math/prim/constraint/cholesky_factor_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_factor_constrain.hpp @@ -27,7 +27,7 @@ namespace math { */ template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cholesky_factor_constrain(const T& x, int M, int N) { +cholesky_factor_constrain(T&& x, int M, int N) { using std::exp; using T_scalar = value_type_t; check_greater_or_equal("cholesky_factor_constrain", @@ -39,7 +39,7 @@ cholesky_factor_constrain(const T& x, int M, int N) { Eigen::Matrix y(M, N); int pos = 0; - const auto& x_ref = to_ref(x); + auto&& x_ref = to_ref(std::forward(x)); for (int m = 0; m < N; ++m) { y.row(m).head(m) = x_ref.segment(pos, m); pos += m; @@ -73,17 +73,17 @@ cholesky_factor_constrain(const T& x, int M, int N) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) { +cholesky_factor_constrain(T&& x, int M, int N, Lp& lp) { check_size_match("cholesky_factor_constrain", "x.size()", x.size(), "((N * (N + 1)) / 2 + (M - N) * N)", ((N * (N + 1)) / 2 + (M - N) * N)); int pos = 0; - const auto& x_ref = to_ref(x); + auto&& x_ref = to_ref(std::forward(x)); for (int n = 0; n < N; ++n) { pos += n; lp += x_ref.coeff(pos++); } - return cholesky_factor_constrain(x_ref, M, N); + return cholesky_factor_constrain(std::forward(x_ref), M, N); } /** @@ -102,7 +102,7 @@ cholesky_factor_constrain(const T& x, int M, int N, Lp& lp) { */ template * = nullptr> inline auto cholesky_factor_constrain(T&& x, int M, int N) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [M, N](auto&& v) { return cholesky_factor_constrain(std::forward(v), M, N); }); @@ -128,7 +128,7 @@ inline auto cholesky_factor_constrain(T&& x, int M, int N) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto cholesky_factor_constrain(T&& x, int M, int N, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&lp, M, N](auto&& v) { return cholesky_factor_constrain(std::forward(v), M, N, lp); diff --git a/stan/math/prim/constraint/cholesky_factor_free.hpp b/stan/math/prim/constraint/cholesky_factor_free.hpp index affe6d32d04..ad693ec5044 100644 --- a/stan/math/prim/constraint/cholesky_factor_free.hpp +++ b/stan/math/prim/constraint/cholesky_factor_free.hpp @@ -23,25 +23,24 @@ namespace math { * @throw std::domain_error If the matrix is not a Cholesky factor. */ template * = nullptr> -Eigen::Matrix, Eigen::Dynamic, 1> cholesky_factor_free( - const T& y) { +Eigen::Matrix, Eigen::Dynamic, 1> cholesky_factor_free(T&& y) { using std::log; - const auto& y_ref = to_ref(y); + auto&& y_ref = to_ref(std::forward(y)); check_cholesky_factor("cholesky_factor_free", "y", y_ref); - int M = y.rows(); - int N = y.cols(); + const auto M = y_ref.rows(); + const auto N = y_ref.cols(); Eigen::Matrix, Eigen::Dynamic, 1> x((N * (N + 1)) / 2 + (M - N) * N); - int pos = 0; + Eigen::Index pos = 0; - for (int m = 0; m < N; ++m) { + for (Eigen::Index m = 0; m < N; ++m) { x.segment(pos, m) = y_ref.row(m).head(m); pos += m; x.coeffRef(pos++) = log(y_ref.coeff(m, m)); } - for (int m = N; m < M; ++m) { + for (Eigen::Index m = N; m < M; ++m) { x.segment(pos, N) = y_ref.row(m); pos += N; } @@ -57,7 +56,7 @@ Eigen::Matrix, Eigen::Dynamic, 1> cholesky_factor_free( */ template * = nullptr> inline auto cholesky_factor_free(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return cholesky_factor_free(std::forward(v)); }); diff --git a/stan/math/prim/constraint/corr_constrain.hpp b/stan/math/prim/constraint/corr_constrain.hpp index f19f2ea8669..c5cd1862139 100644 --- a/stan/math/prim/constraint/corr_constrain.hpp +++ b/stan/math/prim/constraint/corr_constrain.hpp @@ -44,7 +44,7 @@ inline plain_type_t corr_constrain(T&& x) { */ template inline auto corr_constrain(T_x&& x, T_lp& lp) { - plain_type_t tanh_x = tanh(x); + plain_type_t tanh_x = tanh(std::forward(x)); lp += sum(log1m(square(tanh_x))); return tanh_x; } diff --git a/stan/math/prim/constraint/corr_matrix_constrain.hpp b/stan/math/prim/constraint/corr_matrix_constrain.hpp index 1df645f7ff3..4d56edd954a 100644 --- a/stan/math/prim/constraint/corr_matrix_constrain.hpp +++ b/stan/math/prim/constraint/corr_matrix_constrain.hpp @@ -92,7 +92,7 @@ corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) { */ template * = nullptr> inline auto corr_matrix_constrain(T&& y, int K) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [K](auto&& v) { return corr_matrix_constrain(std::forward(v), K); }); @@ -117,7 +117,7 @@ inline auto corr_matrix_constrain(T&& y, int K) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto corr_matrix_constrain(T&& y, int K, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [&lp, K](auto&& v) { return corr_matrix_constrain(std::forward(v), K, lp); }); diff --git a/stan/math/prim/constraint/corr_matrix_free.hpp b/stan/math/prim/constraint/corr_matrix_free.hpp index b482e88d9f8..96f6a6a9a51 100644 --- a/stan/math/prim/constraint/corr_matrix_free.hpp +++ b/stan/math/prim/constraint/corr_matrix_free.hpp @@ -31,7 +31,7 @@ namespace math { * factor_cov_matrix() on log scale are unconstrained. */ template * = nullptr> -Eigen::Matrix, Eigen::Dynamic, 1> corr_matrix_free(const T& y) { +Eigen::Matrix, Eigen::Dynamic, 1> corr_matrix_free(T&& y) { using Eigen::Array; using Eigen::Dynamic; @@ -42,7 +42,7 @@ Eigen::Matrix, Eigen::Dynamic, 1> corr_matrix_free(const T& y) { Eigen::Index k_choose_2 = (k * (k - 1)) / 2; Eigen::Matrix, Dynamic, 1> x(k_choose_2); Array, Dynamic, 1> sds(k); - bool successful = factor_cov_matrix(y, x.array(), sds); + bool successful = factor_cov_matrix(std::forward(y), x.array(), sds); if (!successful) { throw_domain_error("corr_matrix_free", "factor_cov_matrix failed on y", y, ""); @@ -61,7 +61,7 @@ Eigen::Matrix, Eigen::Dynamic, 1> corr_matrix_free(const T& y) { */ template * = nullptr> inline auto corr_matrix_free(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return corr_matrix_free(std::forward(v)); }); } diff --git a/stan/math/prim/constraint/cov_matrix_constrain.hpp b/stan/math/prim/constraint/cov_matrix_constrain.hpp index 785016872ef..f9ead70d76f 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain.hpp @@ -103,7 +103,7 @@ cov_matrix_constrain(T&& x, Eigen::Index K, Lp& lp) { */ template * = nullptr> inline auto cov_matrix_constrain(T&& x, Eigen::Index K) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [K](auto&& v) { return cov_matrix_constrain(std::forward(v), K); }); @@ -127,7 +127,7 @@ inline auto cov_matrix_constrain(T&& x, Eigen::Index K) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto cov_matrix_constrain(T&& x, Eigen::Index K, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&lp, K](auto&& v) { return cov_matrix_constrain(std::forward(v), K, lp); }); diff --git a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp index 9e630a91b39..9b4cedef02c 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp @@ -114,7 +114,7 @@ inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { */ template * = nullptr> inline auto cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&lp, k](auto&& v) { return cov_matrix_constrain_lkj(std::forward(v), k, lp); diff --git a/stan/math/prim/constraint/cov_matrix_free.hpp b/stan/math/prim/constraint/cov_matrix_free.hpp index 310bf897b14..876adf3980e 100644 --- a/stan/math/prim/constraint/cov_matrix_free.hpp +++ b/stan/math/prim/constraint/cov_matrix_free.hpp @@ -67,7 +67,7 @@ Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free(T&& y) { */ template * = nullptr> inline auto cov_matrix_free(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return cov_matrix_free(std::forward(v)); }); } diff --git a/stan/math/prim/constraint/cov_matrix_free_lkj.hpp b/stan/math/prim/constraint/cov_matrix_free_lkj.hpp index d547261b814..fcf6768203b 100644 --- a/stan/math/prim/constraint/cov_matrix_free_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_free_lkj.hpp @@ -28,8 +28,7 @@ namespace math { * factorized by factor_cov_matrix() */ template * = nullptr> -Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free_lkj( - const T& y) { +Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free_lkj(T&& y) { using Eigen::Array; using Eigen::Dynamic; using Eigen::Matrix; @@ -41,7 +40,7 @@ Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free_lkj( Eigen::Index k_choose_2 = (k * (k - 1)) / 2; Matrix x(k_choose_2 + k); bool successful - = factor_cov_matrix(y, x.head(k_choose_2).array(), x.tail(k).array()); + = factor_cov_matrix(std::forward(y), x.head(k_choose_2).array(), x.tail(k).array()); if (!successful) { throw_domain_error("cov_matrix_free_lkj", "factor_cov_matrix failed on y", "", ""); @@ -58,7 +57,7 @@ Eigen::Matrix, Eigen::Dynamic, 1> cov_matrix_free_lkj( */ template * = nullptr> auto cov_matrix_free_lkj(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return cov_matrix_free_lkj(std::forward(v)); }); diff --git a/stan/math/prim/constraint/lb_constrain.hpp b/stan/math/prim/constraint/lb_constrain.hpp index f77c4056033..11ce3a52471 100644 --- a/stan/math/prim/constraint/lb_constrain.hpp +++ b/stan/math/prim/constraint/lb_constrain.hpp @@ -255,11 +255,11 @@ inline auto lb_constrain(const std::vector& x, const std::vector& lb, */ template , Lp>* = nullptr> -inline auto lb_constrain(const T& x, const L& lb, Lp& lp) { +inline auto lb_constrain(T&& x, L&& lb, Lp& lp) { if constexpr (Jacobian) { - return lb_constrain(x, lb, lp); + return lb_constrain(std::forward(x), std::forward(lb), lp); } else { - return lb_constrain(x, lb); + return lb_constrain(std::forward(x), std::forward(lb)); } } diff --git a/stan/math/prim/constraint/offset_multiplier_constrain.hpp b/stan/math/prim/constraint/offset_multiplier_constrain.hpp index 640110f56d3..a6e8f68d039 100644 --- a/stan/math/prim/constraint/offset_multiplier_constrain.hpp +++ b/stan/math/prim/constraint/offset_multiplier_constrain.hpp @@ -42,24 +42,24 @@ namespace math { template * = nullptr> -inline auto offset_multiplier_constrain(const T& x, const M& mu, - const S& sigma) { - const auto& mu_ref = to_ref(mu); - const auto& sigma_ref = to_ref(sigma); +inline auto offset_multiplier_constrain(T&& x, M&& mu, S&& sigma) { + auto&& mu_ref = to_ref(std::forward(mu)); + auto&& sigma_ref = to_ref(std::forward(sigma)); if (is_matrix::value && is_matrix::value) { - check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu); + check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu_ref); } if (is_matrix::value && is_matrix::value) { - check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma); + check_matching_dims("offset_multiplier_constrain", "x", x, "sigma", sigma_ref); } else if (is_matrix::value && is_matrix::value) { - check_matching_dims("offset_multiplier_constrain", "mu", mu, "sigma", - sigma); + check_matching_dims("offset_multiplier_constrain", "mu", mu_ref, "sigma", + sigma_ref); } check_finite("offset_multiplier_constrain", "offset", value_of_rec(mu_ref)); check_positive_finite("offset_multiplier_constrain", "multiplier", value_of_rec(sigma_ref)); - return stan::math::eval(fma(sigma_ref, x, mu_ref)); + return stan::math::eval(fma(std::forward(sigma_ref), + std::forward(x), std::forward(mu_ref))); } /** @@ -93,10 +93,9 @@ template , Lp>* = nullptr, require_all_not_nonscalar_prim_or_rev_kernel_expression_t< T, M, S>* = nullptr> -inline auto offset_multiplier_constrain(const T& x, const M& mu, const S& sigma, - Lp& lp) { - const auto& mu_ref = to_ref(mu); - const auto& sigma_ref = to_ref(sigma); +inline auto offset_multiplier_constrain(T&& x, M&& mu, S&& sigma, Lp& lp) { + auto&& mu_ref = to_ref(std::forward(mu)); + auto&& sigma_ref = to_ref(std::forward(sigma)); if (is_matrix::value && is_matrix::value) { check_matching_dims("offset_multiplier_constrain", "x", x, "mu", mu); } @@ -115,7 +114,8 @@ inline auto offset_multiplier_constrain(const T& x, const M& mu, const S& sigma, } else { lp += sum(log(sigma_ref)); } - return stan::math::eval(fma(sigma_ref, x, mu_ref)); + return stan::math::eval(fma(std::forward(sigma_ref), + std::forward(x), std::forward(mu_ref))); } /** diff --git a/stan/math/prim/constraint/offset_multiplier_free.hpp b/stan/math/prim/constraint/offset_multiplier_free.hpp index 90d81a1745a..4bfc6acd12d 100644 --- a/stan/math/prim/constraint/offset_multiplier_free.hpp +++ b/stan/math/prim/constraint/offset_multiplier_free.hpp @@ -44,21 +44,23 @@ template (mu)); auto&& sigma_ref = to_ref(std::forward(sigma)); - if (is_matrix::value && is_matrix::value) { - check_matching_dims("offset_multiplier_constrain", "y", y, "mu", mu); + if constexpr (is_matrix::value && is_matrix::value) { + check_matching_dims("offset_multiplier_constrain", "y", y, "mu", mu_ref); } - if (is_matrix::value && is_matrix::value) { - check_matching_dims("offset_multiplier_constrain", "y", y, "sigma", sigma); - } else if (is_matrix::value && is_matrix::value) { - check_matching_dims("offset_multiplier_constrain", "mu", mu, "sigma", - sigma); + if constexpr (is_matrix::value && is_matrix::value) { + check_matching_dims("offset_multiplier_constrain", "y", y, "sigma", sigma_ref); + } else if constexpr (is_matrix::value && is_matrix::value) { + check_matching_dims("offset_multiplier_constrain", "mu", mu_ref, "sigma", + sigma_ref); } check_finite("offset_multiplier_constrain", "offset", value_of(mu_ref)); check_positive_finite("offset_multiplier_constrain", "multiplier", value_of(sigma_ref)); return stan::math::eval( - divide(subtract(std::forward(y), mu_ref), sigma_ref)); + divide(subtract(std::forward(y), + std::forward(mu_ref)), + std::forward(sigma_ref))); } /** diff --git a/stan/math/prim/constraint/ordered_constrain.hpp b/stan/math/prim/constraint/ordered_constrain.hpp index e2a2280d842..e585c523e25 100644 --- a/stan/math/prim/constraint/ordered_constrain.hpp +++ b/stan/math/prim/constraint/ordered_constrain.hpp @@ -77,7 +77,7 @@ inline auto ordered_constrain(EigVec&& x, Lp& lp) { */ template * = nullptr> inline auto ordered_constrain(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return ordered_constrain(std::forward(v)); }); } @@ -100,7 +100,7 @@ inline auto ordered_constrain(T&& x) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto ordered_constrain(T&& x, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&lp](auto&& v) { return ordered_constrain(std::forward(v), lp); }); diff --git a/stan/math/prim/constraint/ordered_free.hpp b/stan/math/prim/constraint/ordered_free.hpp index 609d8a8f9f9..f06db3693d7 100644 --- a/stan/math/prim/constraint/ordered_free.hpp +++ b/stan/math/prim/constraint/ordered_free.hpp @@ -50,7 +50,7 @@ plain_type_t ordered_free(const EigVec& y) { */ template * = nullptr> inline auto ordered_free(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return ordered_free(std::forward(v)); }); } diff --git a/stan/math/prim/constraint/positive_constrain.hpp b/stan/math/prim/constraint/positive_constrain.hpp index 193b9721890..7551f8afb76 100644 --- a/stan/math/prim/constraint/positive_constrain.hpp +++ b/stan/math/prim/constraint/positive_constrain.hpp @@ -96,7 +96,7 @@ template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto positive_constrain(T&& x, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&lp](auto&& v) { return positive_constrain(std::forward(v), lp); }); diff --git a/stan/math/prim/constraint/positive_ordered_constrain.hpp b/stan/math/prim/constraint/positive_ordered_constrain.hpp index 608da90af22..dc517fcb91e 100644 --- a/stan/math/prim/constraint/positive_ordered_constrain.hpp +++ b/stan/math/prim/constraint/positive_ordered_constrain.hpp @@ -71,7 +71,7 @@ inline auto positive_ordered_constrain(Vec&& x, Lp& lp) { */ template * = nullptr> inline auto positive_ordered_constrain(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return positive_ordered_constrain(std::forward(v)); }); @@ -94,7 +94,7 @@ inline auto positive_ordered_constrain(T&& x) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto positive_ordered_constrain(T&& x, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&lp](auto&& v) { return positive_ordered_constrain(std::forward(v), lp); }); diff --git a/stan/math/prim/constraint/positive_ordered_free.hpp b/stan/math/prim/constraint/positive_ordered_free.hpp index 3602f405810..d2013ef4de8 100644 --- a/stan/math/prim/constraint/positive_ordered_free.hpp +++ b/stan/math/prim/constraint/positive_ordered_free.hpp @@ -50,7 +50,7 @@ inline auto positive_ordered_free(const EigVec& y) { */ template * = nullptr> inline auto positive_ordered_free(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return positive_ordered_free(std::forward(v)); }); diff --git a/stan/math/prim/constraint/simplex_constrain.hpp b/stan/math/prim/constraint/simplex_constrain.hpp index 0f9c9fc4af4..01dbabe93a4 100644 --- a/stan/math/prim/constraint/simplex_constrain.hpp +++ b/stan/math/prim/constraint/simplex_constrain.hpp @@ -146,7 +146,7 @@ inline plain_type_t simplex_constrain(const Vec& y, Lp& lp) { */ template * = nullptr> inline auto simplex_constrain(T&& y) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [](auto&& v) { return simplex_constrain(std::forward(v)); }); } @@ -167,7 +167,7 @@ inline auto simplex_constrain(T&& y) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto simplex_constrain(T&& y, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [&lp](auto&& v) { return simplex_constrain(std::forward(v), lp); }); @@ -195,9 +195,9 @@ template , Lp>* = nullptr> inline plain_type_t simplex_constrain(Vec&& y, Lp& lp) { if constexpr (Jacobian) { - return simplex_constrain(std::forward(y), lp); + return simplex_constrain(std::forward(y), lp); } else { - return simplex_constrain(std::forward(y)); + return simplex_constrain(std::forward(y)); } } diff --git a/stan/math/prim/constraint/simplex_free.hpp b/stan/math/prim/constraint/simplex_free.hpp index 2b2bd9ae68c..649212d9590 100644 --- a/stan/math/prim/constraint/simplex_free.hpp +++ b/stan/math/prim/constraint/simplex_free.hpp @@ -54,7 +54,7 @@ inline plain_type_t simplex_free(const Vec& x) { */ template * = nullptr> inline auto simplex_free(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return simplex_free(std::forward(v)); }); } diff --git a/stan/math/prim/constraint/stochastic_column_constrain.hpp b/stan/math/prim/constraint/stochastic_column_constrain.hpp index 104877f0dbf..61cbbed193c 100644 --- a/stan/math/prim/constraint/stochastic_column_constrain.hpp +++ b/stan/math/prim/constraint/stochastic_column_constrain.hpp @@ -72,7 +72,7 @@ inline plain_type_t stochastic_column_constrain(const Mat& y, Lp& lp) { */ template * = nullptr> inline auto stochastic_column_constrain(T&& y) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [](auto&& v) { return stochastic_column_constrain(std::forward(v)); }); @@ -95,7 +95,7 @@ inline auto stochastic_column_constrain(T&& y) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto stochastic_column_constrain(T&& y, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [&lp](auto&& v) { return stochastic_column_constrain(std::forward(v), lp); }); @@ -123,9 +123,9 @@ template , Lp>* = nullptr> inline plain_type_t stochastic_column_constrain(Mat&& y, Lp& lp) { if constexpr (Jacobian) { - return stochastic_column_constrain(std::forward(y), lp); + return stochastic_column_constrain(std::forward(y), lp); } else { - return stochastic_column_constrain(std::forward(y)); + return stochastic_column_constrain(std::forward(y)); } } diff --git a/stan/math/prim/constraint/stochastic_column_free.hpp b/stan/math/prim/constraint/stochastic_column_free.hpp index c1cd90ece06..7be689e4f10 100644 --- a/stan/math/prim/constraint/stochastic_column_free.hpp +++ b/stan/math/prim/constraint/stochastic_column_free.hpp @@ -37,7 +37,7 @@ inline plain_type_t stochastic_column_free(Mat&& y) { */ template * = nullptr> inline auto stochastic_column_free(T&& y) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [](auto&& v) { return stochastic_column_free(std::forward(v)); }); diff --git a/stan/math/prim/constraint/stochastic_row_constrain.hpp b/stan/math/prim/constraint/stochastic_row_constrain.hpp index 0b0c1fa2723..cda1ee27402 100644 --- a/stan/math/prim/constraint/stochastic_row_constrain.hpp +++ b/stan/math/prim/constraint/stochastic_row_constrain.hpp @@ -69,7 +69,7 @@ inline plain_type_t stochastic_row_constrain(Mat&& y, Lp& lp) { */ template * = nullptr> inline auto stochastic_row_constrain(T&& y) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [](auto&& v) { return stochastic_row_constrain(std::forward(v)); }); @@ -91,7 +91,7 @@ inline auto stochastic_row_constrain(T&& y) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto stochastic_row_constrain(T&& y, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [&lp](auto&& v) { return stochastic_row_constrain(std::forward(v), lp); }); @@ -119,9 +119,9 @@ template , Lp>* = nullptr> inline plain_type_t stochastic_row_constrain(Mat&& y, Lp& lp) { if constexpr (Jacobian) { - return stochastic_row_constrain(std::forward(y), lp); + return stochastic_row_constrain(std::forward(y), lp); } else { - return stochastic_row_constrain(std::forward(y)); + return stochastic_row_constrain(std::forward(y)); } } diff --git a/stan/math/prim/constraint/stochastic_row_free.hpp b/stan/math/prim/constraint/stochastic_row_free.hpp index 068d287435d..003bef0f47e 100644 --- a/stan/math/prim/constraint/stochastic_row_free.hpp +++ b/stan/math/prim/constraint/stochastic_row_free.hpp @@ -36,7 +36,7 @@ inline plain_type_t stochastic_row_free(Mat&& y) { */ template * = nullptr> inline auto stochastic_row_free(T&& y) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [](auto&& v) { return stochastic_row_free(std::forward(v)); }); diff --git a/stan/math/prim/constraint/sum_to_zero_constrain.hpp b/stan/math/prim/constraint/sum_to_zero_constrain.hpp index f928f14e67e..d615aa3925c 100644 --- a/stan/math/prim/constraint/sum_to_zero_constrain.hpp +++ b/stan/math/prim/constraint/sum_to_zero_constrain.hpp @@ -139,7 +139,7 @@ inline plain_type_t sum_to_zero_constrain(T&& y, Lp& lp) { */ template * = nullptr> inline auto sum_to_zero_constrain(T&& y) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [](auto&& v) { return sum_to_zero_constrain(std::forward(v)); }); @@ -160,7 +160,7 @@ inline auto sum_to_zero_constrain(T&& y) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto sum_to_zero_constrain(T&& y, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [&lp](auto&& v) { return sum_to_zero_constrain(std::forward(v), lp); }); diff --git a/stan/math/prim/constraint/sum_to_zero_free.hpp b/stan/math/prim/constraint/sum_to_zero_free.hpp index 85a679cad2d..06340b8224f 100644 --- a/stan/math/prim/constraint/sum_to_zero_free.hpp +++ b/stan/math/prim/constraint/sum_to_zero_free.hpp @@ -116,7 +116,7 @@ inline plain_type_t sum_to_zero_free(const Mat& z) { */ template * = nullptr> inline auto sum_to_zero_free(T&& z) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(z), [](auto&& v) { return sum_to_zero_free(std::forward(v)); }); } diff --git a/stan/math/prim/constraint/ub_constrain.hpp b/stan/math/prim/constraint/ub_constrain.hpp index bef8aee2324..fe836471456 100644 --- a/stan/math/prim/constraint/ub_constrain.hpp +++ b/stan/math/prim/constraint/ub_constrain.hpp @@ -88,9 +88,8 @@ inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { template * = nullptr, require_stan_scalar_t* = nullptr, require_all_not_st_var* = nullptr> -inline auto ub_constrain(T&& x, const U& ub) { - return eval(std::forward(x).unaryExpr( - [ub](auto&& xx) { return ub_constrain(xx, ub); })); +inline auto ub_constrain(const T& x, const U& ub) { + return eval(x.unaryExpr([ub](auto&& xx) { return ub_constrain(xx, ub); })); } /** @@ -109,9 +108,9 @@ template * = nullptr, require_stan_scalar_t* = nullptr, require_all_not_st_var* = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto ub_constrain(T&& x, const U& ub, Lp& lp) { - return eval(std::forward(x).unaryExpr( - [ub, &lp](auto&& xx) { return ub_constrain(xx, ub, lp); })); +inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { + return eval( + x.unaryExpr([ub, &lp](auto&& xx) { return ub_constrain(xx, ub, lp); })); } /** @@ -126,9 +125,9 @@ inline auto ub_constrain(T&& x, const U& ub, Lp& lp) { */ template * = nullptr, require_all_not_st_var* = nullptr> -inline auto ub_constrain(T&& x, const U& ub) { +inline auto ub_constrain(const T& x, const U& ub) { check_matching_dims("ub_constrain", "x", x, "ub", ub); - return eval(std::forward(x).binaryExpr( + return eval(x.binaryExpr( ub, [](auto&& xx, auto&& ubb) { return ub_constrain(xx, ubb); })); } @@ -148,9 +147,9 @@ template * = nullptr, require_all_not_st_var* = nullptr, require_convertible_t, Lp>* = nullptr> -inline auto ub_constrain(T&& x, const U& ub, Lp& lp) { +inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { check_matching_dims("ub_constrain", "x", x, "ub", ub); - return eval(std::forward(x).binaryExpr( + return eval(x.binaryExpr( ub, [&lp](auto&& xx, auto&& ubb) { return ub_constrain(xx, ubb, lp); })); } @@ -262,11 +261,11 @@ inline auto ub_constrain(const std::vector& x, const std::vector& ub, */ template , Lp>* = nullptr> -inline auto ub_constrain(T&& x, U&& ub, Lp& lp) { +inline auto ub_constrain(const T& x, const U& ub, Lp& lp) { if constexpr (Jacobian) { - return ub_constrain(std::forward(x), std::forward(ub), lp); + return ub_constrain(x, ub, lp); } else { - return ub_constrain(std::forward(x), std::forward(ub)); + return ub_constrain(x, ub); } } diff --git a/stan/math/prim/constraint/ub_free.hpp b/stan/math/prim/constraint/ub_free.hpp index 05a3acce775..415fe38ff5d 100644 --- a/stan/math/prim/constraint/ub_free.hpp +++ b/stan/math/prim/constraint/ub_free.hpp @@ -34,7 +34,7 @@ template * = nullptr, require_stan_scalar_t* = nullptr> inline auto ub_free(T&& y, U&& ub) { if (value_of_rec(ub) == INFTY) { - return identity_free(y, ub); + return identity_free(std::forward(y), std::forward(ub)); } else { auto&& y_ref = to_ref(std::forward(y)); auto&& ub_ref = to_ref(std::forward(ub)); diff --git a/stan/math/prim/constraint/unit_vector_constrain.hpp b/stan/math/prim/constraint/unit_vector_constrain.hpp index 7b3f29e2d5e..66828e74fa9 100644 --- a/stan/math/prim/constraint/unit_vector_constrain.hpp +++ b/stan/math/prim/constraint/unit_vector_constrain.hpp @@ -24,13 +24,15 @@ namespace math { */ template * = nullptr, require_not_vt_autodiff* = nullptr> -inline plain_type_t unit_vector_constrain(const T& y) { +inline auto unit_vector_constrain(T&& y) { using std::sqrt; check_nonzero_size("unit_vector_constrain", "y", y); - auto&& y_ref = to_ref(y); + auto&& y_ref = to_ref(std::forward(y)); value_type_t SN = dot_self(y_ref); check_positive_finite("unit_vector_constrain", "norm", SN); - return y_ref.array() / sqrt(SN); + return make_holder([SN](auto&& y_) { + return std::forward(y_).array() / sqrt(SN); + }, std::forward(y_ref)); } /** @@ -46,14 +48,16 @@ inline plain_type_t unit_vector_constrain(const T& y) { */ template * = nullptr, require_all_not_vt_autodiff* = nullptr> -inline plain_type_t unit_vector_constrain(const T1& y, T2& lp) { +inline auto unit_vector_constrain(T1&& y, T2& lp) { using std::sqrt; check_nonzero_size("unit_vector_constrain", "y", y); auto&& y_ref = to_ref(y); value_type_t SN = dot_self(y_ref); check_positive_finite("unit_vector_constrain", "norm", SN); lp -= 0.5 * SN; - return y_ref.array() / sqrt(SN); + return make_holder([SN](auto&& y_) { + return std::forward(y_).array() / sqrt(SN); + }, std::forward(y_ref)); } /** * Return the unit length vector corresponding to the free vector y. @@ -67,7 +71,7 @@ inline plain_type_t unit_vector_constrain(const T1& y, T2& lp) { */ template * = nullptr> inline auto unit_vector_constrain(T&& y) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [](auto&& v) { return unit_vector_constrain(std::forward(v)); }); @@ -89,7 +93,7 @@ inline auto unit_vector_constrain(T&& y) { template * = nullptr, require_convertible_t, Lp>* = nullptr> inline auto unit_vector_constrain(T&& y, Lp& lp) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(y), [&lp](auto&& v) { return unit_vector_constrain(std::forward(v), lp); }); diff --git a/stan/math/prim/constraint/unit_vector_free.hpp b/stan/math/prim/constraint/unit_vector_free.hpp index 6c8a9f19091..c7df0615f29 100644 --- a/stan/math/prim/constraint/unit_vector_free.hpp +++ b/stan/math/prim/constraint/unit_vector_free.hpp @@ -25,7 +25,7 @@ inline auto unit_vector_free(EigVec&& x) { auto&& x_ref = to_ref(std::forward(x)); check_unit_vector("stan::math::unit_vector_free", "Unit vector variable", x_ref); - return x_ref; + return std::forward(x_ref); } /** @@ -37,7 +37,7 @@ inline auto unit_vector_free(EigVec&& x) { */ template * = nullptr> inline auto unit_vector_free(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return unit_vector_free(std::forward(v)); }); } diff --git a/stan/math/prim/fun/abs.hpp b/stan/math/prim/fun/abs.hpp index fa13ff2b853..e0cb2acd74f 100644 --- a/stan/math/prim/fun/abs.hpp +++ b/stan/math/prim/fun/abs.hpp @@ -79,7 +79,7 @@ inline auto abs(Container&& x) { template * = nullptr> inline auto abs(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&](auto&& v) { return std::forward(v).array().abs(); }); } diff --git a/stan/math/prim/fun/acos.hpp b/stan/math/prim/fun/acos.hpp index b78663a587a..868f1a424b4 100644 --- a/stan/math/prim/fun/acos.hpp +++ b/stan/math/prim/fun/acos.hpp @@ -81,7 +81,7 @@ inline auto acos(Container&& x) { template * = nullptr> inline auto acos(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().acos(); }); } diff --git a/stan/math/prim/fun/asin.hpp b/stan/math/prim/fun/asin.hpp index feb756fb4fd..60821a5abd3 100644 --- a/stan/math/prim/fun/asin.hpp +++ b/stan/math/prim/fun/asin.hpp @@ -79,7 +79,7 @@ inline auto asin(Container&& x) { template * = nullptr> inline auto asin(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().asin(); }); } diff --git a/stan/math/prim/fun/atan.hpp b/stan/math/prim/fun/atan.hpp index 8366ba41428..7cb08d4e89e 100644 --- a/stan/math/prim/fun/atan.hpp +++ b/stan/math/prim/fun/atan.hpp @@ -77,7 +77,7 @@ inline auto atan(Container&& x) { template * = nullptr> inline auto atan(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().atan(); }); } diff --git a/stan/math/prim/fun/ceil.hpp b/stan/math/prim/fun/ceil.hpp index 2185a50b313..b9bf9e2691a 100644 --- a/stan/math/prim/fun/ceil.hpp +++ b/stan/math/prim/fun/ceil.hpp @@ -54,7 +54,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto ceil(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().ceil(); }); } diff --git a/stan/math/prim/fun/cos.hpp b/stan/math/prim/fun/cos.hpp index ae8eb3b275e..00c866db87f 100644 --- a/stan/math/prim/fun/cos.hpp +++ b/stan/math/prim/fun/cos.hpp @@ -76,7 +76,7 @@ inline auto cos(Container&& x) { template * = nullptr> inline auto cos(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&](auto&& v) { return std::forward(v).array().cos(); }); } diff --git a/stan/math/prim/fun/cosh.hpp b/stan/math/prim/fun/cosh.hpp index 8a6bae2571b..6782e9856b5 100644 --- a/stan/math/prim/fun/cosh.hpp +++ b/stan/math/prim/fun/cosh.hpp @@ -75,7 +75,7 @@ inline auto cosh(Container&& x) { template * = nullptr> inline auto cosh(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().cosh(); }); } diff --git a/stan/math/prim/fun/exp.hpp b/stan/math/prim/fun/exp.hpp index 01ec45e5888..8298467d498 100644 --- a/stan/math/prim/fun/exp.hpp +++ b/stan/math/prim/fun/exp.hpp @@ -84,7 +84,7 @@ inline auto exp(Container&& x) { template * = nullptr> inline auto exp(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().exp(); }); } diff --git a/stan/math/prim/fun/fabs.hpp b/stan/math/prim/fun/fabs.hpp index 51bb3f554bd..95533450b8b 100644 --- a/stan/math/prim/fun/fabs.hpp +++ b/stan/math/prim/fun/fabs.hpp @@ -64,7 +64,7 @@ inline auto fabs(Container&& x) { template * = nullptr> inline auto fabs(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().abs(); }); } diff --git a/stan/math/prim/fun/floor.hpp b/stan/math/prim/fun/floor.hpp index 09e7828acab..d6f38484776 100644 --- a/stan/math/prim/fun/floor.hpp +++ b/stan/math/prim/fun/floor.hpp @@ -59,7 +59,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto floor(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().floor(); }); } diff --git a/stan/math/prim/fun/inv.hpp b/stan/math/prim/fun/inv.hpp index 72d2cd26733..bcdec1defbe 100644 --- a/stan/math/prim/fun/inv.hpp +++ b/stan/math/prim/fun/inv.hpp @@ -51,7 +51,7 @@ template * = nullptr> inline auto inv(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().inverse(); }); } diff --git a/stan/math/prim/fun/inv_cloglog.hpp b/stan/math/prim/fun/inv_cloglog.hpp index 714cef57b7c..499dcc7d926 100644 --- a/stan/math/prim/fun/inv_cloglog.hpp +++ b/stan/math/prim/fun/inv_cloglog.hpp @@ -106,7 +106,7 @@ inline auto inv_cloglog(Container&& x) { template * = nullptr> inline auto inv_cloglog(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](const auto& v) { return (1 - (-std::forward(v).array().exp()).exp()); }); diff --git a/stan/math/prim/fun/inv_square.hpp b/stan/math/prim/fun/inv_square.hpp index 79a2c755f35..73cba9e0e65 100644 --- a/stan/math/prim/fun/inv_square.hpp +++ b/stan/math/prim/fun/inv_square.hpp @@ -35,7 +35,7 @@ inline auto inv_square(const Container& x) { template * = nullptr> inline auto inv_square(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().square().inverse(); }); diff --git a/stan/math/prim/fun/log.hpp b/stan/math/prim/fun/log.hpp index d3df5d3c82d..25a78453216 100644 --- a/stan/math/prim/fun/log.hpp +++ b/stan/math/prim/fun/log.hpp @@ -83,7 +83,7 @@ inline auto log(Container&& x) { template * = nullptr> inline auto log(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().log(); }); } diff --git a/stan/math/prim/fun/log10.hpp b/stan/math/prim/fun/log10.hpp index 6b928c39fd1..7ef368fa77e 100644 --- a/stan/math/prim/fun/log10.hpp +++ b/stan/math/prim/fun/log10.hpp @@ -74,7 +74,7 @@ inline auto log10(Container&& x) { template * = nullptr> inline auto log10(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().log10(); }); } diff --git a/stan/math/prim/fun/minus.hpp b/stan/math/prim/fun/minus.hpp index 58c8c682677..c7e8b7c9585 100644 --- a/stan/math/prim/fun/minus.hpp +++ b/stan/math/prim/fun/minus.hpp @@ -34,7 +34,7 @@ inline auto minus(T&& x) { */ template * = nullptr> inline auto minus(T&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return -std::forward(v); }); } diff --git a/stan/math/prim/fun/quad_form.hpp b/stan/math/prim/fun/quad_form.hpp index 46cabbe601a..e4d7fa8066c 100644 --- a/stan/math/prim/fun/quad_form.hpp +++ b/stan/math/prim/fun/quad_form.hpp @@ -52,11 +52,13 @@ template * = nullptr, require_eigen_col_vector_t* = nullptr, require_vt_same* = nullptr, require_all_vt_arithmetic* = nullptr> -inline value_type_t quad_form(const EigMat& A, const ColVec& B) { +inline value_type_t quad_form(EigMat&& A, ColVec&& B) { check_square("quad_form", "A", A); check_multiplicable("quad_form", "A", A, "B", B); - const auto& B_ref = to_ref(B); - return B_ref.dot(A * B_ref); + return make_holder([](auto&& b, auto&& a) { + auto&& b_ref = to_ref(std::forward(b)); + return b_ref.dot(a * b_ref); + }, std::forward(B), std::forward(A)); } } // namespace math diff --git a/stan/math/prim/fun/quad_form_sym.hpp b/stan/math/prim/fun/quad_form_sym.hpp index 7b9359d82ae..2b6d5f78613 100644 --- a/stan/math/prim/fun/quad_form_sym.hpp +++ b/stan/math/prim/fun/quad_form_sym.hpp @@ -27,14 +27,14 @@ template * = nullptr, require_vt_same* = nullptr, require_all_vt_arithmetic* = nullptr> -inline plain_type_t quad_form_sym(const EigMat1& A, const EigMat2& B) { +inline plain_type_t quad_form_sym(EigMat1&& A, EigMat2&& B) { check_multiplicable("quad_form_sym", "A", A, "B", B); - const auto& A_ref = to_ref(A); - const auto& B_ref = to_ref(B); + auto&& A_ref = to_ref(std::forward(A)); check_symmetric("quad_form_sym", "A", A_ref); - return make_holder( - [](const auto& ret) { return 0.5 * (ret + ret.transpose()); }, - (B_ref.transpose() * A_ref * B_ref).eval()); + return make_holder([](auto&& a, auto&& b) { + auto ret = (a.transpose() * a * b).eval(); + return 0.5 * (ret + ret.transpose()); + }, std::forward(A_ref), to_ref(std::forward(B))); } /** @@ -53,12 +53,13 @@ template * = nullptr, require_eigen_col_vector_t* = nullptr, require_vt_same* = nullptr, require_all_vt_arithmetic* = nullptr> -inline value_type_t quad_form_sym(const EigMat& A, const ColVec& B) { +inline value_type_t quad_form_sym(EigMat&& A, ColVec&& B) { check_multiplicable("quad_form_sym", "A", A, "B", B); - const auto& A_ref = to_ref(A); - const auto& B_ref = to_ref(B); + auto&& A_ref = to_ref(std::forward(A)); check_symmetric("quad_form_sym", "A", A_ref); - return B_ref.dot(A_ref * B_ref); + return make_holder([](auto&& a, auto&& b) { + return b.dot(a * b); + }, std::forward(A_ref), to_ref(std::forward(B))); } } // namespace math diff --git a/stan/math/prim/fun/read_corr_L.hpp b/stan/math/prim/fun/read_corr_L.hpp index 5e646ef82ae..53194e855a0 100644 --- a/stan/math/prim/fun/read_corr_L.hpp +++ b/stan/math/prim/fun/read_corr_L.hpp @@ -34,7 +34,7 @@ namespace math { * canonical partial correlations. */ template * = nullptr> -Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> read_corr_L( +inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> read_corr_L( const T& CPCs, // on (-1, 1) size_t K) { using T_scalar = value_type_t; @@ -101,8 +101,8 @@ Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> read_corr_L( */ template * = nullptr, require_convertible_t, Lp>* = nullptr> -Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> read_corr_L( - const T& CPCs, size_t K, Lp& log_prob) { +inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> read_corr_L( + T&& CPCs, size_t K, Lp& log_prob) { using T_scalar = value_type_t; if (K == 0) { return {}; @@ -112,7 +112,7 @@ Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> read_corr_L( 1); } - const Eigen::Ref>& CPCs_ref = CPCs; + auto&& CPCs_ref = to_ref(std::forward(CPCs)); size_t pos = 0; T_scalar acc = 0; // no need to abs() because this Jacobian determinant diff --git a/stan/math/prim/fun/read_corr_matrix.hpp b/stan/math/prim/fun/read_corr_matrix.hpp index 53b6e2cdff8..fe8c3d01917 100644 --- a/stan/math/prim/fun/read_corr_matrix.hpp +++ b/stan/math/prim/fun/read_corr_matrix.hpp @@ -23,13 +23,12 @@ namespace math { * canonical partial correlations. */ template * = nullptr> -Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -read_corr_matrix(const T_CPCs& CPCs, size_t K) { +inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> +read_corr_matrix(T_CPCs&& CPCs, size_t K) { if (K == 0) { return {}; } - - return multiply_lower_tri_self_transpose(read_corr_L(CPCs, K)); + return multiply_lower_tri_self_transpose(read_corr_L(std::forward(CPCs), K)); } /** @@ -56,13 +55,12 @@ read_corr_matrix(const T_CPCs& CPCs, size_t K) { template * = nullptr, require_convertible_t, Lp>* = nullptr> -Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -read_corr_matrix(const T_CPCs& CPCs, size_t K, Lp& log_prob) { +inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> +read_corr_matrix(T_CPCs&& CPCs, size_t K, Lp& log_prob) { if (K == 0) { return {}; } - - return multiply_lower_tri_self_transpose(read_corr_L(CPCs, K, log_prob)); + return multiply_lower_tri_self_transpose(read_corr_L(std::forward(CPCs), K, log_prob)); } } // namespace math diff --git a/stan/math/prim/fun/read_cov_matrix.hpp b/stan/math/prim/fun/read_cov_matrix.hpp index 0e7e629845d..b7043d20666 100644 --- a/stan/math/prim/fun/read_cov_matrix.hpp +++ b/stan/math/prim/fun/read_cov_matrix.hpp @@ -30,7 +30,7 @@ read_cov_matrix(const T_CPCs& CPCs, const T_sds& sds, value_type_t& log_prob) { Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> L = read_cov_L(CPCs, sds, log_prob); - return multiply_lower_tri_self_transpose(L); + return multiply_lower_tri_self_transpose(std::move(L)); } /** @@ -53,7 +53,7 @@ read_cov_matrix(const T_CPCs& CPCs, const T_sds& sds) { D.diagonal() = sds; Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> L = D * read_corr_L(CPCs, K); - return multiply_lower_tri_self_transpose(L); + return multiply_lower_tri_self_transpose(std::move(L)); } } // namespace math diff --git a/stan/math/prim/fun/rep_matrix.hpp b/stan/math/prim/fun/rep_matrix.hpp index 7333ec3833d..3bd3dab0775 100644 --- a/stan/math/prim/fun/rep_matrix.hpp +++ b/stan/math/prim/fun/rep_matrix.hpp @@ -51,13 +51,17 @@ inline auto rep_matrix(const T& x, int m, int n) { * @param n Number of rows or columns. */ template * = nullptr> -inline auto rep_matrix(const Vec& x, int n) { - if (is_eigen_row_vector::value) { +inline auto rep_matrix(Vec&& x, int n) { + if constexpr (is_eigen_row_vector::value) { check_nonnegative("rep_matrix", "rows", n); - return x.replicate(n, 1); + return make_holder([n](auto&& xx) { + return xx.replicate(n, 1); + }, std::forward(x)); } else { check_nonnegative("rep_matrix", "cols", n); - return x.replicate(1, n); + return make_holder([n](auto&& xx) { + return xx.replicate(1, n); + }, std::forward(x)); } } diff --git a/stan/math/prim/fun/reverse.hpp b/stan/math/prim/fun/reverse.hpp index 82c58ca1d70..5062cb9a4e3 100644 --- a/stan/math/prim/fun/reverse.hpp +++ b/stan/math/prim/fun/reverse.hpp @@ -32,8 +32,11 @@ inline std::vector reverse(const std::vector& x) { * @return Vector or row vector in reversed order. */ template > -inline auto reverse(const T& x) { - return x.reverse(); +inline auto reverse(T&& x) { + return make_holder( + [](auto&& xx) { + return xx.reverse(); + }, std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/round.hpp b/stan/math/prim/fun/round.hpp index 09e8a7d8ac6..74b9191370e 100644 --- a/stan/math/prim/fun/round.hpp +++ b/stan/math/prim/fun/round.hpp @@ -52,7 +52,7 @@ inline auto round(Container&& x) { template * = nullptr> inline auto round(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().round(); }); } diff --git a/stan/math/prim/fun/row.hpp b/stan/math/prim/fun/row.hpp index 9e4ce94f33a..fea6571143e 100644 --- a/stan/math/prim/fun/row.hpp +++ b/stan/math/prim/fun/row.hpp @@ -21,10 +21,11 @@ namespace math { * @throw std::out_of_range if i is out of range. */ template * = nullptr> -inline auto row(const T& m, size_t i) { +inline auto row(T&& m, size_t i) { check_row_index("row", "i", m, i); - - return m.row(i - 1); + return make_holder([i](auto&& mm) { + return std::forward(mm).row(i - 1); + }, std::forward(m), i); } } // namespace math diff --git a/stan/math/prim/fun/rows.hpp b/stan/math/prim/fun/rows.hpp index b6e5f9925fe..d9a3ef980bc 100644 --- a/stan/math/prim/fun/rows.hpp +++ b/stan/math/prim/fun/rows.hpp @@ -17,8 +17,10 @@ namespace math { * @return Number of rows. */ template * = nullptr> -inline int64_t rows(const T& m) { - return m.rows(); +inline int64_t rows(T&& m) { + return make_holder( + [](auto&& mm) { return std::forward(mm).rows(); }, + std::forward(m)); } } // namespace math diff --git a/stan/math/prim/fun/rows_dot_product.hpp b/stan/math/prim/fun/rows_dot_product.hpp index 29bdc14e647..80d29388e21 100644 --- a/stan/math/prim/fun/rows_dot_product.hpp +++ b/stan/math/prim/fun/rows_dot_product.hpp @@ -24,10 +24,12 @@ namespace math { template * = nullptr, require_all_not_eigen_vt* = nullptr> -inline Eigen::Matrix, Mat1::RowsAtCompileTime, 1> -rows_dot_product(const Mat1& v1, const Mat2& v2) { +inline auto rows_dot_product(Mat1&& v1, Mat2&& v2) { check_matching_sizes("rows_dot_product", "v1", v1, "v2", v2); - return (v1.cwiseProduct(v2)).rowwise().sum(); + return make_holder( + [](auto&& v1, auto&& v2) { + return (std::forward(v1).cwiseProduct(std::forward(v2))).rowwise().sum(); + }, std::forward(v1), std::forward(v2)); } } // namespace math diff --git a/stan/math/prim/fun/rows_dot_self.hpp b/stan/math/prim/fun/rows_dot_self.hpp index 06d0af10de0..0f3c8465485 100644 --- a/stan/math/prim/fun/rows_dot_self.hpp +++ b/stan/math/prim/fun/rows_dot_self.hpp @@ -17,9 +17,11 @@ namespace math { */ template * = nullptr, require_not_st_var* = nullptr> -inline Eigen::Matrix, T::RowsAtCompileTime, 1> rows_dot_self( - const T& x) { - return x.rowwise().squaredNorm(); +inline auto rows_dot_self( + T&& x) { + return make_holder([](const auto& xx) { + return std::forward(xx).rowwise().squaredNorm(); + }, std::forward(x)); } } // namespace math diff --git a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp index 7901aafc80e..9e7e859f2eb 100644 --- a/stan/math/prim/fun/scale_matrix_exp_multiply.hpp +++ b/stan/math/prim/fun/scale_matrix_exp_multiply.hpp @@ -55,16 +55,17 @@ template * = nullptr, require_any_autodiff_t, value_type_t>* = nullptr> -inline Eigen::Matrix, Eigen::Dynamic, - EigMat2::ColsAtCompileTime> -scale_matrix_exp_multiply(const Tt& t, const EigMat1& A, const EigMat2& B) { +inline auto scale_matrix_exp_multiply(Tt&& t, EigMat1&& A, EigMat2&& B) { check_square("scale_matrix_exp_multiply", "input matrix", A); check_multiplicable("scale_matrix_exp_multiply", "A", A, "B", B); if (A.size() == 0) { return {0, B.cols()}; } - return multiply(matrix_exp(multiply(A, t)), B); + return make_holder([t](auto&& A_, auto&& B_) { + return multiply(matrix_exp(multiply(std::forward(A_), t)), + std::forward(B_)); + }, std::forward(A), std::forward(B)); } } // namespace math diff --git a/stan/math/prim/fun/sd.hpp b/stan/math/prim/fun/sd.hpp index bee70a57c32..8eb4292cfa2 100644 --- a/stan/math/prim/fun/sd.hpp +++ b/stan/math/prim/fun/sd.hpp @@ -25,7 +25,6 @@ template * = nullptr, require_not_st_var* = nullptr> inline auto sd(T&& m) { using std::sqrt; - return apply_vector_unary>::reduce( std::forward(m), [](auto&& x) { check_nonzero_size("sd", "x", x); @@ -34,7 +33,7 @@ inline auto sd(T&& m) { return scalar_type_t(0.0); } - return sqrt(variance(x)); + return sqrt(variance(std::forward(x))); }); } diff --git a/stan/math/prim/fun/segment.hpp b/stan/math/prim/fun/segment.hpp index 38ef04a1697..ebd197c733e 100644 --- a/stan/math/prim/fun/segment.hpp +++ b/stan/math/prim/fun/segment.hpp @@ -15,7 +15,7 @@ namespace math { * @tparam T type of the vector */ template * = nullptr> -inline auto segment(const Vec& v, size_t i, size_t n) { +inline auto segment(Vec&& v, size_t i, size_t n) { check_greater("segment", "n", i, 0.0); check_less_or_equal("segment", "n", i, static_cast(v.size())); if (n != 0) { @@ -23,7 +23,9 @@ inline auto segment(const Vec& v, size_t i, size_t n) { check_less_or_equal("segment", "n", i + n - 1, static_cast(v.size())); } - return v.segment(i - 1, n); + return make_holder([i, n](auto&& v_) { + return v_.segment(i - 1, n); + }, std::forward(v)); } template diff --git a/stan/math/prim/fun/select.hpp b/stan/math/prim/fun/select.hpp index 99f3a461061..b4dcac58d01 100644 --- a/stan/math/prim/fun/select.hpp +++ b/stan/math/prim/fun/select.hpp @@ -121,7 +121,7 @@ inline ReturnT select(const bool c, const T_true y_true, T_false&& y_false) { }, T_true(y_true), std::forward(y_false)); } else { - return y_false; + return std::forward(y_false); } } @@ -145,11 +145,11 @@ template * = nullptr> inline auto select(T_bool&& c, const T_true y_true, const T_false y_false) { using ret_t = return_type_t; - return std::forward(c) - .unaryExpr([y_true, y_false](bool cond) { - return cond ? ret_t(y_true) : ret_t(y_false); - }) - .eval(); + return make_holder([y_true, y_false](auto&& c_){ + return std::forward(c_).unaryExpr([y_true, y_false](bool cond) { + return cond ? ret_t(y_true) : ret_t(y_false); + }); + }, std::forward(c)); } /** @@ -171,10 +171,13 @@ inline auto select(T_bool&& c, T_true&& y_true, T_false&& y_false) { check_consistent_sizes("select", "boolean", c, "left hand side", y_true, "right hand side", y_false); using ret_t = return_type_t; - return std::forward(c) - .select(std::forward(y_true), std::forward(y_false)) - .template cast() - .eval(); + return make_holder([](auto&& c_, auto&& y_true_, auto&& y_false_){ + return std::forward(c_) + .select(std::forward(y_true_), + std::forward(y_false_)) + .template cast().eval(); + }, std::forward(c), std::forward(y_true), + std::forward(y_false)); } } // namespace math diff --git a/stan/math/prim/fun/sin.hpp b/stan/math/prim/fun/sin.hpp index 850bbcabc9f..15349da0455 100644 --- a/stan/math/prim/fun/sin.hpp +++ b/stan/math/prim/fun/sin.hpp @@ -74,7 +74,7 @@ inline auto sin(T&& x) { template * = nullptr> inline auto sin(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [&](auto&& v) { return std::forward(v).array().sin(); }); } diff --git a/stan/math/prim/fun/sinh.hpp b/stan/math/prim/fun/sinh.hpp index a6a8f93c09d..7a5c744d97d 100644 --- a/stan/math/prim/fun/sinh.hpp +++ b/stan/math/prim/fun/sinh.hpp @@ -73,7 +73,7 @@ inline auto sinh(Container&& x) { template * = nullptr> inline auto sinh(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().sinh(); }); } diff --git a/stan/math/prim/fun/sqrt.hpp b/stan/math/prim/fun/sqrt.hpp index 75c680373a2..343a8ae4492 100644 --- a/stan/math/prim/fun/sqrt.hpp +++ b/stan/math/prim/fun/sqrt.hpp @@ -74,7 +74,7 @@ template * = nullptr, require_not_var_matrix_t* = nullptr> inline auto sqrt(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().sqrt(); }); } diff --git a/stan/math/prim/fun/square.hpp b/stan/math/prim/fun/square.hpp index 10859a54beb..53dd3b68076 100644 --- a/stan/math/prim/fun/square.hpp +++ b/stan/math/prim/fun/square.hpp @@ -37,8 +37,8 @@ inline double square(const T x) { */ struct square_fun { template - static inline auto fun(const T& x) { - return square(x); + static inline auto fun(T&& x) { + return square(std::forward(x)); } }; @@ -66,7 +66,7 @@ inline auto square(Container&& x) { template * = nullptr> inline auto square(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().square(); }); } diff --git a/stan/math/prim/fun/tan.hpp b/stan/math/prim/fun/tan.hpp index 9c1d758bc3a..e7b4b1796d9 100644 --- a/stan/math/prim/fun/tan.hpp +++ b/stan/math/prim/fun/tan.hpp @@ -75,7 +75,7 @@ inline auto tan(Container&& x) { template * = nullptr> inline auto tan(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().tan(); }); } diff --git a/stan/math/prim/fun/tanh.hpp b/stan/math/prim/fun/tanh.hpp index bcc25653ab3..5aaa2016801 100644 --- a/stan/math/prim/fun/tanh.hpp +++ b/stan/math/prim/fun/tanh.hpp @@ -75,7 +75,7 @@ inline auto tanh(Container&& x) { template * = nullptr> inline auto tanh(Container&& x) { - return apply_vector_unary>::apply( + return apply_vector_unary::apply( std::forward(x), [](auto&& v) { return std::forward(v).array().tanh(); }); } diff --git a/stan/math/prim/functor/apply_scalar_binary.hpp b/stan/math/prim/functor/apply_scalar_binary.hpp index b0f60807031..7ee18d0ec01 100644 --- a/stan/math/prim/functor/apply_scalar_binary.hpp +++ b/stan/math/prim/functor/apply_scalar_binary.hpp @@ -34,8 +34,8 @@ namespace math { */ template * = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { - return f(x, y); +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { + return std::forward(f)(std::forward(x), std::forward(y)); } /** @@ -57,7 +57,8 @@ inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { check_matching_dims("Binary function", "x", x, "y", y); return make_holder( [](auto&& f_inner, auto&& x_inner, auto&& y_inner) { - return x_inner.binaryExpr(y_inner, f_inner); + return std::forward(x_inner).binaryExpr( + std::forward(y_inner), std::forward(f_inner)); }, std::forward(f), std::forward(x), std::forward(y)); } @@ -84,7 +85,8 @@ inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { using int_vec_t = promote_scalar_t, plain_type_t>; Eigen::Map y_map(y_inner.data(), y_inner.size()); - return x_inner.binaryExpr(y_map, f_inner); + return std::forward(x_inner).binaryExpr( + y_map, std::forward(f_inner)); }, std::forward(f), std::forward(x), std::forward(y)); } @@ -111,7 +113,8 @@ inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { using int_vec_t = promote_scalar_t, plain_type_t>; Eigen::Map x_map(x_inner.data(), x_inner.size()); - return x_map.binaryExpr(y_inner, f_inner); + return x_map.binaryExpr(std::forward(y_inner), + std::forward(f_inner)); }, std::forward(f), std::forward(x), std::forward(y)); } @@ -132,7 +135,7 @@ template * = nullptr, require_std_vector_vt* = nullptr, require_std_vector_st* = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { if (num_elements(x) != num_elements(y)) { std::ostringstream msg; msg << "Inputs to vectorized binary function must match in" @@ -165,7 +168,7 @@ template * = nullptr, require_std_vector_st* = nullptr, require_eigen_matrix_dynamic_vt* = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { if (num_elements(x) != num_elements(y)) { std::ostringstream msg; msg << "Inputs to vectorized binary function must match in" @@ -202,9 +205,9 @@ template * = nullptr, inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { return make_holder( [](auto&& f_inner, auto&& x_inner, auto&& y_inner) { - return x_inner.unaryExpr( + return std::forward(x_inner).unaryExpr( [f_inner_ = std::forward(f_inner), - y_inner](const auto& v) { return f_inner_(v, y_inner); }); + y_inner](auto&& v) { return f_inner_(v, y_inner); }); }, std::forward(f), std::forward(x), std::forward(y)); } @@ -229,7 +232,7 @@ template (y_inner).unaryExpr( [f_inner_ = std::forward(f_inner), x_inner](auto&& v) { return f_inner_(x_inner, std::forward(v)); @@ -257,14 +260,14 @@ inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { */ template * = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { check_matching_sizes("Binary function", "x", x, "y", y); - decltype(auto) x_vec = as_column_vector_or_scalar(x); - decltype(auto) y_vec = as_column_vector_or_scalar(y); + decltype(auto) x_vec = as_column_vector_or_scalar(std::forward(x)); + decltype(auto) y_vec = as_column_vector_or_scalar(std::forward(y)); using T_return = std::decay_t; std::vector result(x.size()); Eigen::Map>(result.data(), result.size()) - = x_vec.binaryExpr(y_vec, f); + = x_vec.binaryExpr(y_vec, std::forward(f)); return result; } @@ -289,12 +292,12 @@ inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { - decltype(auto) x_vec = as_column_vector_or_scalar(x); +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { + decltype(auto) x_vec = as_column_vector_or_scalar(std::forward(x)); using T_return = std::decay_t; std::vector result(x.size()); Eigen::Map>(result.data(), result.size()) - = x_vec.unaryExpr([f, y](const auto& v) { return f(v, y); }); + = x_vec.unaryExpr([f_ = std::forward(f), y](auto&& v) { return f_(v, y); }); return result; } @@ -319,12 +322,12 @@ inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { template * = nullptr, require_std_vector_vt* = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { decltype(auto) y_vec = as_column_vector_or_scalar(y); using T_return = std::decay_t; std::vector result(y.size()); Eigen::Map>(result.data(), result.size()) - = y_vec.unaryExpr([f, x](const auto& v) { return f(x, v); }); + = y_vec.unaryExpr([f_ = std::forward(f), x](auto&& v) { return f_(x, v); }); return result; } @@ -345,7 +348,7 @@ inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { template < typename F, typename T1, typename T2, require_all_std_vector_vt* = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { check_matching_sizes("Binary function", "x", x, "y", y); using T_return = plain_type_t; size_t y_size = y.size(); @@ -373,7 +376,7 @@ inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { using T_return = plain_type_t; size_t x_size = x.size(); std::vector result(x_size); @@ -400,7 +403,7 @@ inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { template * = nullptr, require_std_vector_vt* = nullptr> -inline auto apply_scalar_binary(const F& f, const T1& x, const T2& y) { +inline auto apply_scalar_binary(F&& f, T1&& x, T2&& y) { using T_return = plain_type_t; size_t y_size = y.size(); std::vector result(y_size); diff --git a/stan/math/prim/functor/apply_scalar_ternary.hpp b/stan/math/prim/functor/apply_scalar_ternary.hpp index 81dc4bf081e..b1f4ff797e6 100644 --- a/stan/math/prim/functor/apply_scalar_ternary.hpp +++ b/stan/math/prim/functor/apply_scalar_ternary.hpp @@ -36,9 +36,8 @@ namespace math { */ template * = nullptr> -inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, - const T3& z) { - return f(x, y, z); +inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { + return std::forward(f)(std::forward(x), std::forward(y), std::forward(z)); } /** @@ -93,8 +92,7 @@ inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { */ template * = nullptr> -inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, - const T3& z) { +inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { check_matching_sizes("Ternary function", "x", x, "y", y); check_matching_sizes("Ternary function", "y", y, "z", z); decltype(auto) x_vec = as_column_vector_or_scalar(x); @@ -103,7 +101,7 @@ inline auto apply_scalar_ternary(const F& f, const T1& x, const T2& y, using T_return = std::decay_t; std::vector result(x.size()); Eigen::Map>(result.data(), result.size()) - = apply_scalar_ternary(f, x_vec, y_vec, z_vec); + = apply_scalar_ternary(std::forward(f), x_vec, y_vec, z_vec); return result; } @@ -186,10 +184,10 @@ inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, T3&& z) { +inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { return apply_scalar_binary( - [f, y](auto&& a, auto&& c) { - return f(std::forward(a), y, std::forward(c)); + [f_ = std::forward(f), y](auto&& a, auto&& c) { + return f_(std::forward(a), y, std::forward(c)); }, std::forward(x), std::forward(z)); } @@ -213,10 +211,10 @@ inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, T3&& z) { template * = nullptr, require_stan_scalar_t* = nullptr> -inline auto apply_scalar_ternary(const F& f, T1&& x, T2&& y, T3&& z) { +inline auto apply_scalar_ternary(F&& f, T1&& x, T2&& y, T3&& z) { return apply_scalar_binary( - [f, x](auto&& b, auto&& c) { - return f(x, std::forward(b), std::forward(c)); + [f_ = std::forward(f), x](auto&& b, auto&& c) { + return f_(x, std::forward(b), std::forward(c)); }, std::forward(y), std::forward(z)); } diff --git a/stan/math/prim/functor/apply_scalar_unary.hpp b/stan/math/prim/functor/apply_scalar_unary.hpp index cad17ca8301..58312c1ac56 100644 --- a/stan/math/prim/functor/apply_scalar_unary.hpp +++ b/stan/math/prim/functor/apply_scalar_unary.hpp @@ -124,8 +124,8 @@ struct apply_scalar_unary> { * @return Result of applying F to the scalar. */ template - static inline auto apply(const T2& x) { - return F::fun(x); + static inline auto apply(T2&& x) { + return F::fun(std::forward(x)); } /** * The return type diff --git a/stan/math/prim/functor/apply_vector_unary.hpp b/stan/math/prim/functor/apply_vector_unary.hpp index f4c1b2f00b4..8d9b8a8a1d4 100644 --- a/stan/math/prim/functor/apply_vector_unary.hpp +++ b/stan/math/prim/functor/apply_vector_unary.hpp @@ -129,12 +129,12 @@ struct apply_vector_unary> { * @param f functor to apply to vector input. * @return std::vector with result of applying functor to input. */ - template - static inline auto apply(const T& x, const F& f) { - using T_return = value_type_t; + template + static inline auto apply(T2&& x, F&& f) { + using T_return = value_type_t(x))))>; std::vector result(x.size()); Eigen::Map>(result.data(), result.size()) - = f(as_column_vector_or_scalar(x)).matrix(); + = std::forward(f)(as_column_vector_or_scalar(std::forward(x))).matrix(); return result; } diff --git a/stan/math/prim/meta/conjunction.hpp b/stan/math/prim/meta/conjunction.hpp index 19733e3ee90..d9025860220 100644 --- a/stan/math/prim/meta/conjunction.hpp +++ b/stan/math/prim/meta/conjunction.hpp @@ -17,6 +17,10 @@ template struct conjunction : std::conditional_t, std::false_type> {}; + +template