Week 4 - Matrices make linear mappings

Matrices make linear mappings

Matrices as objects that map one vector onto another; all the types of matrices

Introduction: Einstein summation convention and the Symmetry of the dot product

  • Einstein Summation Convention (00:00-04:24)

    • We can represent a Matrix Multiplication between matrix AA and matrix BB like this:

      [a11a12a13a21a22a23a31a32a33][b11b12b13b21b22b23b31b32b33]=AB\begin{bmatrix}a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33}\end{bmatrix} \begin{bmatrix}b_{11} & b_{12} & b_{13} \\ b_{21} & b_{22} & b_{23} \\ b_{31} & b_{32} & b_{33}\end{bmatrix} = AB

      We can think of one of the elements in row 2, column 3 of ABAB as:

      (ab)23=a21b13+a22b23+a23b33{(ab)}_{23} = a_{21}b_{13} + a_{22}b_{23} + a_{23}b_{33}

      We can rewrite that as the sum of elements jj:

      abik=jaijbjkab_{ik} = \sum\limits_{j} a_{ij} \cdot b_{jk}

      In Einstein's convention, if you have a repeated index like jj you can remove it alongside the Sigma notation:

      abik=aijbjkab_{ik} = a_{ij} \cdot b_{jk}

      The Einstein Summation Convention also describes a way to code Matrix Multiplication.

  • Matrix Multiplication with rectangular matrices (04:24-05:55)

    • We can multiply rectangular matrices as long as the columns in the first are equal to the rows in the 2nd.

      A(2×3)B(3×4)A_{(2 \times 3)} \cdot B_{(3 \times 4)}

      The Einstein Summation Convention shows those operations work. As long as both matrices have the same number of jjs, you can do it.

      Cik=aijbjkC_{ik} = a_{ij}b_{jk}

  • Dot Product revisited using Einstein's Summation Convention (05:55-07:14)

    • The dot product between vectors u\vec{u} and v\vec{v} in the summation convention is: uiviu_{i}v_{i}
    • We can also consider a dot product a matrix product of a row vector and a column vector:

      Dot product as matrix multiplication

      So there's some equivalence between Matrix Multiplication and the Dot Product.

  • Symmetry of the Dot Product (07:15-09:32)

    • If we have unit vector u^=[u1u2]\hat{u} = \begin{bmatrix}u_1 \\ u_2\end{bmatrix} and we do a projection onto one of the axis vectors: e1^=[10]\hat{e_1} = \begin{bmatrix}1 \\ 0\end{bmatrix}, we get a length of u1u_1

      Projection onto axis vectors

      • If we then project e1e_1 onto u^\hat{u}, we can draw a line of Symmetry between where the two projections cross:

        Symmetry of dot product

        • The two triangles on either side are the same size, proving that the projection is the same length in either direction. So that proves the Dot Product is symmetrical and also that projection is the Dot Product.
          • That explains why matrix multiplication with a vector is considered a projection onto the vectors composing the matrix (the matrix columns).

Matrics transform into the new basis vector set

Matrics changing basis

  • Transforming a Vector between Basis Vectors (00:00-08:31)

    • We can think of the columns of a transformation matrix, as the axes of the new basis vectors described in our coordinate system.
      • Then, how do we transform a vector from one set of basis vectors to another?
    • If we have 2 basis vectors that describe the world of Panda bear in at [31]\begin{bmatrix}3 \\ 1\end{bmatrix} and [11]\begin{bmatrix}1 \\ 1\end{bmatrix}. Noting that these vectors are describes in the normal coordinate system with basis vectors [01]\begin{bmatrix}0 \\ 1\end{bmatrix} and [10]\begin{bmatrix}1 \\ 0\end{bmatrix}

      Pandas basis vectors

      • In Panda's world, those basis vectors are [10]\begin{bmatrix}1 \\ 0\end{bmatrix} and [01]\begin{bmatrix}0 \\ 1\end{bmatrix}
        • If we have a vector described in Panda's world as 12[31]\frac{1}{2} \begin{bmatrix}3 \\ 1\end{bmatrix}, we can get it in our frame, by multiplying with Panda's basis vectors: [3111][3212]=[52]\begin{bmatrix}3 & 1 \\ 1 & 1\end{bmatrix} \begin{bmatrix}\frac{3}{2} \\ \frac{1}{2} \end{bmatrix} = \begin{bmatrix}5 \\ 2 \end{bmatrix}
      • Which begs the question: how can we translate between Panda's world into our world?
        • Using the Matrix Inverse of Panda's basis vector matrix we can get our basis in Bear's world:

      B1=12[1113]B^{-1} = \frac{1}{2} \begin{bmatrix}1 & -1 \\ -1 & 3\end{bmatrix}

      • If we use that to transform the vector in our world, we should get it in Bear's world.

        12[1113][52]=[3212]\frac{1}{2} \begin{bmatrix}1 & -1 \\ -1 & 3\end{bmatrix} \begin{bmatrix}5 \\ 2\end{bmatrix} = \begin{bmatrix}\frac{3}{2} \\ \frac{1}{2}\end{bmatrix}

  • Translate between basis vectors using projections (08:32-11:14)

    • If the new basis vectors are orthogonal then we can translate between bases using only the dot product.

