From 5cba9763e41ae4070b0d7c88af72ac2f6b71791f Mon Sep 17 00:00:00 2001 From: Lafrite Date: Mon, 22 Dec 2014 15:54:07 +0100 Subject: [PATCH] overload ^ and ** for monoms --- pymath/polynom.py | 39 ++++++++++++++++++++++++++++++++------- test/test_polynom.py | 33 ++++++++------------------------- 2 files changed, 40 insertions(+), 32 deletions(-) diff --git a/pymath/polynom.py b/pymath/polynom.py index 1df73db..924392d 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -86,14 +86,15 @@ class Polynom(object): else: self._coef.append(coef) - def get_degree(self): + @property + def degree(self): """Getting the degree fo the polynom :returns: the degree of the polynom - >>> Polynom([1, 2, 3]).get_degree() + >>> Polynom([1, 2, 3]).degree 2 - >>> Polynom([1]).get_degree() + >>> Polynom([1]).degree 0 """ return len(self._coef) - 1 @@ -373,6 +374,29 @@ class Polynom(object): return o_poly.__mul__(self) + def __pow__(self, power): + if not type(power): + raise ValueError("Can't raise Polynom to {} power".format(str(power))) + + steps = [] + + if self.is_monom(): + coefs = [0]*self.degree*power + [Expression([self._coef[self.degree] , power, op.pw])] + p = Polynom(coefs, letter = self._letter) + steps.append(p) + + steps += p.simplify() + + else: + raise AttributeError("__pw__ not implemented yet") + + return steps + + def __xor__(self, power): + return self.__pow__(power) + + + def test(p,q): print("---------------------") @@ -411,10 +435,11 @@ def test(p,q): if __name__ == '__main__': #from .fraction import Fraction Expression.set_render(txt) - p = Polynom([0, -2 ]) - q = Polynom([4, 0, 4]) - r = (p-q)[-1] - print(r) + p = Polynom([0, 0, 3 ]) + #q = Polynom([4, 0, 4]) + r = (p**3) + for i in r: + print(i) #test(p,q) diff --git a/test/test_polynom.py b/test/test_polynom.py index 6320fb1..33ba472 100644 --- a/test/test_polynom.py +++ b/test/test_polynom.py @@ -153,33 +153,16 @@ class TestPolynom(unittest.TestCase): r = (p - q)[-1] self.assertEqual(str(r), '- x ^ 2 - 2 x + 1') + def test_pow_monome(self): + p = Polynom([0,-2]) + r = (p**3)[-1] + self.assertEqual(str(r), '- 8 x ^ 3') - def test_radd_int(self): - pass + def test_pow2_monome(self): + p = Polynom([0,-2]) + r = (p^3)[-1] + self.assertEqual(str(r), '- 8 x ^ 3') - def test_radd_frac(self): - pass - - def test_radd_poly(self): - pass - - def test_mul_int(self): - pass - - def test_mul_frac(self): - pass - - def test_mul_poly(self): - pass - - def test_rmul_int(self): - pass - - def test_rmul_frac(self): - pass - - def test_rmul_poly(self): - pass if __name__ == '__main__':