Intro to SymPy

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.

Expressions

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.


In [ ]:
from sympy import *
init_printing()

SymPy symbols are created with the symbols() function. SymPy expressions are built up from symbols, numbers, and SymPy functions,


In [ ]:
x, y, z = symbols('x y z')

SymPy automatically pretty prints symbols with greek letters and subscripts.


In [ ]:
alpha1, omega_2 = symbols('alpha1 omega_2')
alpha1, omega_2

In [ ]:
sin(x + 1) - cos(y)

Exercise

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.


In [ ]:
# Write your answer here

Some reminders on common gotchas:

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.


In [ ]:
x + 1/2

In [ ]:
x + S(1)/2

^ is the XOR operator. Use ** for powers.


In [ ]:
x ^ y

In [ ]:
x ** y

All SymPy expressions are immutable. Functions that operate on an expression return a new expression.


In [ ]:
expr = x + 1
expr

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

In [ ]:
expr

Floating point representation

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


In [ ]:
sqrt(2)

In [ ]:
sqrt(2).evalf(7)

Exercise

Compute 100 digits of $\pi$.


In [ ]:
# Write your answer here

Undefined functions

Undefined functions are created with Function(). Undefined are useful to state that one variable depends on another (for the purposes of differentiation).


In [ ]:
f = Function('f')

In [ ]:
f(x) + 1

Derivatives

Derivatives are computed with the diff() function, using the syntax diff(expr, var1, var2, ...).


In [ ]:
diff(sin(x + 1)*cos(y), x, y)

In [ ]:
diff(f(x) + 1, x)

Exercise

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


In [ ]:
Eq(x, y)

In [ ]:
# Write your answer here

Matrices

Matrices are created with Matrix. A matrix can contain any symbolic expression. See Matrix? for different ways to create a Matrix.


In [ ]:
Matrix([[1, 2], [3, 4]])

In [ ]:
# An unnested list will create a column vector
Matrix([1, 2, 3])

In [ ]:
Matrix([x, y, z])

Matrices support all common operations, and have many methods for performing operations.


In [ ]:
Matrix([[1, 2], [3, 4]])*Matrix([x, y])

for example, calculating the Jacobian matrix is as easy as:


In [ ]:
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.

Exercise

Create the following matrix $$\left[\begin{matrix}1 & 0 & 1\\-1 & 2 & 3\\1 & 2 & 3\end{matrix}\right]$$


In [ ]:
# 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].$$


In [ ]:
# Write your answer here

Now take the Jacobian of that matrix with respect to your column vector, to get the original matrix back.


In [ ]:
# Write your answer here

Matrix symbols

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


In [ ]:
n, m = symbols('n m', integer=True)
M = MatrixSymbol("M", n, m)
b = MatrixSymbol("b", m, 1)

In [ ]:
M*b

In [ ]:
(M*b).shape

Some matrix expression functions do not evaluate unless you call doit.


In [ ]:
Transpose(M*b)

In [ ]:
Transpose(M*b).doit()

Exercise

Write a matrix expression representing $$Au + Bv,$$ where $A$ and $B$ are $100\times 100$ and $u$ and $v$ are $100 \times 1$.


In [ ]:
# Write your answer here

Indexed

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


In [ ]:
A = IndexedBase("A")
i = Idx('i')

In [ ]:
A[i]

Exercise

Write an Indexed expression for $$A[i, j, k]$$


In [ ]:
# Write your answer here

Solving systems of equations

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.


In [ ]:
solve(Eq(x**2, 4), x)

In [ ]:
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.


In [ ]:
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])

Exercises

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}$$


In [ ]:
# Write your answer here

Solving differential equations

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.


In [ ]:
f = Function('f')

In [ ]:
dsolve(f(x).diff(x, 2) + f(x))

Exercise

Solve the following ODE: $$f''(x) + 2f'(x) + f(x) = \sin(x)$$


In [ ]:
# Write your answer here