From 49317cdb6e7ae9d94a8c6d17e6c653639d55e65f Mon Sep 17 00:00:00 2001 From: Eric Wolf Date: Wed, 7 Dec 2022 05:38:29 +0100 Subject: [PATCH 1/2] add Polynomial.calculate_horner This allows to evaluate polynomials more efficiently. --- polynomial/core.py | 13 +++++++++++++ tests/test_polynomials_operations.py | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/polynomial/core.py b/polynomial/core.py index b2617c4..ade127e 100644 --- a/polynomial/core.py +++ b/polynomial/core.py @@ -291,6 +291,19 @@ def calculate(self, x): return sum(ak * (x ** k) for ak, k in self.terms) + def calculate_horner(self, x): + """Calculate the value of the polynomial at a given point + using Horner`s method. + + This method is faster than calling calculate for almost all + polynomials except monomials but might produce slightly different + results when using floats. + """ + result = 0 + for coeff in self: + result = coeff + x * result + return result + def __call__(self, x): """Calculate the value of the polynomial at a given point.""" return self.calculate(x) diff --git a/tests/test_polynomials_operations.py b/tests/test_polynomials_operations.py index af06f00..1e04578 100644 --- a/tests/test_polynomials_operations.py +++ b/tests/test_polynomials_operations.py @@ -1310,6 +1310,25 @@ def test_calculate_zero_polynomial(self): self.assertEqual(0, Constant(0).calculate(5)) self.assertEqual(0, Monomial(0, 1).calculate(1.1)) + def test_calculate_horner(self): + """Tests calculate_horner on a simple polynomial.""" + a = Polynomial(1, 2, 3) + + def eqn(x): + return x ** 2 + 2 * x + 3 + + for i in range(-100, 100): + i = i / 100 + self.assertAlmostEqual(eqn(i), a.calculate_horner(i)) + + def test_calculate_horner_zero_polynomial(self): + """Test that calculations of zero polynomial values + using Horner`s method always give 0. + """ + self.assertEqual(0, ZeroPolynomial().calculate_horner(1)) + self.assertEqual(0, Constant(0).calculate_horner(5)) + self.assertEqual(0, Monomial(0, 1).calculate_horner(1.1)) + def test_call(self): """Tests calculate on a simple polynomial.""" a = Polynomial(1, 2, 3) From c1daa6270992b89c2a3765e532791346fe288dc4 Mon Sep 17 00:00:00 2001 From: Eric Wolf Date: Thu, 8 Dec 2022 06:52:07 +0100 Subject: [PATCH 2/2] fix docstring style --- polynomial/core.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/polynomial/core.py b/polynomial/core.py index ade127e..7fbda6f 100644 --- a/polynomial/core.py +++ b/polynomial/core.py @@ -292,12 +292,11 @@ def calculate(self, x): return sum(ak * (x ** k) for ak, k in self.terms) def calculate_horner(self, x): - """Calculate the value of the polynomial at a given point - using Horner`s method. + """Calculate the value of the polynomial at a given point. - This method is faster than calling calculate for almost all - polynomials except monomials but might produce slightly different - results when using floats. + This method is using Horner`s method and faster than calling + calculate for almost all polynomials except monomials but might + produce slightly different results when using floats. """ result = 0 for coeff in self: