#!/usr/bin/env python # coding: utf-8 # In[ ]: 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[ ]: rot = Matrix([[r*cos(theta), -r*sin(theta)], [r*sin(theta), r*cos(theta)]]) rot # ### Standard methods # In[ ]: rot.det() # In[ ]: rot.inv() # In[ ]: rot.singular_values() # ### Exercise # # Find the inverse of the following Matrix: # # $$ \left[\begin{matrix}1 & x\\y & 1\end{matrix}\right] $$ # In[ ]: # Create a matrix and use the `.inv` method to find the inverse # ### Operators # # The standard SymPy operators work on matrices # In[ ]: rot * 2 # In[ ]: rot * rot # In[ ]: v = Matrix([[x], [y]]) v # In[ ]: rot * v # ### Exercise # # In the last exercise you found the inverse of the following matrix # In[ ]: M = Matrix([[1, x], [y, 1]]) M # In[ ]: M.inv() # Now verify that this is the true inverse by multiplying the matrix times its inverse. Do you get the identity matrix back? # In[ ]: # Multiply `M` by its inverse. Do you get back the identity matrix? # ### Exercise # # What are the eigenvectors and eigenvalues of `M`? # In[ ]: # Find the methods to compute eigenvectors and eigenvalues. Use these methods on `M` # ### NumPy-like Item access # In[ ]: rot[0, 0] # In[ ]: rot[:, 0] # In[ ]: rot[1, :] # ### Mutation # # We can change elements in the matrix. # In[ ]: rot[0, 0] += 1 rot # In[ ]: simplify(rot.det()) # In[ ]: rot.singular_values() # ### 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[ ]: # Play with matrices