Doing a transformation in a changed basis

  • Doing a transformation of a Vector in a changed basis (00:00-04:13)

    • How would you do a 45° rotation in Panda's basis?
    • You could first do it in a normal basis.

      12[1111]\frac{1}{\sqrt{2}} \begin{bmatrix}1 & -1 \\ 1 & 1\end{bmatrix}

    • And multiply that by the vector in our basis:

      12[1111][3111][xy]\frac{1}{\sqrt{2}} \begin{bmatrix}1 & -1 \\ 1 & 1\end{bmatrix} \begin{bmatrix}3 & 1 \\ 1 & 1\end{bmatrix} \begin{bmatrix}x \\ y\end{bmatrix}

    • That gives us the transformation in our basis vector. You can then multiply that by the inverse of the coordinate matrix, to get it in Panda's coordinate system.

      12[1113]12[1111][3111][xy]\frac{1}{2} \begin{bmatrix}1 & -1 \\ -1 & 3\end{bmatrix} \frac{1}{\sqrt{2}} \begin{bmatrix}1 & -1 \\ 1 & 1\end{bmatrix} \begin{bmatrix}3 & 1 \\ 1 & 1\end{bmatrix} \begin{bmatrix}x \\ y\end{bmatrix}

    • In short: B1RB=RbB^{-1} R B = R_{b}

Making Multiple Mappings, deciding if these are reversible

Orthogonal matrices

  • Matrix Transpose (00:15-01:08)
    • An operation where we interchange the rows and columns of a matrix.
    • ATij=Aji{A^{T}}_{ij} = A_{ji}
    • [1234]T=[1324]\begin{bmatrix}1 & 2 \\ 3 & 4\end{bmatrix}^{T} = \begin{bmatrix}1 & 3 \\ 2 & 4\end{bmatrix}
  • Orthonormal Basis Set (01:09-06:35)
    • If you have a square matrix with vectors that are basis vectors in new space, with the condition that the vectors are orthogonal to each other and they're unit length (1)
      • In math: aiaj=0,ija_i \cdot a_j = 0, i \neq j aiaj=1,i=ka_i \cdot a_j = 1, i = k
      • When you multiple one of these matrices by their transpose, the identity matrix is returned.
        • That means ATA^{T} is a valid identity for these examples.
      • A matrix composed of these is called Orthogonal Matrix.
      • The transpose of these matrices is another orthogonal matrix.
      • The determinant of these is 1 or -1.
      • In Data Science, we want an orthonormal basis set where ever possible.

Recognising mapping matrices and applying these to data

