Here we give a (quick) introduction to SymPy. This tutorial assumes you are already familiar with SymPy expressions, so this notebook should serve as a refresher.
The function init_printing()
will enable LaTeX pretty printing in the notebook for SymPy expressions. We recommend calling it at the top of any notebook that uses SymPy.
from sympy import *
init_printing()
SymPy symbols are created with the symbols()
function. SymPy expressions are built up from symbols, numbers, and SymPy functions,
x, y, z = symbols('x y z')
SymPy automatically pretty prints symbols with greek letters and subscripts.
alpha1, omega_2 = symbols('alpha1 omega_2')
alpha1, omega_2
sin(x + 1) - cos(y)
Write a symbolic expression for $$\frac{1}{\sqrt{2\pi\sigma^2} } \; e^{ -\frac{(x-\mu)^2}{2\sigma^2} }.$$ Remember that the function for $e^x$ is exp(x)
. You will need to create symbols for sigma
and mu
. Square root is sqrt
.
# Write your answer here
Dividing two integers in Python creates a float, like 1/2
-> 0.5
. If you want a rational number, use Rational(1, 2)
or S(1)/2
.
x + 1/2
x + S(1)/2
^
is the XOR operator. Use **
for powers.
x ^ y
x ** y
All SymPy expressions are immutable. Functions that operate on an expression return a new expression.
expr = x + 1
expr
expr.subs(x, 2)
expr
Since most languages targeted will not support symbolic representation it is useful to let SymPy evaluate a floating point approximation (up to a user specified number of digits).
sqrt(2)
sqrt(2).evalf(7)
Compute 100 digits of $\pi$.
# Write your answer here
Undefined functions are created with Function()
. Undefined are useful to state that one variable depends on another (for the purposes of differentiation).
f = Function('f')
f(x) + 1
Derivatives are computed with the diff()
function, using the syntax diff(expr, var1, var2, ...)
.
diff(sin(x + 1)*cos(y), x, y)
diff(f(x) + 1, x)
Write an expression representing the wave equation in one dimension: $${\partial^2 u\over \partial t^2 } = c^2 { \partial^2 u\over \partial x^2}.$$ Remember that $u$ is a function in two variables. You can represent an equation using Eq
, like
Eq(x, y)
# Write your answer here
Matrices are created with Matrix
. A matrix can contain any symbolic expression. See Matrix?
for different ways to create a Matrix.
Matrix([[1, 2], [3, 4]])
# An unnested list will create a column vector
Matrix([1, 2, 3])
Matrix([x, y, z])
Matrices support all common operations, and have many methods for performing operations.
Matrix([[1, 2], [3, 4]])*Matrix([x, y])
for example, calculating the Jacobian matrix is as easy as:
Matrix([sin(x) + y, cos(y) + x, z]).jacobian([x, y, z])
and for those of you who don't remember, the Jacobian is defined as:
$$ J = \begin{bmatrix} \frac{\partial f_1}{\partial x_1} & \frac{\partial f_1}{\partial x_2} & \cdots \\ \frac{\partial f_2}{\partial x_1} & \frac{\partial f_2}{\partial x_2} & ~ \\ \vdots & ~ & \ddots \\ \end{bmatrix} $$
you will come across this mathematical entity in later notebooks in this tutorial.
Create the following matrix $$\left[\begin{matrix}1 & 0 & 1\\-1 & 2 & 3\\1 & 2 & 3\end{matrix}\right]$$
# Write your answer here
Now create a matrix representing $$\left[\begin{matrix}x\\y\\z\end{matrix}\right]$$ and multiply it with the previous matrix to get $$\left[\begin{matrix}x + z\\- x + 2 y + 3 z\\x + 2 y + 3 z\end{matrix}\right].$$
# Write your answer here
Now take the Jacobian of that matrix with respect to your column vector, to get the original matrix back.
# Write your answer here
SymPy can also operate on matrices of symbolic dimension ($n \times m$). MatrixSymbol("M", n, m)
creates a matrix $M$ of shape $n \times m$.
n, m = symbols('n m', integer=True)
M = MatrixSymbol("M", n, m)
b = MatrixSymbol("b", m, 1)
M*b
(M*b).shape
Some matrix expression functions do not evaluate unless you call doit
.
Transpose(M*b)
Transpose(M*b).doit()
Write a matrix expression representing $$Au + Bv,$$ where $A$ and $B$ are $100\times 100$ and $u$ and $v$ are $100 \times 1$.
# Write your answer here
Indexed symbols can be created with IndexedBase
and Idx
. IndexedBase("A")
represents an array A
and Idx('i')
represents an index i
. You can also use symbols('i')
instead of Idx('i')
.
A = IndexedBase("A")
i = Idx('i')
A[i]
Write an Indexed expression for $$A[i, j, k]$$
# Write your answer here
A useful tool in your toolbelt when manipulating expressions is the solve
function. solve
solves equations symbolically (not numerically). The return value is a list of solutions.
You can give solve
an Eq
, or if you give it an expression, it automatically assumes that it is equal to 0.
solve(Eq(x**2, 4), x)
solve(x**2 + 3*x - 3, x)
It can also handle systems of equations. The return is a list of dictionaries, mapping symbols to solutions.
eq1 = x**2 + y**2 - 4 # circle of radius 2
eq2 = 2*x + y - 1 # straight line: y(x) = -2*x + 1
solve([eq1, eq2], [x, y])
Solve the following system of equations: $$\begin{align}z &= x^2 - y^2\\z^2 &= x^2 + y^2 + 4\\z &= x + y\end{align}$$
# Write your answer here
If you are dealing with a differential equation, say:
$$ f''(x) = -f(x) $$
SymPy's dsolve can (sometimes) produce an exact symbolic solution. Like solve
, dsolve
assumes that expressions are equal to 0.
f = Function('f')
dsolve(f(x).diff(x, 2) + f(x))
Solve the following ODE: $$f''(x) + 2f'(x) + f(x) = \sin(x)$$
# Write your answer here