Print substituted expression without numerical evaluation

不打扰是莪最后的温柔 提交于 2019-12-23 16:08:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!