From e9da8a10bf045b1a5ed5017a9b69ae7678871b2b Mon Sep 17 00:00:00 2001 From: "Jeremy W. Murphy" Date: Sun, 27 Mar 2016 22:03:33 +1100 Subject: [PATCH 1/4] polynomial: Left and right shift operators. Shifting adds or removes a factor of x in the same way that shifting adds or removes a factor of 2 to integers. --- include/boost/math/tools/polynomial.hpp | 33 ++++++++++++++++++++++++- test/test_polynomial.cpp | 31 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/include/boost/math/tools/polynomial.hpp b/include/boost/math/tools/polynomial.hpp index 73980d6aa..e167c7b37 100644 --- a/include/boost/math/tools/polynomial.hpp +++ b/include/boost/math/tools/polynomial.hpp @@ -448,7 +448,22 @@ public: return *this; } - /** Remove zero coefficients 'from the top', that is for which there are no + template + polynomial& operator >>=(U const &n) + { + BOOST_ASSERT(n <= m_data.size()); + m_data.erase(m_data.begin(), m_data.begin() + n); + return *this; + } + + template + polynomial& operator <<=(U const &n) + { + m_data.insert(m_data.begin(), n, static_cast(0)); + return *this; + } + + /** Remove zero coefficients 'from the top', that is for which there are no * non-zero coefficients of higher degree. */ void normalize() { @@ -600,6 +615,22 @@ bool operator == (const polynomial &a, const polynomial &b) return a.data() == b.data(); } +template +polynomial operator >> (const polynomial& a, const U& b) +{ + polynomial result(a); + result >>= b; + return result; +} + +template +polynomial operator << (const polynomial& a, const U& b) +{ + polynomial result(a); + result <<= b; + return result; +} + // Unary minus (negate). template polynomial operator - (polynomial a) diff --git a/test/test_polynomial.cpp b/test/test_polynomial.cpp index 978f4cac8..d1d196eef 100644 --- a/test/test_polynomial.cpp +++ b/test/test_polynomial.cpp @@ -38,6 +38,8 @@ boost::array const d2a = {{-2, 2, 3}}; boost::array const d2b = {{-7, 5, 6}}; boost::array const d2c = {{31, -21, -22}}; boost::array const d0a = {{6}}; +boost::array const d0a1 = {{0, 6}}; +boost::array const d0a5 = {{0, 0, 0, 0, 0, 6}}; boost::array const d0b = {{3}}; boost::array const d8 = {{-5, 2, 8, -3, -3, 0, 1, 0, 1}}; @@ -216,3 +218,32 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_non_integral_arithmetic_relations, T, non_int BOOST_CHECK_EQUAL(a * T(0.5), a / T(2)); } + +BOOST_AUTO_TEST_CASE_TEMPLATE(test_right_shift, T, all_test_types ) +{ + polynomial a(d8b.begin(), d8b.end()); + polynomial const aa(a); + polynomial const b(d8b.begin() + 1, d8b.end()); + polynomial const c(d8b.begin() + 5, d8b.end()); + a >>= 0u; + BOOST_CHECK_EQUAL(a, aa); + a >>= 1u; + BOOST_CHECK_EQUAL(a, b); + a = a >> 4u; + BOOST_CHECK_EQUAL(a, c); +} + + +BOOST_AUTO_TEST_CASE_TEMPLATE(test_left_shift, T, all_test_types ) +{ + polynomial a(d0a.begin(), d0a.end()); + polynomial const aa(a); + polynomial const b(d0a1.begin(), d0a1.end()); + polynomial const c(d0a5.begin(), d0a5.end()); + a <<= 0u; + BOOST_CHECK_EQUAL(a, aa); + a <<= 1u; + BOOST_CHECK_EQUAL(a, b); + a = a << 4u; + BOOST_CHECK_EQUAL(a, c); +} From 238cd6078bb173310869caddc189d8b092b64da7 Mon Sep 17 00:00:00 2001 From: "Jeremy W. Murphy" Date: Mon, 28 Mar 2016 16:04:27 +1100 Subject: [PATCH 2/4] polynomial: odd/even predicates. We use the definition of odd and even consistent with considering x as the smallest prime factor. That is, an even polynomial has a constant of zero. --- include/boost/math/tools/polynomial.hpp | 12 ++++++++++++ test/test_polynomial.cpp | 14 ++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/boost/math/tools/polynomial.hpp b/include/boost/math/tools/polynomial.hpp index e167c7b37..25276025f 100644 --- a/include/boost/math/tools/polynomial.hpp +++ b/include/boost/math/tools/polynomial.hpp @@ -639,6 +639,18 @@ polynomial operator - (polynomial a) return a; } +template +bool odd(polynomial const &a) +{ + return a.size() > 0 && a[0] != static_cast(0); +} + +template +bool even(polynomial const &a) +{ + return !odd(a); +} + template inline std::basic_ostream& operator << (std::basic_ostream& os, const polynomial& poly) { diff --git a/test/test_polynomial.cpp b/test/test_polynomial.cpp index d1d196eef..3722f0f71 100644 --- a/test/test_polynomial.cpp +++ b/test/test_polynomial.cpp @@ -247,3 +247,17 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_left_shift, T, all_test_types ) a = a << 4u; BOOST_CHECK_EQUAL(a, c); } + + +BOOST_AUTO_TEST_CASE_TEMPLATE(test_odd_even, T, all_test_types) +{ + polynomial const zero = zero_element(multiplies< polynomial >()); + BOOST_CHECK_EQUAL(odd(zero), false); + BOOST_CHECK_EQUAL(even(zero), true); + polynomial const a(d0a.begin(), d0a.end()); + BOOST_CHECK_EQUAL(odd(a), true); + BOOST_CHECK_EQUAL(even(a), false); + polynomial const b(d0a1.begin(), d0a1.end()); + BOOST_CHECK_EQUAL(odd(b), false); + BOOST_CHECK_EQUAL(even(b), true); +} From cff870545851520bdd27ce62f0f7de769785f810 Mon Sep 17 00:00:00 2001 From: "Jeremy W. Murphy" Date: Mon, 28 Mar 2016 16:08:04 +1100 Subject: [PATCH 3/4] Consistent white space. --- include/boost/math/tools/polynomial.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/boost/math/tools/polynomial.hpp b/include/boost/math/tools/polynomial.hpp index 25276025f..c89b7a3f9 100644 --- a/include/boost/math/tools/polynomial.hpp +++ b/include/boost/math/tools/polynomial.hpp @@ -454,14 +454,14 @@ public: BOOST_ASSERT(n <= m_data.size()); m_data.erase(m_data.begin(), m_data.begin() + n); return *this; - } + } - template - polynomial& operator <<=(U const &n) - { - m_data.insert(m_data.begin(), n, static_cast(0)); - return *this; - } + template + polynomial& operator <<=(U const &n) + { + m_data.insert(m_data.begin(), n, static_cast(0)); + return *this; + } /** Remove zero coefficients 'from the top', that is for which there are no * non-zero coefficients of higher degree. */ From 045aa6ae114f05e7969b38629b4f512b02102c54 Mon Sep 17 00:00:00 2001 From: "Jeremy W. Murphy" Date: Tue, 29 Mar 2016 09:11:02 +1100 Subject: [PATCH 4/4] Basic specification of shift operators. --- doc/internals/polynomial.qbk | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/doc/internals/polynomial.qbk b/doc/internals/polynomial.qbk index 5ea2e53b0..5f7c0feac 100644 --- a/doc/internals/polynomial.qbk +++ b/doc/internals/polynomial.qbk @@ -90,6 +90,15 @@ template polynomial operator - (const polynomial& a); + template + polynomial operator >>= (const U& a); + template + polynomial operator >> (polynomial const &a, const U& b); + template + polynomial operator <<= (const U& a); + template + polynomial operator << (polynomial const &a, const U& b); + template bool operator == (const polynomial &a, const polynomial &b); template