solve issue wit - and polynoms

This commit is contained in:
Lafrite 2014-12-21 08:53:10 +01:00
parent 8f4d63595d
commit 2425a2dfca
2 changed files with 48 additions and 13 deletions

View File

@ -137,36 +137,52 @@ class Polynom(object):
""" """
postfix = [] postfix = []
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 = []
if type(a) == Expression: if type(a) == Expression:
# 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 != []:
postfix.append(c) postfix.append(c)
if len(postfix) > 1: if len(postfix) > 1:
postfix.append(operator) postfix += operator
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 len(postfix) > 0 and isNumber(b) and b < 0: if len(postfix) == 0 and isNumber(b) and b < 0:
b = -b b = [-b]
operator = op.sub operator_sub1 = [op.sub1]
c = self.coef_postfix([b],i) elif len(postfix) > 0 and isNumber(b) and b < 0:
b = [-b]
operator = [op.sub]
else:
b = [b]
c = self.coef_postfix(b,i)
if c != []: if c != []:
postfix.append(c) postfix.append(c)
if len(postfix) > 1: if len(postfix) > 1:
postfix.append(operator) postfix += operator_sub1
postfix += operator
postfix += operator_sub1
elif a != 0: elif a != 0:
if len(postfix) > 0 and a < 0: if len(postfix) == 0 and a < 0:
a = -a a = [-a]
operator = op.sub operator_sub1 = [op.sub1]
c = self.coef_postfix([a],i) elif len(postfix) > 0 and a < 0:
a = [-a]
operator = [op.sub]
else:
a = [a]
c = self.coef_postfix(a,i)
if c != []: if c != []:
postfix.append(c) postfix.append(c)
if len(postfix) > 1: if len(postfix) > 1:
postfix.append(operator) postfix += operator_sub1
postfix += operator
postfix += operator_sub1
return flatten_list(postfix) return flatten_list(postfix)

View File

@ -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):
@ -118,6 +118,25 @@ class TestPolynom(unittest.TestCase):
r = (p + q)[-1] r = (p + q)[-1]
self.assertEqual(str(r), '6 x ^ 2 + 2 x + 1') self.assertEqual(str(r), '6 x ^ 2 + 2 x + 1')
def test_sub_int(self):
p = Polynom([1, 2, 3])
q = (p - 2)[-1]
self.assertEqual(str(q), '3 x ^ 2 + 2 x - 1')
def test_sub_frac(self):
p = Polynom([1, 2, 3])
f = Fraction(1, 2)
q = (p - f)[-1]
#ans = repr(Polynom([Expression(Fraction(3, 2)), Expression(2), Expression(3)]))
self.assertEqual(str(q),'3 x ^ 2 + 2 x + 1 / 2')
def test_sub_poly(self):
p = Polynom([1, 0, 2])
q = Polynom([0, 2, 3])
r = (p - q)[-1]
self.assertEqual(str(r), '- x ^ 2 - 2 x + 1')
def test_radd_int(self): def test_radd_int(self):
pass pass