def check_expression(expr, var_symbols):
"""Does eval(expr) both in Sage and SymPy and does other checks."""
# evaluate the expression in the context of Sage:
if var_symbols:
sage.var(var_symbols)
a = globals().copy()
# safety checks...
assert not "sin" in a
a.update(sage.__dict__)
assert "sin" in a
e_sage = eval(expr, a)
assert not isinstance(e_sage, sympy.Basic)
# evaluate the expression in the context of SymPy:
if var_symbols:
sympy.var(var_symbols)
b = globals().copy()
assert not "sin" in b
b.update(sympy.__dict__)
assert "sin" in b
b.update(sympy.__dict__)
e_sympy = eval(expr, b)
assert isinstance(e_sympy, sympy.Basic)
# Do the actual checks:
assert sympy.S(e_sage) == e_sympy
assert e_sage == sage.SR(e_sympy)
评论列表
文章目录