How do I display a full expression in sympy?
Asked Answered
P

4

9

I am trying to use sympy in a Jupyter notebook to document and perform a series of mathematical cacluations in a reporducible way.

If I define the following:

from sympy import *

init_printing()
x, y, z = symbols("x y z")

x=y+z
x

then I can display the value of x (that is, y+z).

How do I display the full equation (x=y+z)?

Running Eq(x,y+z), even with evaluate=False) returns the expression with the value of x substituted (y+z=y+z).

Parrish answered 25/11, 2016 at 13:54 Comment(0)
R
5

Although you first declare x as a sympy.Symbol, once you perform the assignment x=y+z, x becomes an alias for y+z. Whenever you use x from that point after, x will be automatically translated by python as y+z.

If you insist on this workflow, you could use Eq(S('x'),y+z) to display the equation.

Romie answered 25/11, 2016 at 16:39 Comment(4)
Is there a method off x that returns the equivalent of S('x')?Parrish
S() just converts a string to a sympy expression. You can just use Eq(x, y + z) here since x is already defined as a Symbol.Kigali
When I try this way it returns False instead of the expression itself.Westernmost
@iury simoes-sousa: use display(Eq(S('x'), y+z, evaluate=False)). The full expression will be shown, not 'True' or 'False'Cherie
W
8

I tried using Eq(S('x'),y+z), also Eq(S('x'),x) and sympy keep returning a boolean variable.

So I found a way to display it using the Ipython built-in functions:

from sympy import *
from IPython.display import display, Math

init_printing()
x, y, z = symbols("x y z")

x=y+z
display(Math('x = '+latex(x)))

I think that this is a more general solution to the problem.

Westernmost answered 21/4, 2020 at 17:12 Comment(1)
Doesn't work for me: KaTex parse error. Maybe my variable are badly namedKriskrischer
R
5

Although you first declare x as a sympy.Symbol, once you perform the assignment x=y+z, x becomes an alias for y+z. Whenever you use x from that point after, x will be automatically translated by python as y+z.

If you insist on this workflow, you could use Eq(S('x'),y+z) to display the equation.

Romie answered 25/11, 2016 at 16:39 Comment(4)
Is there a method off x that returns the equivalent of S('x')?Parrish
S() just converts a string to a sympy expression. You can just use Eq(x, y + z) here since x is already defined as a Symbol.Kigali
When I try this way it returns False instead of the expression itself.Westernmost
@iury simoes-sousa: use display(Eq(S('x'), y+z, evaluate=False)). The full expression will be shown, not 'True' or 'False'Cherie
C
0

I know this isn't exactly the answer, but for those just looking for a neat print of the right-hand-side of a function f(x,y,z,...), you can just do f.subs(x,x) like so:

import sympy as sp
x,y,z=sp.symbols('x,y,z')
f=x+2*y+3*sp.exp(z)
f.subs(x,x)
Coffeehouse answered 18/11, 2022 at 8:33 Comment(0)
T
0

If you keep getting True or False in your sympy equations:

Try to wrap either left or right hand side (lhs, rhs) of your equation expression with sympy.UnevaluatedExpr, and this should work. sympy.UnevaluatedExpr leaves the wrapped expression untouched when operated with other expressions, symbols, or numbers. GOOD LUCK!

# this code snippet is from a jupyterlab 
cell
import sympy as sp
x, y, z = sp.symbols('x, y, z')
x       = x.subs({x: y + z})
x       = sp.UnevaluatedExpr(x)
eq      = sp.Eq(x, y + z)
display(eq)

You get: y + z = y + z

Tobacconist answered 30/6, 2024 at 8:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.