Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions include/boost/math/tools/polynomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ std::pair< polynomial<T>, polynomial<T> >
division(polynomial<T> u, const polynomial<T>& v)
{
BOOST_ASSERT(v.size() <= u.size());
BOOST_ASSERT(v != zero_element(std::multiplies< polynomial<T> >()));
BOOST_ASSERT(u != zero_element(std::multiplies< polynomial<T> >()));
BOOST_ASSERT(v);
BOOST_ASSERT(u);

typedef typename polynomial<T>::size_type N;

Expand Down Expand Up @@ -245,9 +245,9 @@ template <typename T>
std::pair< polynomial<T>, polynomial<T> >
quotient_remainder(const polynomial<T>& dividend, const polynomial<T>& divisor)
{
BOOST_ASSERT(divisor != zero_element(std::multiplies< polynomial<T> >()));
BOOST_ASSERT(divisor);
if (dividend.size() < divisor.size())
return std::make_pair(zero_element(std::multiplies< polynomial<T> >()), dividend);
return std::make_pair(polynomial<T>(), dividend);
return detail::division(dividend, divisor);
}

Expand Down Expand Up @@ -389,7 +389,7 @@ class polynomial :
polynomial& operator %=(const U& /*value*/)
{
// We can always divide by a scalar, so there is no remainder:
*this = zero_element(std::multiplies<polynomial>());
this->clear();
return *this;
}

Expand All @@ -412,10 +412,9 @@ class polynomial :
polynomial& operator *=(const polynomial<U>& value)
{
// TODO: FIXME: use O(N log(N)) algorithm!!!
polynomial const zero = zero_element(std::multiplies<polynomial>());
if (value == zero)
if (!value)
{
*this = zero;
this->clear();
return *this;
}
std::vector<T> prod(size() + value.size() - 1, T(0));
Expand Down Expand Up @@ -455,6 +454,18 @@ class polynomial :
normalize();
return *this;
}

// Useful for fast test of equality to zero.
operator bool() const

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be explicit when BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS is not set, otherwise use an implicit conversion to an "unmentionable" type, for example multiprecision::number uses:

typedef bool (self_type::*unmentionable_type)()const;

BOOST_MP_FORCEINLINE operator unmentionable_type()const
{
return is_zero() ? 0 : &self_type::is_zero;
}

Which prevents accidental conversion to an arithmetic type.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, wow, that's a new one on me. What about naming a function that sets the polynomial to zero? I have used the verb clear but maybe set_zero is more appropriate? This is on the assumption that it is more efficient than assignment from zero, which I assume the compiler is not quite smart enough to optimize away for a class such as polynomial.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 12/05/2016 12:08, Jeremy W. Murphy wrote:

In include/boost/math/tools/polynomial.hpp
#39 (comment):

@@ -455,6 +454,18 @@ class polynomial :
normalize();
return *this;
}
+

  • // Useful for fast test of equality to zero.
  • operator bool() const

Ahh, wow, that's a new one on me. What about naming a function that
sets the polynomial to zero? I have used the verb |clear| but maybe
|set_zero| is more appropriate? This is on the assumption that it is
more efficient than assignment from zero, which I assume the compiler
is not quite smart enough to optimize away for a class such as polynomial.

clear() works for me.

{
return !m_data.empty();
}

// Fast way to set a polynomial to zero.
void clear()
{
m_data.clear();
}

/** Remove zero coefficients 'from the top', that is for which there are no
* non-zero coefficients of higher degree. */
Expand Down
18 changes: 18 additions & 0 deletions test/test_polynomial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,21 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(test_odd_even, T, all_test_types)
BOOST_CHECK_EQUAL(odd(b), false);
BOOST_CHECK_EQUAL(even(b), true);
}


BOOST_AUTO_TEST_CASE_TEMPLATE(test_bool, T, all_test_types)
{
polynomial<T> const zero;
polynomial<T> const a(d0a.begin(), d0a.end());
BOOST_CHECK_EQUAL(bool(zero), false);
BOOST_CHECK_EQUAL(bool(a), true);
}


BOOST_AUTO_TEST_CASE_TEMPLATE(test_clear, T, all_test_types)
{
polynomial<T> const zero;
polynomial<T> a(d0a.begin(), d0a.end());
a.clear();
BOOST_CHECK_EQUAL(a, zero);
}