Introduction to Linear Algebra

Michael Taylor

2019/01/17

library(knitr)
library(kableExtra)
$$ ( \[\begin{array}{cc} 1 & -1 \\ 2 & 1 \\ 4 & 2 \end{array}\]

)

( \[\begin{array}{c} 1 \\ 2 \end{array}\]

) =

( \[\begin{array}{ccc} 1 \times 1 & + &(-1)\times 2 \\ 2 \times 1 & + &1\ \times 2 \\ 4 \times 1 & + &(-2)\times 2 \end{array}\]

) =

( \[\begin{array}{c} -1 \\ 4 \\ 0 \end{array}\]

) $$

# Create a matrix of all 1's and all 2's that are 2 by 3 and 3 by 2, respectively
matrix(1, nrow = 2, ncol = 3)
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    1    1
print(matrix(2, nrow = 3, ncol = 2))
##      [,1] [,2]
## [1,]    2    2
## [2,]    2    2
## [3,]    2    2
# Create a matrix B and changing the byrow designation.
B <- matrix(c(1, 2, 3, 2), nrow = 2, ncol = 2, byrow = TRUE)

A <- matrix(1, nrow = 2, ncol = 2)
# Add A to the previously-created matrix
A + B
##      [,1] [,2]
## [1,]    2    3
## [2,]    4    3

Matrix-Vector Operations

A matric with m columns and n rows can only be multiplied by a vector with n elements. The resulting vector will have m elements.

b <- c(1,2)
(A <- matrix(c(1,2,4,-1,1,-2), nrow = 3, byrow = F))
##      [,1] [,2]
## [1,]    1   -1
## [2,]    2    1
## [3,]    4   -2

Matrix-Vector Compatibility

(A = matrix(c(1, 2, 3, -1, 0, 3), nrow = 2, ncol = 3, byrow = TRUE))
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    3

Matrix A can only be multipled by a vector with 3 elements. Ex. c(1, 1, -1).

Matrix Multiplication as a Transformation

(A <- matrix(c(4,0,0,1), nrow = 2, byrow = TRUE ))
##      [,1] [,2]
## [1,]    4    0
## [2,]    0    1
b <- c(1, 1)
A %*% b
##      [,1]
## [1,]    4
## [2,]    1
  • Observe that the x component of the vector c(1, 1) is stretched by a factor 4.
(B <- matrix(c(1, 0, 0, 2/3), nrow = 2, byrow = TRUE))
##      [,1]   [,2]
## [1,]    1 0.0000
## [2,]    0 0.6667
B%*%b
##        [,1]
## [1,] 1.0000
## [2,] 0.6667
  • Observe that the y component of the vector c(1, 1) is shrunk by a factor of 2/3.

Reflections

  • apply a reflection matrix to the vector b <- c(2 ,1).
b <- c(2, 1)
(A <- matrix(c(-1,0,0,1), nrow = 2))
##      [,1] [,2]
## [1,]   -1    0
## [2,]    0    1
A %*% b
##      [,1]
## [1,]   -2
## [2,]    1
(B <- matrix(c(1,0,0,-1), nrow = 2))
##      [,1] [,2]
## [1,]    1    0
## [2,]    0   -1
B%*%b
##      [,1]
## [1,]    2
## [2,]   -1
(C <- matrix(c(-4,0,0,-2), nrow = 2))
##      [,1] [,2]
## [1,]   -4    0
## [2,]    0   -2
C%*%b
##      [,1]
## [1,]   -8
## [2,]   -2

Matrix-Matrix Calculations

Matrix Multiplication Compatibility

(A = matrix(c(1, 3, 2, -1, 0, 1), nrow = 2, ncol = 3))
##      [,1] [,2] [,3]
## [1,]    1    2    0
## [2,]    3   -1    1
(B = matrix(c(-1, 1, 2, -3), nrow = 2, ncol = 2))
##      [,1] [,2]
## [1,]   -1    2
## [2,]    1   -3
B%*%A
##      [,1] [,2] [,3]
## [1,]    5   -4    2
## [2,]   -8    5   -3

Are A and B compatible in terms of matrix multiplication?

Yes, the multiplication BA results in a 2 by 3 matrix

Matrix Multiplication - Order Matters

b <- c(1,1)
(A <- matrix(c(1/sqrt(2), 1/sqrt(2), -1/sqrt(2), 1/sqrt(2)), nrow = 2 ))
##        [,1]    [,2]
## [1,] 0.7071 -0.7071
## [2,] 0.7071  0.7071
(B <- matrix(c(1, 0, 0, -1), nrow = 2))
##      [,1] [,2]
## [1,]    1    0
## [2,]    0   -1
# Multiply A by B on the left
A%*%B
##        [,1]    [,2]
## [1,] 0.7071  0.7071
## [2,] 0.7071 -0.7071
# Multiply A by B on the right
B%*%A
##         [,1]    [,2]
## [1,]  0.7071 -0.7071
## [2,] -0.7071 -0.7071
  • A by B is not equal to B by A.
# Multiply b by B then A (on the left)
A%*%B%*%b
##       [,1]
## [1,] 1.414
## [2,] 0.000
# Multiply b by A then B (on the left)
B%*%A%*%b
##        [,1]
## [1,]  0.000
## [2,] -1.414

Intro to The Matrix Inverse

diag() constructs a diagonal matrix.

The solve() function in R will find the inverse of a matrix if it exists, and provide an error if it does not.

(A <- matrix(c(1, -1, 2, 2), nrow = 2))
##      [,1] [,2]
## [1,]    1    2
## [2,]   -1    2
diag(2)
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
# Take the inverse of the 2 by 2 identity matrix
solve(diag(2))
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
# Take the inverse of the matrix A
(Ainv <- solve(A))
##      [,1]  [,2]
## [1,] 0.50 -0.50
## [2,] 0.25  0.25
# Multiply A by its inverse on the left
Ainv%*%A
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
# Multiply A by its inverse on the right
A%*%Ainv
##      [,1] [,2]
## [1,]    1    0
## [2,]    0    1
sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.1 LTS
## 
## Matrix products: default
## BLAS: /home/michael/anaconda3/lib/R/lib/libRblas.so
## LAPACK: /home/michael/anaconda3/lib/R/lib/libRlapack.so
## 
## locale:
##  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8    
##  [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
##  [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] kableExtra_0.9.0     knitr_1.20           RevoUtils_11.0.1    
## [4] RevoUtilsMath_11.0.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.18      rstudioapi_0.7    xml2_1.2.0       
##  [4] magrittr_1.5      hms_0.4.2         munsell_0.5.0    
##  [7] rvest_0.3.2       viridisLite_0.3.0 colorspace_1.3-2 
## [10] R6_2.2.2          rlang_0.2.1       plyr_1.8.4       
## [13] httr_1.3.1        stringr_1.3.1     tools_3.5.1      
## [16] xfun_0.4.11       htmltools_0.3.6   yaml_2.2.0       
## [19] rprojroot_1.3-2   digest_0.6.15     tibble_1.4.2     
## [22] crayon_1.3.4      bookdown_0.7      readr_1.1.1      
## [25] codetools_0.2-15  evaluate_0.11     rmarkdown_1.10   
## [28] blogdown_0.9.8    stringi_1.2.4     compiler_3.5.1   
## [31] pillar_1.3.0      scales_0.5.0      backports_1.1.2  
## [34] pkgconfig_2.0.1

References

R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Xie, Yihui. 2018. Knitr: A General-Purpose Package for Dynamic Report Generation in R. https://CRAN.R-project.org/package=knitr.