Feat: dirty way to manage () and * in render
This commit is contained in:
		| @@ -117,7 +117,7 @@ x^7 | |||||||
| (2x - 3)(- x + 2) | (2x - 3)(- x + 2) | ||||||
| (2x - 3)(- x + 2) | (2x - 3)(- x + 2) | ||||||
| 2x(- x) + 2x * 2 - 3(- x) - 3 * 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 | 4x + 3x - 2x^2 - 6 | ||||||
| (4 + 3) * x - 2x^2 - 6 | (4 + 3) * x - 2x^2 - 6 | ||||||
| - 2x^2 + 7x - 6 | - 2x^2 + 7x - 6 | ||||||
|   | |||||||
| @@ -91,9 +91,11 @@ def mul2tex(left, right): | |||||||
|     '- 3x' |     '- 3x' | ||||||
|     >>> mul2tex(a, a) |     >>> mul2tex(a, a) | ||||||
|     'x \\times x' |     'x \\times x' | ||||||
|  |     >>> mul2tex(a, MO.factory(-3)) | ||||||
|  |     'x(- 3)' | ||||||
|     """ |     """ | ||||||
|     left_ = render_with_parenthesis(left, "*") |     left_ = render_with_parenthesis(left, "*") | ||||||
|     right_ = render_with_parenthesis(right, "*") |     right_ = render_with_parenthesis(right, "*", is_at_right=True) | ||||||
|  |  | ||||||
|     display_time = True |     display_time = True | ||||||
|     # if (right_[0].isalpha() and (left_.isnumeric() or left_.isdecimal())) or right_[ |     # if (right_[0].isalpha() and (left_.isnumeric() or left_.isdecimal())) or right_[ | ||||||
| @@ -183,23 +185,17 @@ def pow2tex(left, right): | |||||||
|     return f"{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 |     subtree_need_parenthesis = False | ||||||
|     try: |     try: | ||||||
|         subtree.node |         subtree.node | ||||||
|     except AttributeError: |     except AttributeError: | ||||||
|         try: |  | ||||||
|             if ( |  | ||||||
|                 OPERATORS[subtree.MAINOP]["precedence"] |  | ||||||
|                 < OPERATORS[operator]["precedence"] |  | ||||||
|             ): |  | ||||||
|                 subtree_need_parenthesis = True |  | ||||||
|         except (AttributeError, KeyError): |  | ||||||
|             pass |  | ||||||
|         try: |         try: | ||||||
|             subtree_ = subtree.__tex__ |             subtree_ = subtree.__tex__ | ||||||
|         except AttributeError: |         except AttributeError: | ||||||
|             subtree_ = str(subtree) |             subtree_ = str(subtree) | ||||||
|  |         if subtree_.startswith("-") and OPERATORS["-"]["precedence"] < OPERATORS[operator]["precedence"] and is_at_right: | ||||||
|  |             subtree_need_parenthesis = True | ||||||
|     else: |     else: | ||||||
|         if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]: |         if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]: | ||||||
|             subtree_need_parenthesis = True |             subtree_need_parenthesis = True | ||||||
|   | |||||||
| @@ -91,11 +91,13 @@ def mul2txt(left, right): | |||||||
|     '- 3x' |     '- 3x' | ||||||
|     >>> mul2txt(a, a) |     >>> mul2txt(a, a) | ||||||
|     'x * x' |     'x * x' | ||||||
|  |     >>> mul2txt(a, MO.factory(-3)) | ||||||
|  |     'x(- 3)' | ||||||
|     """ |     """ | ||||||
|     display_time = True |     display_time = True | ||||||
|  |  | ||||||
|     left_ = render_with_parenthesis(left, "*") |     left_ = render_with_parenthesis(left, "*") | ||||||
|     right_ = render_with_parenthesis(right, "*") |     right_ = render_with_parenthesis(right, "*", is_at_right=True) | ||||||
|  |  | ||||||
|     if right_[0].isalpha(): |     if right_[0].isalpha(): | ||||||
|         # TODO: C'est bien beurk en dessous... |ven. déc. 21 12:03:07 CET 2018 |         # 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_}" |     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 |     subtree_need_parenthesis = False | ||||||
|     try: |     try: | ||||||
|         subtree.node |         subtree.node | ||||||
|     except AttributeError: |     except AttributeError: | ||||||
|         try: |  | ||||||
|             if ( |  | ||||||
|                 OPERATORS[subtree.MAINOP]["precedence"] |  | ||||||
|                 < OPERATORS[operator]["precedence"] |  | ||||||
|             ): |  | ||||||
|                 subtree_need_parenthesis = True |  | ||||||
|         except (AttributeError, KeyError): |  | ||||||
|             pass |  | ||||||
|         try: |         try: | ||||||
|             subtree_ = subtree.__txt__ |             subtree_ = subtree.__txt__ | ||||||
|         except AttributeError: |         except AttributeError: | ||||||
|             subtree_ = str(subtree) |             subtree_ = str(subtree) | ||||||
|  |         if subtree_.startswith("-") and OPERATORS["-"]["precedence"] < OPERATORS[operator]["precedence"] and is_at_right: | ||||||
|  |             subtree_need_parenthesis = True | ||||||
|     else: |     else: | ||||||
|         if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]: |         if OPERATORS[subtree.node]["precedence"] < OPERATORS[operator]["precedence"]: | ||||||
|             subtree_need_parenthesis = True |             subtree_need_parenthesis = True | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user