solve left parenthesis issue

This commit is contained in:
Lafrite 2015-04-27 19:09:03 +02:00
parent 67efaaee62
commit 0545d73751
2 changed files with 21 additions and 19 deletions

View File

@ -149,7 +149,8 @@ class Operator(str):
""" Add parenthesis for left operand if necessary """ """ Add parenthesis for left operand if necessary """
ans = opl ans = opl
try: try:
if opl.mainOp == op.sub1: # TODO: Je pige pas pourquoi quand on enlève .name ça marche plus... |lun. avril 27 19:07:24 CEST 2015
if opl.mainOp.name == op.sub1.name:
ans = opl ans = opl
elif opl.mainOp.priority < self.priority: elif opl.mainOp.priority < self.priority:
ans = flatten_list(["(", opl, ")"]) ans = flatten_list(["(", opl, ")"])
@ -163,7 +164,7 @@ class Operator(str):
return ans return ans
def r_parenthesis(self, op, str_join=False): def r_parenthesis(self, op, str_join=False):
""" Add parenthesis for left operand if necessary """ """ Add parenthesis for rigth operand if necessary """
# TODO: /!\ Parenthèses pour -2abc et l'opérateur * |lun. mars 9 19:02:32 CET 2015 # TODO: /!\ Parenthèses pour -2abc et l'opérateur * |lun. mars 9 19:02:32 CET 2015
try: try:
if op.mainOp.priority < self.priority: if op.mainOp.priority < self.priority:
@ -212,6 +213,12 @@ def operatorize(fun):
def mod_fun(self, *args): def mod_fun(self, *args):
ans = fun(self, *args) ans = fun(self, *args)
def _eq(op1, op2):
""" op1 == op2 """
return op1.name == op2.name == name and op1.arity == op2.arity
ans["__eq__"] = _eq
new_op = Operator(ans["operator"]) new_op = Operator(ans["operator"])
for (attr, value) in ans.items(): for (attr, value) in ans.items():
if hasattr(value, '__call__'): if hasattr(value, '__call__'):
@ -603,18 +610,8 @@ if __name__ == '__main__':
#print("\t op.can_be_operator('+') :" + str(op.can_be_operator('+'))) #print("\t op.can_be_operator('+') :" + str(op.can_be_operator('+')))
#print("\t op.can_be_operator('t') :" + str(op.can_be_operator('t'))) #print("\t op.can_be_operator('t') :" + str(op.can_be_operator('t')))
from .render import tex print("op.sub.__dict__ -> ", op.sub.__dict__)
print(tex([-2, 3, op.add ])) print(op.sub == op.sub1)
print("-----------------")
print(tex([-2, 3, op.mul ]))
print("-----------------")
from .polynom import Polynom
print(tex([Polynom([1,2,3]), 2, op.mul]))
print("-----------------")
from .fraction import Fraction
print(tex([2, Fraction(1,2), op.mul]))
print("-----------------")
#import doctest #import doctest
#doctest.testmod() #doctest.testmod()

View File

@ -74,11 +74,16 @@ p2i = Render(p2i_render)
if __name__ == '__main__': if __name__ == '__main__':
from .operator import op from .operator import op
exp = [ 2, 3, op.add, 4, op.mul] from itertools import permutations
print(exp) from pymath import Polynom
print("txt(exp) :" + str(txt(exp))) from pymath import Expression
print("tex(exp) :" + str(tex(exp))) coefs_p = [[(i-2),(j-2)] for i,j in permutations(range(3),2)]
print("p2i(exp) :" + str(p2i(exp))) coefs_q = [[2*(i-2),2*(j-2)] for i,j in permutations(range(3),2)]
l_p = [Polynom(i) for i in coefs_p]
l_q = [Polynom(i) for i in coefs_q]
operations = [Expression([l_p[i],l_q[j],op.mul]) for i,j in permutations(range(len(coefs_p)),2)]
for i in operations:
print(i)