add missing step when multiply polynoms

This commit is contained in:
Benjamin Bertrand 2016-02-09 09:23:04 +03:00
parent 47a2fb973a
commit ba579c9ab3

View File

@ -156,7 +156,6 @@ class AbstractPolynom(Explicable):
['x', 2, '^']
"""
# TODO: Couille certaine avec txt à qui il fait donner des opérateurs tout beau! |mar. nov. 11 13:08:35 CET 2014
ans =[]
if a == [0]:
pass
@ -309,37 +308,46 @@ class AbstractPolynom(Explicable):
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, 12, 6]>
>>> for i in Q.explain():
... print(i)
6 x^{ 2 } + 3 x + 4 x + 5 x + 1 + 2
6 x^{ 2 } + ( 3 + 4 + 5 ) x + 1 + 2
6 x^{ 2 } + ( 7 + 5 ) x + 3
6 x^{ 2 } + 12 x + 3
>>> Q.steps
[< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.calculus.expression.Expression'> [3, 4, '+', 5, '+'] >, 6]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.calculus.expression.Expression'> [7, 5, '+'] >, 6]>]
[< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, 2], [3, 4, 5], 6]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, 2, '+'] >, < <class 'pymath.calculus.expression.Expression'> [3, 4, '+', 5, '+'] >, 6]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [3, < <class 'pymath.calculus.expression.Expression'> [7, 5, '+'] >, 6]>]
"""
# TODO: It doesn't not compute quick enough |ven. févr. 27 18:04:01 CET 2015
# gather steps for every coeficients
# gather steps for every coefficients
coefs_steps = []
for coef in self._coef:
coef_steps = []
if type(coef) == list:
# On converti en postfix avec une addition
postfix_add = self.postfix_add([i for i in coef if i!=0])
# On converti en Expression
if type(coef) ==list:
# Simplify each element before adding them
s = []
for c in coef:
try:
with Expression.tmp_render():
s.append(list(c.simplify().explain()))
except AttributeError:
s.append([c])
s = list(transpose_fill(s))
last = s[-1]
coef_steps += s
# Convert last element into postfix addition.
postfix_add = self.postfix_add([i for i in last if i!=0])
# Convert it to an expression
coef_exp = Expression(postfix_add)
with Expression.tmp_render():
coef_steps = list(coef_exp.simplify().explain())
coef_steps += list(coef_exp.simplify().explain())
#print('\t 1.coef_steps -> ', coef_steps)
elif type(coef) == Expression:
with Expression.tmp_render():
coef_steps = list(coef.simplify().explain())
#print('\t 2.coef_steps -> ', coef_steps)
else:
try:
with Expression.tmp_render():
@ -408,8 +416,14 @@ class AbstractPolynom(Explicable):
>>> R = P+Q
>>> R
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [5, 7, 3]>
>>> for i in R.explain():
... print(i)
3 x^{ 2 } + 2 x + 1 + 5 x + 4
3 x^{ 2 } + 2 x + 5 x + 1 + 4
3 x^{ 2 } + ( 2 + 5 ) x + 1 + 4
3 x^{ 2 } + 7 x + 5
>>> R.steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.calculus.expression.Expression'> [2, 5, '+'] >, 3]>]
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 5, 'x', '*', 4, '+', '+'] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, 4], [2, 5], 3]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, 4, '+'] >, < <class 'pymath.calculus.expression.Expression'> [2, 5, '+'] >, 3]>]
"""
o_poly = self.conv2poly(other)
@ -448,14 +462,15 @@ class AbstractPolynom(Explicable):
>>> R = P - Q
>>> R
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [-3, -3, -3]>
>>> R.steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', 5, 'x', '*', '+', 4, '+', '-'] >, < <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', '-', 5, 'x', '*', '-', 4, '-', '+'] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.calculus.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.calculus.expression.Expression'> [3, -6, '+'] >]>]
>>> for i in R.explain():
... print(i)
3 x^{ 2 } + 2 x + 1 - ( 6 x^{ 2 } + 5 x + 4 )
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
>>> R.steps
[< <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', 5, 'x', '*', '+', 4, '+', '-'] >, < <class 'pymath.calculus.expression.Expression'> [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+', 6, 'x', 2, '^', '*', '-', 5, 'x', '*', '-', 4, '-', '+'] >, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [[1, -4], [2, -5], [3, -6]]>, < <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [< <class 'pymath.calculus.expression.Expression'> [1, -4, '+'] >, < <class 'pymath.calculus.expression.Expression'> [2, -5, '+'] >, < <class 'pymath.calculus.expression.Expression'> [3, -6, '+'] >]>]
"""
o_poly = self.conv2poly(other)
ini_step = [Expression(self.postfix_tokens + o_poly.postfix_tokens + [op.sub])]
@ -501,7 +516,6 @@ class AbstractPolynom(Explicable):
>>> P*Q
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [4, 13, 28, 27, 18]>
"""
# TODO: Je trouve qu'elle grille trop d'étapes... |ven. févr. 27 19:08:44 CET 2015
o_poly = self.conv2poly(other)
coefs = [0]*(self.degree + o_poly.degree + 1)
@ -549,7 +563,7 @@ class AbstractPolynom(Explicable):
>>> p**2
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [1, 4, 4]>
>>> (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, < <class 'pymath.calculus.expression.Expression'> [2, 2, '+'] >, < <class 'pymath.calculus.expression.Expression'> [2, 2, '*'] >]>]
[< <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**3
< <class 'pymath.calculus.abstract_polynom.AbstractPolynom'> [0, 0, 0, 0, 0, 0, 1]>