Skip to content

Expressions and Substitutions

Expressions and Substitutions

Symbolic math variables can be combined into symbolic math expressions. Once in an expression, symbolic math variables can be exchanged with substituion.

Expressions

A symbolic math expression is a combination of symbolic math variables with numbers and mathematical operators such as +, -, / and *. The standard Python rules for calculating numbers apply in SymPy symbolic math expressions.

After the symbols x and y are created, a symbolic math expression using x and y can be defined.

In [1]:
from sympy import symbols

x, y = symbols('x y') expr = 2*x + y

Substitution

Use SymPy's .subs() method to insert a numerical value into a symbolic math expression. The first argument of the .subs() method is the mathematical symbol and the second argument is the numerical value. In the expression below:

2x + y

If we substitute:

x = 2

The resulting expression is:

2(2) + y $$ $$ 4 + y

We can code the substitution above using SymPy's .subs() method as shown below.

In [2]:
expr.subs(x, 2)

Out[2]:
$\displaystyle y + 4$

The .subs() method does not replace variables in place, .subs() only completes a one-time substitution. If expr is called after the .subs() method is applied, the original expr expression is returned.
In [3]:
expr

Out[3]:
$\displaystyle 2 x + y$

To make the substitution permanent, a new expression object needs to be assigned to the output of the .subs() method.
In [4]:
expr = 2*x + y
expr2 = expr.subs(x, 2)
expr2

Out[4]:
$\displaystyle y + 4$

SymPy variables can also be substituted into SymPy expressions. In the code section below, the symbol z is substituted for the symbol x (z replaces x).
In [5]:
x, y, z = symbols('x y z')
expr = 2*x + y
expr2 = expr.subs(x, z)
expr2

Out[5]:
$\displaystyle y + 2 z$

Expressions can also be substituted into other expressions. Consider the following:

y + 2x^2 + z^{-3}

substitute in

y = 2x

results in

2x + 2x^2 + z^{-3}
In [6]:
x, y, z = symbols('x y z')
expr = y + 2*x**2 + z**(-3)
expr2 = expr.subs(y, 2*x)
expr2
Out[6]:
$\displaystyle 2 x^{2} + 2 x + \frac{1}{z^{3}}$

A practical example involving symbolic math variables, expressions and substitutions can include a complex expression and several replacements.

n_0e^{-Q_v/RT}
n_0 = 3.48 \times 10^{-6}
Q_v = 12,700
R = 8.31
T = 1000 + 273

We can create four symbolic math variables and combine the variables into an expression with the code below.

In [7]:
from sympy import symbols, exp
n0, Qv, R, T = symbols('n0 Qv R T')
expr = n0exp(-Qv/(RT))

Multiple SymPy subs() methods can be chained together to substitute multiple variables in one line of code.
In [8]:
expr.subs(n0, 3.48e-6).subs(Qv,12700).subs(R, 8031).subs(T, 1000+273)

Out[8]:
$\displaystyle \frac{3.48 \cdot 10^{-6}}{e^{\frac{12700}{10223463}}}$

To evaluate an expression as a floating point number, use SymPy's .evalf() method.
In [9]:
expr2 = expr.subs(n0, 3.48e-6).subs(Qv,12700).subs(R, 8031).subs(T, 1000+273)

In [10]:
expr2.evalf()

Out[10]:
$\displaystyle 3.47567968697765 \cdot 10^{-6}$

You can control the number of digits the .evalf() method outputs by passing a number as an argument.
In [11]:
expr2.evalf(4)

Out[11]:
$\displaystyle 3.476 \cdot 10^{-6}$

Summary

The SymPy functions and methods used in this section are summarized in the table below.

SymPy function or method Description Example
symbols() create symbolic math variables x, y = symbols('x y')
.subs() substitute a value into a symbolic math expression expr.subs(x,2)
.evalf() evaluate a symbolic math expression as a floating point number expr.evalf()