diff --git a/pymath/expression.py b/pymath/expression.py index 2e38fbd..5a9c743 100644 --- a/pymath/expression.py +++ b/pymath/expression.py @@ -21,7 +21,9 @@ class Expression(object): #self._exp = exp self.postfix_tokens = str2tokens(exp) # les tokens seront alors stockés dans self.tokens temporairement elif type(exp) == list: - self.postfix_tokens = exp + self.postfix_tokens = flatten_list([tok.postfix_tokens if self.isExpression(tok) else tok for tok in exp]) + + self._isExpression = 1 def __str__(self): """ @@ -91,9 +93,9 @@ class Expression(object): # Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes del tokenList[0:3] - # Et les motifs du gens - A, quand l'operateur est d'arité 1 + # Et les motifs du gens A -, quand l'operateur est d'arité 1 elif isNumber(tokenList[0]) \ - and isOperator(tokenList[1]) and tokenList[2].arity == 1: + and isOperator(tokenList[1]) and tokenList[1].arity == 1: # S'il y a une opération à faire op1 = tokenList[0] @@ -119,23 +121,33 @@ class Expression(object): self.child = Expression(steps[-1]) + def isExpression(self, other): + try: + other._isExpression + except AttributeError: + return 0 + return 1 + + def test(exp): a = Expression(exp) print(a) - #for i in a.simplify(): - # print(i) + for i in a.simplify(): + print(i) print("\n") if __name__ == '__main__': Expression.STR_RENDER = txt - exp = "2 ^ 3 * 5" + exp1 = "2 ^ 3 * 5" + test(exp1) + + from pymath.operator import op + exp = [2, 3, op.pw, 5, op.mul] test(exp) - from pymath.operator import add, pw, mul - exp = [2, 3, pw, 5, mul] - test(exp) + test([Expression(exp1), Expression(exp), op.add]) exp = "1 + 3 * 5" test(exp) diff --git a/pymath/polynom.py b/pymath/polynom.py index b016198..eb5d014 100644 --- a/pymath/polynom.py +++ b/pymath/polynom.py @@ -146,7 +146,7 @@ class Polynom(object): def isPolynom(self, other): try: - exp._isPolynom + other._isPolynom except AttributeError: return 0 return 1 @@ -248,7 +248,7 @@ class Polynom(object): if a == 0 or b == 0: elem = 0 else: - elem = [a, b, "*"] + elem = Expression([a, b, op.mul]) try: if coefs[i+j]==0: coefs[i+j] = elem @@ -259,20 +259,15 @@ class Polynom(object): p = Polynom(coefs) steps.append(p) - + steps += p.simplify() + return steps def __rmul__(self, other): o_poly = self.conv2poly(other) return o_poly.__mul__(self) - - def __div__(self, other): - pass - - def __truediv__(self, other): - pass def test(p,q): @@ -304,20 +299,20 @@ if __name__ == '__main__': from .fraction import Fraction p = Polynom([1, -2 ]) q = Polynom([4, 7]) - #test(p,q) + test(p,q) q = Polynom([0, Fraction(1,2), 0, Fraction(-4,3)]) #test(p,q) p = Polynom([1, 1, 1 ]) - print(p) + #print(p) #print("-- Poly étrange --") - p = Polynom([1, [2, 3], 4], "x") - print(repr(p)) - for i in p.simplify(): - print(i) + #p = Polynom([1, [2, 3], 4], "x") + #print(repr(p)) + #for i in p.simplify(): + # print(i) #print("-- Poly étrange --") #p = Polynom([1, [[2, 3, "*"], [4,5,"*"]], 4], "x") #print(repr(p))