def cells(): from sympy import * init_printing() %matplotlib notebook import matplotlib.pyplot as mpl from util.plot_helpers import plot_augmat, plot_plane, plot_point, plot_line, plot_vec, plot_vecs 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 ''' ''' ''' ### E4.7 ''' ''' ''' p = Point([10,10,10]) pL1 = Point([3,0,5]) pL2 = Point([6,0,0]) d = Vector([1,-2,0]) # define a vector from a point on L1 to p: v1 = p - pL1 # proj_{L1}(p) = proj_{L1}(v1) + pL1 p_proj_L1 = (d.dot(v1)/d.norm()**2)*d + pL1 ''' ''' plot_line(d, pL1) plot_vec(d, at=pL1) plot_vec(v1, at=pL1) plot_vec((d.dot(v1)/d.norm()**2)*d, at=pL1) plot_vec(p_proj_L1) ax = mpl.gca() mpl.xlim([0,10]) mpl.ylim([0,10]) ax.set_zlim([0,10]) ax.grid(True,which='both') ''' ''' '''
def cells(): ''' # Testing SymPy and plot helpers are working ''' ''' ''' # setup SymPy from sympy import * x, y, z, t = symbols('x y z t') init_printing() # download plot_helpers.py for use in colab if 'google.colab' in str(get_ipython()): print('Downloading plot_helpers.py to util/ (only neded for colab') !mkdir util; wget https://raw.githubusercontent.com/minireference/noBSLAnotebooks/master/util/plot_helpers.py -P util # setup plotting %matplotlib inline import matplotlib.pyplot as mpl from util.plot_helpers import plot_vec, plot_vecs, autoscale_arrows ''' ''' # check if SymPy knows how to simplify trig expressions simplify(sin(2*x)*cos(2*x)) ''' ''' # define a column vector a a = Matrix([1,1,1]) a ''' ''' # BAD define the floating point number approximation 1/3 1/3 ''' ''' # define the fraction 1/3 (an exact rational number) S(1)/3 ''' ''' # The S()-stuff is necessary to avoid Python behaviour, # which is to treat 1/3 as a floating point number: type(1/3) ''' ''' type(S(1)/3) ''' ''' ''' ''' # obtain numeric approximation (as a float) N(S(1)/3) ''' ''' # N, .n(), and .evalf() are equivalent ways to obtain numeric approx: N((S(1)/3)), (S(1)/3).n(), (S(1)/3).evalf() ''' ''' # the .n() method allows for arbitrary level precisions pi.n(100) ''' ''' # Euler's constant E ''' ''' ''' ''' ''' ''' ''' ## Plot helpers ''' ''' ''' # vector defined as a Python list u = [1,2] plot_vec(u) autoscale_arrows() ''' ''' # vector defined as a SymPy 2x1 Matrix (a column vector) v = Matrix([1,2]) plot_vec(v) autoscale_arrows() ''' ''' '''
def cells(): ''' # 4/ Exercise solutions ''' ''' ''' # helper code needed for running in colab if 'google.colab' in str(get_ipython()): print('Downloading plot_helpers.py to util/ (only neded for colab') !mkdir util; wget https://raw.githubusercontent.com/minireference/noBSLAnotebooks/master/util/plot_helpers.py -P util ''' ''' from sympy import * init_printing() %matplotlib inline import matplotlib.pyplot as mpl from util.plot_helpers import plot_augmat, plot_plane, plot_point, plot_line, plot_vec, plot_vecs 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 ''' ''' ''' ### E4.5 ''' ''' ''' # Given point r r = Point([1,3,0]) # line L pL = Point([0,0,2]) vL = Vector([1,-1,0]) # note this changed in v2.2 of LA book # plane P nP = Vector([1,1,1]) dP = 1 # compute the following closest distances: ''' ''' # a) d(r,O) = ||r|| r.norm() ''' ''' # b) d(L,O) = || pL - proj_{L}(pL) || (pL - (pL.dot(vL)/vL.norm()**2)*vL).norm() ''' ''' # c) d(P,O) = || proj_{nP}(pP) || pP = Matrix([1,0,0]) ( (nP.dot(pP)/nP.norm()**2)*nP).norm() ''' ''' # d) u=pL-r d(r,L)=||u - proj_{L}(u)|| u = pL - r (u - (u.dot(vL)/vL.norm()**2)*vL).norm() ''' ''' # e) v=pP-r d(r,P)=||proj_{nP}(v)|| pP = Matrix([1,0,0]) v = pP - r ( (nP.dot(v)/nP.norm()**2)*nP).norm() ''' ''' #f) d(L,P) # STEP1: check relative orientation of L and P print('vL·nP =', vL.dot(nP), 'so line is parallel to plane. OK to proceed...') # STEP2: find vector w=pP-pL arbitrary points on plane nad line, pP = Matrix([1,0,0]) w = pP - pL # then compute component of w in perp. to the plane d(L,P)=||proj_{nP}(w)|| ( (nP.dot(w)/nP.norm()**2)*nP).norm() ''' ''' # # debug viz to make sure L parallel to P # plot_vec(5*vL, at=pL, color='b') # plot_vec(-5*vL, at=pL, color='b') # plot_vec(w, color='r', at=pL) # plot_plane(nP,dP) ''' ''' ''' ''' ''' ''' ''' ### E4.7 ''' ''' ''' # Setup the variables of the exercise: p = Point([10,10,10]) pL1 = Point([3,0,5]) # an arbitrary point on L1 pL2 = Point([6,0,0]) # an arbitrary point on L2 d = Vector([1,-2,0]) # direction vector of L1 and L2 ''' ''' ''' a) Projection of $p$ onto $\ell_1$ ''' ''' ''' # define a vector from a point on L1 to p: v1 = p - pL1 # proj_{L1}(p) = proj_{L1}(v1) + pL1 p_proj_L1 = (d.dot(v1)/d.norm()**2)*d + pL1 p_proj_L1 ''' ''' ''' ^ This is the point on the line $\ell_1$ that is closes to the point $p$ ''' ''' ''' ''' b) (shortest) distance form $p$ to $\ell_1$ ''' ''' ''' # d(p, L1) = subtract from v1 the part that is perp to L1 and compute the length: (v1 - (d.dot(v1)/d.norm()**2)*d).norm() ''' ''' # ... or compute the distance directly: (p_proj_L1-p).norm() ''' ''' (p_proj_L1-p).norm().n() # numeric approx. ''' ''' plot_line(d, pL1) # Line L1 # vector v1 and it's decomposition into parallel-to-L1 and perp-to-L1 components plot_vec(v1, at=pL1) plot_vec(p_proj_L1-pL1, at=pL1, color='b') plot_vec(p-p_proj_L1, at=p_proj_L1, color='r') ax = mpl.gca() mpl.xlim([0,10]) mpl.ylim([0,10]) ax.set_zlim([0,10]) ax.grid(True,which='both') ''' ''' ''' Answer to a) is the tip of the blue vector; answer to b) is the length of the red vector. ''' ''' ''' ''' ''' ''' Use a similar approach for c) and d) ''' ''' ''' # define a vector from a point on L2 to p: v2 = p - pL2 ''' ''' # p_proj_L2 = (d.dot(v2)/d.norm()**2)*d + pL2 ''' ''' # d(p, L2) = (v2 - (d.dot(v2)/d.norm()**2)*d).norm() ''' ''' (v2 - (d.dot(v2)/d.norm()**2)*d).norm().n() ''' ''' ''' ''' ''' ''' ''' e) distance $\ell_1$ to $\ell_2$ ''' ''' ''' # first define a vector from a point on L1 to a point on L2: v3 = pL2 - pL1 v3 ''' ''' # d(L1, L2) = d_L1L2 = (v3 - (d.dot(v3)/d.norm()**2)*d).norm() d_L1L2 ''' ''' d_L1L2.n() ''' ''' ''' ''' ''' ''' '''
def cells(): ''' ''' from sympy import * init_printing() %matplotlib notebook import matplotlib.pyplot as mpl from util.plot_helpers import plot_augmat, plot_plane, plot_point, plot_line, plot_vec, plot_vecs 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 ''' ''' ''' ## P4.11 ''' ''' ''' v = Vector([3, 4, 1]) normal = Vector([2, -1, 4]) vPperp = (normal.dot(v)/normal.norm()**2)*normal print('vPperp =', vPperp) vP = v - vPperp print('vP =', vP) plot_plane(normal, 0) # plane P plot_vec(0.2*normal, color='r') # its normal vec plot_vecs(v, vPperp, vP) ''' ''' v = Vector([3, 4, 1]) normal = Vector([2, -1, 4]) D=4 # point on P closest to the origin alpha = D/normal.norm()**2 p_closest = alpha*normal # print('len normal', normal.norm()) # print('p_closest', p_closest) assert p_closest.dot(normal) == 4 vPperp = (normal.dot(v)/normal.norm()**2)*normal print('vPperp', vPperp) v_wrong = v - vPperp print('v_wrong', v_wrong) plot_plane(normal, D) # plane P plot_vec(0.2*normal, at=p_closest, color='r') # its normal vec plot_vecs(v, vPperp, v_wrong) ax = mpl.gca() ax.grid(True,which='both') ''' ''' v = Vector([3, 4, 1]) normal = Vector([2, -1, 4]) D=4 # some point on P p0 = Point([2,0,0]) u = v-p0 # vector from p0 to tip of v uPperp = (normal.dot(u)/normal.norm()**2)*normal print('uPperp', uPperp) uInP = u - uPperp proj_v_on_P = p0 + uInP print('proj_v_on_P', proj_v_on_P) plot_plane(normal, D) # plane P plot_vec(0.2*normal, at=p_closest, color='r') # its normal vec plot_vec(v) plot_vec(u, at=p0, color='r') plot_vec(uPperp, at=p0, color='b') plot_vec(uInP, at=p0, color='g') plot_vec(proj_v_on_P, color='y') ax = mpl.gca() ax.grid(True,which='both') ''' ''' '''
def cells(): ''' # Linear algebra overview ''' ''' ''' ''' 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. ''' ''' ''' ''' ## Chapters overview - 1/ Math fundamentals - 2/ Intro to linear algebra - Vectors - Matrices - Matrix-vector product representation of linear transformations - Linear property: $f(a\mathbf{x} + b\mathbf{y}) = af(\mathbf{x}) + bf(\mathbf{y})$ - 3/ Computational linear algebra - Gauss-Jordan elimination procedure - Augemnted matrix representaiton of systems of linear equations - Reduced row echelon form - Matrix equations - Matrix operations - Matrix product - Determinant - Matrix inverse - 4/ Geometrical linear algebra - Points, lines, and planes - Projection operation - Coordinates - Vector spaces - Vector space techniques - 5/ Linear transformations - Vector functions - Input and output spaces - Matrix representation of linear transformations - Column space and row spaces of matrix representations - Invertible matrix theorem - 6/ Theoretical linear algebra - Eigenvalues and eigenvectors - Special types of matrices - Abstract vectors paces - Abstract inner product spaces - Gram–Schmidt orthogonalization - Matrix decompositions - Linear algebra with complex numbers - 7/ Applications - 8/ Probability theory - 9/ Quantum mechanics - Notation appendix ''' ''' ''' # helper code needed for running in colab if 'google.colab' in str(get_ipython()): print('Downloading plot_helpers.py to util/ (only neded for colab') !mkdir util; wget https://raw.githubusercontent.com/minireference/noBSLAnotebooks/master/util/plot_helpers.py -P util ''' ''' # 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 ''' ''' ''' # 1/ Math fundamentals ''' ''' ''' ''' Linear algebra builds upon high school math concepts like: - Numbers (integers, rationals, reals, complex numbers) - Functions ($f(x)$ takes an input $x$ and produces an output $y$) - Basic rules of algebra - Geometry (lines, curves, areas, triangles) - The cartesian plane ''' ''' ''' ''' ''' ''' # 2/ Intro to linear algebra Linear algebra is the study of vectors and matrices. ''' ''' ''' ''' ## Vectors ''' ''' ''' # define two vectors u = Vector([2,3]) v = Vector([3,0]) u ''' ''' v ''' ''' plot_vecs(u, v) autoscale_arrows() ''' ''' ''' ## Vector operations ''' ''' ''' ''' - Addition (denoted $\vec{u}+\vec{v}$) - Subtraction, the inverse of addition (denoted $\vec{u}-\vec{v}$) - Scaling (denoted $\alpha \vec{u}$) - Dot product (denoted $\vec{u} \cdot \vec{v}$) - Cross product (denoted $\vec{u} \times \vec{v}$) ''' ''' ''' ''' ### Vector addition ''' ''' ''' # algebraic u+v ''' ''' # graphical plot_vecs(u, v) plot_vec(v, at=u, color='b') plot_vec(u+v, color='r') autoscale_arrows() ''' ''' ''' ### Basis 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)$. ''' ''' ''' # the standard basis ihat = Vector([1,0]) jhat = Vector([0,1]) v = 4*ihat + 6*jhat v ''' ''' # 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}$. ''' ''' ''' # 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]) ''' ''' # geomtrically... plot_vecs(b1, b2, 5*b1, -1*b2, v) autoscale_arrows() ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ## Matrix operations ''' ''' ''' ''' - Addition (denoted $A+B$) - Subtraction, the inverse of addition (denoted $A-B$) - Scaling by a constant $\alpha$ (denoted $\alpha A$) - Matrix-vector product (denoted $A\vec{x}$, related to linear transformations) - Matrix product (denoted $AB$) - Matrix inverse (denoted $A^{-1}$) - Trace (denoted $\textrm{Tr}(A)$) - Determinant (denoted $\textrm{det}(A)$ or $|A|$) ''' ''' ''' ''' 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*} ''' ''' ''' ''' ## Linear property $$ T(a\mathbf{x}_1 + b\mathbf{x}_2) = aT(\mathbf{x}_1) + bT(\mathbf{x}_2) $$ ''' ''' ''' ''' ## Matrix-vector product representation of linear transformations ''' ''' ''' ''' 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} $$ ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' # 3/ Computational linear algebra ''' ''' ''' ''' ## Gauss-Jordan elimination procedure Suppose you're asked to solve for $x_1$ and $x_2$ in the following system of equations \begin{align*} 1x_1 + 2x_2 &= 5 \\ 3x_1 + 9x_2 &= 21. \end{align*} ''' ''' ''' # represent as an augmented matrix AUG = Matrix([ [1, 2, 5], [3, 9, 21]]) AUG ''' ''' # eliminate x_1 in second equation by subtracting 3x times the first equation AUG[1,:] = AUG[1,:] - 3*AUG[0,:] AUG ''' ''' # simplify second equation by dividing by 3 AUG[1,:] = AUG[1,:]/3 AUG ''' ''' # eliminate x_2 from first equation by subtracting 2x times the second equation AUG[0,:] = AUG[0,:] - 2*AUG[1,:] AUG ''' ''' ''' 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$. ''' ''' ''' ''' ## Matrix equations ''' ''' ''' ''' See **page 177** in v2.2 of the book. ''' ''' ''' ''' ''' ''' ''' ''' ## Matrix product ''' ''' ''' 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 ''' ''' A*B ''' ''' 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) ''' ''' # mat_prod(B,A) ''' ''' ''' ## Determinant ''' ''' ''' a, b, c, d = symbols('a b c d') A = Matrix([[a,b], [c,d]]) A.det() ''' ''' # 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? ''' ''' # base = 3, height = 2, so area is 6 ''' ''' # Compute the area of the parallelogram with sides u1 and u2 using the deteminant A = Matrix([[3,0], [2,2]]) A.det() ''' ''' ''' ''' ''' ''' ''' ## Matrix inverse For an invertible matrix $A$, the matrix inverse $A^{-1}$ acts to undo the effects of $A$: $$ A^{-1} A \vec{v} = \vec{v}. $$ The effect applying $A$ followed by $A^{-1}$ (or the other way around) is the identity transformation: $$ A^{-1}A \ = \ \mathbb{1} \ = \ AA^{-1}. $$ ''' ''' ''' A = Matrix([[1, 2], [3, 9]]) A ''' ''' # Compute deteminant to check if inverse matrix exists A.det() ''' ''' ''' The deteminant is non-zero so inverse exists. ''' ''' ''' A.inv() ''' ''' A.inv()*A ''' ''' ''' ### Adjugate-matrix formula The *adjugate matrix* of the matrix $A$ is obtained by replacing each entry of the matrix with a partial determinant calculation (called *minors*). The minor $M_{ij}$ is the determinant of $A$ with its $i$th row and $j$th columns removed. ''' ''' ''' A.adjugate() / A.det() ''' ''' ''' ### Augmented matrix approach $$ \left[ \, A \, | \, \mathbb{1} \, \right] \qquad -\textrm{Gauss-Jordan elimination}\rightarrow \qquad \left[ \, \mathbb{1} \, | \, A^{-1} \, \right] $$ ''' ''' ''' AUG = A.row_join(eye(2)) AUG ''' ''' # perform row operations until left side of AUG is in RREF AUG[1,:] = AUG[1,:] - 3*AUG[0,:] AUG[1,:] = AUG[1,:]/3 AUG[0,:] = AUG[0,:] - 2*AUG[1,:] AUG ''' ''' # the inverse of A is in the right side of RREF(AUG) AUG[:,2:5] # == A-inverse ''' ''' # verify A times A-inverse gives the identity matrix... A*AUG[:,2:5] ''' ''' ''' ### Using elementary matrices Each row operation $\mathcal{R}_i$ can be represented as an elementary matrix $E_i$. The elementary matrix of a given row operation is obtained by performing the row operation on the identity matrix. ''' ''' ''' 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 ''' ''' # the sequence of three row operations transforms the matrix A into RREF E3*E2*E1*A ''' ''' ''' 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$. ''' ''' ''' E3*E2*E1 ''' ''' ''' ''' ''' ''' ''' # 4/ Geometrical linear algebra Points, lines, and planes are geometrical objects that are conveniently expressed using the language of vectors. ''' ''' ''' ''' ## Points A point $p=(p_x,p_y,p_z)$ refers to a single location in $\mathbb{R}^3$. ''' ''' ''' p = Point([2,4,5]) p ''' ''' ''' ## Lines A line is a one dimensional infinite subset of $\mathbb{R}^3$ that can be described as $$ \ell: \{ p_o + \alpha \vec{v} \ | \ \forall \alpha \in \mathbb{R} \}. $$ ''' ''' ''' po = Point([1,1,1]) v = Vector([1,1,0]) plot_line(v, po) ''' ''' ''' ## Planes 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}$. ''' ''' ''' # plot plane 2x + 1y + 1z = 5 normal = Vector([2, 1, 1]) D = 5 plot_plane(normal, D) ''' ''' ''' ''' ''' ''' ''' ## Projection operation ''' ''' ''' ''' 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}\|}. $$ ''' ''' ''' def proj(v, d): """Computes the projection of vector `v` onto direction `d`.""" return v.dot( d/d.norm() )*( d/d.norm() ) ''' ''' 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). ''' ''' ''' ''' ## Bases and coordinate projections ''' ''' ''' ''' See [page 225](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=68) in v2.2 of the book: - Different types of bases - Orthonormal - Orthogonal - Generic - Change of basis operation ''' ''' ''' ''' ## Vector spaces ''' ''' ''' ''' See **page 231** in v2.2 of the book. ''' ''' ''' ''' ## Vector space techniques ''' ''' ''' ''' See **page 244** in the book. ''' ''' ''' ''' ''' ''' ''' ''' # 5/ Linear transformations ''' ''' ''' ''' See [page 257](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=70) in v2.2 of the book. ''' ''' ''' ''' ## Vector functions ''' ''' ''' ''' Functions that take vectors as inputs and produce vectors as outputs: $$ T:\mathbb{R}^{n}\! \to \mathbb{R}^{m} $$ ''' ''' ''' ''' ## Matrix representation of linear transformations ''' ''' ''' ''' $$ T : \mathbb{R}^n \to \mathbb{R}^m \qquad \Leftrightarrow \qquad M_T \in \mathbb{R}^{m \times n} $$ ''' ''' ''' ''' ''' ''' ''' ''' ## Input and output spaces ''' ''' ''' ''' 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. ''' ''' ''' ''' ## Finding matrix representations ''' ''' ''' ''' See [page 269](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=74) in v2.2 of the book. ''' ''' ''' ''' ''' ''' ''' ''' ## Invertible matrix theorem See [page 288](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=78) in the book. ''' ''' ''' ''' ''' ''' ''' ''' # 6/ Theoretical linear algebra ''' ''' ''' ''' ## Eigenvalues and eigenvectors An eigenvector of the matirx $A$ is a special input vector, for which the matrix $A$ acts as a scaling: $$ A\vec{e}_\lambda = \lambda\vec{e}_\lambda, $$ where $\lambda$ is called the *eigenvalue* and $\vec{e}_\lambda$ is the corresponding eigenvector. ''' ''' ''' A = Matrix([[1, 5], [5, 1]]) A ''' ''' A*Vector([1,0]) ''' ''' A*Vector([1,1]) ''' ''' ''' The *characterisitic polynomial* of the matrix $A$ is defined as $$ p(\lambda) \equiv \det(A-\lambda \mathbb{1}). $$ ''' ''' ''' l = symbols('lambda') (A-l*eye(2)).det() ''' ''' # the roots of the characteristic polynomial are the eigenvalues of A solve( (A-l*eye(2)).det(), l) ''' ''' # or call `eigenvals` method A.eigenvals() ''' ''' A.eigenvects() # can also find eigenvects using (A-6*eye(2)).nullspace() and (A+4*eye(2)).nullspace() ''' ''' Q, Lambda = A.diagonalize() Q, Lambda ''' ''' Q*Lambda*Q.inv() # == eigendecomposition of A ''' ''' ''' ''' ''' ''' ''' ## Special types of matrices ''' ''' ''' ''' See [page 312](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=83) in v2.2 of the book. ''' ''' ''' ''' ''' ''' ''' ''' ## Abstract vectors paces ''' ''' ''' ''' Generalize vector techniques to other vector like quantities. Allow us to talk about basis, dimention, etc. See [page 318](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=84) in the book. ''' ''' ''' ''' ''' ''' ''' ''' ## Abstract inner product spaces ''' ''' ''' ''' Use geometrical notions like length and orthogonaloty for abstract vectors. See **page 322** in the book. ''' ''' ''' ''' ''' ''' ''' ''' ## Gram–Schmidt orthogonalization ''' ''' ''' ''' See **page 328**. ''' ''' ''' ''' ''' ''' ''' ''' ## Matrix decompositions ''' ''' ''' ''' See **page 332**. ''' ''' ''' ''' ''' ''' ''' ''' ## Linear algebra with complex numbers ''' ''' ''' ''' See [page 339](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=88) in v2.2 of the book. ''' ''' ''' ''' ''' ''' ''' ''' # Applications chapters ''' ''' ''' ''' - Chapter 7: Applications - Chapter 8: Probability theory - Chapter 9: Quantum mechanics ''' ''' ''' ''' ''' ''' ''' ''' # Notation appendix ''' ''' ''' ''' Check out [page 571](https://minireference.com/static/excerpts/noBSLA_v2_preview.pdf#page=142) in the book. ''' ''' ''' ''' ''' '''
def cells(): from sympy import * init_printing() %matplotlib notebook import matplotlib.pyplot as mpl from util.plot_helpers import plot_augmat, plot_plane, plot_point, plot_line, plot_vec, plot_vecs 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 ''' ''' ''' ### E4.7 ''' ''' ''' # Setup the variables of the exercise: p = Point([10,10,10]) pL1 = Point([3,0,5]) # an arbitrary point on L1 pL2 = Point([6,0,0]) # an arbitrary point on L2 d = Vector([1,-2,0]) # direction vector of L1 and L2 ''' ''' ''' a) Projection of $p$ onto $\ell_1$ ''' ''' ''' # define a vector from a point on L1 to p: v1 = p - pL1 # proj_{L1}(p) = proj_{L1}(v1) + pL1 p_proj_L1 = (d.dot(v1)/d.norm()**2)*d + pL1 p_proj_L1 ''' ''' ''' ^ This is the point on the line $\ell_1$ that is closes to the point $p$ ''' ''' ''' ''' b) (shortest) distance form $p$ to $\ell_1$ ''' ''' ''' # d(p, L1) = subtract from v1 the part that is perp to L1 and compute the length: (v1 - (d.dot(v1)/d.norm()**2)*d).norm() ''' ''' # ... or compute the distance directly: (p_proj_L1-p).norm() ''' ''' (p_proj_L1-p).norm().n() # numeric approx. ''' ''' plot_line(d, pL1) # Line L1 # vector v1 and it's decomposition into parallel-to-L1 and perp-to-L1 components plot_vec(v1, at=pL1) plot_vec(p_proj_L1-pL1, at=pL1, color='b') plot_vec(p-p_proj_L1, at=p_proj_L1, color='r') ax = mpl.gca() mpl.xlim([0,10]) mpl.ylim([0,10]) ax.set_zlim([0,10]) ax.grid(True,which='both') ''' ''' ''' Answer to a) is the tip of the blue vector; answer to b) is the length of the red vector. ''' ''' ''' ''' ''' ''' Use a similar approach for c) and d) ''' ''' ''' # define a vector from a point on L2 to p: v2 = p - pL2 ''' ''' # p_proj_L2 = (d.dot(v2)/d.norm()**2)*d + pL2 ''' ''' # d(p, L2) = (v2 - (d.dot(v2)/d.norm()**2)*d).norm() ''' ''' (v2 - (d.dot(v2)/d.norm()**2)*d).norm().n() ''' ''' ''' ''' ''' ''' ''' e) distance $\ell_1$ to $\ell_2$ ''' ''' ''' # first define a vector from a point on L1 to a point on L2: v3 = pL2 - pL1 v3 ''' ''' # d(L1, L2) = d_L1L2 = (v3 - (d.dot(v3)/d.norm()**2)*d).norm() d_L1L2 ''' ''' d_L1L2.n() ''' ''' ''' ''' ''' ''' '''
def cells(): ''' # Linear algebra overview ''' ''' ''' ''' 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. ''' ''' ''' ''' ## Chapters overview - 1/ Math fundamentals - 2/ Intro to linear algebra - Vectors - Matrices - Matrix-vector product representation of linear transformations - Linear property: $f(a\mathbf{x} + b\mathbf{y}) = af(\mathbf{x}) + bf(\mathbf{y})$ - 3/ Computational linear algebra - Gauss-Jordan elimination procedure - Augemnted matrix representaiton of systems of linear equations - Reduced row echelon form - Matrix equations - Matrix operations - Matrix product - Determinant - Matrix inverse - 4/ Geometrical linear algebra - Points, lines, and planes - Projection operation - Coordinates - Vector spaces - Vector space techniques - 5/ Linear transformations - Vector functions - Input and output spaces - Matrix representation of linear transformations - Column space and row spaces of matrix representations - Invertible matrix theorem - 6/ Theoretical linear algebra - Eigenvalues and eigenvectors - Special types of matrices - Abstract vectors paces - Abstract inner product spaces - Gram–Schmidt orthogonalization - Matrix decompositions - Linear algebra with complex numbers - 7/ Applications - 8/ Probability theory - 9/ Quantum mechanics - Notation appendix ''' ''' ''' # 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 ''' ''' ''' # 1/ Math fundamentals ''' ''' ''' ''' Linear algebra builds upon high school math concepts like: - Numbers (integers, rationals, reals, complex numbers) - Functions ($f(x)$ takes an input $x$ and produces an output $y$) - Basic rules of algebra - Geometry (lines, curves, areas, triangles) - The cartesian plane ''' ''' ''' ''' ''' ''' # 2/ Intro to linear algebra Linear algebra is the study of vectors and matrices. ''' ''' ''' ''' ## Vectors ''' ''' ''' # define two vectors u = Vector([2,3]) v = Vector([3,0]) u ''' ''' v ''' ''' plot_vecs(u, v) autoscale_arrows() ''' ''' ''' ## Vector operations ''' ''' ''' ''' - Addition (denoted $\vec{u}+\vec{v}$) - Subtraction, the inverse of addition (denoted $\vec{u}-\vec{v}$) - Scaling (denoted $\alpha \vec{u}$) - Dot product (denoted $\vec{u} \cdot \vec{v}$) - Cross product (denoted $\vec{u} \times \vec{v}$) ''' ''' ''' ''' ### Vector addition ''' ''' ''' # algebraic u+v ''' ''' # graphical plot_vecs(u, v) plot_vec(v, at=u, color='b') plot_vec(u+v, color='r') autoscale_arrows() ''' ''' ''' ### Basis 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)$. ''' ''' ''' # the standard basis ihat = Vector([1,0]) jhat = Vector([0,1]) v = 4*ihat + 6*jhat v ''' ''' # 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}$. ''' ''' ''' # 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]) ''' ''' # geomtrically... plot_vecs(b1, b2, 5*b1, -1*b2, v) autoscale_arrows() ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ## Matrix operations ''' ''' ''' ''' - Addition (denoted $A+B$) - Subtraction, the inverse of addition (denoted $A-B$) - Scaling by a constant $\alpha$ (denoted $\alpha A$) - Matrix-vector product (denoted $A\vec{x}$, related to linear transformations) - Matrix product (denoted $AB$) - Matrix inverse (denoted $A^{-1}$) - Trace (denoted $\textrm{Tr}(A)$) - Determinant (denoted $\textrm{det}(A)$ or $|A|$) ''' ''' ''' ''' 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*} ''' ''' ''' ''' ## Linear property $$ T(a\mathbf{x}_1 + b\mathbf{x}_2) = aT(\mathbf{x}_1) + bT(\mathbf{x}_2) $$ ''' ''' ''' ''' ## Matrix-vector product representation of linear transformations ''' ''' ''' ''' 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} $$ ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' ''' # 3/ Computational linear algebra ''' ''' ''' ''' ## Gauss-Jordan elimination procedure Suppose you're asked to solve for $x_1$ and $x_2$ in the following system of equations \begin{align*} 1x_1 + 2x_2 &= 5 \\ 3x_1 + 9x_2 &= 21. \end{align*} ''' ''' ''' # represent as an augmented matrix AUG = Matrix([ [1, 2, 5], [3, 9, 21]]) AUG ''' ''' # eliminate x_1 in second equation by subtracting 3x times the first equation AUG[1,:] = AUG[1,:] - 3*AUG[0,:] AUG ''' ''' # simplify second equation by dividing by 3 AUG[1,:] = AUG[1,:]/3 AUG ''' ''' # eliminate x_2 from first equation by subtracting 2x times the second equation AUG[0,:] = AUG[0,:] - 2*AUG[1,:] AUG ''' ''' ''' 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$. ''' ''' ''' ''' ## Matrix equations ''' ''' ''' ''' page 150 ''' ''' ''' ''' ''' ''' ''' ''' ## Matrix product ''' ''' ''' 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 ''' ''' A*B ''' ''' 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) ''' ''' # mat_prod(B,A) ''' ''' ''' ## Determinant ''' ''' ''' a,b,c,d = symbols('a b c d') A = Matrix([[a,b], [c,d]]) A.det() ''' ''' # 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? ''' ''' # base = 3, height = 2, so area is 6 ''' ''' # Compute the area of the parallelogram with sides u1 and u2 using the deteminant A = Matrix([[3,0], [2,2]]) A.det() ''' ''' ''' ''' ''' ''' ''' ## Matrix inverse For an invertible matrix $A$, the matrix inverse $A^{-1}$ acts to undo the effects of $A$: $$ A^{-1} A \vec{v} = \vec{v}. $$ The effect applying $A$ followed by $A^{-1}$ (or the other way around) is the identity transformation: $$ A^{-1}A \ = \ \mathbb{1} \ = \ AA^{-1}. $$ ''' ''' ''' A = Matrix([[1, 2], [3, 9]]) A ''' ''' # Compute deteminant to check if inverse matrix exists A.det() # if non-zero, then inverse exists ''' ''' A.inv() ''' ''' A.inv()*A ''' ''' ''' ### Adjugate-matrix formula The *adjugate matrix* of the matrix $A$ is obtained by replacing each entry of the matrix with a partial determinant calculation (called *minors*). The minor $M_{ij}$ is the determinant of $A$ with its $i$th row and $j$th columns removed. ''' ''' ''' A.adjugate() / A.det() ''' ''' ''' ### Augmented matrix approach $$ \left[ \, A \, | \, \mathbb{1} \, \right] \qquad -\textrm{Gauss-Jordan elimination}\rightarrow \qquad \left[ \, \mathbb{1} \, | \, A^{-1} \, \right] $$ ''' ''' ''' AUG = A.row_join(eye(2)) AUG ''' ''' # performd row operations AUG[1,:] = AUG[1,:] - 3*AUG[0,:] AUG[1,:] = AUG[1,:]/3 AUG[0,:] = AUG[0,:] - 2*AUG[1,:] AUG ''' ''' AUG[:,2:5] ''' ''' ''' ### Using elementary matrices Each row operation $\mathcal{R}_i$ can be represented as an elementary matrix $E_i$. The elementary matrix of a given row operation is obtained by performing the row operation on the identity matrix. ''' ''' ''' 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 ''' ''' # the sequence of three row operations transforms the matrix A into RREF E3*E2*E1*A ''' ''' ''' 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$. ''' ''' ''' E3*E2*E1 ''' ''' ''' ''' ''' ''' ''' # 4/ Geometrical linear algebra Points, lines, and planes are geometrical objects that are conveniently expressed using the language of vectors. ''' ''' ''' ''' ## Points A point $p=(p_x,p_y,p_z)$ refers to a single location in $\mathbb{R}^3$. ''' ''' ''' p = Point([2,4,5]) p ''' ''' ''' ## Lines A line is a one dimensional infinite subset of $\mathbb{R}^3$ that can be described as $$ \ell: \{ p_o + \alpha \vec{v} \ | \ \forall \alpha \in \mathbb{R} \}. $$ ''' ''' ''' po = Point([1,1,1]) v = Vector([1,1,0]) plot_line(v, po) ''' ''' ''' ## Planes 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}$. ''' ''' ''' # plot plane 2x + y + z = 5 normal = Vector([2, 1, 1]) D = 5 plot_plane(normal, D) ''' ''' ''' ''' ''' ''' ''' ## Projection operation ''' ''' ''' ''' 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}\|}. $$ ''' ''' ''' def proj(v, d): """Computes the projection of vector `v` onto direction `d`.""" return v.dot( d/d.norm() )*( d/d.norm() ) ''' ''' 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). ''' ''' ''' ''' ## Bases and coordinate projections ''' ''' ''' ''' page 194 - Different types of bases - Orthonormal - Orthogonal - Generic - Change of basis operation ''' ''' ''' ''' ## Vector spaces ''' ''' ''' ''' page 199 ''' ''' ''' ''' ## Vector space techniques ''' ''' ''' ''' page 211 ''' ''' ''' ''' ''' ''' ''' ''' # 5/ Linear transformations ''' ''' ''' ''' page 223 ''' ''' ''' ''' ## Vector functions ''' ''' ''' ''' Functions that take vectors as inputs and produce vectors as outputs: $$ T:\mathbb{R}^{n}\! \to \mathbb{R}^{m} $$ ''' ''' ''' ''' ## Matrix representation of linear transformations ''' ''' ''' ''' $$ T : \mathbb{R}^n \to \mathbb{R}^m \qquad \Leftrightarrow \qquad M_T \in \mathbb{R}^{m \times n} $$ ''' ''' ''' ''' ''' ''' ''' ''' ## Input and output spaces ''' ''' ''' ''' 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. ''' ''' ''' ''' ## Finding matrix representations ''' ''' ''' ''' page 234 ''' ''' ''' ''' ''' ''' ''' ''' ## Invertible matrix theorem page 250 ''' ''' ''' ''' ''' ''' ''' ''' # 6/ Theoretical linear algebra ''' ''' ''' ''' ## Eigenvalues and eigenvectors An eigenvector of the matirx $A$ is a special input vector, for which the matrix $A$ acts as a scaling: $$ A\vec{e}_\lambda = \lambda\vec{e}_\lambda, $$ where $\lambda$ is called the *eigenvalue* and $\vec{e}_\lambda$ is the corresponding eigenvector. ''' ''' ''' A = Matrix([[1, 5], [5, 1]]) A ''' ''' A*Vector([1,0]) ''' ''' A*Vector([1,1]) ''' ''' ''' The *characterisitic polynomial* of the matrix $A$ is defined as $$ p(\lambda) \equiv \det(A-\lambda \mathbb{1}). $$ ''' ''' ''' l = symbols('lambda') (A-l*eye(2)).det() ''' ''' # the roots of the characteristic polynomial are the eigenvalues of A solve( (A-l*eye(2)).det(), l) ''' ''' # or call `eigenvals` method A.eigenvals() ''' ''' A.eigenvects() # can also find eigenvects using (A-6*eye(2)).nullspace() and (A+4*eye(2)).nullspace() ''' ''' Q, Lambda = A.diagonalize() Q, Lambda ''' ''' Q*Lambda*Q.inv() # == eigendecomposition of A ''' ''' ''' ''' ''' ''' ''' ## Special types of matrices ''' ''' ''' ''' page 271 ''' ''' ''' ''' ''' ''' ''' ''' ## Abstract vectors paces ''' ''' ''' ''' Generalize vector techniques to other vector like quantities. Allow us to talk about basis, dimention, etc. page 277 ''' ''' ''' ''' ''' ''' ''' ''' ## Abstract inner product spaces ''' ''' ''' ''' Use geometrical notions like length and orthogonaloty for abstract vectors. page 281 ''' ''' ''' ''' ''' ''' ''' ''' ## Gram–Schmidt orthogonalization ''' ''' ''' ''' page 288 ''' ''' ''' ''' ''' ''' ''' ''' ## Matrix decompositions ''' ''' ''' ''' page 292 ''' ''' ''' ''' ''' ''' ''' ''' ## Linear algebra with complex numbers ''' ''' ''' ''' page 298 ''' ''' ''' ''' ''' ''' ''' ''' # Applications ''' ''' ''' ''' - Chapter 7: General applications - Chapter 8: Probability theory - Chapter 9: Quantum mechanics ''' ''' ''' ''' ''' ''' ''' ''' # Notation appendix ''' ''' ''' ''' Check out page 499 for notation. ''' ''' ''' ''' ''' '''
def cells(): ''' # 4/ Problem solutions ''' ''' ''' # helper code needed for running in colab if 'google.colab' in str(get_ipython()): print('Downloading plot_helpers.py to util/ (only neded for colab') !mkdir util; wget https://raw.githubusercontent.com/minireference/noBSLAnotebooks/master/util/plot_helpers.py -P util ''' ''' # setup SymPy from sympy import * init_printing() # setup plotting %matplotlib inline import matplotlib.pyplot as mpl from util.plot_helpers import plot_plane, plot_line, plot_vec, plot_vecs # aliases 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 ''' ''' ''' ## P4.2 Find the lines of intersection between these pairs of planes: **a)** $P_1$: $3x-2y-z=2$ and $P_2$: $x+2y+z=0$, **b)** $P_3$: $2x+y-z=0$ and $P_4$: $x+2y+z=3$. <!-- \begin{answer}\textbf{a)}~; \textbf{b)}~.\end{answer} % A = Matrix([[1,-2,-1,2],[1,2,1,0]]) % A.rref() % --> ''' ''' ''' # a) x y z | c A = Matrix([[3, -2, -1, 2], [1, 2, 1, 0]]) A.rref() ''' ''' ''' So $z=s$ is a free variable, and the rest of the equation can be written as $$ \begin{array}{rl} x &= \frac{1}{2}\\ y + \frac{1}{2}s &= -\frac{1}{4}\\ z &= s \end{array} $$ The answer is $(x,y,z) = (\frac{1}{2},-\frac{1}{4},0) + s(0,-\frac{1}{2},1), \forall s \in \mathbb{R}$. ''' ''' ''' # b) x y z | c B = Matrix([[2, 1, -1, 0], [1, 2, 1, 3]]) B.rref() ''' ''' ''' The free variable is $z=t$. The answer to b) is $\{ (-1,2,0) + t(1,-1,1), \forall t \in \mathbb{R}\}$. ''' ''' ''' ''' ## P4.11 ''' ''' ''' v = Vector([3, 4, 1]) normal = Vector([2, -1, 4]) vPperp = (normal.dot(v)/normal.norm()**2)*normal print('vPperp =', vPperp) vP = v - vPperp print('vP =', vP) plot_plane(normal, 0) # plane P plot_vec(0.2*normal, color='r') # its normal vec plot_vecs(v, vPperp, vP) ''' ''' v = Vector([3, 4, 1]) normal = Vector([2, -1, 4]) D=4 # point on P closest to the origin alpha = D/normal.norm()**2 p_closest = alpha*normal # print('len normal', normal.norm()) # print('p_closest', p_closest) assert p_closest.dot(normal) == 4 vPperp = (normal.dot(v)/normal.norm()**2)*normal print('vPperp', vPperp) v_wrong = v - vPperp print('v_wrong', v_wrong) plot_plane(normal, D) # plane P plot_vec(0.2*normal, at=p_closest, color='r') # its normal vec plot_vecs(v, vPperp, v_wrong) ax = mpl.gca() ax.grid(True,which='both') ''' ''' v = Vector([3, 4, 1]) normal = Vector([2, -1, 4]) D = 4 # some point on P p0 = Point([2,0,0]) u = v - p0 # vector from p0 to tip of v uPperp = (normal.dot(u)/normal.norm()**2)*normal print('uPperp', uPperp) uInP = u - uPperp proj_v_on_P = p0 + uInP print('proj_v_on_P', proj_v_on_P) plot_plane(normal, D) # plane P plot_vec(0.2*normal, at=p_closest, color='r') # its normal vec plot_vec(v) plot_vec(u, at=p0, color='r') plot_vec(uPperp, at=p0, color='b') plot_vec(uInP, at=p0, color='g') plot_vec(proj_v_on_P, color='y') ax = mpl.gca() ax.grid(True,which='both') ''' ''' '''