问题
Is it possible to print a substitute SymPy expression without calculation? I want to print both substituted express and result.
e.g.
x = Symbol('x')
expr = x**2
pprint(expr) # this prints expression
result = expr.subs({x:2})
print(result) # this print result 4
How can I print the "middle result", the expression 2**2?
回答1:
You can pass in UnevaluatedExpr for this purpose, as shown below:
result = expr.subs(x, UnevaluatedExpr(2))
print(result) # prints 2**2
result = result.doit()
print(result) # prints 4
Documentation: Prevent expression evaluation
来源:https://stackoverflow.com/questions/45331069/print-substituted-expression-without-numerical-evaluation