Linear algebra is the study of vectors and linear transformations. This notebook introduces concepts form linear algebra in a birds-eye overview. The goal is not to get into the details, but to give the reader a taste of the different types of thinking: computational, geometrical, and theoretical, that are used in linear algebra.
In [1]:
# setup SymPy
from sympy import *
x, y, z, t = symbols('x y z t')
init_printing()
# a vector is a special type of matrix (an n-vector is either a nx1 or a 1xn matrix)
Vector = Matrix # define alias Vector so I don't have to explain this during video
Point = Vector # define alias Point for Vector since they're the same thing
# setup plotting
%matplotlib inline
import matplotlib.pyplot as mpl
from util.plot_helpers import plot_vec, plot_vecs, plot_line, plot_plane, autoscale_arrows
Linear algebra builds upon high school math concepts like:
In [ ]:
In [2]:
# define two vectors
u = Vector([2,3])
v = Vector([3,0])
u
Out[2]:
In [3]:
v
Out[3]:
In [4]:
plot_vecs(u, v)
autoscale_arrows()
In [5]:
# algebraic
u+v
Out[5]:
In [6]:
# graphical
plot_vecs(u, v)
plot_vec(v, at=u, color='b')
plot_vec(u+v, color='r')
autoscale_arrows()
When we describe the vector as the coordinate pair $(4,6)$, we're implicitly using the standard basis $B_s = \{ \hat{\imath}, \hat{\jmath} \}$. The vector $\hat{\imath} \equiv (1,0)$ is a unit-length vector in the $x$-direciton, and $\hat{\jmath} \equiv (0,1)$ is a unit-length vector in the $y$-direction.
To be more precise when referring to vectors, we can indicate the basis as a subscript of every cooridnate vector $\vec{v}=(4,6)_{B_s}$, which tells $\vec{v}= 4\hat{\imath}+6\hat{\jmath}=4(1,0) +6(0,1)$.
In [7]:
# the standard basis
ihat = Vector([1,0])
jhat = Vector([0,1])
v = 4*ihat + 6*jhat
v
Out[7]:
In [8]:
# geomtrically...
plot_vecs(ihat, jhat, 4*ihat, 6*jhat, v)
autoscale_arrows()
The same vector $\vec{v}$ will correspond to the a different pair of coefficients if a differebt basis is used. For example, if we use the basis $B^\prime = \{ (1,1), (1,-1) \}$, the same vector $\vec{v}$ must be expressed as $\vec{v} = 5\vec{b}_1 +(-1)\vec{b}_2=(5,-1)_{B^\prime}$.
In [9]:
# another basis B' = { (1,1), (1,-1) }
b1 = Vector([ 1, 1])
b2 = Vector([ 1, -1])
v = 5*b1 + (-1)*b2
v
# How did I know 5 and -1 are the coefficients w.r.t basis {b1,b2}?
# Matrix([[1,1],[1,-1]]).inv()*Vector([4,6])
Out[9]:
In [10]:
# geomtrically...
plot_vecs(b1, b2, 5*b1, -1*b2, v)
autoscale_arrows()
In linear algebra we'll extend the notion of funttion $f:\mathbb{R}\to \mathbb{R}$, to functions that act on vectors called linear transformations. We can understand the properties of linear transformations $T$ in analogy with ordinary functions:
\begin{align*} \textrm{function } f:\mathbb{R}\to \mathbb{R} & \ \Leftrightarrow \, \begin{array}{l} \textrm{linear transformation } T:\mathbb{R}^{n}\! \to \mathbb{R}^{m} \end{array} \\ \textrm{input } x\in \mathbb{R} & \ \Leftrightarrow \ \textrm{input } \vec{x} \in \mathbb{R}^n \\ \textrm{output } f(x) \in \mathbb{R} & \ \Leftrightarrow \ \textrm{output } T(\vec{x})\in \mathbb{R}^m \\ g\circ\! f \: (x) = g(f(x)) & \ \Leftrightarrow \ % \textrm{matrix product } S(T(\vec{x})) \\ \textrm{function inverse } f^{-1} & \ \Leftrightarrow \ \textrm{inverse transformation } T^{-1} \\ \textrm{zeros of } f & \ \Leftrightarrow \ \textrm{kernel of } T \\ \textrm{image of } f & \ \Leftrightarrow \ \begin{array}{l} \textrm{image of } T \end{array} \end{align*}Equivalence between linear transformstions $T$ and matrices $M_T$:
$$ T : \mathbb{R}^n \to \mathbb{R}^m \qquad \Leftrightarrow \qquad M_T \in \mathbb{R}^{m \times n} $$$$ \vec{y} = T(\vec{x}) \qquad \Leftrightarrow \qquad \vec{y} = M_T\vec{x} $$
In [11]:
# represent as an augmented matrix
AUG = Matrix([
[1, 2, 5],
[3, 9, 21]])
AUG
Out[11]:
In [12]:
# eliminate x_1 in second equation by subtracting 3x times the first equation
AUG[1,:] = AUG[1,:] - 3*AUG[0,:]
AUG
Out[12]:
In [13]:
# simplify second equation by dividing by 3
AUG[1,:] = AUG[1,:]/3
AUG
Out[13]:
In [14]:
# eliminate x_2 from first equation by subtracting 2x times the second equation
AUG[0,:] = AUG[0,:] - 2*AUG[1,:]
AUG
Out[14]:
This augmented matrix is in reduced row echelon form (RREF), and corresponds to the system of equations:
\begin{align*} 1x_1 \ \ \qquad &= 1 \\ 1x_2 &= 2, \end{align*}so the the solution is $x_1=1$ and $x_2=2$.
page 150
In [15]:
a,b,c,d,e,f, g,h,i,j = symbols('a b c d e f g h i j')
A = Matrix([[a,b],
[c,d],
[e,f]])
B = Matrix([[g,h],
[i,j]])
A, B
Out[15]:
In [16]:
A*B
Out[16]:
In [17]:
def mat_prod(A, B):
"""Compute the matrix product of matrices A and B."""
assert A.cols == B.rows, "Error: matrix dimensions not compatible."
m, ell = A.shape # A is a m x ell matrix
ell, n = B.shape # B is a ell x n matrix
C = zeros(m,n)
for i in range(0,m):
for j in range(0,n):
C[i,j] = A[i,:].dot(B[:,j])
return C
mat_prod(A,B)
Out[17]:
In [18]:
# mat_prod(B,A)
In [19]:
a,b,c,d = symbols('a b c d')
A = Matrix([[a,b],
[c,d]])
A.det()
Out[19]:
In [20]:
# Consider the parallelogram with sides:
u1 = Vector([3,0])
u2 = Vector([2,2])
plot_vecs(u1,u2)
plot_vec(u1, at=u2, color='k')
plot_vec(u2, at=u1, color='b')
autoscale_arrows()
# What is the area of this parallelogram?
In [21]:
# base = 3, height = 2, so area is 6
In [22]:
# Compute the area of the parallelogram with sides u1 and u2 using the deteminant
A = Matrix([[3,0],
[2,2]])
A.det()
Out[22]:
In [23]:
A = Matrix([[1, 2],
[3, 9]])
A
Out[23]:
In [24]:
# Compute deteminant to check if inverse matrix exists
A.det()
# if non-zero, then inverse exists
Out[24]:
In [25]:
A.inv()
Out[25]:
In [26]:
A.inv()*A
Out[26]:
In [27]:
A.adjugate() / A.det()
Out[27]:
In [28]:
AUG = A.row_join(eye(2))
AUG
Out[28]:
In [29]:
# performd row operations
AUG[1,:] = AUG[1,:] - 3*AUG[0,:]
AUG[1,:] = AUG[1,:]/3
AUG[0,:] = AUG[0,:] - 2*AUG[1,:]
AUG
Out[29]:
In [30]:
AUG[:,2:5]
Out[30]:
In [31]:
E1 = eye(2)
E1[1,:] = E1[1,:] - 3*E1[0,:]
E2 = eye(2)
E2[1,:] = E2[1,:]/3
E3 = eye(2)
E3[0,:] = E3[0,:] - 2*E3[1,:]
E1,E2,E3
Out[31]:
In [32]:
# the sequence of three row operations transforms the matrix A into RREF
E3*E2*E1*A
Out[32]:
Recall definition $A^{-1}A=\mathbb{1}$, and we just observed that $E_3E_2E_1 A =\mathbb{1}$, so it must be that $A^{-1}=E_3E_2E_1$.
In [33]:
E3*E2*E1
Out[33]:
In [34]:
p = Point([2,4,5])
p
Out[34]:
In [35]:
po = Point([1,1,1])
v = Vector([1,1,0])
plot_line(v, po)
A plane is a two-dimensional infinite subset of $\mathbb{R}^3$ that can be described in one of three ways:
The general equation:
$$ P: \left\{ \, Ax+By+Cz=D \, \right\} $$The parametric equation:
$$ P: \{ p_{\textrm{o}}+s\,\vec{v} + t\,\vec{w}, \ \forall s,t \in \mathbb{R} \}, $$which defines a plane that that contains the point $p_{\textrm{o}}$ and the vectors $\vec{v}$ and $\vec{w}$.
Or the geometric equation:
$$ P: \left\{ \vec{n} \cdot [ (x,y,z) - p_{\textrm{o}} ] = 0 \,\right\}, $$which defines a plane that contains point $p_{\textrm{o}}$ and has normal vector $\hat{n}$.
In [36]:
# plot plane 2x + y + z = 5
normal = Vector([2, 1, 1])
D = 5
plot_plane(normal, D)
A projection of the vector $\vec{v}$ in the direction $\vec{d}$ is denoted $\Pi_{\vec{d}}(\vec{v})$. The formula for computing the projections uses the dot product operation:
$$ \Pi_{\vec{d}}(\vec{v}) \ \equiv \ (\vec{v} \cdot \hat{d}) \hat{d} \ = \ \left(\vec{v} \cdot \frac{\vec{d}}{\|\vec{d}\|} \right) \frac{\vec{d}}{\|\vec{d}\|}. $$
In [37]:
def proj(v, d):
"""Computes the projection of vector `v` onto direction `d`."""
return v.dot( d/d.norm() )*( d/d.norm() )
In [38]:
v = Vector([2,2])
d = Vector([3,0])
proj_v_on_d = proj(v,d)
plot_vecs(d, v, proj_v_on_d)
autoscale_arrows()
The basic projection operation can be used to compute projection onto planes, and compute distances between geomteirc objects (page 192).
page 194
page 199
page 211
page 223
Functions that take vectors as inputs and produce vectors as outputs:
$$ T:\mathbb{R}^{n}\! \to \mathbb{R}^{m} $$We can understand the properties of linear transformations $T$, and their matrix representations $M_T$ in analogy with ordinary functions:
\begin{align*} \textrm{function } f:\mathbb{R}\to \mathbb{R} & \ \Leftrightarrow \, \begin{array}{l} \textrm{linear transformation } T:\mathbb{R}^{n}\! \to \mathbb{R}^{m} \\ \textrm{represented by the matrix } M_T \in \mathbb{R}^{m \times n} \end{array} \\ % \textrm{input } x\in \mathbb{R} & \ \Leftrightarrow \ \textrm{input } \vec{x} \in \mathbb{R}^n \\ %\textrm{compute } \textrm{output } f(x) \in \mathbb{R} & \ \Leftrightarrow \ % \textrm{compute matrix-vector product } \textrm{output } T(\vec{x}) \equiv M_T\vec{x} \in \mathbb{R}^m \\ %\textrm{function composition } g\circ\! f \: (x) = g(f(x)) & \ \Leftrightarrow \ % \textrm{matrix product } S(T(\vec{x})) \equiv M_SM_T \vec{x} \\ \textrm{function inverse } f^{-1} & \ \Leftrightarrow \ \textrm{matrix inverse } M_T^{-1} \\ \textrm{zeros of } f & \ \Leftrightarrow \ \textrm{kernel of } T \equiv \textrm{null space of } M_T \equiv \mathcal{N}(A) \\ \textrm{image of } f & \ \Leftrightarrow \ \begin{array}{l} \textrm{image of } T \equiv \textrm{column space of } M_T \equiv \mathcal{C}(A) \end{array} \end{align*}Observe we refer to the linear transformation $T$ and its matrix representation $M_T$ interchangeably.
page 234
In [39]:
A = Matrix([[1, 5],
[5, 1]])
A
Out[39]:
In [40]:
A*Vector([1,0])
Out[40]:
In [41]:
A*Vector([1,1])
Out[41]:
The characterisitic polynomial of the matrix $A$ is defined as
$$ p(\lambda) \equiv \det(A-\lambda \mathbb{1}). $$
In [42]:
l = symbols('lambda')
(A-l*eye(2)).det()
Out[42]:
In [43]:
# the roots of the characteristic polynomial are the eigenvalues of A
solve( (A-l*eye(2)).det(), l)
Out[43]:
In [44]:
# or call `eigenvals` method
A.eigenvals()
Out[44]:
In [45]:
A.eigenvects()
# can also find eigenvects using (A-6*eye(2)).nullspace() and (A+4*eye(2)).nullspace()
Out[45]:
In [46]:
Q, Lambda = A.diagonalize()
Q, Lambda
Out[46]:
In [47]:
Q*Lambda*Q.inv() # == eigendecomposition of A
Out[47]:
page 271
Generalize vector techniques to other vector like quantities. Allow us to talk about basis, dimention, etc.
page 277
Use geometrical notions like length and orthogonaloty for abstract vectors.
page 281
page 288
page 292
page 298
Check out page 499 for notation.