From def2212eb80637da510793d0881237c40904e633 Mon Sep 17 00:00:00 2001 From: bbbales2 Date: Mon, 30 Mar 2020 16:50:10 -0700 Subject: [PATCH 1/9] Added arithmetic version of pow (Fixes: #1809) --- stan/math/prim/fun/pow.hpp | 15 +++++++-------- test/unit/math/mix/fun/pow_test.cpp | 5 ----- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/stan/math/prim/fun/pow.hpp b/stan/math/prim/fun/pow.hpp index da61000e00f..d4d02669793 100644 --- a/stan/math/prim/fun/pow.hpp +++ b/stan/math/prim/fun/pow.hpp @@ -9,23 +9,22 @@ namespace stan { namespace math { -// This overload is required becuase MINGW32 does not include the -// specialization std::pow(double, int) as required by the C++11 -// standard library interface specification. Use with other compilers -// than MINGW32 would introduce an ambiguity with the standard library. -#if __MINGW32__ /** * Return the first argument raised to the power of the second * argument. * + * @tparam T1 type of base + * @tparam T2 type of exponent * @param x base * @param y exponent * @return base raised to the power of the exponent */ -inline double pow(double x, int y) { - return std::pow(x, static_cast(y)); +template, + typename = require_arithmetic_t> +inline double pow(T1 x, T2 y) { + return std::pow(x, y); } -#endif namespace internal { diff --git a/test/unit/math/mix/fun/pow_test.cpp b/test/unit/math/mix/fun/pow_test.cpp index 4a48fa235b7..483ca166165 100644 --- a/test/unit/math/mix/fun/pow_test.cpp +++ b/test/unit/math/mix/fun/pow_test.cpp @@ -6,7 +6,6 @@ template void expect_arith_instantiate() { using stan::math::pow; - using std::pow; auto a1 = pow(T(1.0), 1); auto b1 = pow(T(1.0), 1.0); auto c1 = pow(1, T(1.0)); @@ -36,7 +35,6 @@ TEST(mathMixScalFun, powInstantiations) { TEST(mathMixScalFun, pow) { auto f = [](const auto& x1, const auto& x2) { - using std::pow; using stan::math::pow; return pow(x1, x2); }; @@ -57,7 +55,6 @@ TEST(mathMixScalFun, pow) { } TEST(mathMixFun, complexPow) { auto f = [](const auto& x1, const auto& x2) { - using std::pow; using stan::math::pow; return pow(x1, x2); }; @@ -96,7 +93,6 @@ TEST(mathMixFun, powIntAmbiguityTest) { using stan::math::pow; // included to check ambiguities using stan::math::var; using std::complex; - using std::pow; int i = 2; double d = 2.5; var v = 2.5; @@ -141,7 +137,6 @@ TEST(mathMixFun, powIntAmbiguityTestFvar) { using stan::math::fvar; using stan::math::pow; // included to check ambiguities using std::complex; - using std::pow; int i = 2; double d = 2.5; fvar v = 2.5; From d539b587942685abca1fa4901de432e5dbbb9da7 Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Mon, 30 Mar 2020 19:53:43 -0400 Subject: [PATCH 2/9] [Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/stable/2017-11-14) --- stan/math/opencl/kernel_generator/load.hpp | 2 +- stan/math/prim/fun/pow.hpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/stan/math/opencl/kernel_generator/load.hpp b/stan/math/opencl/kernel_generator/load.hpp index 799cdb551a0..ae0f7d1eb6d 100644 --- a/stan/math/opencl/kernel_generator/load.hpp +++ b/stan/math/opencl/kernel_generator/load.hpp @@ -49,7 +49,7 @@ class load_ * Creates a deep copy of this expression. * @return copy of \c *this */ - inline load_ deep_copy() const & { return load_(a_); } + inline load_ deep_copy() const& { return load_(a_); } inline load_ deep_copy() && { return load_(std::forward(a_)); } /** diff --git a/stan/math/prim/fun/pow.hpp b/stan/math/prim/fun/pow.hpp index d4d02669793..0433c59441a 100644 --- a/stan/math/prim/fun/pow.hpp +++ b/stan/math/prim/fun/pow.hpp @@ -19,9 +19,8 @@ namespace math { * @param y exponent * @return base raised to the power of the exponent */ -template, - typename = require_arithmetic_t> +template , + typename = require_arithmetic_t> inline double pow(T1 x, T2 y) { return std::pow(x, y); } From bb9d624ac98865cdd2ead9867f1a08ddc82d9414 Mon Sep 17 00:00:00 2001 From: bbbales2 Date: Tue, 31 Mar 2020 05:57:24 -0700 Subject: [PATCH 3/9] Added std::pow to stan::math namespace to handle arithmetic types (Issue #1809) --- stan/math/prim/fun/pow.hpp | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/stan/math/prim/fun/pow.hpp b/stan/math/prim/fun/pow.hpp index d4d02669793..d9a617c9200 100644 --- a/stan/math/prim/fun/pow.hpp +++ b/stan/math/prim/fun/pow.hpp @@ -9,22 +9,7 @@ namespace stan { namespace math { -/** - * Return the first argument raised to the power of the second - * argument. - * - * @tparam T1 type of base - * @tparam T2 type of exponent - * @param x base - * @param y exponent - * @return base raised to the power of the exponent - */ -template, - typename = require_arithmetic_t> -inline double pow(T1 x, T2 y) { - return std::pow(x, y); -} +using std::pow; namespace internal { From 2c14a1f53eccfa2258382cb6f6c3f3f89936c3f3 Mon Sep 17 00:00:00 2001 From: bbbales2 Date: Tue, 31 Mar 2020 06:57:11 -0700 Subject: [PATCH 4/9] Added some tests to make sure stan::math::pow, pow, and std::pow are working together (Issue #1809) --- test/unit/math/prim/fun/pow_test.cpp | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 test/unit/math/prim/fun/pow_test.cpp diff --git a/test/unit/math/prim/fun/pow_test.cpp b/test/unit/math/prim/fun/pow_test.cpp new file mode 100644 index 00000000000..a38c25e13a0 --- /dev/null +++ b/test/unit/math/prim/fun/pow_test.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +// Make sure stan::math has a pow implementation +namespace test1 { + using namespace stan::math; + void test_pow() { + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); + } +} + +// Make sure std and stan::math don't conflict +namespace test2 { + using namespace std; + using namespace stan::math; + void test_pow() { + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); + } +} + +TEST(MathFunctions, powNamespaceChecks) { + EXPECT_FLOAT_EQ(stan::math::pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(stan::math::pow(1.7, 2), 2.89); + + { + using stan::math::pow; + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); + } + + { + using stan::math::pow; + using std::pow; + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); + } + + test1::test_pow(); + test2::test_pow(); +} From 5c69da81622eb0828ecfc6daa2315ed1bed5f2ed Mon Sep 17 00:00:00 2001 From: Stan Jenkins Date: Tue, 31 Mar 2020 09:59:59 -0400 Subject: [PATCH 5/9] [Jenkins] auto-formatting by clang-format version 6.0.0 (tags/google/stable/2017-11-14) --- test/unit/math/prim/fun/pow_test.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/unit/math/prim/fun/pow_test.cpp b/test/unit/math/prim/fun/pow_test.cpp index a38c25e13a0..89e1f3c569f 100644 --- a/test/unit/math/prim/fun/pow_test.cpp +++ b/test/unit/math/prim/fun/pow_test.cpp @@ -6,22 +6,22 @@ // Make sure stan::math has a pow implementation namespace test1 { - using namespace stan::math; - void test_pow() { - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } +using namespace stan::math; +void test_pow() { + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); } +} // namespace test1 // Make sure std and stan::math don't conflict namespace test2 { - using namespace std; - using namespace stan::math; - void test_pow() { - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } +using namespace std; +using namespace stan::math; +void test_pow() { + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); } +} // namespace test2 TEST(MathFunctions, powNamespaceChecks) { EXPECT_FLOAT_EQ(stan::math::pow(1.5, 1.5), 1.837117); From b92f149f6968c1425526f2981ad40f94a8402567 Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Tue, 31 Mar 2020 10:24:59 -0400 Subject: [PATCH 6/9] NOLINT for using namespace tests for pow --- test/unit/math/prim/fun/pow_test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/math/prim/fun/pow_test.cpp b/test/unit/math/prim/fun/pow_test.cpp index 89e1f3c569f..9455029dd89 100644 --- a/test/unit/math/prim/fun/pow_test.cpp +++ b/test/unit/math/prim/fun/pow_test.cpp @@ -6,7 +6,7 @@ // Make sure stan::math has a pow implementation namespace test1 { -using namespace stan::math; +using namespace stan::math; // NOLINT(build/namespaces) void test_pow() { EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); @@ -15,8 +15,8 @@ void test_pow() { // Make sure std and stan::math don't conflict namespace test2 { -using namespace std; -using namespace stan::math; +using namespace std; // NOLINT(build/namespaces) +using namespace stan::math; // NOLINT(build/namespaces) void test_pow() { EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); From 90e3f8ffabfa036f92a2e56bd210fea86bae379a Mon Sep 17 00:00:00 2001 From: bbbales2 Date: Wed, 1 Apr 2020 13:13:55 -0700 Subject: [PATCH 7/9] Added more tests for the arithmetic behavior of stan::math::pow (Issue #1809) --- test/unit/math/prim/fun/pow_test.cpp | 37 ++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/test/unit/math/prim/fun/pow_test.cpp b/test/unit/math/prim/fun/pow_test.cpp index a38c25e13a0..5750daa08cb 100644 --- a/test/unit/math/prim/fun/pow_test.cpp +++ b/test/unit/math/prim/fun/pow_test.cpp @@ -4,7 +4,6 @@ #include #include -// Make sure stan::math has a pow implementation namespace test1 { using namespace stan::math; void test_pow() { @@ -13,7 +12,6 @@ namespace test1 { } } -// Make sure std and stan::math don't conflict namespace test2 { using namespace std; using namespace stan::math; @@ -23,6 +21,32 @@ namespace test2 { } } +namespace test3 { + using namespace stan::math; + void test_pow() { + using std::pow; + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); + } +} + +namespace test4 { + using namespace std; + void test_pow() { + using stan::math::pow; + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); + } +} + +namespace test5 { + using namespace std; + void test_pow() { + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); + } +} + TEST(MathFunctions, powNamespaceChecks) { EXPECT_FLOAT_EQ(stan::math::pow(1.5, 1.5), 1.837117); EXPECT_FLOAT_EQ(stan::math::pow(1.7, 2), 2.89); @@ -33,6 +57,12 @@ TEST(MathFunctions, powNamespaceChecks) { EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); } + { + using std::pow; + EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); + EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); + } + { using stan::math::pow; using std::pow; @@ -42,4 +72,7 @@ TEST(MathFunctions, powNamespaceChecks) { test1::test_pow(); test2::test_pow(); + test3::test_pow(); + test4::test_pow(); + test5::test_pow(); } From eb97a6427015c9091801cb65d8d0e0921db20ae3 Mon Sep 17 00:00:00 2001 From: bbbales2 Date: Fri, 3 Apr 2020 15:47:38 -0700 Subject: [PATCH 8/9] Don't include using std::pow in stan::math namespace just yet (Issue #1809) --- stan/math/prim/fun/pow.hpp | 2 - test/unit/math/prim/fun/pow_test.cpp | 78 ---------------------------- 2 files changed, 80 deletions(-) delete mode 100644 test/unit/math/prim/fun/pow_test.cpp diff --git a/stan/math/prim/fun/pow.hpp b/stan/math/prim/fun/pow.hpp index d9a617c9200..29516b96893 100644 --- a/stan/math/prim/fun/pow.hpp +++ b/stan/math/prim/fun/pow.hpp @@ -9,8 +9,6 @@ namespace stan { namespace math { -using std::pow; - namespace internal { /** diff --git a/test/unit/math/prim/fun/pow_test.cpp b/test/unit/math/prim/fun/pow_test.cpp deleted file mode 100644 index 5750daa08cb..00000000000 --- a/test/unit/math/prim/fun/pow_test.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#include -#include -#include - -namespace test1 { - using namespace stan::math; - void test_pow() { - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } -} - -namespace test2 { - using namespace std; - using namespace stan::math; - void test_pow() { - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } -} - -namespace test3 { - using namespace stan::math; - void test_pow() { - using std::pow; - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } -} - -namespace test4 { - using namespace std; - void test_pow() { - using stan::math::pow; - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } -} - -namespace test5 { - using namespace std; - void test_pow() { - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } -} - -TEST(MathFunctions, powNamespaceChecks) { - EXPECT_FLOAT_EQ(stan::math::pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(stan::math::pow(1.7, 2), 2.89); - - { - using stan::math::pow; - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } - - { - using std::pow; - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } - - { - using stan::math::pow; - using std::pow; - EXPECT_FLOAT_EQ(pow(1.5, 1.5), 1.837117); - EXPECT_FLOAT_EQ(pow(1.7, 2), 2.89); - } - - test1::test_pow(); - test2::test_pow(); - test3::test_pow(); - test4::test_pow(); - test5::test_pow(); -} From 087a58450837d886258b5aa0e471ed24fa41e72a Mon Sep 17 00:00:00 2001 From: bbbales2 Date: Sat, 4 Apr 2020 06:43:00 -0700 Subject: [PATCH 9/9] Fixed namespace issues in pow tests in mix (Issue #1809) --- test/unit/math/mix/fun/pow_test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/unit/math/mix/fun/pow_test.cpp b/test/unit/math/mix/fun/pow_test.cpp index 483ca166165..b08e86ad8f2 100644 --- a/test/unit/math/mix/fun/pow_test.cpp +++ b/test/unit/math/mix/fun/pow_test.cpp @@ -6,6 +6,7 @@ template void expect_arith_instantiate() { using stan::math::pow; + using std::pow; auto a1 = pow(T(1.0), 1); auto b1 = pow(T(1.0), 1.0); auto c1 = pow(1, T(1.0)); @@ -36,6 +37,7 @@ TEST(mathMixScalFun, powInstantiations) { TEST(mathMixScalFun, pow) { auto f = [](const auto& x1, const auto& x2) { using stan::math::pow; + using std::pow; return pow(x1, x2); }; stan::test::expect_ad(f, -0.4, 0.5); @@ -56,6 +58,7 @@ TEST(mathMixScalFun, pow) { TEST(mathMixFun, complexPow) { auto f = [](const auto& x1, const auto& x2) { using stan::math::pow; + using std::pow; return pow(x1, x2); }; stan::test::ad_tolerances tols; @@ -93,6 +96,7 @@ TEST(mathMixFun, powIntAmbiguityTest) { using stan::math::pow; // included to check ambiguities using stan::math::var; using std::complex; + using std::pow; int i = 2; double d = 2.5; var v = 2.5; @@ -137,6 +141,7 @@ TEST(mathMixFun, powIntAmbiguityTestFvar) { using stan::math::fvar; using stan::math::pow; // included to check ambiguities using std::complex; + using std::pow; int i = 2; double d = 2.5; fvar v = 2.5;