Problem Set 1.

  1. Show that \(A^TA \ne AA^T\)in general. (Proof and demonstration.)

  2. For a special type of square matrix A, we get \(A^T A = AA^T\). 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  rst initial, last name, assignment and problem set within the assignment. E.g. LFulton_Assignment2_PS1.png

Let’s create matrix to see if the the equation works or not.

set.seed(1000)
A<-matrix(sample(30,16, replace=F), nrow=4, byrow=T)
A
##      [,1] [,2] [,3] [,4]
## [1,]   16    4   11   22
## [2,]   19   24    3   18
## [3,]   27    6   13   21
## [4,]    1    9   20   10

Transpose the A.

AT<-t(A)
AT
##      [,1] [,2] [,3] [,4]
## [1,]   16   19   27    1
## [2,]    4   24    6    9
## [3,]   11    3   13   20
## [4,]   22   18   21   10

Using matrix multiplication to get ATA and AAT.

ATA<-AT %*% A
ATA
##      [,1] [,2] [,3] [,4]
## [1,] 1347  691  604 1271
## [2,]  691  709  374  736
## [3,]  604  374  699  769
## [4,] 1271  736  769 1349
AAT<-A %*% AT
AAT
##      [,1] [,2] [,3] [,4]
## [1,]  877  829 1061  492
## [2,]  829 1270 1074  475
## [3,] 1061 1074 1375  551
## [4,]  492  475  551  582

Compare the result of ATA and AAT.

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

As the result shows that the AAT is not equal to ATA.

according to the following website, [phrase][‘https://www.r-bloggers.com/how-do-i-create-the-identity-matrix-in-r/’]

In linear algebra, a symmetric matrix is a square matrix that is equal to its transpose. A = AT, then ATA = AAT; every square diagnomal matrix is symmetric.

a<-diag(5)
a
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0
## [5,]    0    0    0    0    1
at<-t(a)
at
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0
## [5,]    0    0    0    0    1

I can see that the at and a is equal, there for, it is going to make the AAT and ATA equal.

aat<- a %*% at
aat
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0
## [5,]    0    0    0    0    1
ata<-at %*% a
ata
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    0    0    0    0
## [2,]    0    1    0    0    0
## [3,]    0    0    1    0    0
## [4,]    0    0    0    1    0
## [5,]    0    0    0    0    1

compare the ata and aat.

ata == aat
##      [,1] [,2] [,3] [,4] [,5]
## [1,] TRUE TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE TRUE
## [5,] TRUE TRUE TRUE TRUE TRUE

Let me try with another symmetric matrix with different numbers.

b<-matrix(c(5,0,0,0,65,0,0,0,7),nrow = 3, byrow = T)
b
##      [,1] [,2] [,3]
## [1,]    5    0    0
## [2,]    0   65    0
## [3,]    0    0    7
bt<-t(b)
bt
##      [,1] [,2] [,3]
## [1,]    5    0    0
## [2,]    0   65    0
## [3,]    0    0    7
bbt<-b %*% bt
bbt
##      [,1] [,2] [,3]
## [1,]   25    0    0
## [2,]    0 4225    0
## [3,]    0    0   49
btb<-bt %*% b
btb
##      [,1] [,2] [,3]
## [1,]   25    0    0
## [2,]    0 4225    0
## [3,]    0    0   49
bbt == btb
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

With trying other numbers, it still works. so, from the left to to right bottom has to be the same numbers in order to make AAT and ATA equal, rest of the numbers should be 0.

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 trackights use a technique called Kalman Filtering. At the heart of Kalman Filtering is a Matrix Factorization operation. Kalman Filters are solving linear systems of equations when they track your flight 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.

LU <- function(A){
  
  # Check if matrix is square.
  
  if(dim(A)[1]!=dim(A)[2]){
  return('The matrix is not square')
  }
  
  r <- nrow(A)   
  U <- A
  L <- diag(r)  

  for (x in c(1:r)){
    for(y in c(2:r)){
      if(y > x){
          row <- U[x,]
        multiplier <- U[y, x] / row[x]
        U[y,] <- U[y,] - (multiplier * row)
        L[y,x] <- multiplier
      }
    }
  }
  
  return(list(L=L, U=U))
}

Get the matix A

set.seed(1000)
A <- matrix((sample(9,9, replace = F)), nrow=3, byrow=TRUE)
A
##      [,1] [,2] [,3]
## [1,]    4    3    6
## [2,]    8    5    7
## [3,]    1    2    9

Test the function with the samples.

LUA<-LU(A)
LUA
## $L
##      [,1]  [,2] [,3]
## [1,] 1.00  0.00    0
## [2,] 2.00  1.00    0
## [3,] 0.25 -1.25    1
## 
## $U
##      [,1] [,2]  [,3]
## [1,]    4    3  6.00
## [2,]    0   -1 -5.00
## [3,]    0    0  1.25

Use lu.decomposition() function to see if any difference.

library(matrixcalc)
A<-matrix(c(4,3,6,8,5,7,1,2,9), nrow = 3, byrow = T)
A
##      [,1] [,2] [,3]
## [1,]    4    3    6
## [2,]    8    5    7
## [3,]    1    2    9
LUDA<-lu.decomposition(A)
LUDA
## $L
##      [,1]  [,2] [,3]
## [1,] 1.00  0.00    0
## [2,] 2.00  1.00    0
## [3,] 0.25 -1.25    1
## 
## $U
##      [,1] [,2]  [,3]
## [1,]    4    3  6.00
## [2,]    0   -1 -5.00
## [3,]    0    0  1.25
LUDA$L == LUA$L
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
LUDA$U == LUA$U
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

According to the comparison, the LUDA and LUA are equal, and the function works.