Dr Jon Shiach, Department of Computing and Mathematics, Manchester Metropolitan University
On successful completion of this page readers will be able to:
The Python library SymPy allows us to use a suite of functions for symbolically evaluating mathematical expressions. Before we can use SymPy functions we need to import the libary using the following
from sympy import *
This command will import all SymPy functions so that we can use them without the sympy.
prefix. Execute the cell cell above to import all functions from the SymPy library.
The first thing we need to do when working with symbolic mathematics is to define any variable and function that we want Python to treat symbolically. This is done using the symbols
and function
commands
variable1, variable2 = symbols('variable1, variable2')
function = Function('function')(variable1, variable2)
This defines the variables variable1
and variable2
as symbolic variables and function
as a symbolic function expressed in terms of variable1
and variable2
. Once symbolic variables and functions are defined they can be used to create symbolic expressions.
The commands below declare symbolic variable x
and the function $f(x) = e^x + \cos(x)$
x = symbols('x')
f = Function('f')(x)
f = exp(x) + cos(x)
f
Enter them into the code cell below and execute it.
x = symbols('x')
f = Function('f')(x)
f = exp(x) + cos(x)
f
We can also define the function $g(x) = x^2 + 1$ and use the subs
command to substitute functions into one another to form composite functions.
g = Function('g')(x)
g = x ** 2 + 1
g
g = Function('g')(x)
g = x ** 2 + 1
g
To evaluate the composite function $f\circ g$ we substitute g
in place of the x
variable in the function f
f.subs(x, g)
f.subs(x, g)
Or to evaluate $g\circ f$ we substitute f
in place of x
in g
g.subs(x, f)
g.subs(x, f)
y = symbols('y')
expand((x + y) ** 4)
factor(x ** 3 - 3 * x ** 2 + 3 * x - 1)
To split up a fraction of the form $\dfrac{f(x)}{g(x)}$ where $f$ and $g$ are polynomial functions we can use the apart
command
apart(expression)
The commands below determine the partial fraction decomposition of the following expression
\begin{align*} \frac{3x+1}{x^3 - 3x+ 2}. \end{align*}apart((3 * x + 1) / (x ** 3 - 3 * x + 2))
apart((3 * x + 1) / (x ** 3 - 3 * x + 2))
expand
command to expand the following expressions:(a) $(x + 2)^2$;
expand((x + 2) ** 2)
(b) $(x + 1)(ax^2 + aby + cxy)$
a, b, c = symbols("a, b, c")
expand((x + 1) * (a * x ** 2 + a * b * y + c * x * y))
(c) $(x + y + z)^3$.
z = symbols("z")
expand((x + y + z) ** 3)
factor
command to simplify the following expressions:(a) $x^3 + 4x^2 + 4x$;
factor(x ** 3 + 4 * x ** 2 + 4 * x)
(b) $x^4 - 12x^3 + 54x^2 - 108x + 81$;
factor(x ** 4 - 12 * x ** 3 + 54 * x ** 2 - 108 * x + 81)
(c) $\dfrac{2x}{x^2-4x+4} + \dfrac{1}{x^2 - 4x + 4} + \dfrac{x^2}{x^2 - 4x + 4}$
factor(2 * x / (x ** 2 - 4 * x + 4) + 1 / (x ** 2 - 4 * x + 4) + x ** 2 / (x ** 2 - 4 * x + 4))
f = Function("f")(x)
g = Function("g")(x)
h = Function("h")(x)
f = -2 * x - 3
g = x ** 2 + 3 * x + 1
h = x / 2
f.subs(x, g.subs(x, h))
(b) $g \circ f \circ h$;
g.subs(x, f.subs(x, h))
(c) $h \circ g \circ f$;
h.subs(x, g.subs(x, f))
(d) $f \circ f$;
f.subs(x, f)
(e) $g \circ g$;
g.subs(x, g)
(f) $h \circ h$.
h.subs(x, h)
(a) $\dfrac{13t - 23}{2(3t - 1)(t - 3)}$;
t = symbols("t")
apart((13 * t - 23) / (2 * (3 * t - 1) * (t - 3)))
(b) $-\dfrac{9t^2 + 10t + 15}{3(t^2 + 1)(3t - 5)}$;
apart(-(9 * t ** 2 + 10 * t + 15) / (3 * (t ** 2 + 1) * (3 * t - 5)))
(c) $\dfrac{2t^2 - 11 t + 14}{(5t - 4) (t + 1)^2}$;
apart((2 * t ** 2 - 11 * t + 14) / ((5 * t - 4) * (t + 1) ** 2))
(d) $-\dfrac{25 t + 14}{ (2t^2 + 5t + 5)(t-4)}$.
apart(-(25 * t + 14) / ((2 * t ** 2 + 5 * t + 5) * (t - 4)))
To evaluate the derivative of the function $f(x)$ with respect to $x$
\begin{align*} \dfrac{d}{dx}f(x) = \lim_{\Delta x \to 0} \frac{f(x + \Delta x) - f(x)}{\Delta x}, \end{align*}we can use the diff
command.
diff(function, variable)
Where function
is a symbolic function which is differentiated with respect to the variable
and n
is an optional argument which is the order of the derivative.
The commands below evaluate the first, second and third order derivatives of the function $f(x) = 2x^3$. Enter them into the code cells below and execute each one.
f = Function('f')(x)
f = 2 * x ** 3
diff(f, x)
f = Function('f')(x)
f = 2 * x ** 3
diff(f, x)
diff(f, x, 2)
diff(f, x, 2)
diff(f, x, 3)
diff(f, x, 3)
The commands below produce the product and quotient rules for differentiation of single variable functions
\begin{align*} \frac{d}{dx}f(x)g(x) &= f(x)\frac{d}{dx}g(x) + g(x) \frac{d}{dx}f(x), \\ \frac{d}{dx}\left(\frac{f(x)}{g(x)}\right) &= \frac{g(x) \dfrac{d}{dx}f(x) - f(x) \dfrac{d}{dx}g(x)}{g(x)^2}, \\ \frac{d}{dx}f(g(x)) &= \frac{d}{dg(x)}f(x) \frac{d}{dx}g(x). \end{align*}Enter each one into the code cell below and execute each one.
f = Function('f')(x)
g = Function('g')(x)
diff(f * g, x)
f = Function('f')(x)
g = Function('g')(x)
diff(f * g, x)
simplify(diff(f / g, x))
simplify(diff(f / g, x))
diff(f.subs(x, g))
diff(f.subs(x, g))
To evaluate the integral of the function $f(x)$ with respect to $x$,
\begin{align*} F(x) = \int f(x) dx, \end{align*}we can use the int
command
integral(function, variable)
The command below evaluates the integral
\begin{align*} \int 2x^3 dx = \frac{x^4}{2} + c. \end{align*}integrate(2 * x ** 3, x)
integrate(2 * x ** 3, x)
Note that Python does not include the constant of integration in the output.
To calculate the value of the definite integral
\begin{align*} \int_a^b f(x) dx = [F(x)]_a^b = F(b) - F(a), \end{align*}we specify the limits of integration $a$ and $b$ in the integrate
command
integrate(function, (variable, a, b) )
The commands below evaluates the definite integral
\begin{align*} \int_{\pi/4}^{\pi/2} \cos(x) dx = \sin\left(\frac{\pi}{2}\right) - \sin\left(\frac{\pi}{4}\right) = 1 - \frac{\sqrt{2}}{2}. \end{align*}integrate(cos(x), (x, pi/4, pi/2) )
integrate(cos(x), (x, pi/4, pi/2) )
diff
command to obtain the derivatives of the following functions(a) $f(x) = x^5 + 3x^4 - 5x^2 + 10$;
diff(x ** 5 + 3 * x ** 4 - 5 * x ** 2 + 10, x)
(b) $g(x) = 3x^{1/2} - x^{3/2} + 2x^{-1/2}$; (hint: use Rational(a, b)
to ensure fractional values in the output)
diff(3 * x ** Rational(1, 2) - x ** Rational(3, 2) + 2 * x ** -Rational(1, 2), x)
(c) $\lambda(t) = \sqrt{3t} + 3\sqrt{t}$;
diff(sqrt(3 * t) + 3 * sqrt(t), t)
(d) $\mu(y) = \left(2 + 5y + \dfrac{1}{2}y^2\right)^{1/2}$;
diff(sqrt(2 + 5 * y + 1 / 2 * y ** 2), y)
(e) $\phi(z) = (z + 1) \sqrt{z^4 - 2z + 2}$
diff((z + 1) * sqrt(z ** 4 - 2 * z + 2), z)
(f) $h(x) = \dfrac{x}{\sqrt{1 - 4x^2}}$;
diff(x / sqrt(1 - 4 * x ** 2))
integrate
command to determine the following integrals:(a) $\int 4x^3 + 2x^2 - 3x + 4 \, dx$;
integrate(4 * x ** 3 + 2 * x ** 2 - 4 * x + 4, x)
(b) $\int \cos(x) \sin(x)\, dx$;
integrate(cos(x) * sin(x), x)
(c) $\int x + e^{x^{1/3}} \, dx$;
integrate(x + exp(x ** Rational(1, 3)), x)
(d) $\displaystyle \int_1^3 x^2 + \cos(x) \, dx$;
integrate(x ** 2 + cos(x), (x, 1, 3))
(e) $\displaystyle \int_0^{\pi/2} x^3 \cos(x) \, dx$;
integrate(x ** 3 * cos(x), (x, 0, pi / 2))
We can use Python to solve equations or systems of equations symbolically using the solve
command
solve((equation1, equation2, ...))
Where equation1
, equation2
etc. are equations written using symbolic variables and variable1
, variable2
etc. are the variables that are being solved. The equations being solved are written such that they are equal to zero, e.g., for the equation $x + y = 1$
x + y - 1
The commands below solve the equation $y = 2x - 5$ for $x$.
solve(y - 2 * x + 5)
solve(y - 2 * x + 5, x)
[y/2 + 5/2]
The commands below solves the following linear system of equations
\begin{align*} x + y + z &= 0, \\ 2x + z &= -1, \\ x - 2y + 3z &= -12. \end{align*}eqn1 = x + y + z
eqn2 = 2 * x + z + 1
eqn3 = x - 2 * y + 3 * z + 12
solve( (eqn1, eqn2, eqn3))
Enter them into the code cell below and execute it
eqn1 = x + y + z
eqn2 = 2 * x + z + 1
eqn3 = x - 2 * y + 3 * z + 12
solve( (eqn1, eqn2, eqn3), (x, y, z))
{x: 1, y: 2, z: -3}
The solve
command can also solve non-linear equations. The commands below derive the quadratic formula used to find the roots of the quadratic equation $f(x) = ax^2 + bx + c$ which is
solve(a * x ** 2 + b * x + c, x)
solve(a * x ** 2 + b * x + c, x)
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
To solve differential equations (where solutions exist) we can use the dsolve
command.
dsolve(diffeq, function)
Where diffeq
is a differential equation defined using the Eq
command and function
is the solution function. Eq
is used specify an equivalance relationship between two objects for example
Eq(a, b)
specifies that a
and b
are equivalent. So to define a differential equation we can use
diffeq = Eq(lhs, rhs)
The commands below determine the solution to the simplest differential equation
\begin{align*} \frac{d}{dt}y(t) &= ay & &\implies & y(t) &= C e^{at}. \end{align*}y = Function('y')(t)
diffeq = Eq(diff(y, t), a * y)
dsolve(diffeq, y)
Enter them into the code cell below and execute it.
y = Function('y')(t)
diffeq = Eq(diff(y, t), a * y)
dsolve(diffeq, y)
The commands below use dsolve
to solve the second-order ODE
y = Function('y')(x)
diffeq = Eq(diff(y, x, 2) + diff(y, x) + y, 0)
expand(dsolve(diffeq, y))
y = Function('y')(x)
diffeq = Eq(diff(y, x, 2) + diff(y, x) + y, 0)
expand(dsolve(diffeq, y))
The commands below use dsolve
to solve the following system of ODEs
Enter them into the code cell below and run the live script.
y = Function('y')(t)
z = Function('z')(t)
diffeq1 = diff(y, t, 1) - z
diffeq2 = diff(z, t, 1) + y
dsolve((diffeq1, diffeq2))
y = Function('y')(t)
z = Function('z')(t)
diffeq1 = diff(y, t, 1) - z
diffeq2 = diff(z, t, 1) + y
dsolve((diffeq1, diffeq2))
[Eq(y(t), C1*sin(t) + C2*cos(t)), Eq(z(t), C1*cos(t) - C2*sin(t))]
solve
command to find the solution to the following linear system of equationsx, y, z = symbols('x, y, z')
eq1 = x + y + z
eq2 = 2 * x + 3 * y + z - 1
eq3 = -x - 2 * y + 3 * z - 2
solve((eq1, eq2, eq3))
{x: -3, y: 2, z: 1}
p, q = symbols('p, q')
eq1 = p ** 2 + q ** 2 - 10
eq2 = 2 * p + q - 1
solve((eq1, eq2))
[{p: -1, q: 3}, {p: 9/5, q: -13/5}]
dsolve
command to find the solutions to the following system of differential equationsy = Function('y')(t)
z = Function('z')(t)
diffeq1 = diff(y, t, 1) - 4 * y - 7 * z
diffeq2 = diff(z, t, 1) + 2 * y - 5 * z
dsolve((diffeq1, diffeq2))
[Eq(y(t), (C1/4 + sqrt(55)*C2/4)*exp(9*t/2)*cos(sqrt(55)*t/2) + (sqrt(55)*C1/4 - C2/4)*exp(9*t/2)*sin(sqrt(55)*t/2)), Eq(z(t), C1*exp(9*t/2)*cos(sqrt(55)*t/2) - C2*exp(9*t/2)*sin(sqrt(55)*t/2))]