From 01eda97ff64ad2570b7105cc5b734f488ce0d47a Mon Sep 17 00:00:00 2001 From: Hong-Phuc Bui <hong-phuc.bui@htwsaar.de> Date: Wed, 04 Jun 2025 18:30:14 +0200 Subject: [PATCH] Implement plus operator --- polymonial/polynomial.py | 34 ++++++++++++++++ .gitignore | 2 + polymonial/polynomial_test.py | 25 +++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index b22ec92..e8153cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea/ __pycache__/ +.coverage +htmlcov/ diff --git a/polymonial/polynomial.py b/polymonial/polynomial.py index b847224..2ca4f73 100644 --- a/polymonial/polynomial.py +++ b/polymonial/polynomial.py @@ -1,7 +1,11 @@ class Polynomial: def __init__(self, *argv: float): + if len(argv) < 1: + raise ValueError("A Polynomial has at least one coefficient.") + if argv[-1] == 0: + raise ValueError("The coefficient at the highest degree must not be zero.") self.__coefficients = tuple(argv) - + self.__order = len(argv) def evaluate(self, x: float) -> tuple[float, list[float]]: p = self.__coefficients[-1] @@ -11,8 +15,36 @@ c.insert(0, p) return c[0], c[1:] + def order(self): + return self.__order + + def __add__(self, other: "Polynomial") -> "Polynomial": + if not isinstance(other, Polynomial): + raise ValueError("Operator + is only applicable for Polynomial.") + coefficients = [] + if self.__order > other.__order: + low_order = other.__order + heigh_order = self.__order + higher_coefficients = self.__coefficients + else: + low_order = self.__order + heigh_order = other.__order + higher_coefficients = other.__coefficients + + for i in range(0, low_order): + coefficients.append(self.__coefficients[i] + other.__coefficients[i]) + for j in range(i+1, heigh_order): + coefficients.append(higher_coefficients[j]) + return Polynomial(*coefficients) + + def __getitem__(self, key): + return self.__coefficients[key] + + def __repr__(self) -> str: coe = [] for c in self.__coefficients: coe.append(f'{c}') return ' '.join(coe) + + diff --git a/polymonial/polynomial_test.py b/polymonial/polynomial_test.py index f232260..2fa1f53 100644 --- a/polymonial/polynomial_test.py +++ b/polymonial/polynomial_test.py @@ -7,12 +7,14 @@ s = str(c) print(r, s, c) + def test_evaluate(): l = Polynomial(-12.5, 3.6) x = 0.0 (y, rest) = l.evaluate(x) assert abs(y + 12.5) < EPSILON + def test_evaluate_2(): l = Polynomial(11, 7, -5, -4, 2) x = 2 @@ -23,8 +25,27 @@ assert abs(y - y_expected) < EPSILON for (i, r) in enumerate( q_expected ): assert abs(r - q[i]) < EPSILON - print(Polynomial(*q)) + + +def test_polynomial_add(): + p = Polynomial(-3, 4.5, 6) # ~$p(x) = -3 + 4.5x + 6x^2$~ + q = Polynomial( 1, 0, 3, -4) # ~$q(x) = 1 + 3x^2 - 4x^3$~ + s = p + q # ~$s(x) = -2 + 4.5x + 9x^2 - 4x^3$~ + expected = [-2, 4.5, 9, -4] + for (i, ec) in enumerate( expected ): + pc = s[i] + assert abs(pc - ec) < EPSILON + +def test_polynomial_add_2(): + p = Polynomial(1, 0, 3, -4) # ~$q(x) = 1 + 3x^2 - 4x^3$~ + q = Polynomial(-3, 4.5, 6) # ~$p(x) = -3 + 4.5x + 6x^2$~ + s = p + q # ~$s(x) = -2 + 4.5x + 9x^2 - 4x^3$~ + expected = [-2, 4.5, 9, -4] + for (i, ec) in enumerate( expected ): + pc = s[i] + assert abs(pc - ec) < EPSILON + if __name__ == "__main__": #test_evaluate_2() - test_polynomial_representation() \ No newline at end of file + test_polynomial_add() \ No newline at end of file -- Gitblit v1.10.0-SNAPSHOT