solve issue with - and polynoms
This commit is contained in:
parent
a77d779a22
commit
8f4d63595d
@ -149,7 +149,7 @@ class Polynom(object):
|
|||||||
elif type(a) == list:
|
elif type(a) == list:
|
||||||
# case need to repeat the x^i
|
# case need to repeat the x^i
|
||||||
for b in a:
|
for b in a:
|
||||||
if isNumber(b) and b < 0:
|
if len(postfix) > 0 and isNumber(b) and b < 0:
|
||||||
b = -b
|
b = -b
|
||||||
operator = op.sub
|
operator = op.sub
|
||||||
c = self.coef_postfix([b],i)
|
c = self.coef_postfix([b],i)
|
||||||
@ -159,7 +159,7 @@ class Polynom(object):
|
|||||||
postfix.append(operator)
|
postfix.append(operator)
|
||||||
|
|
||||||
elif a != 0:
|
elif a != 0:
|
||||||
if a < 0:
|
if len(postfix) > 0 and a < 0:
|
||||||
a = -a
|
a = -a
|
||||||
operator = op.sub
|
operator = op.sub
|
||||||
c = self.coef_postfix([a],i)
|
c = self.coef_postfix([a],i)
|
||||||
@ -196,18 +196,28 @@ class Polynom(object):
|
|||||||
coefs_steps = []
|
coefs_steps = []
|
||||||
for coef in self._coef:
|
for coef in self._coef:
|
||||||
coef_steps = []
|
coef_steps = []
|
||||||
if type(coef) != Expression:
|
if type(coef) == list:
|
||||||
# On converti en postfix avec une addition
|
# On converti en postfix avec une addition
|
||||||
postfix_add = self.postfix_add(coef)
|
postfix_add = self.postfix_add(coef)
|
||||||
# On converti en Expression
|
# On converti en Expression
|
||||||
coef_exp = Expression(postfix_add)
|
coef_exp = Expression(postfix_add)
|
||||||
else:
|
|
||||||
coef_exp = coef
|
|
||||||
|
|
||||||
# On fait réduire l'expression puis on ajoute dans steps
|
Expression.set_render(lambda _,x:Expression(x))
|
||||||
Expression.set_render(lambda _,x:Expression(x))
|
coef_steps = list(coef_exp.simplify())
|
||||||
coef_steps = list(coef_exp.simplify())
|
Expression.set_default_render()
|
||||||
Expression.set_default_render()
|
|
||||||
|
elif type(coef) == Expression:
|
||||||
|
|
||||||
|
Expression.set_render(lambda _,x:Expression(x))
|
||||||
|
coef_steps = list(coef.simplify())
|
||||||
|
Expression.set_default_render()
|
||||||
|
|
||||||
|
else:
|
||||||
|
coef_steps = [coef]
|
||||||
|
try:
|
||||||
|
coef_steps.append(coef.simplify())
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
# On ajoute toutes ces étapes
|
# On ajoute toutes ces étapes
|
||||||
coefs_steps.append(coef_steps)
|
coefs_steps.append(coef_steps)
|
||||||
@ -317,6 +327,7 @@ def test(p,q):
|
|||||||
print("q : ",q)
|
print("q : ",q)
|
||||||
|
|
||||||
print("\n Plus ------")
|
print("\n Plus ------")
|
||||||
|
print(p, "+", q)
|
||||||
for i in (p + q):
|
for i in (p + q):
|
||||||
#print(repr(i))
|
#print(repr(i))
|
||||||
#print("\t", str(i.postfix))
|
#print("\t", str(i.postfix))
|
||||||
@ -344,6 +355,10 @@ if __name__ == '__main__':
|
|||||||
q = Polynom([0, Fraction(1,2), 0, Fraction(-4,3)])
|
q = Polynom([0, Fraction(1,2), 0, Fraction(-4,3)])
|
||||||
test(p,q)
|
test(p,q)
|
||||||
|
|
||||||
|
print("\n")
|
||||||
|
p = Polynom([-1,-2,-3])
|
||||||
|
print(p)
|
||||||
|
|
||||||
#p = Polynom([1, 1, 1 ])
|
#p = Polynom([1, 1, 1 ])
|
||||||
#print(p)
|
#print(p)
|
||||||
|
|
||||||
@ -360,10 +375,10 @@ if __name__ == '__main__':
|
|||||||
#for i in p.simplify():
|
#for i in p.simplify():
|
||||||
# print(repr(i))
|
# print(repr(i))
|
||||||
|
|
||||||
print("\n")
|
#print("\n")
|
||||||
poly = Polynom.random(["[{a**2}, {a}]", "{2*a*b}", "{b**2}"])
|
#poly = Polynom.random(["[{a**2}, {a}]", "{2*a*b}", "{b**2}"])
|
||||||
for i in poly.simplify():
|
#for i in poly.simplify():
|
||||||
print(i)
|
# print(i)
|
||||||
|
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
@ -77,7 +77,7 @@ class TestPolynom(unittest.TestCase):
|
|||||||
# TODO: Choix arbitraire (vis à vis des + et des -) il faudra faire en fonction de render |sam. juin 14 09:45:55 CEST 2014
|
# TODO: Choix arbitraire (vis à vis des + et des -) il faudra faire en fonction de render |sam. juin 14 09:45:55 CEST 2014
|
||||||
p = Polynom([-1,-2,-3])
|
p = Polynom([-1,-2,-3])
|
||||||
#ans = [-1, -2, "x", "*", "+", -3, "x", 2, "^", "*", "+"]
|
#ans = [-1, -2, "x", "*", "+", -3, "x", 2, "^", "*", "+"]
|
||||||
ans = [-3, 'x', 2, '^', '*', -2, 'x', '*', '+', -1, '+']
|
ans = [-3, 'x', 2, '^', '*', 2, 'x', '*', '-', 1, '-']
|
||||||
self.assertEqual(ans, p.postfix)
|
self.assertEqual(ans, p.postfix)
|
||||||
|
|
||||||
def test_postfix_multi_coef(self):
|
def test_postfix_multi_coef(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user