steps for poly fixed! All test pass except one (need to fix)
This commit is contained in:
parent
fba7f16762
commit
9529c32b50
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
from .explicable import Explicable
|
from .explicable import Explicable
|
||||||
from .expression import Expression
|
from .expression import Expression
|
||||||
|
from .step import Step
|
||||||
|
from .renderable import Renderable
|
||||||
from .operator import op
|
from .operator import op
|
||||||
from .generic import spe_zip, isNumber, transpose_fill, flatten_list, isPolynom, postfix_op
|
from .generic import spe_zip, isNumber, transpose_fill, flatten_list, isPolynom, postfix_op
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
@ -211,7 +213,7 @@ class AbstractPolynom(Explicable):
|
|||||||
for (i, a) in list(enumerate(self._coef))[::-1]:
|
for (i, a) in list(enumerate(self._coef))[::-1]:
|
||||||
operator = [op.add]
|
operator = [op.add]
|
||||||
operator_sub1 = []
|
operator_sub1 = []
|
||||||
if isinstance(a, Expression):
|
if isinstance(a, Renderable):
|
||||||
# case coef is an arithmetic expression
|
# case coef is an arithmetic expression
|
||||||
c = self.coef_postfix(a.postfix_tokens, i)
|
c = self.coef_postfix(a.postfix_tokens, i)
|
||||||
if c != []:
|
if c != []:
|
||||||
@ -302,13 +304,13 @@ class AbstractPolynom(Explicable):
|
|||||||
>>> Q
|
>>> Q
|
||||||
< AbstractPolynom x [1, 2, 3]>
|
< AbstractPolynom x [1, 2, 3]>
|
||||||
>>> Q.steps
|
>>> 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])
|
>>> P = AbstractPolynom([[1,2], [3,4,5], 6])
|
||||||
>>> Q = P.reduce()
|
>>> Q = P.reduce()
|
||||||
>>> Q
|
>>> Q
|
||||||
< AbstractPolynom x [3, 12, 6]>
|
< AbstractPolynom x [3, 12, 6]>
|
||||||
>>> Q.steps
|
>>> 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():
|
>>> for i in Q.explain():
|
||||||
... print(i)
|
... print(i)
|
||||||
6 x^{ 2 } + 3 x + 4 x + 5 x + 1 + 2
|
6 x^{ 2 } + 3 x + 4 x + 5 x + 1 + 2
|
||||||
@ -336,13 +338,9 @@ class AbstractPolynom(Explicable):
|
|||||||
|
|
||||||
ans = AbstractPolynom(smpl_coef, self._letter)
|
ans = AbstractPolynom(smpl_coef, self._letter)
|
||||||
|
|
||||||
print("smpl_coef -> ", smpl_coef)
|
ini_step = [Step(self)]
|
||||||
print("Expression.develop_steps(smpl_coef) -> ", Expression.develop_steps(smpl_coef))
|
for s in Explicable.merge_history(smpl_coef):
|
||||||
|
ini_step.append(Step(AbstractPolynom(s)))
|
||||||
ini_step = [self]
|
|
||||||
for s in Expression.develop_steps(smpl_coef):
|
|
||||||
print("s -> ", repr(s))
|
|
||||||
ini_step.append(AbstractPolynom(s))
|
|
||||||
|
|
||||||
ans.this_append_before(
|
ans.this_append_before(
|
||||||
ini_step
|
ini_step
|
||||||
@ -352,16 +350,6 @@ class AbstractPolynom(Explicable):
|
|||||||
|
|
||||||
return ans
|
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):
|
def simplify(self):
|
||||||
"""Same as reduce """
|
"""Same as reduce """
|
||||||
return self.reduce()
|
return self.reduce()
|
||||||
@ -377,12 +365,12 @@ class AbstractPolynom(Explicable):
|
|||||||
>>> c
|
>>> c
|
||||||
6
|
6
|
||||||
>>> c.steps
|
>>> 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 = AbstractPolynom.smpl_coef_list([Expression('2*2'), 3])
|
||||||
>>> c
|
>>> c
|
||||||
7
|
7
|
||||||
>>> c.steps
|
>>> 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
|
# Simplify each element before adding them
|
||||||
@ -428,15 +416,15 @@ class AbstractPolynom(Explicable):
|
|||||||
3 x^{ 2 } + ( 2 + 5 ) x + 1 + 4
|
3 x^{ 2 } + ( 2 + 5 ) x + 1 + 4
|
||||||
3 x^{ 2 } + 7 x + 5
|
3 x^{ 2 } + 7 x + 5
|
||||||
>>> R.steps
|
>>> 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)
|
o_poly = self.conv2poly(other)
|
||||||
|
|
||||||
n_coef = spe_zip(self._coef, o_poly._coef)
|
n_coef = spe_zip(self._coef, o_poly._coef)
|
||||||
p = AbstractPolynom(n_coef, letter=self._letter)
|
p = AbstractPolynom(n_coef, letter=self._letter)
|
||||||
|
|
||||||
ini_step = [Expression(self.postfix_tokens +
|
ini_step = [Step([self, o_poly, op.add])]
|
||||||
o_poly.postfix_tokens + [op.add])]
|
|
||||||
ans = p.simplify()
|
ans = p.simplify()
|
||||||
ans.this_append_before(ini_step)
|
ans.this_append_before(ini_step)
|
||||||
return ans
|
return ans
|
||||||
@ -453,9 +441,9 @@ class AbstractPolynom(Explicable):
|
|||||||
>>> Q
|
>>> Q
|
||||||
< AbstractPolynom x [-1, -2, -3]>
|
< AbstractPolynom x [-1, -2, -3]>
|
||||||
>>> Q.steps
|
>>> 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],
|
ans = AbstractPolynom([-i for i in self._coef],
|
||||||
letter=self._letter).simplify()
|
letter=self._letter).simplify()
|
||||||
ans.this_append_before(ini_step)
|
ans.this_append_before(ini_step)
|
||||||
@ -477,10 +465,10 @@ class AbstractPolynom(Explicable):
|
|||||||
( 3 - 6 ) x^{ 2 } + ( 2 - 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
|
>>> 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)
|
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.postfix_tokens + [op.sub])]
|
||||||
o_poly = -o_poly
|
o_poly = -o_poly
|
||||||
|
|
||||||
@ -506,21 +494,31 @@ class AbstractPolynom(Explicable):
|
|||||||
2 \times 3 x + 3
|
2 \times 3 x + 3
|
||||||
6 x + 3
|
6 x + 3
|
||||||
>>> (p*3).steps
|
>>> (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 = AbstractPolynom([0,0,4])
|
||||||
>>> q*3
|
>>> q*3
|
||||||
< AbstractPolynom x [0, 0, 12]>
|
< 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
|
>>> (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 = AbstractPolynom([0,1])
|
||||||
>>> r*3
|
>>> r*3
|
||||||
< AbstractPolynom x [0, 3]>
|
< AbstractPolynom x [0, 3]>
|
||||||
>>> (r*3).steps
|
>>> (r*3).steps
|
||||||
[< Expression ['x', 3, *]>]
|
[< Step ['x', 3, *]>, < Step [3, 'x', *]>, < Step [3, 'x', *]>]
|
||||||
>>> p*q
|
>>> p*q
|
||||||
< AbstractPolynom x [0, 0, 4, 8]>
|
< AbstractPolynom x [0, 0, 4, 8]>
|
||||||
>>> (p*q).steps
|
>>> (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
|
>>> p*r
|
||||||
< AbstractPolynom x [0, 1, 2]>
|
< AbstractPolynom x [0, 1, 2]>
|
||||||
>>> P = AbstractPolynom([1,2,3])
|
>>> P = AbstractPolynom([1,2,3])
|
||||||
@ -551,7 +549,7 @@ class AbstractPolynom(Explicable):
|
|||||||
coefs[i + j] = [coefs[i + j], elem]
|
coefs[i + j] = [coefs[i + j], elem]
|
||||||
|
|
||||||
p = AbstractPolynom(coefs, letter=self._letter)
|
p = AbstractPolynom(coefs, letter=self._letter)
|
||||||
ini_step = [Expression(self.postfix_tokens +
|
ini_step = [Step(self.postfix_tokens +
|
||||||
o_poly.postfix_tokens + [op.mul])]
|
o_poly.postfix_tokens + [op.mul])]
|
||||||
ans = p.simplify()
|
ans = p.simplify()
|
||||||
|
|
||||||
@ -565,18 +563,30 @@ class AbstractPolynom(Explicable):
|
|||||||
|
|
||||||
@power_cache
|
@power_cache
|
||||||
def __pow__(self, power):
|
def __pow__(self, power):
|
||||||
""" Overload **
|
r""" Overload **
|
||||||
|
|
||||||
>>> p = AbstractPolynom([0,0,3])
|
>>> p = AbstractPolynom([0,0,3])
|
||||||
>>> p**2
|
>>> p**2
|
||||||
< AbstractPolynom x [0, 0, 0, 0, 9]>
|
< AbstractPolynom x [0, 0, 0, 0, 9]>
|
||||||
>>> (p**2).steps
|
>>> (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 = AbstractPolynom([1,2])
|
||||||
>>> p**2
|
>>> p**2
|
||||||
< AbstractPolynom x [1, 4, 4]>
|
< AbstractPolynom x [1, 4, 4]>
|
||||||
>>> (p**2).steps
|
>>> (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 = AbstractPolynom([0,0,1])
|
||||||
>>> p**3
|
>>> p**3
|
||||||
< AbstractPolynom x [0, 0, 0, 0, 0, 0, 1]>
|
< AbstractPolynom x [0, 0, 0, 0, 0, 0, 1]>
|
||||||
@ -590,7 +600,7 @@ class AbstractPolynom(Explicable):
|
|||||||
"Can't raise {obj} to {pw} power".format(
|
"Can't raise {obj} to {pw} power".format(
|
||||||
obj=self.__class__, pw=str(power)))
|
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.is_monom():
|
||||||
if self._coef[self.degree] == 1:
|
if self._coef[self.degree] == 1:
|
||||||
|
@ -200,7 +200,6 @@ class Expression(Explicable):
|
|||||||
with Step.tmp_render():
|
with Step.tmp_render():
|
||||||
tmp_steps = list(Explicable.merge_history(tokenList))
|
tmp_steps = list(Explicable.merge_history(tokenList))
|
||||||
|
|
||||||
tmp_steps = list(tmp_steps)
|
|
||||||
steps = [Step(s) for s in tmp_steps]
|
steps = [Step(s) for s in tmp_steps]
|
||||||
return steps
|
return steps
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user