Skip to content

Instantly share code, notes, and snippets.

@ananthp
Last active November 28, 2017 12:23
Show Gist options
  • Save ananthp/ff2d33a98931cb48c51f0d8ec170a79d to your computer and use it in GitHub Desktop.
Save ananthp/ff2d33a98931cb48c51f0d8ec170a79d to your computer and use it in GitHub Desktop.
sympy: Symbols and their expressions
# using a symbol and associated expression in sympy
from sympy import *
init_printing()
i, L = symbols('i, L')
s_beta = symbols('beta')
e_beta = i * pi / L
Eq(s_beta, e_beta)

Sympy: symbols vs expressions

Consider this: β=iπ/L

If we want to model this in sympy, we need both the symbol β and the expression iπ/L.

If we also declare beta as a symbol, beta=symbols('beta'), we get a nice symbol, that prints as β. But when if we try to assign the expression to it, beta = i * pi * L, a disconnect between variable beta and the symbol beta (β) happens. Python variable beta is now overwritten with the expression, and doesn't point to symbol β anymore.

Solution: Define both symbol and the associated expression in a consistant manner.

s_beta = symbols('beta')    # symbol beta
e_beta = i * pi / L         # expression beta

This will give us power to use the symbol and the expression in any calculation.

Run the code in sympy live.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment