diff --git a/mapytex/calculus/API/__init__.py b/mapytex/calculus/API/__init__.py index f5b120e..1502cdf 100644 --- a/mapytex/calculus/API/__init__.py +++ b/mapytex/calculus/API/__init__.py @@ -117,7 +117,7 @@ x^7 (2x - 3)(- x + 2) (2x - 3)(- x + 2) 2x(- x) + 2x * 2 - 3(- x) - 3 * 2 -2 * - 1 * x^(1 + 1) + 2 * 2 * x - 3 * - 1 * x - 6 +2(- 1) * x^(1 + 1) + 2 * 2 * x - 3(- 1) * x - 6 4x + 3x - 2x^2 - 6 (4 + 3) * x - 2x^2 - 6 - 2x^2 + 7x - 6 diff --git a/mapytex/calculus/core/renders/tree2tex.py b/mapytex/calculus/core/renders/tree2tex.py index 531c08b..ae09150 100644 --- a/mapytex/calculus/core/renders/tree2tex.py +++ b/mapytex/calculus/core/renders/tree2tex.py @@ -91,9 +91,11 @@ def mul2tex(left, right): '- 3x' >>> mul2tex(a, a) 'x \\times x' + >>> mul2tex(a, MO.factory(-3)) + 'x(- 3)' """ left_ = render_with_parenthesis(left, "*") - right_ = render_with_parenthesis(right, "*") + right_ = render_with_parenthesis(right, "*", is_at_right=True) display_time = True # if (right_[0].isalpha() and (left_.isnumeric() or left_.isdecimal())) or right_[ @@ -183,23 +185,17 @@ def pow2tex(left, right): return f"{left_}^{{{right_}}}" -def render_with_parenthesis(subtree, operator): +def render_with_parenthesis(subtree, operator, is_at_right=False): subtree_need_parenthesis = False try: subtree.node except AttributeError: - try: - if ( - OPERATORS[subtree.MAINOP]["precedence"] - < OPERATORS[operator]["precedence"] - ): - subtree_need_parenthesis = True - except (AttributeError, KeyError): - pass try: subtree_ = subtree.__tex__ except AttributeError: subtree_ = str(subtree) + if subtree_.startswith("-") and OPERATORS["-"]["precedence"] < OPERATORS[operator]["precedence"] and is_at_right: + subtree_need_parenthesis = True else: if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]: subtree_need_parenthesis = True diff --git a/mapytex/calculus/core/renders/tree2txt.py b/mapytex/calculus/core/renders/tree2txt.py index 2942d9e..d46a11e 100644 --- a/mapytex/calculus/core/renders/tree2txt.py +++ b/mapytex/calculus/core/renders/tree2txt.py @@ -91,11 +91,13 @@ def mul2txt(left, right): '- 3x' >>> mul2txt(a, a) 'x * x' + >>> mul2txt(a, MO.factory(-3)) + 'x(- 3)' """ display_time = True left_ = render_with_parenthesis(left, "*") - right_ = render_with_parenthesis(right, "*") + right_ = render_with_parenthesis(right, "*", is_at_right=True) if right_[0].isalpha(): # TODO: C'est bien beurk en dessous... |ven. déc. 21 12:03:07 CET 2018 @@ -187,23 +189,22 @@ def pow2txt(left, right): return f"{left_}^{right_}" -def render_with_parenthesis(subtree, operator): +def tree_with_parenthesis(subtree, operator): + """ Assuming the subtree is a tree, then have .node """ + pass + + +def render_with_parenthesis(subtree, operator, is_at_right=False): subtree_need_parenthesis = False try: subtree.node except AttributeError: - try: - if ( - OPERATORS[subtree.MAINOP]["precedence"] - < OPERATORS[operator]["precedence"] - ): - subtree_need_parenthesis = True - except (AttributeError, KeyError): - pass try: subtree_ = subtree.__txt__ except AttributeError: subtree_ = str(subtree) + if subtree_.startswith("-") and OPERATORS["-"]["precedence"] < OPERATORS[operator]["precedence"] and is_at_right: + subtree_need_parenthesis = True else: if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]: subtree_need_parenthesis = True