Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Laboratory 04

Matrix Algebra

Statisticians and data scientists use the language of vectors and matrices to organize data and work with models. We will take a close look at matrix algebra, taking time to relate matrix algebra to the more familar algebra that you have worked with in the past.

Recap on vectors and operations on vectors

Definition

A vector vv is an ordered list of real numbers. We denote a vector with a lower case letter and enclose the values in the vector with square brackets. We can write a vector vv as a row vector

v=[1,2,3,4,5]\begin{aligned} v = [1,2,3,4,5] \end{aligned}

or as a column vector

v=[12345]\begin{aligned} v = \left [ \begin{matrix} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ \end{matrix} \right ] \end{aligned}

.

A vector has a length, defined as the number of values included in that vector. For example, the above vector is length 5.

We can create a vector in R by using the c operator.

``` {}
\PY{n}{v} \PY{o}{=}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{)}
```

Vector times a scalar

A vector of length one is called a scalar. We can define the results of multiplying a vector v=[1,2,3,4]v=[1,2,3,4] by a scalar α\alpha as each entry of the vector times the scalar α\alpha.

αv=[α×1α×2α×3α×4]\begin{aligned} \alpha v = \left [ \begin{matrix} \alpha \times 1 \\ \alpha \times 2 \\ \alpha \times 3 \\ \alpha \times 4 \\ \end{matrix} \right ] \end{aligned}

For example, the vector v=[1,2,3,4,5]v = [1,2,3,4,5] times the scalar α=7\alpha=7 will result in a vector

αv=[7×1,7×2,7×3,7×4]=[7,14,21,28]\begin{aligned} \alpha v &= [7 \times 1, 7 \times 2, 7 \times 3, 7 \times 4 ]\\ &= [7, 14, 21, 28] \end{aligned}

R understands how to multiply vectors and scalars with no additional syntax.

``` {}
\PY{n}{v} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{)}
\PY{n}{alpha} \PY{o}{=} \PY{l+m}{7}

\PY{n}{alpha}\PY{o}{*}\PY{n}{v}
```
7

14

21

28

Vector plus/minus a vector

A vector vv plus a vector qq creates a new vector that adds the individual entries of vv and qq

v=[4,−1,7]q=[0,32,9]v+q=[4+0,−1+32,7+9]=[4,31,16]\begin{aligned} v &= [4,-1,7] \\ q &= [0,32,9] \\ v + q &= [ 4+0, -1+32, 7+9] = [4,31,16] \end{aligned}

A vector vv minus a vector qq creates a new vector that subtracts the individual entries of qq from vv

v=[4,−1,7]q=[0,32,9]v−q=[4−0,−1−32,7−9]=[4,−33,−2]q−v=[0−4,32−(−1),9−7]=[−4,33,2]\begin{aligned} v &= [4,-1,7] \\ q &= [0,32,9] \\ v - q &= [ 4-0, -1-32, 7-9] = [4,-33,-2]\\ q - v &= [ 0-4, 32-(-1), 9-7] = [-4,33,2] \end{aligned}

R also understands vector addition and subtraction with no special syntax.

``` {}
\PY{n}{v} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{4}\PY{p}{,} \PY{l+m}{\PYZhy{}1}\PY{p}{,} \PY{l+m}{7}\PY{p}{)}
\PY{n}{q} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{0}\PY{p}{,} \PY{l+m}{32}\PY{p}{,} \PY{l+m}{9}\PY{p}{)}

\PY{n}{add} \PY{o}{=} \PY{n}{v}\PY{o}{+}\PY{n}{q}
\PY{n}{subtract} \PY{o}{=} \PY{n}{v}\PY{o}{\PYZhy{}}\PY{n}{q}

\PY{n+nf}{print}\PY{p}{(}\PY{n}{add}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{subtract}\PY{p}{)}
```
[1]  4 31 16
[1]   4 -33  -2

The Matrix

A matrix is an ordered list of vectors.

M=[123456321390230]\begin{aligned} M = \left [ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 3 & 2 & 13 \\ 90 & 23 & 0 \\ \end{matrix} \right ] \end{aligned}

A matrix has a dimension which is an ordered pair of numbers: the first number indicates the number of rows of the matrix and the second number indicates the number of columns. For example, he matrix MM above has dimension (4,3)(4,3).

