Feat: dirty way to manage () and * in render

This commit is contained in:
Bertrand Benjamin 2020-12-15 16:01:01 +01:00
parent 460255b151
commit ff4d8471ef
3 changed files with 18 additions and 21 deletions

View File

@ -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

View File

@ -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

View File

@ -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