The Gram-Schmidt process

  • Gram-Schmidt Process (00:00-06:07)
    • If you have vector set v=v1,v2...vnv = {v_1, v_2 ... v_n} that span your space and are linear independent (don't have a determinate of 0) but aren't orthogonal or unit length, you can convert them into an Orthonormal Basis Set using the Gram-Schmidt Process.
    • Process:
      • Take the first vector in the set, v1v_1 and normalise so it's of unit length giving you e1e_1 the first basis vector: e1=v1v1e_1 = \frac{v_1}{|v_1|}
      • We can think of v2v_2 as having a component in the direction of e1e_1 and a component that's perpendicular.

        e2 in Gram-Schmidt

        • We can find the component in the direction of e1e_1 by finding the vector projection of v2v_2 onto e1e_1: v2e1e1\frac{v_2 \cdot e_1}{|e_1|}
          • To get as a vector we multiply by e1e_1 (noting that it's already of unit length): v2e1e1e1\frac{v_2 \cdot e_1}{|e_1|} e_1
          • We know that v2v_2 is equal to that + the perpendicular component: v2=v2e1e1e1+u2v_2 = \frac{v_2 \cdot e_1}{|e_1|} e_1 + u_2
          • We can rearrange the expression to find u2u_2: u2=v2(v2e1)e1u_2 = v_2 - (v_2 \cdot e_1) e_1
          • If we normalise u2u_2: u2u2\frac{u_2}{|u_2|} the result is e2e_2
            • Now to find e3e_3, which we know isn't a linear combination of e1e_1 and e2e_2:
        • We can project is onto the plane of e1e_1 and e2e_2, which will result in a vector in the plane composed of e2e_2 and e1e_1s.
        • We can then find the components of v3v_3 that aren't made up of v1v_1 and v2v_2:
          • u3=v3(v3e1)e1(v3e2)e2u_3 = v_3 - (v_3 \cdot e_1)e_1 - (v_3 \cdot e_2)e_2
          • If we normalise u3u_3, we get e3e_3: e3=u3u3e_3 = \frac{u_3}{|u_3|} we have another unit vector that's normal to the plane.
            • We can keep doing this through all vnv_n until we have basis vectors that span the space.

Example: Reflecting in a plane

  • Example: performing a rotation on a vector with an unfamilar plane.
    • Create Orthogonal Matrix plane with Gram-Schmidt Process.
  • Challenge: performing a rotation on a vector with an unfamiliar plane.
    • You know 2 vectors in the space: [111]\begin{bmatrix}1 \\ 1 \\ 1\end{bmatrix} and [201]\begin{bmatrix}2 \\ 0 \\ 1\end{bmatrix}
    • You have a 3rd vector out of the mirror's plane: [311]\begin{bmatrix}3 \\ 1 \\ -1\end{bmatrix}
    • First part, find basis vectors:
      • Find e1e_1: normalise v1v_1 to find e1e_1: e1=v1v1=13[111]e_1 = \frac{v_1}{|v_1|} = \frac{1}{\sqrt{3}} \begin{bmatrix}1 \\ 1 \\ 1 \end{bmatrix}
      • Then e2e_2, starts with u2u_2: u2=v2(v2e1)e1u_2 = v_2 - (v_{2} \cdot e_{1}) * e_{1} then normalise that.
      • Lastly, e3e_3: is the normalised: u3=v3(v3e1)e1(v3e2)e2u_3 = v_3 - (v_{3} \cdot e_{1}) e_{1} - (v_{3} \cdot e_{2}) e_{2}
      • Result is a transformation matrix described by the basis refactors: E=[(e1)(e2)(e3)]=(13[111]12[110]16[112])E = \begin{bmatrix}(e_1) & (e_2) & (e_3) \end{bmatrix} = \left(\frac{1}{\sqrt{3}} \begin{bmatrix}1 \\ 1 \\ 1 \end{bmatrix} \frac{1}{\sqrt{2}} \begin{bmatrix}1 \\ -1 \\ 0\end{bmatrix} \frac{1}{\sqrt{6}} \begin{bmatrix}1 \\ 1 \\ -2 \end{bmatrix} \right)
      • We can rotate a vector by 45° in a single plane, we can flip only the third column vector: TE=[100010001]T_{E} = \begin{bmatrix}1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & -1\end{bmatrix}
    • Now, we need to use this to convert a vector rr and want to apply to a transformation matrix but in a different space to make: rr'
      • Going from rr to rr' is hard.
      • But if you first convert rr into the ee basis, then apply transformation and convert back into rr, it's easy:

        Reflecting in a plane

        • ETEE1r=rE T_{E} E^{-1} r = r'
          • Because ee is orthonormal, we know the transpose is the inverse.