def check_expression(expr, var_symbols, only_from_sympy=False):
"""
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...
a.update(sage.__dict__)
assert "sin" in a
is_different = False
try:
e_sage = eval(expr, a)
assert not isinstance(e_sage, sympy.Basic)
except (NameError, TypeError):
is_different = True
pass
# evaluate the expression in the context of SymPy:
if var_symbols:
sympy_vars = sympy.var(var_symbols)
b = globals().copy()
b.update(sympy.__dict__)
assert "sin" in b
b.update(sympy.__dict__)
e_sympy = eval(expr, b)
assert isinstance(e_sympy, sympy.Basic)
# Sympy func may have specific _sage_ method
if is_different:
_sage_method = getattr(e_sympy.func, "_sage_")
e_sage = _sage_method(sympy.S(e_sympy))
# Do the actual checks:
if not only_from_sympy:
assert sympy.S(e_sage) == e_sympy
assert e_sage == sage.SR(e_sympy)
评论列表
文章目录