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 flights 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.

Defining LU Factorization Function Logic

LU_fact <- function(A) {
  
  UM <- A # Initial upper triangular matrix
  n <- dim(A)[1] # counter of number of rows in the matrix to loop through
  LM <- diag(n) # Identity matrix as an initial lower triangular matrix

  ## Get and loop through multipliers (E21, E31, E32,..., etc.) to calculate the UpperM and apply the inverse for the LowerM  
  
  for(i in 2:n) {
    for(j in 1:(i-1)) {
      mult <- -UM[i,j]
      UM[i, ] <- mult * UM[j, ] + UM[i, ]
      LM[i,j] <- -mult    
    }
  }
  return(list(LM,UM))
}

Testing LU Factorization Function

A <- matrix(c(1,-2,3,3,2,1,4,5,6), nrow=3, ncol=3, byrow=TRUE)
## A <- matrix(c(1,2,3,1), nrow=2, ncol=2, byrow=TRUE)
A_LU <- LU_fact(A)
LM <- A_LU[[1]]
UM <- A_LU[[2]]

LM
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    3    1    0
## [3,]    4   13    1
UM
##      [,1] [,2] [,3]
## [1,]    1   -2    3
## [2,]    0    8   -8
## [3,]    0  -91   98
A
##      [,1] [,2] [,3]
## [1,]    1   -2    3
## [2,]    3    2    1
## [3,]    4    5    6
LM%*%UM
##      [,1] [,2] [,3]
## [1,]    1   -2    3
## [2,]    3    2    1
## [3,]    4    5    6
A == LM%*%UM
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE