Printing a substituted expression without a numeric evaluation
Is it possible to print the SymPy replacement expression without evaluating it? I want to print both the replaced express and the result.
eg.
x = Symbol('x')
expr = x**2
pprint(expr) # this prints expression
result = expr.subs({x:2})
print(result) # this print result 4
How do I print the "average" expression 2**2
?
+3
source to share
1 answer
You can pass UnevaluatedExpr
for this purpose as below:
result = expr.subs(x, UnevaluatedExpr(2))
print(result) # prints 2**2
result = result.doit()
print(result) # prints 4
Documentation: Preventing Expression Evaluation
+2
source to share