所以我打算写一个关于数学的网络应用程序,我需要将用户输入转换为 SymPy 表达式而不修改它(简化),例如。所以我想像这个例子一样取消这种行为。
>>> srepr(Rational(2,4)) #this is the problem
'Rational(1, 2)'
>>> srepr(Rational(2,4,evaluate=False)) #doesn't work
Traceback...
但我已经设法在其他类型的表示中做到了。
>>> srepr(Pow(x,(Mul(e,e,evaluate=False)),evaluate=False)) #nice
"Pow(Symbol('x'), Mul(Symbol('e'), Symbol('e')))"
>>> srepr(sqrt(Integer(8))) #not what I want
'Mul(Integer(2), Pow(Integer(2), Rational(1, 2)))'
>>> srepr(Pow(Integer(8),Rational(1,2),evaluate=False)) #this is the way
'Pow(Integer(8), Rational(1, 2))'
>>> from sympy import E
>>> log(E,evaluate=False)
log(E)
还有没有办法告诉 SymPy不应该评估所有表示?