mod expression with operands

This commit is contained in:
Lafrite 2014-12-22 14:14:37 +01:00
parent 8dd90bc85a
commit 2de1b1add8
1 changed files with 63 additions and 61 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
from .generic import Stack, flatten_list, expand_list, isNumber, isOperator from .generic import Stack, flatten_list, expand_list, isNumber, isOperator, isNumerand
from .render import txt, tex from .render import txt, tex
from .str2tokens import str2tokens from .str2tokens import str2tokens
from .operator import op from .operator import op
@ -121,15 +121,15 @@ class Expression(object):
old_s = new_s old_s = new_s
yield new_s yield new_s
try: try:
for s in self.child.simplify(): for s in self.child.simplify():
if old_s != s: if old_s != s:
yield s yield s
if not Expression.isExpression(self.child): if not Expression.isExpression(self.child):
yield self.STR_RENDER([self.child]) yield self.STR_RENDER([self.child])
except AttributeError: except AttributeError:
yield self.STR_RENDER([self.child]) yield self.STR_RENDER([self.child])
def simplified(self): def simplified(self):
""" Get the simplified version of the expression """ """ Get the simplified version of the expression """
@ -157,7 +157,7 @@ class Expression(object):
while len(tokenList) > 2: while len(tokenList) > 2:
# on va chercher les motifs du genre A B +, quand l'operateur est d'arité 2, pour les calculer # on va chercher les motifs du genre A B +, quand l'operateur est d'arité 2, pour les calculer
if isNumber(tokenList[0]) and isNumber(tokenList[1]) \ if isNumerand(tokenList[0]) and isNumerand(tokenList[1]) \
and isOperator(tokenList[2]) and tokenList[2].arity == 2 : and isOperator(tokenList[2]) and tokenList[2].arity == 2 :
# S'il y a une opération à faire # S'il y a une opération à faire
@ -173,7 +173,7 @@ class Expression(object):
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 isNumerand(tokenList[0]) \
and isOperator(tokenList[1]) and tokenList[1].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
@ -192,7 +192,7 @@ class Expression(object):
del tokenList[0] del tokenList[0]
if len(tokenList) == 2 and isNumber(tokenList[0]) \ if len(tokenList) == 2 and isNumerand(tokenList[0]) \
and isOperator(tokenList[1]) and tokenList[1].arity == 1: and isOperator(tokenList[1]) and tokenList[1].arity == 1:
# S'il reste deux éléments dont un operation d'arité 1 # S'il reste deux éléments dont un operation d'arité 1
op1 = tokenList[0] op1 = tokenList[0]
@ -283,81 +283,83 @@ class Expression(object):
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(type(i))
print(i) print(i)
print(type(a.simplified()), ":", a.simplified()) #print(type(a.simplified()), ":", a.simplified())
print("\n") print("\n")
if __name__ == '__main__': if __name__ == '__main__':
Expression.set_render(txt) Expression.set_render(txt)
exp1 = "2 ^ 3 * 5" #exp = "2 ^ 3 * 5"
test(exp1) #test(exp)
Expression.set_render(tex) exp = "2x + 5"
test(exp1)
from pymath.operator import op
exp = [2, 3, op.pw, 5, op.mul]
test(exp) test(exp)
Expression.set_render(txt) #Expression.set_render(tex)
test([Expression(exp1), Expression(exp), op.add]) #test(exp1)
exp = "1 + 3 * 5" #from pymath.operator import op
e = Expression(exp) #exp = [2, 3, op.pw, 5, op.mul]
f = -e #test(exp)
print(f)
exp = "2 * 3 * 3 * 5" #test([Expression(exp1), Expression(exp), op.add])
test(exp)
exp = "2 * 3 + 3 * 5" #exp = "1 + 3 * 5"
test(exp) #e = Expression(exp)
#f = -e
#print(f)
exp = "2 * ( 3 + 4 ) + 3 * 5" #exp = "2 * 3 * 3 * 5"
test(exp) #test(exp)
exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5" #exp = "2 * 3 + 3 * 5"
test(exp) #test(exp)
exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5"
test(exp)
exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
test(exp)
exp = "2 + 5 * ( 3 - 4 )"
test(exp)
exp = "( 2 + 5 ) * ( 3 - 4 )^4" #exp = "2 * ( 3 + 4 ) + 3 * 5"
test(exp) #test(exp)
exp = "( 2 + 5 ) * ( 3 * 4 )" #exp = "2 * ( 3 + 4 ) + ( 3 - 4 ) * 5"
test(exp) #test(exp)
#
#exp = "2 * ( 2 - ( 3 + 4 ) ) + ( 3 - 4 ) * 5"
#test(exp)
#
#exp = "2 * ( 2 - ( 3 + 4 ) ) + 5 * ( 3 - 4 )"
#test(exp)
#
#exp = "2 + 5 * ( 3 - 4 )"
#test(exp)
exp = "( 2 + 5 - 1 ) / ( 3 * 4 )" #exp = "( 2 + 5 ) * ( 3 - 4 )^4"
test(exp) #test(exp)
exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12" #exp = "( 2 + 5 ) * ( 3 * 4 )"
test(exp) #test(exp)
exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2" #exp = "( 2 + 5 - 1 ) / ( 3 * 4 )"
test(exp) #test(exp)
exp="(-2+5)/(3*4)+1/12+5*5" #exp = "( 2 + 5 ) / ( 3 * 4 ) + 1 / 12"
test(exp) #test(exp)
exp="-2*4(12 + 1)(3-12)" #exp = "( 2+ 5 )/( 3 * 4 ) + 1 / 2"
test(exp) #test(exp)
#exp="(-2+5)/(3*4)+1/12+5*5"
#test(exp)
#exp="-2*4(12 + 1)(3-12)"
#test(exp)
exp="(-2+5)/(3*4)+1/12+5*5" #exp="(-2+5)/(3*4)+1/12+5*5"
test(exp) #test(exp)
# TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014 # TODO: The next one doesn't work |ven. janv. 17 14:56:58 CET 2014
#exp="-2*(-a)(12 + 1)(3-12)" #exp="-2*(-a)(12 + 1)(3-12)"