To build a matrix in R we can issue the matrix command. Matrix is a function that takes a vector as an argument and can take one of many optional arguments.

If we give the matrix function only a vector, the default behavior of this function is to create a vector.

``` {}
\PY{n}{A} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{,}\PY{l+m}{6}\PY{p}{)}\PY{p}{)}
\PY{n}{A}
```

A matrix: 6 × 1 of type dbl


1 2 3 4 5 6

This is because a vector can be thought of as a matrix with one column. In other words, a matrix with dimension (N,1)(N,1) is a vector of length NN.

We can provide another arguement to the matrix function---ncol---to specify the number of columns we want to create.

``` {}
\PY{n}{A} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{,}\PY{l+m}{6}\PY{p}{)}\PY{p}{,} \PY{n}{ncol}\PY{o}{=}\PY{l+m}{2}\PY{p}{)}
\PY{n}{A}
```

A matrix: 3 × 2 of type dbl


1 4 2 5 3 6


Above we used the ncol argument to specify a matrix with two columns. R will automatically determine the number of rows for the matrix and fill-in the values of the matrix column by column.

If we want, we can ask R to fill in values of the matrix row by row by including an additional argument in the matrix functionbyrow

``` {}
\PY{n}{v} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{,}\PY{l+m}{6}\PY{p}{)}
\PY{n}{A} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n}{v}\PY{p}{,} \PY{n}{ncol}\PY{o}{=}\PY{l+m}{2}\PY{p}{)}

\PY{n}{B} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n}{v}\PY{p}{,} \PY{n}{ncol}\PY{o}{=}\PY{l+m}{2}\PY{p}{,} \PY{n}{byrow}\PY{o}{=}\PY{k+kc}{TRUE}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{n}{A}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{B}\PY{p}{)}
```
[,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6

We see that matrix A was “column filled” and matrix B was “row filled”.

There are similar operations for matrices as there are for vectors.

Matrix plus/minus a matrix

Given a matrix AA and matrix BB, the sum of AA and BB is a new matrix CC where the i,j entry of CC (CijC_{ij}) is the sum of the corresponding entries from AA and BB (Cij=Aij+BijC_{ij} = A_{ij} + B_{ij}). To add two matrices they must have the same dimension.

A=[123456]B=[654321]C=A+B=[1+62+53+44+35+26+1]=[777777]D=A−B=[1−62−53−44−35−26−1]=[−5−3−1135]\begin{aligned} A &= \left [ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ \end{matrix} \right ] \\ B &= \left [ \begin{matrix} 6 & 5 & 4 \\ 3 & 2 & 1 \\ \end{matrix} \right ] \\ % C &= A + B = \left [ \begin{matrix} 1+6 & 2+5 & 3+4 \\ 4+3 & 5+2 & 6+1 \\ \end{matrix} \right ] = \left [ \begin{matrix} 7 & 7 & 7 \\ 7 & 7 & 7 \\ \end{matrix} \right ] \\ D &= A - B = \left [ \begin{matrix} 1-6 & 2-5 & 3-4 \\ 4-3 & 5-2 & 6-1 \\ \end{matrix} \right ] = \left [ \begin{matrix} -5 & -3 & -1 \\ 1 & 3 & 5 \\ \end{matrix} \right ] \\ \end{aligned}

R understands matrix addition and subtraction without any additional syntax.

``` {}
\PY{n}{A} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{\PYZhy{}1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{8}\PY{p}{)}\PY{p}{,} \PY{n}{ncol}\PY{o}{=}\PY{l+m}{3}\PY{p}{)}
\PY{n}{D} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{90}\PY{p}{,}\PY{l+m}{0.34}\PY{p}{,}\PY{l+m}{1.54}\PY{p}{,}\PY{l+m}{\PYZhy{}9.43}\PY{p}{,}\PY{l+m}{10}\PY{p}{,}\PY{l+m}{0}\PY{p}{)}\PY{p}{,} \PY{n}{ncol}\PY{o}{=}\PY{l+m}{3}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{n}{A}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{D}\PY{p}{)}

\PY{n}{A}\PY{o}{+}\PY{n}{D}
```
[,1] [,2] [,3]
[1,]   -1    4    3
[2,]    2    1    8
      [,1]  [,2] [,3]
[1,] 90.00  1.54   10
[2,]  0.34 -9.43    0

A matrix: 2 × 3 of type dbl


89.00 5.54 13 2.34 -8.43 8


``` {}
\PY{n}{A}\PY{o}{\PYZhy{}}\PY{n}{D}
```

A matrix: 2 × 3 of type dbl


-91.00 2.46 -7 1.66 10.43 8


Matrix times a Matrix

Two matrices AA and BB can be multiplied together C=ABC = AB if the number of columns of AA is equal to the number of rows of BB. In other words, if the dimension of A is (a,b) and the dimension of B is (c,d) then b and c must be equal.

The i,j entry of this product, CijC_{ij} is the following sum of products:

Ci,j=ai,1b1,j+ai,2b2,j+ai,3b3,j+⋯+ai,NbN,j=∑e=1Nai,ebe,j\begin{aligned} C_{i,j} &= a_{i,1}b_{1,j} + a_{i,2}b_{2,j} + a_{i,3}b_{3,j} + \cdots + a_{i,N}b_{N,j}\\ &= \sum_{e=1}^{N} a_{i,e}b_{e,j} \end{aligned}

For example, suppose that

A=[1234]\begin{aligned} A = \left [ \begin{matrix} 1 & 2\\ 3 & 4 \\ \end{matrix} \right] \end{aligned}

and that

B=[12−134−2].\begin{aligned} B = \left [ \begin{matrix} 1 & 2 & -1\\ 3 & 4 & -2 \\ \end{matrix} \right] . \end{aligned}

Then the product C=ABC = AB equals

C=AB=[1∗1+2∗31∗2+2∗41∗−1+2∗−23∗1+4∗33∗2+4∗43∗−1+4∗−2]=[710−51522−11]\begin{aligned} C = AB = \left [ \begin{matrix} 1*1 + 2*3 & 1*2 + 2*4 & 1*-1 + 2*-2 \\ 3*1 + 4*3 & 3*2 + 4*4 & 3*-1 + 4*-2 \\ \end{matrix} \right] = \left [ \begin{matrix} 7 & 10 & -5 \\ 15 & 22 & -11 \\ \end{matrix} \right] \end{aligned}

The above definition and computation can feel cumbersome. We can simplify the above calculations by introducing the inner product.

The inner product between two vectors vv and qq, or v′qv'q, is

v=[1,2,3]q=[4,5,6]v′q=1⋅4+2⋅5+3⋅6=4+10+18=32\begin{aligned} v &= [1,2,3]\\ q &= [4,5,6]\\ \\ v'q &= 1 \cdot 4 + 2 \cdot 5 + 3 \cdot 6 \\ &= 4 + 10 + 18 = 32 \end{aligned}

Let us use the inner product to simplify the above matrix multiplication. First, we rewrite the matrix A as a stack of two row vectors

A=[a1a2]\begin{aligned} A = \begin{bmatrix} a_{1} \\ a_{2} \\ \end{bmatrix} \end{aligned}

where a1=[1,2]a_{1} = [1 , 2] and a2=[3,4]a_{2} = [3,4]

Second, we rewrite the matrix B as a stack of three column vectors

B=[b1b2b3]\begin{aligned} B = \begin{bmatrix} b_{1} & b_{2} & b_{3} \end{bmatrix} \end{aligned}

where $b_{1} =

1\
3

$, $b_{2} =

2\
4

$, and $b_{3} =

-1\
-2

$

Then the product AB is a matrix of inner products

C=[a1′b1a1′b2a1′b3a2′b1a2′b2a2′b3]\begin{aligned} C = \begin{bmatrix} a_{1}'b_{1} & a_{1}'b_{2} & a_{1}'b_{3} \\ a_{2}'b_{1} & a_{2}'b_{2} & a_{2}'b_{3} \\ \end{bmatrix} \end{aligned}

Matrix multiplication is different than mulitplication between two variables that represent real numbers because matrix multiplication is not commutative---the product ABAB is not rarely equal to BABA.

To compute the product of two matrices in R we need the %*% operator

``` {}
\PY{n}{A} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{\PYZhy{}1}\PY{p}{,}\PY{l+m}{0}\PY{p}{,}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{\PYZhy{}2}\PY{p}{,}\PY{l+m}{\PYZhy{}1}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{,}\PY{l+m}{6}\PY{p}{)}\PY{p}{,}\PY{n}{ncol}\PY{o}{=}\PY{l+m}{3}\PY{p}{)}
\PY{n}{B} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{,}\PY{l+m}{6}\PY{p}{,}\PY{l+m}{7}\PY{p}{,}\PY{l+m}{8}\PY{p}{,}\PY{l+m}{9}\PY{p}{)}\PY{p}{,}\PY{n}{ncol}\PY{o}{=}\PY{l+m}{3}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{n}{A}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{B}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{n}{A}\PY{o}{\PYZpc{}*\PYZpc{}}\PY{n}{B}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{B}\PY{o}{\PYZpc{}*\PYZpc{}}\PY{n}{A}\PY{p}{)}
```
[,1] [,2] [,3]
[1,]   -1   -2    4
[2,]    0   -1    5
[3,]    1    3    6
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
     [,1] [,2] [,3]
[1,]    7   10   13
[2,]   13   25   37
[3,]   25   55   85
     [,1] [,2] [,3]
[1,]    6   15   66
[2,]    6   15   81
[3,]    6   15   96

Note above that we computed the product AB and BA and these products resulted in different matrices.

Matrix times a vector

A matrix AA with dimension (r,c)(r,c) can be multiplied by a vector vv if the length of vv is cc. Then the product AvAv is a vector with the the ith^{th} entry of AvAv defined as

(Av)i=∑k=1cAi,kvk\begin{aligned} (Av)_{i} = \sum_{k=1}^{c} A_{i,k} v_{k} \end{aligned}

For example, Let the matrix

A=[123456]\begin{aligned} A = \left [ \begin{matrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \\ \end{matrix} \right ] \end{aligned}

and the vector

v=[4−3]\begin{aligned} v = \left [ \begin{matrix} 4 \\ -3 \\ \end{matrix} \right ] \end{aligned}

then

Av=[1∗4+2∗−33∗4+4∗−35∗4+6∗−3]=[−202]\begin{aligned} Av = \left [ \begin{matrix} 1*4+2*-3 \\ 3*4+4*-3 \\ 5*4+6*-3 \\ \end{matrix} \right ] = \left [ \begin{matrix} -2 \\ 0 \\ 2 \\ \end{matrix} \right ] \end{aligned}

The definition above can also be simplifed by using inner products. Let

A=[a1a2a3]\begin{aligned} A = \begin{bmatrix} a_{1} \\ a_{2} \\ a_{3} \end{bmatrix} \end{aligned}

where a1=[1,2]a_{1} = [1,2], a2=[3,4]a_{2} = [3,4], and a3=[5,6]a_{3} = [5,6]. Then the product AvAv simplifies to

Av=[a1′va2′va3′v]\begin{aligned} Av = \begin{bmatrix} a_{1}'v\\ a_{2}'v\\ a_{3}'v \end{bmatrix} \end{aligned}

We can use the %*% operator in R to multiply together a matrix and a vector

``` {}
\PY{n}{A} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{,}\PY{l+m}{6}\PY{p}{)}\PY{p}{,} \PY{n}{ncol}\PY{o}{=}\PY{l+m}{2}\PY{p}{,} \PY{n}{byrow}\PY{o}{=}\PY{k+kc}{TRUE}\PY{p}{)}
\PY{n}{v} \PY{o}{=} \PY{n+nf}{c}\PY{p}{(}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{\PYZhy{}3}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{Matrix A\PYZdq{}}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{A}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{Vector v\PYZdq{}}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{v}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{Av\PYZdq{}}\PY{p}{)}
\PY{n}{product} \PY{o}{=} \PY{n}{A}\PY{o}{\PYZpc{}*\PYZpc{}}\PY{n}{v}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{product}\PY{p}{)}
```
[1] "Matrix A"
     [,1] [,2]
[1,]    1    2
[2,]    3    4
[3,]    5    6
[1] "Vector v"
[1]  4 -3
[1] "Av"
     [,1]
[1,]   -2
[2,]    0
[3,]    2

Matrix Transpose

Given a matrix MM, the tranpose of MM, M′M^{'} or MTM^{T}, is a matrix where the first row of M′M^{'} corresponds to the first column of MM, the second row of M′M^{'} corresponds to the second columns of MM and so on. If MM has dimension (r,c)(r,c) than M′M^{'} has dimensions (c,r)(c,r).

For example, if

M=[123456321390230]\begin{aligned} M = \left [ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 3 & 2 & 13 \\ 90 & 23 & 0 \\ \end{matrix} \right ] \end{aligned}

then the tranpose of MM,

M′=[143902522336130]\begin{aligned} M^{'} = \left [ \begin{matrix} 1 & 4 & 3 & 90 \\ 2 & 5 & 2 & 23 \\ 3 & 6 & 13 & 0 \\ \end{matrix} \right ] \end{aligned}

We can use the t() operator to transpose a matrix in R.

``` {}
\PY{n}{M} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(} \PY{l+m}{1}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{,}\PY{l+m}{6}\PY{p}{,}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{2}\PY{p}{,}\PY{l+m}{13}\PY{p}{,}\PY{l+m}{90}\PY{p}{,}\PY{l+m}{23}\PY{p}{,}\PY{l+m}{0}\PY{p}{)}\PY{p}{,}\PY{n}{ncol}\PY{o}{=}\PY{l+m}{3}\PY{p}{,}\PY{n}{byrow}\PY{o}{=}\PY{k+kc}{TRUE}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{The matrix M\PYZdq{}}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{M}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{The transpose\PYZdq{}}\PY{p}{)}
\PY{n}{Mt} \PY{o}{=} \PY{n+nf}{t}\PY{p}{(}\PY{n}{M}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{n}{Mt}\PY{p}{)}
```
[1] "The matrix M"
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    3    2   13
[4,]   90   23    0
[1] "The transpose"
     [,1] [,2] [,3] [,4]
[1,]    1    4    3   90
[2,]    2    5    2   23
[3,]    3    6   13    0

Matrix inverse

For a super fun overview of matrix multiplication and the concept of identities and inverses, click here!

The identity matrix

The identity matrix of dimension rr, usually labled IrI_{r}, is a matrix with rr rows and rr columns such that the diagonal elements of the matrix, the (1,1) entry, (2,2) entry, up to (r,r) entry are the number 1 and all other entries are 0. For example,

I2=[1001]\begin{aligned} I_{2} = \left [ \begin{matrix} 1 & 0 \\ 0 & 1 \\ \end{matrix} \right ] \end{aligned}

This matrix is called the identity matrix because any matrix AA times II returns AA. To be more precise, this matrix is the multiplicative identity matrix. But the adjective multiplicative is usually dropped.

The inverse matrix

The inverse of a matrix AA---A−1A^{-1}---is a matrix such that, if AA has the same number of rows and columns (called square) then

AA−1=A−1A=I\begin{aligned} A A^{-1} = A^{-1}A = I \end{aligned}

The idea of a matrix inverse is the same as in algebra. If we have a variable aa then the inverse of aa, called a−1a^{-1} is the unique number such that

a⋅a−1=a−1⋅a=1.a \cdot a^{-1} = a^{-1} \cdot a = 1.

In matrix algebra, the identity matrix takes the place of the “1” in algebra.

We can compute the inverse of a matrix AA in R using the solve function.

``` {}
\PY{n}{A} \PY{o}{=} \PY{n+nf}{matrix}\PY{p}{(}\PY{n+nf}{c}\PY{p}{(}\PY{l+m}{3}\PY{p}{,}\PY{l+m}{4}\PY{p}{,}\PY{l+m}{5}\PY{p}{,}\PY{l+m}{6}\PY{p}{)}\PY{p}{,}\PY{n}{ncol}\PY{o}{=}\PY{l+m}{2}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{A\PYZdq{}}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{A}\PY{p}{)}

\PY{n+nf}{print}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{A inverse\PYZdq{}}\PY{p}{)}
\PY{n}{Ainverse} \PY{o}{=} \PY{n+nf}{solve}\PY{p}{(}\PY{n}{A}\PY{p}{)}
\PY{n+nf}{print}\PY{p}{(}\PY{n}{Ainverse}\PY{p}{)}
```
[1] "A"
     [,1] [,2]
[1,]    3    5
[2,]    4    6
[1] "A inverse"
     [,1] [,2]
[1,]   -3  2.5
[2,]    2 -1.5

Assignment

  1. Define the vector v of length 3 with the following numbers: -1,0,1

  2. Define the matrix A with dimension (2,3) by filling, column wise, the values: 1,2,3,4,5,6

  3. Multiply A by v (Av)

  4. Multiply v by A (vA). Why does R return an error?

  5. Write a function that takes two argument which are both matrices and returns the product of these matrices.