Calculus for poly is ok need to adapt test and there is big bug with

unify render!
This commit is contained in:
lafrite 2014-11-11 21:45:24 +01:00
parent 2b67fc682c
commit c82e6c9a44
2 changed files with 31 additions and 24 deletions

View File

@ -21,7 +21,9 @@ class Expression(object):
#self._exp = exp #self._exp = exp
self.postfix_tokens = str2tokens(exp) # les tokens seront alors stockés dans self.tokens temporairement self.postfix_tokens = str2tokens(exp) # les tokens seront alors stockés dans self.tokens temporairement
elif type(exp) == list: 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): 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 # Comme on vient de faire le calcul, on peut détruire aussi les deux prochains termes
del tokenList[0:3] 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]) \ 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 # S'il y a une opération à faire
op1 = tokenList[0] op1 = tokenList[0]
@ -119,23 +121,33 @@ class Expression(object):
self.child = Expression(steps[-1]) self.child = Expression(steps[-1])
def isExpression(self, other):
try:
other._isExpression
except AttributeError:
return 0
return 1
def test(exp): def test(exp):
a = Expression(exp) a = Expression(exp)
print(a) print(a)
#for i in a.simplify(): for i in a.simplify():
# print(i) print(i)
print("\n") print("\n")
if __name__ == '__main__': if __name__ == '__main__':
Expression.STR_RENDER = txt 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) test(exp)
from pymath.operator import add, pw, mul test([Expression(exp1), Expression(exp), op.add])
exp = [2, 3, pw, 5, mul]
test(exp)
exp = "1 + 3 * 5" exp = "1 + 3 * 5"
test(exp) test(exp)

View File

@ -146,7 +146,7 @@ class Polynom(object):
def isPolynom(self, other): def isPolynom(self, other):
try: try:
exp._isPolynom other._isPolynom
except AttributeError: except AttributeError:
return 0 return 0
return 1 return 1
@ -248,7 +248,7 @@ class Polynom(object):
if a == 0 or b == 0: if a == 0 or b == 0:
elem = 0 elem = 0
else: else:
elem = [a, b, "*"] elem = Expression([a, b, op.mul])
try: try:
if coefs[i+j]==0: if coefs[i+j]==0:
coefs[i+j] = elem coefs[i+j] = elem
@ -261,6 +261,7 @@ class Polynom(object):
steps.append(p) steps.append(p)
steps += p.simplify() steps += p.simplify()
return steps return steps
def __rmul__(self, other): def __rmul__(self, other):
@ -268,12 +269,6 @@ class Polynom(object):
return o_poly.__mul__(self) return o_poly.__mul__(self)
def __div__(self, other):
pass
def __truediv__(self, other):
pass
def test(p,q): def test(p,q):
print("---------------------") print("---------------------")
@ -304,20 +299,20 @@ if __name__ == '__main__':
from .fraction import Fraction from .fraction import Fraction
p = Polynom([1, -2 ]) p = Polynom([1, -2 ])
q = Polynom([4, 7]) q = Polynom([4, 7])
#test(p,q) test(p,q)
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)
p = Polynom([1, 1, 1 ]) p = Polynom([1, 1, 1 ])
print(p) #print(p)
#print("-- Poly étrange --") #print("-- Poly étrange --")
p = Polynom([1, [2, 3], 4], "x") #p = Polynom([1, [2, 3], 4], "x")
print(repr(p)) #print(repr(p))
for i in p.simplify(): #for i in p.simplify():
print(i) # print(i)
#print("-- Poly étrange --") #print("-- Poly étrange --")
#p = Polynom([1, [[2, 3, "*"], [4,5,"*"]], 4], "x") #p = Polynom([1, [[2, 3, "*"], [4,5,"*"]], 4], "x")
#print(repr(p)) #print(repr(p))