From 1ecdb458ae83856087b9444a9f801b7ae9581a33 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Wed, 2 Mar 2016 16:52:42 +0300 Subject: [PATCH] this_append_before for explicable ans abstract_polynom --- pymath/calculus/abstract_polynom.py | 30 ++++++++++++++++------------- pymath/calculus/explicable.py | 17 ++++++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pymath/calculus/abstract_polynom.py b/pymath/calculus/abstract_polynom.py index eb6fa95..06a3b0f 100644 --- a/pymath/calculus/abstract_polynom.py +++ b/pymath/calculus/abstract_polynom.py @@ -367,7 +367,7 @@ class AbstractPolynom(Explicable): steps.append(AbstractPolynom(coefs, self._letter)) ans, steps = steps[-1], steps[:-1] - ans.steps = steps + ans.this_append_before(steps) return ans @@ -407,7 +407,7 @@ class AbstractPolynom(Explicable): ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.add])] ans = p.simplify() - ans.steps = ini_step + ans.steps + ans.this_append_before(ini_step) return ans def __radd__(self, other): @@ -427,7 +427,7 @@ class AbstractPolynom(Explicable): ini_step = [Expression(self.postfix_tokens + [op.sub1])] ans = AbstractPolynom([-i for i in self._coef], letter=self._letter).simplify() - ans.steps = ini_step + ans.steps + ans.this_append_before(ini_step) return ans def __sub__(self, other): @@ -452,10 +452,9 @@ class AbstractPolynom(Explicable): ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])] o_poly = -o_poly - #ini_step += o_poly.steps ans = self + o_poly - ans.steps = ini_step + ans.steps + ans.this_append_before(ini_step) return ans @@ -465,27 +464,32 @@ class AbstractPolynom(Explicable): return o_poly.__sub__(self) def __mul__(self, other): - """ Overload * + r""" Overload * >>> p = AbstractPolynom([1,2]) >>> p*3 < [3, 6]> + >>> for i in (p*3).explain(): + ... print(i) + ( 2 x + 1 ) \times 3 + 2 \times 3 x + 3 + 6 x + 3 >>> (p*3).steps - [[< [2, 'x', *, 1, +, 3, *] >], < [3, < [2, 3, *] >]>] + [< [2, 'x', *, 1, +, 3, *] >, < [3, < [2, 3, *] >]>] >>> q = AbstractPolynom([0,0,4]) >>> q*3 < [0, 0, 12]> >>> (q*3).steps - [[< [4, 'x', 2, ^, *, 3, *] >], < [0, 0, < [4, 3, *] >]>] + [< [4, 'x', 2, ^, *, 3, *] >, < [0, 0, < [4, 3, *] >]>] >>> r = AbstractPolynom([0,1]) >>> r*3 < [0, 3]> >>> (r*3).steps - [[< ['x', 3, *] >]] + [< ['x', 3, *] >] >>> p*q < [0, 0, 4, 8]> >>> (p*q).steps - [[< [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *] >], < [0, 0, 4, < [2, 4, *] >]>] + [< [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *] >, < [0, 0, 4, < [2, 4, *] >]>] >>> p*r < [0, 1, 2]> >>> P = AbstractPolynom([1,2,3]) @@ -520,7 +524,7 @@ class AbstractPolynom(Explicable): o_poly.postfix_tokens + [op.mul])] ans = p.simplify() - ans.steps = [ini_step] + ans.steps + ans.this_append_before(ini_step) return ans def __rmul__(self, other): @@ -541,7 +545,7 @@ class AbstractPolynom(Explicable): >>> p**2 < [1, 4, 4]> >>> (p**2).steps - [< [2, 'x', *, 1, +, 2, ^] >, [< [2, 'x', *, 1, +, 2, 'x', *, 1, +, *] >], < [1, [2, 2], < [2, 2, *] >]>, < [1, < [2, 2, +] >, 4]>] + [< [2, 'x', *, 1, +, 2, ^] >, < [2, 'x', *, 1, +, 2, 'x', *, 1, +, *] >, < [1, [2, 2], < [2, 2, *] >]>, < [1, < [2, 2, +] >, 4]>] >>> p = AbstractPolynom([0,0,1]) >>> p**3 < [0, 0, 0, 0, 0, 0, 1]> @@ -576,7 +580,7 @@ class AbstractPolynom(Explicable): raise AttributeError( "__pw__ not implemented yet when power is greatter than 2") - ans.steps = ini_step + ans.steps + ans.this_append_before(ini_step) return ans def __xor__(self, power): diff --git a/pymath/calculus/explicable.py b/pymath/calculus/explicable.py index b131be8..11bffb5 100644 --- a/pymath/calculus/explicable.py +++ b/pymath/calculus/explicable.py @@ -61,6 +61,23 @@ class Explicable(Renderable): else: return False + def this_append_before(self, steps): + """ Add steps at the beginning of self.steps + + :param steps: list of steps that append before + + >>> e = Explicable() + >>> s = ['eat', 'sleep'] + >>> e.this_append_before(s) + >>> print(e.steps) + ['eat', 'sleep'] + >>> s0 = ['cook'] + >>> e.this_append_before(s0) + >>> print(e.steps) + ['cook', 'eat', 'sleep'] + """ + self.steps = steps + self.steps + class Explicable_int(int, Explicable): """ An Explicable_int is an int which can be explain """ isNumber = True