Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print substituted expression without numerical evaluation

Tags:

python

sympy

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?

like image 696
jerry Avatar asked Jun 22 '26 15:06

jerry


1 Answers

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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!