this_append_before for explicable ans abstract_polynom

This commit is contained in:
Benjamin Bertrand 2016-03-02 16:52:42 +03:00
parent 5d4fe72764
commit 1ecdb458ae
2 changed files with 34 additions and 13 deletions

View File

@ -367,7 +367,7 @@ class AbstractPolynom(Explicable):
steps.append(AbstractPolynom(coefs, self._letter)) steps.append(AbstractPolynom(coefs, self._letter))
ans, steps = steps[-1], steps[:-1] ans, steps = steps[-1], steps[:-1]
ans.steps = steps ans.this_append_before(steps)
return ans return ans
@ -407,7 +407,7 @@ class AbstractPolynom(Explicable):
ini_step = [Expression(self.postfix_tokens + ini_step = [Expression(self.postfix_tokens +
o_poly.postfix_tokens + [op.add])] o_poly.postfix_tokens + [op.add])]
ans = p.simplify() ans = p.simplify()
ans.steps = ini_step + ans.steps ans.this_append_before(ini_step)
return ans return ans
def __radd__(self, other): def __radd__(self, other):
@ -427,7 +427,7 @@ class AbstractPolynom(Explicable):
ini_step = [Expression(self.postfix_tokens + [op.sub1])] ini_step = [Expression(self.postfix_tokens + [op.sub1])]
ans = AbstractPolynom([-i for i in self._coef], ans = AbstractPolynom([-i for i in self._coef],
letter=self._letter).simplify() letter=self._letter).simplify()
ans.steps = ini_step + ans.steps ans.this_append_before(ini_step)
return ans return ans
def __sub__(self, other): def __sub__(self, other):
@ -452,10 +452,9 @@ class AbstractPolynom(Explicable):
ini_step = [Expression(self.postfix_tokens + ini_step = [Expression(self.postfix_tokens +
o_poly.postfix_tokens + [op.sub])] o_poly.postfix_tokens + [op.sub])]
o_poly = -o_poly o_poly = -o_poly
#ini_step += o_poly.steps
ans = self + o_poly ans = self + o_poly
ans.steps = ini_step + ans.steps ans.this_append_before(ini_step)
return ans return ans
@ -465,27 +464,32 @@ class AbstractPolynom(Explicable):
return o_poly.__sub__(self) return o_poly.__sub__(self)
def __mul__(self, other): def __mul__(self, other):
""" Overload * r""" Overload *
>>> p = AbstractPolynom([1,2]) >>> p = AbstractPolynom([1,2])
>>> p*3 >>> p*3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, 6]> < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [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 >>> (p*3).steps
[[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 3, *] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.calculus.expression.Expression'> [2, 3, *] >]>] [< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 3, *] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.calculus.expression.Expression'> [2, 3, *] >]>]
>>> q = AbstractPolynom([0,0,4]) >>> q = AbstractPolynom([0,0,4])
>>> q*3 >>> q*3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 12]> < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 12]>
>>> (q*3).steps >>> (q*3).steps
[[< <class 'pymath.calculus.expression.Expression'> [4, 'x', 2, ^, *, 3, *] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, < <class 'pymath.calculus.expression.Expression'> [4, 3, *] >]>] [< <class 'pymath.calculus.expression.Expression'> [4, 'x', 2, ^, *, 3, *] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, < <class 'pymath.calculus.expression.Expression'> [4, 3, *] >]>]
>>> r = AbstractPolynom([0,1]) >>> r = AbstractPolynom([0,1])
>>> r*3 >>> r*3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 3]> < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 3]>
>>> (r*3).steps >>> (r*3).steps
[[< <class 'pymath.calculus.expression.Expression'> ['x', 3, *] >]] [< <class 'pymath.calculus.expression.Expression'> ['x', 3, *] >]
>>> p*q >>> p*q
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 4, 8]> < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 4, 8]>
>>> (p*q).steps >>> (p*q).steps
[[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 4, < <class 'pymath.calculus.expression.Expression'> [2, 4, *] >]>] [< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 4, < <class 'pymath.calculus.expression.Expression'> [2, 4, *] >]>]
>>> p*r >>> p*r
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 1, 2]> < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 1, 2]>
>>> P = AbstractPolynom([1,2,3]) >>> P = AbstractPolynom([1,2,3])
@ -520,7 +524,7 @@ class AbstractPolynom(Explicable):
o_poly.postfix_tokens + [op.mul])] o_poly.postfix_tokens + [op.mul])]
ans = p.simplify() ans = p.simplify()
ans.steps = [ini_step] + ans.steps ans.this_append_before(ini_step)
return ans return ans
def __rmul__(self, other): def __rmul__(self, other):
@ -541,7 +545,7 @@ class AbstractPolynom(Explicable):
>>> p**2 >>> p**2
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, 4, 4]> < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, 4, 4]>
>>> (p**2).steps >>> (p**2).steps
[< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 2, ^] >, [< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 2, 'x', *, 1, +, *] >], < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, [2, 2], < <class 'pymath.calculus.expression.Expression'> [2, 2, *] >]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, < <class 'pymath.calculus.expression.Expression'> [2, 2, +] >, 4]>] [< <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 2, ^] >, < <class 'pymath.calculus.expression.Expression'> [2, 'x', *, 1, +, 2, 'x', *, 1, +, *] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, [2, 2], < <class 'pymath.calculus.expression.Expression'> [2, 2, *] >]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, < <class 'pymath.calculus.expression.Expression'> [2, 2, +] >, 4]>]
>>> p = AbstractPolynom([0,0,1]) >>> p = AbstractPolynom([0,0,1])
>>> p**3 >>> p**3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, 0, 0, 1]> < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, 0, 0, 1]>
@ -576,7 +580,7 @@ class AbstractPolynom(Explicable):
raise AttributeError( raise AttributeError(
"__pw__ not implemented yet when power is greatter than 2") "__pw__ not implemented yet when power is greatter than 2")
ans.steps = ini_step + ans.steps ans.this_append_before(ini_step)
return ans return ans
def __xor__(self, power): def __xor__(self, power):

View File

@ -61,6 +61,23 @@ class Explicable(Renderable):
else: else:
return False 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): class Explicable_int(int, Explicable):
""" An Explicable_int is an int which can be explain """ """ An Explicable_int is an int which can be explain """
isNumber = True isNumber = True