In [1]:
from sympy import *
init_printing(use_latex='mathjax')
x, y, z = symbols('x,y,z')
r, theta = symbols('r,theta', positive=True)

Matrices

The SymPy Matrix object helps us with small problems in linear algebra.


In [2]:
rot = Matrix([[r*cos(theta), -r*sin(theta)],
              [r*sin(theta),  r*cos(theta)]])
rot


Out[2]:
$$\left[\begin{matrix}r \cos{\left (\theta \right )} & - r \sin{\left (\theta \right )}\\r \sin{\left (\theta \right )} & r \cos{\left (\theta \right )}\end{matrix}\right]$$

Standard methods


In [4]:
rot.det()


Out[4]:
$$r^{2} \sin^{2}{\left (\theta \right )} + r^{2} \cos^{2}{\left (\theta \right )}$$

In [5]:
rot.inv()


Out[5]:
$$\left[\begin{matrix}- \frac{\sin^{2}{\left (\theta \right )}}{r \cos{\left (\theta \right )}} + \frac{1}{r \cos{\left (\theta \right )}} & \frac{1}{r} \sin{\left (\theta \right )}\\- \frac{1}{r} \sin{\left (\theta \right )} & \frac{1}{r} \cos{\left (\theta \right )}\end{matrix}\right]$$

In [6]:
rot.singular_values()


Out[6]:
$$\left [ r, \quad r\right ]$$

Exercise

Find the inverse of the following Matrix:

$$ \left[\begin{matrix}1 & x\\y & 1\end{matrix}\right] $$

In [7]:
# Create a matrix and use the `inv` method to find the inverse

Operators

The standard SymPy operators work on matrices.


In [8]:
rot * 2


Out[8]:
$$\left[\begin{matrix}2 r \cos{\left (\theta \right )} & - 2 r \sin{\left (\theta \right )}\\2 r \sin{\left (\theta \right )} & 2 r \cos{\left (\theta \right )}\end{matrix}\right]$$

In [9]:
rot * rot


Out[9]:
$$\left[\begin{matrix}- r^{2} \sin^{2}{\left (\theta \right )} + r^{2} \cos^{2}{\left (\theta \right )} & - 2 r^{2} \sin{\left (\theta \right )} \cos{\left (\theta \right )}\\2 r^{2} \sin{\left (\theta \right )} \cos{\left (\theta \right )} & - r^{2} \sin^{2}{\left (\theta \right )} + r^{2} \cos^{2}{\left (\theta \right )}\end{matrix}\right]$$

In [10]:
v = Matrix([[x], [y]])
v


Out[10]:
$$\left[\begin{matrix}x\\y\end{matrix}\right]$$

In [11]:
rot * v


Out[11]:
$$\left[\begin{matrix}r x \cos{\left (\theta \right )} - r y \sin{\left (\theta \right )}\\r x \sin{\left (\theta \right )} + r y \cos{\left (\theta \right )}\end{matrix}\right]$$

Exercise

In the last exercise you found the inverse of the following matrix


In [12]:
M = Matrix([[1, x], [y, 1]])
M


Out[12]:
$$\left[\begin{matrix}1 & x\\y & 1\end{matrix}\right]$$

In [13]:
M.inv()


Out[13]:
$$\left[\begin{matrix}\frac{x y}{- x y + 1} + 1 & - \frac{x}{- x y + 1}\\- \frac{y}{- x y + 1} & \frac{1}{- x y + 1}\end{matrix}\right]$$

Now verify that this is the true inverse by multiplying the matrix times its inverse. Do you get the identity matrix back?


In [14]:
# Multiply `M` by its inverse.  Do you get back the identity matrix?

Exercise

What are the eigenvectors and eigenvalues of M?


In [15]:
# Find the methods to compute eigenvectors and eigenvalues. Use these methods on `M`

NumPy-like Item access


In [14]:
rot[0, 0]


Out[14]:
$$r \cos{\left (\theta \right )}$$

In [15]:
rot[:, 0]


Out[15]:
$$\left[\begin{matrix}r \cos{\left (\theta \right )}\\r \sin{\left (\theta \right )}\end{matrix}\right]$$

In [17]:
rot[1, :]


Out[17]:
$$\left[\begin{matrix}r \sin{\left (\theta \right )} & r \cos{\left (\theta \right )}\end{matrix}\right]$$

Mutation

We can change elements in the matrix.


In [18]:
rot[0, 0] += 1
rot


Out[18]:
$$\left[\begin{matrix}r \cos{\left (\theta \right )} + 1 & - r \sin{\left (\theta \right )}\\r \sin{\left (\theta \right )} & r \cos{\left (\theta \right )}\end{matrix}\right]$$

In [19]:
simplify(rot.det())


Out[19]:
$$r \left(r + \cos{\left (\theta \right )}\right)$$

In [20]:
rot.singular_values()


Out[20]:
$$\left [ \sqrt{r^{2} + r \cos{\left (\theta \right )} + \frac{1}{2} \sqrt{4 r^{2} + 4 r \cos{\left (\theta \right )} + 1} + \frac{1}{2}}, \quad \sqrt{r^{2} + r \cos{\left (\theta \right )} - \frac{1}{2} \sqrt{4 r^{2} + 4 r \cos{\left (\theta \right )} + 1} + \frac{1}{2}}\right ]$$

Exercise

Play around with your matrix M, manipulating elements in a NumPy like way. Then try the various methods that we've talked about (or others). See what sort of answers you get.


In [21]:
# Play with matrices