From 0c82cc96b9781e9f7874dab0cbd2b4400fbca563 Mon Sep 17 00:00:00 2001 From: Purna-Chandra-4706 Date: Tue, 25 Aug 2026 19:57:39 +0530 Subject: [PATCH 1/3] Efficiency: use multiplication over std::pow for squaring (#3373) --- stan/math/prim/fun/square.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stan/math/prim/fun/square.hpp b/stan/math/prim/fun/square.hpp index 0ed671bb96d..9ab83c70691 100644 --- a/stan/math/prim/fun/square.hpp +++ b/stan/math/prim/fun/square.hpp @@ -25,7 +25,8 @@ namespace math { */ template * = nullptr> inline double square(const T x) { - return std::pow(x, 2); + double x_dbl = x; + return x_dbl * x_dbl; } /** From 05e4a1ad88b7f2881f5a1071e969712fbcc876a9 Mon Sep 17 00:00:00 2001 From: Purna-Chandra-4706 Date: Tue, 25 Aug 2026 22:39:11 +0530 Subject: [PATCH 2/3] Efficiency: update squared_distance to use multiplication instead of pow --- stan/math/rev/fun/squared_distance.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/fun/squared_distance.hpp b/stan/math/rev/fun/squared_distance.hpp index 584114ee2c7..76085153837 100644 --- a/stan/math/rev/fun/squared_distance.hpp +++ b/stan/math/rev/fun/squared_distance.hpp @@ -21,7 +21,7 @@ namespace math { inline var squared_distance(const var& a, const var& b) { check_finite("squared_distance", "a", a); check_finite("squared_distance", "b", b); - return make_callback_vari(std::pow(a.val() - b.val(), 2), + return make_callback_vari((a.val() - b.val()) * (a.val() - b.val()), [a, b](const auto& vi) mutable { const double diff = 2.0 * (a.val() - b.val()); a.adj() += vi.adj_ * diff; @@ -35,7 +35,7 @@ inline var squared_distance(const var& a, const var& b) { inline var squared_distance(const var& a, double b) { check_finite("squared_distance", "a", a); check_finite("squared_distance", "b", b); - return make_callback_vari(std::pow(a.val() - b, 2), + return make_callback_vari((a.val() - b) * (a.val() - b), [a, b](const auto& vi) mutable { a.adj() += vi.adj_ * 2.0 * (a.val() - b); }); From c98e30ef70b308443de39c7534af4b083a98dcef Mon Sep 17 00:00:00 2001 From: Purna-Chandra-4706 Date: Wed, 26 Aug 2026 21:22:50 +0530 Subject: [PATCH 3/3] Refactoring squared_distance to use multiplication instead of pow. --- stan/math/rev/fun/squared_distance.hpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stan/math/rev/fun/squared_distance.hpp b/stan/math/rev/fun/squared_distance.hpp index 76085153837..42042193f01 100644 --- a/stan/math/rev/fun/squared_distance.hpp +++ b/stan/math/rev/fun/squared_distance.hpp @@ -21,7 +21,8 @@ namespace math { inline var squared_distance(const var& a, const var& b) { check_finite("squared_distance", "a", a); check_finite("squared_distance", "b", b); - return make_callback_vari((a.val() - b.val()) * (a.val() - b.val()), + double difference = a.val() - b.val(); + return make_callback_vari(difference * difference, [a, b](const auto& vi) mutable { const double diff = 2.0 * (a.val() - b.val()); a.adj() += vi.adj_ * diff; @@ -35,7 +36,8 @@ inline var squared_distance(const var& a, const var& b) { inline var squared_distance(const var& a, double b) { check_finite("squared_distance", "a", a); check_finite("squared_distance", "b", b); - return make_callback_vari((a.val() - b) * (a.val() - b), + double difference = a.val() - b; + return make_callback_vari(difference * difference, [a, b](const auto& vi) mutable { a.adj() += vi.adj_ * 2.0 * (a.val() - b); });