Skip to content

Commit

Permalink
Merge pull request #10 from AutoResearch/add-standard-operators-and_f…
Browse files Browse the repository at this point in the history
…unctions

bug: make ** work
  • Loading branch information
younesStrittmatter authored Sep 8, 2023
2 parents 67903e0 + d865ae5 commit 6272af3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/equation_tree/src/tree_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ def node_from_prefix(
def _from_prefix_recursion(
prefix_notation, function_test, operator_test, variable_test, constant_test, index=0
):
if index >= len(prefix_notation):
return None, None

attribute = prefix_notation[index]

if function_test(attribute):
Expand Down
8 changes: 7 additions & 1 deletion src/equation_tree/util/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def prefix_to_infix(
Example:
>>> is_function = lambda x: x in ['sin', 'cos']
>>> is_operator = lambda x : x in ['+', '-', '*', 'max']
>>> is_operator = lambda x : x in ['+', '-', '*', 'max', '**']
>>> prefix_to_infix(['-', 'x_1', 'x_2'], is_function, is_operator)
'(x_1-x_2)'
Expand All @@ -39,6 +39,9 @@ def prefix_to_infix(
>>> prefix_to_infix(['max', 'x_1', 'x_2'], is_function, is_operator)
'max(x_1,x_2)'
>>> prefix_to_infix(['**', 'x_1', 'x_2'], is_function, is_operator)
'(x_1**x_2)'
"""
stack = []
for i in range(len(prefix) - 1, -1, -1):
Expand All @@ -51,6 +54,7 @@ def prefix_to_infix(
"/",
"^",
"*",
'**'
]:
# symbol is binary operator
str = "(" + stack.pop() + prefix[i] + stack.pop() + ")"
Expand Down Expand Up @@ -324,6 +328,8 @@ def _infix_to_postfix(infix, function_test, operator_test):
>>> _infix_to_postfix('sin(x_1)-x_2', is_function, is_operator)[::-1]
['-', 'sin', 'x_1', 'x_2']
>>> _infix_to_postfix('x_1**x_2', is_function, is_operator)[::-1]
"""
infix = _tokenize_infix(infix, function_test, operator_test)
infix = [el.lower() for el in infix]
Expand Down

0 comments on commit 6272af3

Please sign in to comment.