Problem set 1

Part 1

Part 2


Problem set 2

Write an R function to factorize a square matrix A into LU or LDU

# Creating a function to factorize A
lufactor <- function(A) {
  
  L <-  diag(x = 1, ncol = ncol(A), nrow = nrow(A))
  U <- A
  
  for (i in 1:(nrow(U) - 1)){
    for (j in (i + 1):nrow(U)){
      if (U[i, i] != 0){
        multiplier = U[j, i] / U[i, i]
        L[j, i] = multiplier
        U[j, ] = U[j, ] - multiplier * U[i, ]
      }
    }
  }
  return(list('L' = L, 'U' = U))
}



# Testing with a 4X4 matrix
A <- matrix(c(1, -2, 12, 6,1, 1, -5, 8, 5, -2,-6,-9,1,5,-9,7), nrow = 4, byrow = TRUE)

lufactor(A)
## $L
##      [,1]     [,2]       [,3] [,4]
## [1,]    1 0.000000  0.0000000    0
## [2,]    1 1.000000  0.0000000    0
## [3,]    5 2.666667  1.0000000    0
## [4,]    1 2.333333 -0.9032258    1
## 
## $U
##      [,1] [,2]      [,3]      [,4]
## [1,]    1   -2  12.00000   6.00000
## [2,]    0    3 -17.00000   2.00000
## [3,]    0    0 -20.66667 -44.33333
## [4,]    0    0   0.00000 -43.70968

I referred to the following sources in order to create this function: