From fcee4a89426b65feb46fe5cc7829e45792700d87 Mon Sep 17 00:00:00 2001 From: Lafrite Date: Mon, 27 Apr 2015 19:16:08 +0200 Subject: [PATCH] solve parenthesis bug for op.pw --- pymath/operator.py | 16 ++++++++++++++++ pymath/render.py | 6 +++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/pymath/operator.py b/pymath/operator.py index c4d12bc..6ffbb49 100644 --- a/pymath/operator.py +++ b/pymath/operator.py @@ -565,6 +565,21 @@ class op(object): """ Calling this operator performs the rigth calculus """ return getattr(op1, "__pow__")(op2) + def l_parenthesis(self, opl, str_join=False): + """ Add parenthesis for left operand if necessary """ + ans = opl + try: + if opl.mainOp.priority < self.priority: + ans = flatten_list(["(", opl, ")"]) + except AttributeError as e: + # op has not the attribute priority + pass + + ans = flatten_list([ans]) + if str_join: + ans = ' '.join([str(i) for i in ans]) + return ans + caract = { "operator" : "^", \ "name" : "pw",\ @@ -573,6 +588,7 @@ class op(object): "actions" : ("__pow__",""), \ "txt" : "{op1} ^ {op2}",\ "tex" : "{op1}^{{ {op2} }}",\ + "l_parenthesis": l_parenthesis,\ "_call":_call,\ } diff --git a/pymath/render.py b/pymath/render.py index 56587bc..67e70d8 100644 --- a/pymath/render.py +++ b/pymath/render.py @@ -77,11 +77,11 @@ if __name__ == '__main__': from itertools import permutations from pymath import Polynom from pymath import Expression - coefs_p = [[(i-2),(j-2)] for i,j in permutations(range(3),2)] - coefs_q = [[2*(i-2),2*(j-2)] for i,j in permutations(range(3),2)] + coefs_p = [[(i-2),(j-2)] for i,j in permutations(range(5),2)] + coefs_q = [[2*(i-2),2*(j-2)] for i,j in permutations(range(5),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)] + operations = [Expression([l_p[i],l_q[j],op.pw]) for i,j in permutations(range(len(coefs_p)),2)] for i in operations: print(i)