Matrix is a set of objects arranged in form of the table of size \(n\times m\).
\[ A+B = \left(\begin{array}{cc}a_{11}&a_{12}\\a_{21}&a_{22}\\a_{31}&a_{32}\\a_{41}&a_{42}\end{array}\right)+\left(\begin{array}{cc}b_{11}&b_{12}\\b_{21}&b_{22}\\b_{31}&b_{32}\\b_{41}&b_{42}\end{array}\right)=\left(\begin{array}{cc}a_{11}+b_{11}&a_{12}+b_{12}\\a_{21}+b_{21}&a_{22}+b_{22}\\a_{31}+b_{31}&a_{32}+b_{32}\\a_{41}+b_{41}&a_{42}+b_{42}\end{array}\right). \]
Number of columns in matrix A should equal to the number of rows in matrix B!
Matrix product is not commutative which means that \(AB\ne BA\) in general.
Matrix multiplication \[ \begin{multline*} AB = \left(\begin{array}{cc}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\end{array}\right)\left(\begin{array}{cc}b_{11}&b_{12}&b_{13}&b_{14}\\b_{21}&b_{22}&b_{23}&b_{24}\\b_{31}&b_{32}&b_{33}&b_{34}\end{array}\right) =\\ \left(\begin{array}{cc}a_{11}b_{11}+a_{12}b_{21}+a_{13}b_{31}&a_{11}b_{12}+a_{12}b_{22}+a_{13}b_{32}&a_{11}b_{13}+a_{12}b_{23}+a_{13}b_{33}\\ a_{21}b_{11}+a_{22}b_{21}+a_{23}b_{31}&a_{21}b_{12}+a_{22}b_{22}+a_{23}b_{32}&a_{21}b_{13}+a_{22}b_{23}+a_{23}b_{33}\\ a_{31}b_{11}+a_{32}b_{21}+a_{33}b_{31}&a_{31}b_{12}+a_{32}b_{22}+a_{33}b_{32}&a_{31}b_{13}+a_{32}b_{23}+a_{33}b_{33}\end{array}\right) \end{multline*} \]
Matrix transposition
Rows become columns and columns become rows. \[ A^T = \left(\begin{array}{cc}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\end{array}\right)^T=\left(\begin{array}{cc}a_{11}&a_{21}\\a_{12}&a_{22}\\a_{13}&a_{23}\end{array}\right) \]
Main formulas: \[ \left|\begin{array}{cc}a_{11}&a_{12}\\a_{21}&a_{22}\end{array}\right| = a_{11}a_{22}-a_{12}a_{21}. \] \[ \begin{multline*} \left|\begin{array}{cc}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\end{array}\right| = a_{11}\left|\begin{array}{cc}a_{22}&a_{23}\\a_{32}&a_{33}\end{array}\right|-a_{12}\left|\begin{array}{cc}a_{21}&a_{23}\\a_{31}&a_{33}\end{array}\right|+a_{31}\left|\begin{array}{cc}a_{21}&a_{22}\\a_{31}&a_{32}\end{array}\right|=\\ a_{11}a_{22}a_{33}-a_{11}a_{23}a_{32}-a_{12}a_{21}a_{33}+a_{12}a_{23}a_{31}+a_{31}a_{21}a_{32}-a_{31}a_{22}a_{31}. \end{multline*} \] Alternatively, it may be convenient to consider a bit different notation such as follows: \[ \begin{multline*} \left|\begin{array}{cc}a_{1}&a_{2}&a_{3}\\b_{1}&b_{2}&b_{3}\\c_{1}&c_{2}&c_{3}\end{array}\right| = a_{1}\left|\begin{array}{cc}b_{2}&c_{2}\\b_{3}&c_{3}\end{array}\right|-b_{1}\left|\begin{array}{cc}a_{2}&a_{3}\\c_{2}&c_{3}\end{array}\right|+c_{1}\left|\begin{array}{cc}a_{2}&b_{2}\\a_{3}&b_{3}\end{array}\right|=\\ a_{1}b_{2}c_{3}-a_{1}b_{3}c_{2}+a_{3}b_{1}c_{2}-a_{2}b_{1}c_{3}+a_{2}b_{3}c_{1}-a_{3}b_{2}c_{1}, \end{multline*} \] where the sign in the last line depend on the odd/even number of permutations to the sequence \(\{1,2,3\}\).
We may also write that: \[ \det A = \sum_{k=1}^n (-1)^{i+k}a_{ik}M_{ik}, \] respectively to the matrix row \(a_{i*}\) and where \(M_{ik}\) is a minor for the element \(a_{ik}\)1 Please see https://en.wikipedia.org/wiki/Minor_(linear_algebra); or: \[ \det A = \sum_{k=1}^n (-1)^{k+j}a_{kj}M_{kj}, \] respectively to the matrix column \(a_{*j}\).
Identity matrix is a matrix with ones on the diagonal, and zeros for nondiagonal elements.
In the literature it is sometimes denoted as \(I\) or \(E\):
\[ E = \left(\begin{array}{cc}1&0&0\\0&1&0\\0&0&1\end{array}\right) \] Note that: \(AE=EA=A\).
Can we find such matrix \(A^{-1}\) such that: \(A^{-1}A=I\)?
Formula: \[ A^{-1} = \frac1{\det A}\mathrm{adj}(A), \] where \(\mathrm{adj}(A)\) is an anjoin (or adjugate) matrix of \(A\). The adjoin matrix is a transposed matrix of co-factors (\(=\) minors with the factors \((-1)^{i+j}\)) of the elements of matrix \(A\). This means that for the matrix \(2\times2\): \[ A = \left(\begin{array}{rr}a_{11}&a_{12}\\a_{21}&a_{22}\end{array}\right), \] we write \[ \mathrm{adj} (A) = \left(\begin{array}{rr}a_{22}&-a_{21}\\-a_{12}&a_{22}\end{array}\right)^T=\left(\begin{array}{rr}a_{22}&-a_{12}\\-a_{21}&a_{22}\end{array}\right). \] Whereas for the matrix \(3\times3\): \[ A = \left(\begin{array}{cc}a_{11}&a_{12}&a_{13}\\a_{21}&a_{22}&a_{23}\\a_{31}&a_{32}&a_{33}\end{array}\right), \] we write: \[ \mathrm{adj} (A) = \left(\begin{array}{rrr}\left|\begin{array}{cc}a_{22}&a_{23}\\a_{32}&a_{33}\end{array}\right|&-\left|\begin{array}{cc}a_{21}&a_{23}\\a_{31}&a_{33}\end{array}\right|&\left|\begin{array}{cc}a_{21}&a_{22}\\a_{31}&a_{32}\end{array}\right|\\ -\left|\begin{array}{cc}a_{12}&a_{13}\\a_{32}&a_{33}\end{array}\right|&\left|\begin{array}{cc}a_{11}&a_{13}\\a_{31}&a_{33}\end{array}\right|&-\left|\begin{array}{cc}a_{11}&a_{12}\\a_{31}&a_{32}\end{array}\right|\\ \left|\begin{array}{cc}a_{12}&a_{13}\\a_{22}&a_{23}\end{array}\right|&-\left|\begin{array}{cc}a_{11}&a_{13}\\a_{21}&a_{23}\end{array}\right|&\left|\begin{array}{cc}a_{11}&a_{12}\\a_{21}&a_{22}\end{array}\right|\end{array}\right)^T. \] Do not forget to transpose the matrix of co-factors at the end!
If we have: \[ Ay = \lambda y, \] the scalar \(\lambda\) is called an eigenvalue, the corresponding vector \(y\) is an eigenvector2 Eigenvectors are not covered in this session.
We write: \[ (A-\lambda E)y = 0 \], which has a non-zero solution \(y\) only if: \[ \det(A-\lambda E) = 0. \]
As I was asked about possible materials on youtube, I may refer to the recent talk on a refresher for linear algebra but targeter to computer scientists: https://youtu.be/Qz58vTa8-SY. Additionally, you may check nice animations here: https://www.youtube.com/watch?v=fNk_zzaMoSs&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab.
Calculate the sum of two vectors: \[ \left(\begin{array}{c} 4 \\ 0 \\ 1 \end{array} \right) + 2\left(\begin{array}{c} {-}1 \\ 5 \\ 1 \end{array} \right) = \left(\begin{array}{cc} 4+2\times(-1) \\ 0+2\times5 \\ 1+2\times1 \end{array} \right) = \left(\begin{array}{cc} 2 \\ 10 \\ 3 \end{array} \right). \]
Calculate the product of two one-row and one-column matrices: \[ \left(\begin{array}{c} 1 & -2 & 4 \end{array} \right) \times\left(\begin{array}{c} 4 \\ 3 \\ 1 \end{array} \right)=1\times4+(-2)\times3+4\times1=2. \]
Calculate the product of two matrices: \[ \left(\begin{array}{cc} 3 & 1 \\ 2 & 1 \\ 4 & 0 \end{array} \right) \times\left(\begin{array}{c} 1 & {-}1 & {-}2 \\ 2 & 1 & 3 \end{array} \right)=\left(\begin{array}{ccc} 3\times1+1\times2 & 3\times(-1)+1\times1 & 3\times({-}2)+1\times3 \\ 2\times1+1\times2 & 2\times(-1)+1\times1 & 2\times(-2)+1\times3 \\ 4\times1+0\times2 & 4\times(-1)+0\times1 & 4\times(-2)+0\times3 \end{array}\right) = \left(\begin{array}{ccc} 5 & -2 & -3 \\ 4 & -1 & -1 \\ 4 & -4 & -8 \end{array}\right) \]
Calculate the transposed matrix: \[ \left(\begin{array}{cccc} 0 & \cdots & 0 & \lambda_1 \\ \vdots & & \lambda_2 & 0 \\ 0 & & & \vdots \\\lambda_n & 0 &\cdots & 0 \end{array} \right)^T=\left(\begin{array}{cccc} 0 & \cdots & 0 & \lambda_n \\ \vdots & & \lambda_{n-1} & 0 \\ 0 & & & \vdots \\\lambda_1 & 0 &\cdots & 0 \end{array} \right) \]
Calculte the determinant: \[ \begin{multline*} \left|\begin{array}{rrr} 2 & 1 & -1 \\ 3 & 1 & -2 \\ 1 & 0 & 1 \end{array} \right| = (-1)^{3+1}\cdot1\cdot\left|\begin{array}{rr} 1 & -1 \\ 1 & -2 \end{array} \right| + (-1)^{3+2}\cdot0\cdot\left|\begin{array}{rr} 2 & -1 \\ 3 & -2 \end{array}\right| + (-1)^{3+3}\cdot1\cdot\left|\begin{array}{rr} 2 & 1 \\ 3 & 1 \end{array}\right|={}\\ (1\times(-2)-1\times(-1))+(2\times1-1\times3) = -1-1=-2 \end{multline*} \]
# We define the matrix A
A = matrix(c(2,1,-1,3,1,-2,1,0,1), 3, 3, byrow = TRUE)
A
## [,1] [,2] [,3]
## [1,] 2 1 -1
## [2,] 3 1 -2
## [3,] 1 0 1
We calculate the determinant:
det(A)
## [1] -2
Calculate the inverse matrix \[ A = \left(\begin{array}{rrr} 2 & -1 & 0 \\ 0 & 2 & -1 \\-1 & -1 & 1\end{array} \right) \] First, we calculate the determinant: \[ \det{A} = \left|\begin{array}{rrr} 2 & -1 & 0 \\ 0 & 2 & -1 \\-1 & -1 & 1 \end{array} \right| = 2\times(2-1)-(-1)\times(-1)=1\,. \] Then we derive the adjoint matrix: \[ \begin{multline*} \mathrm{adj}(A) = \left(\begin{array}{ccc} \left|\begin{array}{rr} 2 & -1 \\-1 & 1 \end{array}\right| & {-}\left|\begin{array}{rr} 0 & -1 \\-1 & 1 \end{array}\right| & \left|\begin{array}{rr} 0 & 2 \\ -1 & -1 \end{array}\right| \\ {-}\left|\begin{array}{rr} -1 & 0 \\-1 & 1 \end{array}\right| & \left|\begin{array}{rr} 2 & 0 \\-1 & 1 \end{array}\right| & {-}\left|\begin{array}{rr} 2 & -1 \\-1 & -1 \end{array}\right| \\ \left|\begin{array}{rr} -1 & 0 \\2 & -1 \end{array}\right| & {-}\left|\begin{array}{rr} 2 & 0 \\0 & -1 \end{array}\right| & \left|\begin{array}{rr} 2 & -1 \\0 & 2 \end{array}\right| \end{array} \right)^T = \\ \left(\begin{array}{rrr} 1 & 1 & 2\\ 1 & 2 & 3 \\ 1 & 2 & 4 \end{array}\right)^T = \left(\begin{array}{rrr} 1 & 1 & 1\\ 1 & 2 & 2 \\ 2 & 3 & 4 \end{array}\right) \end{multline*} \] Thus, we obtain: \[ A^{-1} = \frac{\mathrm{adj}(A)}{\det A}=\left(\begin{array}{rrr} 1 & 1 & 1\\ 1 & 2 & 2 \\ 2 & 3 & 4 \end{array}\right). \]
# We define the matrix A
A = matrix(c(2,-1,0,0,2,-1,-1,-1,1), 3, 3, byrow = TRUE)
A
## [,1] [,2] [,3]
## [1,] 2 -1 0
## [2,] 0 2 -1
## [3,] -1 -1 1
We calculate the determinant:
det(A)
## [1] 1
Inverse matrix3 You can check the functions for matrix algebra in R: http://www.statmethods.net/advstats/matrix.html. Also, there is a quite helpful tutorial here: https://cran.r-project.org/web/packages/matlib/vignettes/eigen-ex1.html.
solve(A)
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 2 2
## [3,] 2 3 4
Find matrix \(X\): \[ \left(\begin{array}{rr} 2 & 5 \\ 1 & 3 \end{array} \right)X=\left(\begin{array}{rr} 2 & 1 \\ 1 & 1 \end{array} \right) \]
We write: \[ \left(\begin{array}{rr} 2 & 5 \\ 1 & 3 \end{array} \right)^{-1}\left(\begin{array}{rr} 2 & 5 \\ 1 & 3 \end{array} \right)X = \left(\begin{array}{rr} 2 & 5 \\ 1 & 3 \end{array} \right)^{-1}\left(\begin{array}{rr} 2 & 1 \\ 1 & 1 \end{array} \right), \] so that we need to calculate first: \[ \left(\begin{array}{rr} 2 & 5 \\ 1 & 3 \end{array} \right)^{-1}. \] The determinant equals one: \[ \left|\begin{array}{rr} 2 & 5 \\ 1 & 3 \end{array} \right|= 2\times3-5\times1=1. \] Then: \[ \left(\begin{array}{rr} 2 & 5 \\ 1 & 3 \end{array} \right)^{-1} = \left(\begin{array}{rr} 3 & -5 \\ -1 & 2 \end{array} \right) \] finally: \[ \begin{multline*} X = \left(\begin{array}{rr} 3 & -5 \\ -1 & 2 \end{array} \right)\left(\begin{array}{rr} 2 & 1 \\ 1 & 1 \end{array} \right)={}\\\left(\begin{array}{cc} 3\times2-5\times1 & 3\times1-5\times1 \\ -1\times2+2\times1 & -1\times1+2\times1 \end{array}\right)=\left(\begin{array}{rr} 1 & -2 \\ 0 & 1 \end{array}\right) \end{multline*} \]
# We define the matrix A
A = matrix(c(2,5,1,3), 2, 2, byrow = TRUE)
A
## [,1] [,2]
## [1,] 2 5
## [2,] 1 3
The inverse matrix:
Ainv = solve(A)
Ainv
## [,1] [,2]
## [1,] 3 -5
## [2,] -1 2
Here we define the matrix in the right-hand side
# Then matrix in the right hand side
B = matrix(c(2,1,1,1), 2, 2, byrow = TRUE)
B
## [,1] [,2]
## [1,] 2 1
## [2,] 1 1
Finally we calculate the result:
Ainv%*%B
## [,1] [,2]
## [1,] 1 -2
## [2,] 0 1
We have the matrix: \[ A = \left(\begin{array}{cc} 25 & 60 \\ 60 & 144 \end{array}\right) \]
We write: \[ \begin{split} \det(A-\lambda E) = \left|\begin{array}{cc} 25-\lambda & 60 \\ 60 & 144-\lambda \end{array}\right|=(\lambda-25)(\lambda-144)-60^2 = \\ \lambda^2-169\lambda+5^2\cdot12^2-60^2=\lambda^2-169\lambda+(5\cdot12-60)(5\cdot12+60)=\lambda(\lambda-169). \end{split} \] We get \(\lambda_1=0\) and \(\lambda_2=169\).
A = matrix(c(25,60,60,144),2,2,byrow=TRUE)
A
## [,1] [,2]
## [1,] 25 60
## [2,] 60 144
We use eigen function to obtain the eigenvalues:
eigen(A)$values
## [1] 1.690000e+02 3.552714e-15
We have the matrix: \[ A = \left(\begin{array}{rrr} 1 & 2 & 1 \\ 1 & 2 & 4 \\ -1 & -2 & -3 \end{array}\right) \]
We need to calculate the determinant: \[ \det(A-\lambda E) = \left|\begin{array}{ccc} 1-\lambda & 2 & 1 \\ 1 & 2-\lambda & 4 \\ -1 & -2 & -3-\lambda \end{array}\right|. \] We get: \[ \begin{split} \det(A-\lambda E) = (1-\lambda)((2-\lambda)(-3-\lambda)+8)-2((-3-\lambda)+4)+(-2+(2-\lambda))=\\ (1-\lambda)((2-\lambda)(-3-\lambda)+8) + (\lambda-2) = (1-\lambda)(\lambda^2+\lambda+2)+\lambda-2={-}\lambda^3=0 \end{split} \] So the eigenvalue is zero.
A = matrix(c(1,2,1,1,2,4,-1,-2,-3),3,3,byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 1
## [2,] 1 2 4
## [3,] -1 -2 -3
First we use eigen function to obtain the eigenvalues:
eigen(A)$values
## [1] 1.377528e-05+0.000000e+00i -6.887639e-06+1.192973e-05i
## [3] -6.887639e-06-1.192973e-05i
We have the matrix: \[ A = \left(\begin{array}{rrrr} 5 & -2 & 4 & -4 \\ -2 & 8 & 2 & -2 \\ 0 & 0 & 4 & 5 \\ 0 & 0 & 5 & 4 \end{array}\right). \] We write: \[ \begin{split} \det(A-\lambda E) = \left|\begin{array}{rrrr} 5-\lambda & -2 & 4 & -4 \\ -2 & 8-\lambda & 2 & -2 \\ 0 & 0 & 4-\lambda & 5 \\ 0 & 0 & 5 & 4-\lambda \end{array}\right| = -5\cdot\left|\begin{array}{rrr} 5-\lambda & -2 & -4 \\ -2 & 8-\lambda & -2 \\ 0 & 0 & 5 \end{array}\right|+(4-\lambda)\left|\begin{array}{rrr} 5-\lambda & -2 & 4 \\ -2 & 8-\lambda & 2 \\ 0 & 0 & 4-\lambda \end{array}\right|=\\-5\cdot 5\cdot\left|\begin{array}{rr} 5-\lambda & -2 \\ -2 & 8-\lambda \end{array}\right|+(\lambda-4)^2\left|\begin{array}{rr} 5-\lambda & -2 \\ -2 & 8-\lambda \end{array}\right|= ((\lambda-4)^2-5^2)\left|\begin{array}{rr} 5-\lambda & -2 \\ -2 & 8-\lambda \end{array}\right|={}\\ (\lambda-4-5)(\lambda-4+5)\bigl[(\lambda-5)(\lambda-8)-4\bigr]=(\lambda-9)(\lambda+1)(\lambda^2-13\lambda+36) \end{split} \] Since the quadratic polynomial is decomposed to: \[ \lambda^2-13\lambda+36=\lambda(\lambda-4)-9(\lambda-4)=(\lambda-9)(\lambda-4), \] we obtain: \[ \det(A-\lambda E) = (\lambda+1)(\lambda-9)^2(\lambda-4). \]
A = matrix(c(5,-2,4,-4,-2,8,2,-2,0,0,4,5,0,0,5,4),4,4,byrow=TRUE)
A
## [,1] [,2] [,3] [,4]
## [1,] 5 -2 4 -4
## [2,] -2 8 2 -2
## [3,] 0 0 4 5
## [4,] 0 0 5 4
We get:
eigen(A)$values
## [1] 9 9 4 -1