new clean postfix_tokens method for Abstact_Poly (still a bug with mass

test render)
This commit is contained in:
Benjamin Bertrand 2016-03-11 18:16:19 +03:00
parent 9529c32b50
commit a0b014dd08
3 changed files with 1121 additions and 1100 deletions

View File

@ -156,19 +156,83 @@ class AbstractPolynom(Explicable):
['x', 2, ^] ['x', 2, ^]
""" """
ans = []
if a == [0]: if a == [0]:
pass ans = []
elif i == 0: elif i == 0:
ans = a ans = a
elif i == 1: elif i == 1:
ans = a * (a != [1]) + [self._letter] + [op.mul] * (a != [1]) ans = a * (a not in [[1], [-1]]) + \
[self._letter] + \
[op.mul] * (a not in [[1], [-1]]) + \
[op.sub1] * (a == [-1])
else: else:
ans = a * (a != [1]) + [self._letter, i, ans = a * (a not in [[1], [-1]]) + \
op.pw] + [op.mul] * (a != [1]) [self._letter, i, op.pw] + \
[op.mul] * (a not in [[1], [-1]]) + \
[op.sub1] * (a == [-1])
return ans return ans
def coefs_postifx(self):
""" Return list of postfix coef with the the right power letter
>>> p = AbstractPolynom([1, 2])
>>> p.coefs_postifx()
[[1], [2, 'x', *]]
>>> p = AbstractPolynom([1, -2])
>>> p.coefs_postifx()
[[1], [-2, 'x', *]]
>>> p = AbstractPolynom([1,2,3])
>>> p.coefs_postifx()
[[1], [2, 'x', *], [3, 'x', 2, ^, *]]
>>> p = AbstractPolynom([1])
>>> p.coefs_postifx()
[[1]]
>>> p = AbstractPolynom([0])
>>> p.coefs_postifx()
[[0]]
>>> p = AbstractPolynom([0, 1, 1, 0])
>>> p.coefs_postifx()
[['x'], ['x', 2, ^]]
>>> p = AbstractPolynom([1,[2,3]])
>>> p.coefs_postifx()
[[1], [3, 'x', *], [2, 'x', *]]
>>> p = AbstractPolynom([1,[2,-3]])
>>> p.coefs_postifx()
[[1], [-3, 'x', *], [2, 'x', *]]
>>> p = AbstractPolynom([1,[-2,-3]])
>>> p.coefs_postifx()
[[1], [-3, 'x', *], [-2, 'x', *]]
>>> p = AbstractPolynom([1,[-2,0]])
>>> p.coefs_postifx()
[[1], [-2, 'x', *]]
>>> from pymath.calculus.expression import Expression
>>> from pymath.calculus.operator import op
>>> e = Expression([2,3,op.add])
>>> p = AbstractPolynom([1,e])
>>> p.coefs_postifx()
[[1], [2, 3, +, 'x', *]]
"""
if not [i for i in self._coef if i!= 0]:
return [[0]]
raw_coefs = []
for (pw, coef) in enumerate(self._coef):
if isinstance(coef, list):
for c in coef[::-1]:
try:
raw_coefs.append(self.coef_postfix(c.postfix_tokens, pw))
except AttributeError:
raw_coefs.append(self.coef_postfix([c], pw))
elif coef != 0:
try:
raw_coefs.append(self.coef_postfix(coef.postfix_tokens, pw))
except AttributeError:
raw_coefs.append(self.coef_postfix([coef], pw))
return [i for i in raw_coefs if i != []]
def compute_postfix_tokens(self): def compute_postfix_tokens(self):
"""Return the postfix form of the polynom """Return the postfix form of the polynom
@ -179,7 +243,7 @@ class AbstractPolynom(Explicable):
[2, 'x', *, 1, +] [2, 'x', *, 1, +]
>>> p = AbstractPolynom([1, -2]) >>> p = AbstractPolynom([1, -2])
>>> p.postfix_tokens >>> p.postfix_tokens
[2, 'x', *, -, 1, +] [-2, 'x', *, 1, +]
>>> p = AbstractPolynom([1,2,3]) >>> p = AbstractPolynom([1,2,3])
>>> p.postfix_tokens >>> p.postfix_tokens
[3, 'x', 2, ^, *, 2, 'x', *, +, 1, +] [3, 'x', 2, ^, *, 2, 'x', *, +, 1, +]
@ -194,10 +258,10 @@ class AbstractPolynom(Explicable):
[2, 'x', *, 3, 'x', *, +, 1, +] [2, 'x', *, 3, 'x', *, +, 1, +]
>>> p = AbstractPolynom([1,[2,-3]]) >>> p = AbstractPolynom([1,[2,-3]])
>>> p.postfix_tokens >>> p.postfix_tokens
[2, 'x', *, 3, 'x', *, -, 1, +] [2, 'x', *, -3, 'x', *, +, 1, +]
>>> p = AbstractPolynom([1,[-2,-3]]) >>> p = AbstractPolynom([1,[-2,-3]])
>>> p.postfix_tokens >>> p.postfix_tokens
[2, 'x', *, -, 3, 'x', *, -, 1, +] [-2, 'x', *, -3, 'x', *, +, 1, +]
>>> from pymath.calculus.expression import Expression >>> from pymath.calculus.expression import Expression
>>> from pymath.calculus.operator import op >>> from pymath.calculus.operator import op
>>> e = Expression([2,3,op.add]) >>> e = Expression([2,3,op.add])
@ -206,73 +270,11 @@ class AbstractPolynom(Explicable):
[2, 3, +, 'x', *, 1, +] [2, 3, +, 'x', *, 1, +]
""" """
if not [i for i in self._coef if i!= 0]: raw_coefs = self.coefs_postifx()
return [0]
postfix = [] pstfx = postfix_op(raw_coefs[::-1], op.add)
for (i, a) in list(enumerate(self._coef))[::-1]: return flatten_list(pstfx)
operator = [op.add]
operator_sub1 = []
if isinstance(a, Renderable):
# case coef is an arithmetic expression
c = self.coef_postfix(a.postfix_tokens, i)
if c != []:
postfix.append(c)
if len(postfix) > 1:
postfix += operator
elif isinstance(a, list):
# case need to repeat the x^i
for b in a:
operator = [op.add]
operator_sub1 = []
if len(postfix) == 0 and isNumber(b) and b < 0:
try:
b = [(-b)[-1]]
except TypeError:
b = [-b]
operator_sub1 = [op.sub1]
elif len(postfix) > 0 and isNumber(b) and b < 0:
try:
b = [(-b)[-1]]
except TypeError:
b = [-b]
operator = [op.sub]
else:
b = [b]
c = self.coef_postfix(b, i)
if c != []:
postfix.append(c)
if len(postfix) > 1:
postfix += operator_sub1
postfix += operator
postfix += operator_sub1
elif a != 0:
if len(postfix) == 0 and a < 0:
try:
a = [(-a)[-1]]
except TypeError:
a = [-a]
operator_sub1 = [op.sub1]
elif len(postfix) > 0 and a < 0:
try:
a = [(-a)[-1]]
except TypeError:
a = [-a]
operator = [op.sub]
else:
a = [a]
c = self.coef_postfix(a, i)
if c != []:
postfix.append(c)
if len(postfix) > 1:
postfix += operator_sub1
postfix += operator
postfix += operator_sub1
return flatten_list(postfix)
def conv2poly(self, other): def conv2poly(self, other):
"""Convert anything number into a polynom """Convert anything number into a polynom
@ -309,14 +311,14 @@ class AbstractPolynom(Explicable):
>>> Q = P.reduce() >>> Q = P.reduce()
>>> Q >>> Q
< AbstractPolynom x [3, 12, 6]> < AbstractPolynom x [3, 12, 6]>
>>> Q.steps
[< 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
6 x^{ 2 } + ( 3 + 4 + 5 ) x + 1 + 2 6 x^{ 2 } + ( 3 + 4 + 5 ) x + 1 + 2
6 x^{ 2 } + ( 7 + 5 ) x + 3 6 x^{ 2 } + ( 7 + 5 ) x + 3
6 x^{ 2 } + 12 x + 3 6 x^{ 2 } + 12 x + 3
>>> Q.steps
[< 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, +]>]
""" """
# TODO: Il manque le ini_step |jeu. mars 10 12:39:40 EAT 2016 # TODO: Il manque le ini_step |jeu. mars 10 12:39:40 EAT 2016
@ -440,8 +442,12 @@ class AbstractPolynom(Explicable):
>>> Q = -P >>> Q = -P
>>> Q >>> Q
< AbstractPolynom x [-1, -2, -3]> < AbstractPolynom x [-1, -2, -3]>
>>> for i in Q.explain():
... print(i)
- ( 3 x^{ 2 } + 2 x + 1 )
-3 x^{ 2 } - 2 x - 1
>>> Q.steps >>> Q.steps
[< Step [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, +, -]>, < Step [-3, 'x', 2, ^, *, -2, 'x', *, +, -1, +]>, < Step [-3, 'x', 2, ^, *, -2, 'x', *, +, -1, +]>]
""" """
ini_step = [Step(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],
@ -465,7 +471,7 @@ 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
[< 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, +]>] [< 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 = [Step(self.postfix_tokens + ini_step = [Step(self.postfix_tokens +

File diff suppressed because it is too large Load Diff

View File

@ -48,26 +48,6 @@ class TestPolynom(unittest.TestCase):
p = Polynom([1, 2]) p = Polynom([1, 2])
self.assertEqual(p("h+1"), Polynom([3, 2], "h")) self.assertEqual(p("h+1"), Polynom([3, 2], "h"))
# def test_print(self):
# p = Polynom([1,2,3])
# ans = "1 + 2 x + 3 x^2"
# self.assertEqual(ans, str(p))
# def test_print_monom(self):
# p = Polynom([0,2])
# ans = "2 x"
# self.assertEqual(ans, str(p))
# def test_print_0_coef(self):
# p = Polynom([0,1,3])
# ans = "x + 3 x^2"
# self.assertEqual(ans, str(p))
# def test_print_multi_coef(self):
# p = Polynom([1,[2, -2],3])
# ans = "1 + 2 x - 2 x + 3 x^2"
# self.assertEqual(ans, str(p))
def test_postfix(self): def test_postfix(self):
p = Polynom([1, 2, 3]) p = Polynom([1, 2, 3])
ans = [3, 'x', 2, op.pw, op.mul, 2, 'x', op.mul, op.add, 1, op.add] ans = [3, 'x', 2, op.pw, op.mul, 2, 'x', op.mul, op.add, 1, op.add]
@ -90,7 +70,7 @@ class TestPolynom(unittest.TestCase):
def test_postfix_neg_coef(self): def test_postfix_neg_coef(self):
p = Polynom([-1, -2, -3]) p = Polynom([-1, -2, -3])
ans = [3, 'x', 2, op.pw, op.mul, op.sub1, 2, 'x', op.mul, op.sub, 1, op.sub] ans = [-3, 'x', 2, op.pw, op.mul, -2, 'x', op.mul, op.add, -1, op.add]
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_multi_coef(self): def test_postfix_multi_coef(self):
@ -98,11 +78,46 @@ class TestPolynom(unittest.TestCase):
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, op.pw, op.mul, 2, 'x', op.mul, op.add, 3, 'x', op.mul, op.add, 1, op.add]
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_postfix_arithm_coef(self): def test_postfix_exp_coef(self):
p = Polynom([1, Expression([2, 3, "+"]), 4]) p = Polynom([1, Expression([2, 3, "+"]), 4])
ans = [4, 'x', 2, op.pw, op.mul, 2, 3, op.add, 'x', op.mul, op.add, 1, op.add] ans = [4, 'x', 2, op.pw, op.mul, 2, 3, op.add, 'x', op.mul, op.add, 1, op.add]
self.assertEqual(ans, p.postfix_tokens) self.assertEqual(ans, p.postfix_tokens)
def test_str(self):
p = Polynom([1, 2, 3])
ans = '3 x^{ 2 } + 2 x + 1'
self.assertEqual(ans, str(p))
def test_str_monom(self):
p = Polynom([0, 2])
ans = '2 x'
self.assertEqual(ans, str(p))
def test_str_0_coef(self):
p = Polynom([0, 2, 0, 4])
ans = '4 x^{ 3 } + 2 x'
self.assertEqual(ans, str(p))
def test_str_1_coef(self):
p = Polynom([0, 1, 1])
ans = 'x^{ 2 } + x'
self.assertEqual(ans, str(p))
def test_str_neg_coef(self):
p = Polynom([-1, -2, -3])
ans = '-3 x^{ 2 } - 2 x - 1'
self.assertEqual(ans, str(p))
def test_str_multi_coef(self):
p = Polynom([1, [2, 3], 4])
ans = '4 x^{ 2 } + 2 x + 3 x + 1'
self.assertEqual(ans, str(p))
def test_str_exp_coef(self):
p = Polynom([1, Expression([2, 3, "+"]), 4])
ans = '4 x^{ 2 } + ( 2 + 3 ) x + 1'
self.assertEqual(ans, str(p))
def test_reduce_nilpo(self): def test_reduce_nilpo(self):
p = Polynom([1, 2, 3]) p = Polynom([1, 2, 3])
self.assertEqual(p, p.reduce()) self.assertEqual(p, p.reduce())