Pass tests for polynoms

This commit is contained in:
Benjamin Bertrand 2016-02-27 12:14:21 +03:00
parent 3530660c0b
commit c2cdbea001
2 changed files with 8 additions and 18 deletions

View File

@ -146,9 +146,6 @@ class Polynom(AbstractPolynom):
3 h^{ 2 } + 8 h + 6 3 h^{ 2 } + 8 h + 6
>>> R = P(Q) >>> R = P(Q)
""" """
# if isNumerand(value) or Expression.isExpression(value):
# postfix_exp = [value if i==self._letter else i for i in self.postfix_tokens]
# else:
postfix_exp = [ postfix_exp = [
Expression(value) if i == self._letter else i for i in self.postfix_tokens] Expression(value) if i == self._letter else i for i in self.postfix_tokens]

View File

@ -8,6 +8,7 @@ from pymath.calculus.polynom import Polynom
from pymath.calculus.fraction import Fraction from pymath.calculus.fraction import Fraction
from pymath.calculus.expression import Expression from pymath.calculus.expression import Expression
from pymath.calculus.render import txt from pymath.calculus.render import txt
from pymath.calculus.operator import op
class TestPolynom(unittest.TestCase): class TestPolynom(unittest.TestCase):
@ -69,45 +70,37 @@ class TestPolynom(unittest.TestCase):
def test_postfix(self): def test_postfix(self):
p = Polynom([1, 2, 3]) p = Polynom([1, 2, 3])
#ans = [1, 2, "x", "*", "+", 3, "x", 2, "^", "*", "+"] ans = [3, 'x', 2, op.pw, op.mul, 2, 'x', op.mul, op.add, 1, op.add]
ans = [3, 'x', 2, '^', '*', 2, 'x', '*', '+', 1, '+']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_monom(self): def test_postfix_monom(self):
p = Polynom([0, 2]) p = Polynom([0, 2])
ans = [2, "x", "*"] ans = [2, "x", op.mul]
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_0_coef(self): def test_postfix_0_coef(self):
p = Polynom([0, 2, 0, 4]) p = Polynom([0, 2, 0, 4])
#ans = [2, "x", "*", 4, "x", 3, "^", "*", "+"] ans = [4, 'x', 3, op.pw, op.mul, 2, 'x', op.mul, op.add]
ans = [4, 'x', 3, '^', '*', 2, 'x', '*', '+']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_1_coef(self): def test_postfix_1_coef(self):
p = Polynom([0, 1, 1]) p = Polynom([0, 1, 1])
#ans = ["x", "x", 2, "^", "+"] ans = ["x", 2, op.pw, "x", op.add]
ans = ["x", 2, "^", "x", "+"]
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_neg_coef(self): def test_postfix_neg_coef(self):
# 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 = [3, 'x', 2, op.pw, op.mul, op.sub1, 2, 'x', op.mul, op.sub, 1, op.sub]
ans = [3, 'x', 2, '^', '*', '-', 2, 'x', '*', '-', 1, '-']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_multi_coef(self): def test_postfix_multi_coef(self):
p = Polynom([1, [2, 3], 4]) p = Polynom([1, [2, 3], 4])
#ans = [1, 2, "x", "*", "+", 3, "x", "*", "+", 4, "x", 2, "^", "*", "+"] ans = [4, 'x', 2, op.pw, op.mul, 2, 'x', op.mul, op.add, 3, 'x', op.mul, op.add, 1, op.add]
ans = [4, 'x', 2, '^', '*', 2, 'x', '*', '+', 3, 'x', '*', '+', 1, '+']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_arithm_coef(self): def test_postfix_arithm_coef(self):
p = Polynom([1, Expression([2, 3, "+"]), 4]) p = Polynom([1, Expression([2, 3, "+"]), 4])
#ans = [1, 2, 3, "+", "x", "*", "+", 4, "x", 2, "^", "*", "+"] ans = [4, 'x', 2, op.pw, op.mul, 2, 3, op.add, 'x', op.mul, op.add, 1, op.add]
ans = [4, 'x', 2, '^', '*', 2, 3, '+', 'x', '*', '+', 1, '+']
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_reduce_nilpo(self): def test_reduce_nilpo(self):