Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infix notation of pow function #48

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion physo/physym/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ def ComputeInfixNotation (program_tokens):
else:
res = "%s(%s)" % (token.sympy_repr, args[0])
elif token.arity == 2:
res = "(%s%s%s)" % (args[0], token.sympy_repr, args[1])
if token.sympy_repr == "pow":
res = "((%s)**(%s))" % (args[0], args[1])
else:
res = "(%s%s%s)" % (args[0], token.sympy_repr, args[1])
elif token.arity > 2 :
args_str = ""
for arg in args: args_str+="%s,"%arg
Expand Down
9 changes: 5 additions & 4 deletions physo/physym/tests/execute_UnitTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ def test_ComputeInfixNotation(self):
"constants_complexity" : {"pi" : 0. , "c" : 0. , "M" : 1. , "1" : 1. },
}
my_lib = Lib.Library(args_make_tokens = args_make_tokens,
superparent_units = [1, -2, 1], superparent_name = "y")
superparent_units = [2, -2, 1], superparent_name = "y")

# TEST PROGRAM
test_program_str = ["mul", "mul", "M", "n2", "c", "sub", "inv", "sqrt", "sub", "1", "div", "n2", "v", "n2",
test_program_str = ["mul", "mul", "M", "n2", "c", "sub", "inv", "sqrt", "sub", "pow", "1", "pi", "div", "n2", "v", "n2",
"c", "cos", "div", "sub", "1", "div", "v", "c", "pi"]
test_program = np.array([my_lib.lib_name_to_token[tok_str] for tok_str in test_program_str])
# Infix output
Expand All @@ -184,12 +184,13 @@ def test_ComputeInfixNotation(self):
print("\nComputeInfixNotation time = %.3f ms"%((t1-t0)*1e3/N))
infix = sympy.parsing.sympy_parser.parse_expr(infix_str)
# Expected infix output
expected_str = "M*(c**2.)*(1./((1.-(v**2)/(c**2))**0.5)-cos((1.-(v/c))/pi))"
expected_str = "M*(c**2.)*(1./((1.**pi-(v**2)/(c**2))**0.5)-cos((1.-(v/c))/pi))"
expected = sympy.parsing.sympy_parser.parse_expr(expected_str)
# difference
# Difference
diff = sympy.simplify(infix - expected, rational = True)
works_bool = diff == 0
self.assertTrue(works_bool)


if __name__ == '__main__':
unittest.main(verbosity=2)