Problem Set 1
#numbers semi-randomly generated by a human brain
M1 <- matrix(c(1,2,3,4,9,8,7,6,2,3,4,5,8,7,6,5), nrow = 4, byrow = TRUE)
M1
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 9 8 7 6
## [3,] 2 3 4 5
## [4,] 8 7 6 5
We’ll use R to get the transpose of this matrix.
M1T <- t(M1)
M1T
## [,1] [,2] [,3] [,4]
## [1,] 1 9 2 8
## [2,] 2 8 3 7
## [3,] 3 7 4 6
## [4,] 4 6 5 5
Now let’s check \(A^TA\) and \(AA^T\).
ATA <- M1 %*% M1T
ATA
## [,1] [,2] [,3] [,4]
## [1,] 30 70 40 60
## [2,] 70 230 100 200
## [3,] 40 100 54 86
## [4,] 60 200 86 174
AAT <- M1T %*% M1
AAT
## [,1] [,2] [,3] [,4]
## [1,] 150 136 122 108
## [2,] 136 126 116 106
## [3,] 122 116 110 104
## [4,] 108 106 104 102
It’s clear that \(A^TA \neq AA^T\) for this “general” matrix.
ATA == AAT
## [,1] [,2] [,3] [,4]
## [1,] FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE
(2) For a special type of square matrix A, we get \(A^TA = AAT\). Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix). Please typeset your response using LaTeX mode in RStudio. If you do it in paper, please either scan or take a picture of the work and submit it. Please ensure that your image is legible and that your submissions are named using your first initial, last name, assignment and problem set within the assignment. E.g. LFulton_Assignment2_PS1.png
Let’s verify that this is true for a basic 4 x 4 identity matrix.
#https://www.r-bloggers.com/how-do-i-create-the-identity-matrix-in-r/
MI2 <- diag(4)
MI2
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 1 0
## [4,] 0 0 0 1
Get the transpose and check the multiplication.
MI2T <- t(MI2)
AAT2 <- MI2 %*% MI2T
ATA2 <- MI2T %*% MI2
ATA2 == AAT2
## [,1] [,2] [,3] [,4]
## [1,] TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE
Yay, it’s true. Based on our conceptual understanding of matrix multiplication, could a scalar mulitplication of an identity matrix work as well?
MI2B <- 3 * MI2
MI2B
## [,1] [,2] [,3] [,4]
## [1,] 3 0 0 0
## [2,] 0 3 0 0
## [3,] 0 0 3 0
## [4,] 0 0 0 3
MI2TB <- t(MI2B)
AAT2B <- MI2B %*% MI2TB
ATA2B <- MI2TB %*% MI2B
ATA2B == AAT2B
## [,1] [,2] [,3] [,4]
## [1,] TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE
OK, that worked too. So \(A^TA = AA^T\) works for the identity matrix and any scalar multiple of it. What about a matrix that has different values in it’s upper-left-to-lower-right diagonal but zeros everywhere else?
Based on our knowledge of matrix multiplication, transposed matrices would only multiple commutatively in rare, special circumstances and not as a rule.
M2C <- matrix(c(3,0,0,0,7,0,0,0,12), nrow = 3, byrow = TRUE)
M2C
## [,1] [,2] [,3]
## [1,] 3 0 0
## [2,] 0 7 0
## [3,] 0 0 12
M2TC <- t(M2C)
AAT2C <- M2C %*% M2TC
ATA2C <- M2TC %*% M2C
ATA2C == AAT2C
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
This works too. So \(A^TA = AA^T\) with seemingly any matrix for which there are zeros in all places but the upper-left-to-lower-right diagonal.
2. Problem set 2
Matrix factorization is a very important problem. There are supercomputers built just to do matrix factorizations. Every second you are on an airplane, matrices are being factorized. Radars that track ???ights use a technique called Kalman ???ltering. At the heart of Kalman Filtering is a Matrix Factorization operation. Kalman Filters are solving linear systems of equations when they track your ???ight using radars. Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer. Please submit your response in an R Markdown document using our class naming convention, E.g. LFulton_Assignment2_PS2.png You don’t have to worry about permuting rows of A and you can assume that A is less than 5x5, if you need to hard-code any variables in your code. If you doing the entire assignment in R, then please submit only one markdown document for both the problems.
We’re gonna go LU, making all elements above the diagnoal zero.
#for is.square.matrix check
library(matrixcalc)
function_LU <- function(M){
#based on specs, only return if square matrix and don't worry about more than 5 x 5
if(nrow(M)>= 5) {
return("Go easy on the matrix size, please.")
}
if(is.square.matrix(M) == FALSE) {
return("Not a square matrix!")
}
#get number of rows from matrix
n <- dim(M)[1]
#create identity matrix L that's same size as M for elimination purposes
L <- diag(n)
#initiatlize variable
f <- 0
while (f <= dim(M)[1]){
f = f+1
i = f
j = f
while (i <= dim(M)[1]-1){
c = M[i+1,j]/M[j,j]
M[i+1,] = M[i+1,]-c*M[j,]
L[i+1,j] = c
i = 1+i
}
}
return(L)
}
Check our work with a couple examples.
library(Matrix)
EM1 <- matrix(c(1,2,3,7,8,9,4,5,6), nrow = 3, byrow = TRUE)
EM1
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 7 8 9
## [3,] 4 5 6
function_LU(EM1)
## [,1] [,2] [,3]
## [1,] 1 0.0 0
## [2,] 7 1.0 0
## [3,] 4 0.5 1
#checking with R
luA <- lu.decomposition(EM1)
luA$L
## [,1] [,2] [,3]
## [1,] 1 0.0 0
## [2,] 7 1.0 0
## [3,] 4 0.5 1
EM2 <- matrix(c(9,7,5,2,4,6,8,6,4), nrow = 3, byrow = TRUE)
EM2
## [,1] [,2] [,3]
## [1,] 9 7 5
## [2,] 2 4 6
## [3,] 8 6 4
function_LU(EM2)
## [,1] [,2] [,3]
## [1,] 1.0000000 0.00000000 0
## [2,] 0.2222222 1.00000000 0
## [3,] 0.8888889 -0.09090909 1
#checking with R
luA2 <- lu.decomposition(EM2)
luA2$L
## [,1] [,2] [,3]
## [1,] 1.0000000 0.00000000 0
## [2,] 0.2222222 1.00000000 0
## [3,] 0.8888889 -0.09090909 1
Got stuck on the function and borrowed some logic from a previous student: https://rpubs.com/cspitmit03/Data605HW2