Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a full expression in sympy?

Tags:

sympy

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).

like image 395
psychemedia Avatar asked Mar 11 '23 14:03

psychemedia


2 Answers

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.

like image 94
iury simoes-sousa Avatar answered May 13 '23 02:05

iury simoes-sousa


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.

like image 44
Stelios Avatar answered May 13 '23 02:05

Stelios