steps for poly fixed! All test pass except one (need to fix)

This commit is contained in:
Benjamin Bertrand 2016-03-10 17:25:13 +03:00
parent fba7f16762
commit 9529c32b50
3 changed files with 51 additions and 42 deletions

View File

@ -3,6 +3,8 @@
from .explicable import Explicable
from .expression import Expression
from .step import Step
from .renderable import Renderable
from .operator import op
from .generic import spe_zip, isNumber, transpose_fill, flatten_list, isPolynom, postfix_op
from functools import wraps
@ -211,7 +213,7 @@ class AbstractPolynom(Explicable):
for (i, a) in list(enumerate(self._coef))[::-1]:
operator = [op.add]
operator_sub1 = []
if isinstance(a, Expression):
if isinstance(a, Renderable):
# case coef is an arithmetic expression
c = self.coef_postfix(a.postfix_tokens, i)
if c != []:
@ -302,13 +304,13 @@ class AbstractPolynom(Explicable):
>>> Q
< AbstractPolynom x [1, 2, 3]>
>>> Q.steps
[< AbstractPolynom x [1, 2, 3]>]
[< Step [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +]>, < Step [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +]>]
>>> P = AbstractPolynom([[1,2], [3,4,5], 6])
>>> Q = P.reduce()
>>> Q
< AbstractPolynom x [3, 12, 6]>
>>> Q.steps
[< AbstractPolynom x [[1, 2], [3, 4, 5], 6]>, < AbstractPolynom x [< Expression [1, 2, +]>, < Expression [3, 4, +, 5, +]>, 6]>, < AbstractPolynom x [3, < Expression [7, 5, +]>, 6]>]
[< Step [6, 'x', 2, ^, *, 3, 'x', *, +, 4, 'x', *, +, 5, 'x', *, +, 1, +, 2, +]>, < Step [6, 'x', 2, ^, *, 3, 4, +, 5, +, 'x', *, +, 1, 2, +, +]>, < Step [6, 'x', 2, ^, *, 7, 5, +, 'x', *, +, 3, +]>, < Step [6, 'x', 2, ^, *, 12, 'x', *, +, 3, +]>]
>>> for i in Q.explain():
... print(i)
6 x^{ 2 } + 3 x + 4 x + 5 x + 1 + 2
@ -336,13 +338,9 @@ class AbstractPolynom(Explicable):
ans = AbstractPolynom(smpl_coef, self._letter)
print("smpl_coef -> ", smpl_coef)
print("Expression.develop_steps(smpl_coef) -> ", Expression.develop_steps(smpl_coef))
ini_step = [self]
for s in Expression.develop_steps(smpl_coef):
print("s -> ", repr(s))
ini_step.append(AbstractPolynom(s))
ini_step = [Step(self)]
for s in Explicable.merge_history(smpl_coef):
ini_step.append(Step(AbstractPolynom(s)))
ans.this_append_before(
ini_step
@ -352,16 +350,6 @@ class AbstractPolynom(Explicable):
return ans
## On retourne la matrice
#steps = []
#for coefs in transpose_fill(coefs_steps):
# steps.append(AbstractPolynom(coefs, self._letter))
#ans, steps = steps[-1], steps[:-1]
#ans.this_append_before(steps)
#return ans
def simplify(self):
"""Same as reduce """
return self.reduce()
@ -377,12 +365,12 @@ class AbstractPolynom(Explicable):
>>> c
6
>>> c.steps
[< Expression [1, 2, +, 3, +]>, < Expression [3, 3, +]>]
[< Step [1, 2, +, 3, +]>, < Step [1, 2, +, 3, +]>, < Step [3, 3, +]>, < Step [3, 3, +]>, < Step [6]>]
>>> c = AbstractPolynom.smpl_coef_list([Expression('2*2'), 3])
>>> c
7
>>> c.steps
[< Expression [2, 2, *, 3, +]>, < Expression [4, 3, +]>]
[< Step [2, 2, *, 3, +]>, < Step [4, 3, +]>, < Step [4, 3, +]>, < Step [7]>]
"""
# Simplify each element before adding them
@ -428,15 +416,15 @@ class AbstractPolynom(Explicable):
3 x^{ 2 } + ( 2 + 5 ) x + 1 + 4
3 x^{ 2 } + 7 x + 5
>>> R.steps
[< Expression [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 5, 'x', *, 4, +, +]>, < AbstractPolynom x [[1, 4], [2, 5], 3]>, < AbstractPolynom x [< Expression [1, 4, +]>, < Expression [2, 5, +]>, 3]>]
[< Step [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 5, 'x', *, 4, +, +]>, < Step [3, 'x', 2, ^, *, 2, 'x', *, +, 5, 'x', *, +, 1, +, 4, +]>, < Step [3, 'x', 2, ^, *, 2, 5, +, 'x', *, +, 1, 4, +, +]>, < Step [3, 'x', 2, ^, *, 7, 'x', *, +, 5, +]>]
"""
o_poly = self.conv2poly(other)
n_coef = spe_zip(self._coef, o_poly._coef)
p = AbstractPolynom(n_coef, letter=self._letter)
ini_step = [Expression(self.postfix_tokens +
o_poly.postfix_tokens + [op.add])]
ini_step = [Step([self, o_poly, op.add])]
ans = p.simplify()
ans.this_append_before(ini_step)
return ans
@ -453,9 +441,9 @@ class AbstractPolynom(Explicable):
>>> Q
< AbstractPolynom x [-1, -2, -3]>
>>> Q.steps
[< Expression [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, -]>]
[< Step [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, -]>, < Step [3, 'x', 2, ^, *, -, 2, 'x', *, -, 1, -]>, < Step [-3, 'x', 2, ^, *, -2, 'x', *, +, -1, +]>]
"""
ini_step = [Expression(self.postfix_tokens + [op.sub1])]
ini_step = [Step(self.postfix_tokens + [op.sub1])]
ans = AbstractPolynom([-i for i in self._coef],
letter=self._letter).simplify()
ans.this_append_before(ini_step)
@ -475,12 +463,12 @@ class AbstractPolynom(Explicable):
3 x^{ 2 } + 2 x + 1 - 6 x^{ 2 } - 5 x - 4
3 x^{ 2 } - 6 x^{ 2 } + 2 x - 5 x + 1 - 4
( 3 - 6 ) x^{ 2 } + ( 2 - 5 ) x + 1 - 4
- 3 x^{ 2 } - 3 x - 3
-3 x^{ 2 } - 3 x - 3
>>> R.steps
[< Expression [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 6, 'x', 2, ^, *, 5, 'x', *, +, 4, +, -]>, < Expression [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 6, 'x', 2, ^, *, -, 5, 'x', *, -, 4, -, +]>, < AbstractPolynom x [[1, -4], [2, -5], [3, -6]]>, < AbstractPolynom x [< Expression [1, -4, +]>, < Expression [2, -5, +]>, < Expression [3, -6, +]>]>]
[< Step [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 6, 'x', 2, ^, *, 5, 'x', *, +, 4, +, -]>, < Step [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +, 6, 'x', 2, ^, *, -, 5, 'x', *, -, 4, -, +]>, < Step [3, 'x', 2, ^, *, 6, 'x', 2, ^, *, -, 2, 'x', *, +, 5, 'x', *, -, 1, +, 4, -]>, < Step [3, -6, +, 'x', 2, ^, *, 2, -5, +, 'x', *, +, 1, -4, +, +]>, < Step [-3, 'x', 2, ^, *, -3, 'x', *, +, -3, +]>]
"""
o_poly = self.conv2poly(other)
ini_step = [Expression(self.postfix_tokens +
ini_step = [Step(self.postfix_tokens +
o_poly.postfix_tokens + [op.sub])]
o_poly = -o_poly
@ -506,21 +494,31 @@ class AbstractPolynom(Explicable):
2 \times 3 x + 3
6 x + 3
>>> (p*3).steps
[< Expression [2, 'x', *, 1, +, 3, *]>, < AbstractPolynom x [3, < Expression [2, 3, *]>]>]
[< Step [2, 'x', *, 1, +, 3, *]>, < Step [2, 3, *, 'x', *, 3, +]>, < Step [2, 3, *, 'x', *, 3, +]>, < Step [6, 'x', *, 3, +]>]
>>> q = AbstractPolynom([0,0,4])
>>> q*3
< AbstractPolynom x [0, 0, 12]>
>>> for i in (q*3).explain():
... print(i)
4 x^{ 2 } \times 3
4 \times 3 x^{ 2 }
12 x^{ 2 }
>>> (q*3).steps
[< Expression [4, 'x', 2, ^, *, 3, *]>, < AbstractPolynom x [0, 0, < Expression [4, 3, *]>]>]
[< Step [4, 'x', 2, ^, *, 3, *]>, < Step [4, 3, *, 'x', 2, ^, *]>, < Step [4, 3, *, 'x', 2, ^, *]>, < Step [12, 'x', 2, ^, *]>]
>>> r = AbstractPolynom([0,1])
>>> r*3
< AbstractPolynom x [0, 3]>
>>> (r*3).steps
[< Expression ['x', 3, *]>]
[< Step ['x', 3, *]>, < Step [3, 'x', *]>, < Step [3, 'x', *]>]
>>> p*q
< AbstractPolynom x [0, 0, 4, 8]>
>>> (p*q).steps
[< Expression [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *]>, < AbstractPolynom x [0, 0, 4, < Expression [2, 4, *]>]>]
[< Step [2, 'x', *, 1, +, 4, 'x', 2, ^, *, *]>, < Step [2, 4, *, 'x', 3, ^, *, 4, 'x', 2, ^, *, +]>, < Step [2, 4, *, 'x', 3, ^, *, 4, 'x', 2, ^, *, +]>, < Step [8, 'x', 3, ^, *, 4, 'x', 2, ^, *, +]>]
>>> for i in (p*q).explain():
... print(i)
( 2 x + 1 ) \times 4 x^{ 2 }
2 \times 4 x^{ 3 } + 4 x^{ 2 }
8 x^{ 3 } + 4 x^{ 2 }
>>> p*r
< AbstractPolynom x [0, 1, 2]>
>>> P = AbstractPolynom([1,2,3])
@ -551,7 +549,7 @@ class AbstractPolynom(Explicable):
coefs[i + j] = [coefs[i + j], elem]
p = AbstractPolynom(coefs, letter=self._letter)
ini_step = [Expression(self.postfix_tokens +
ini_step = [Step(self.postfix_tokens +
o_poly.postfix_tokens + [op.mul])]
ans = p.simplify()
@ -565,18 +563,30 @@ class AbstractPolynom(Explicable):
@power_cache
def __pow__(self, power):
""" Overload **
r""" Overload **
>>> p = AbstractPolynom([0,0,3])
>>> p**2
< AbstractPolynom x [0, 0, 0, 0, 9]>
>>> (p**2).steps
[< Expression [3, 'x', 2, ^, *, 2, ^]>, < AbstractPolynom x [0, 0, 0, 0, < Expression [3, 2, ^]>]>]
[< Step [3, 'x', 2, ^, *, 2, ^]>, < Step [3, 2, ^, 'x', 4, ^, *]>, < Step [3, 2, ^, 'x', 4, ^, *]>, < Step [9, 'x', 4, ^, *]>]
>>> for i in (p**2).explain():
... print(i)
( 3 x^{ 2 } )^{ 2 }
3^{ 2 } x^{ 4 }
9 x^{ 4 }
>>> p = AbstractPolynom([1,2])
>>> p**2
< AbstractPolynom x [1, 4, 4]>
>>> (p**2).steps
[< Expression [2, 'x', *, 1, +, 2, ^]>, < Expression [2, 'x', *, 1, +, 2, 'x', *, 1, +, *]>, < AbstractPolynom x [1, [2, 2], < Expression [2, 2, *]>]>, < AbstractPolynom x [1, < Expression [2, 2, +]>, 4]>]
[< Step [2, 'x', *, 1, +, 2, ^]>, < Step [2, 'x', *, 1, +, 2, 'x', *, 1, +, *]>, < Step [2, 2, *, 'x', 2, ^, *, 2, 'x', *, +, 2, 'x', *, +, 1, +]>, < Step [2, 2, *, 'x', 2, ^, *, 2, 2, +, 'x', *, +, 1, +]>, < Step [4, 'x', 2, ^, *, 4, 'x', *, +, 1, +]>]
>>> for i in (p**2).explain():
... print(i)
( 2 x + 1 )^{ 2 }
( 2 x + 1 ) ( 2 x + 1 )
2 \times 2 x^{ 2 } + 2 x + 2 x + 1
2 \times 2 x^{ 2 } + ( 2 + 2 ) x + 1
4 x^{ 2 } + 4 x + 1
>>> p = AbstractPolynom([0,0,1])
>>> p**3
< AbstractPolynom x [0, 0, 0, 0, 0, 0, 1]>
@ -590,7 +600,7 @@ class AbstractPolynom(Explicable):
"Can't raise {obj} to {pw} power".format(
obj=self.__class__, pw=str(power)))
ini_step = [Expression(self.postfix_tokens + [power, op.pw])]
ini_step = [Step(self.postfix_tokens + [power, op.pw])]
if self.is_monom():
if self._coef[self.degree] == 1:

View File

@ -200,7 +200,6 @@ class Expression(Explicable):
with Step.tmp_render():
tmp_steps = list(Explicable.merge_history(tokenList))
tmp_steps = list(tmp_steps)
steps = [Step(s) for s in tmp_steps]
return steps

View File

@ -155,13 +155,13 @@ class TestPolynom(unittest.TestCase):
def test_pow_monome(self):
p = Polynom([0, -2])
r = p**3
ans = '- 8 x^{ 3 }'
ans = '-8 x^{ 3 }'
self.assertEqual(str(r), ans)
def test_pow2_monome(self):
p = Polynom([0, -2])
r = p ^ 3
ans = '- 8 x^{ 3 }'
ans = '-8 x^{ 3 }'
self.assertEqual(str(r), ans)