a <- matrix(c(1,4,2,5,3,6), nrow = 2, ncol = 3)
b <- t(a)
b%*%a
## [,1] [,2] [,3]
## [1,] 17 22 27
## [2,] 22 29 36
## [3,] 27 36 45
a%*%b
## [,1] [,2]
## [1,] 14 32
## [2,] 32 77
You can see that a does not equal to b.
c <- matrix(c(1, 2, 3, 2, 1, 2, 3, 2 ,1), nrow = 3, ncol = 3)
d <- t(c)
c%*%d
## [,1] [,2] [,3]
## [1,] 14 10 10
## [2,] 10 9 10
## [3,] 10 10 14
d%*%c
## [,1] [,2] [,3]
## [1,] 14 10 10
## [2,] 10 9 10
## [3,] 10 10 14
Write an R function to factorize a square matrix A into LU or LDU.
LU factorize
lu_decomposition <- function(matrix, n) {
# Copy of matrix to upper
upper <- matrix
# Diagonal matrix with 1 in diagonal
lower <- diag(n)
# Row Reduce matrix into Upper and Lower triangular matrices
for(i in 1:(n-1)){
p <- upper[i,i]
for (j in range(i+1,n)){
# Lower Triangular
lower[j,i] <- upper[j,i]/p
# Upper Triangular
upper[j,] <- upper[j,]-lower[j,i]*upper[i,]
if(i+1 == n) {break}
}
}
triangle <- list(lower,upper)
return (triangle)
}
matrix <- matrix(c(1,4,7,2,5,8,3,6,9), nrow = 3, ncol = 3)
print(lu_decomposition(matrix, 3))
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 4 1 0
## [3,] 7 2 1
##
## [[2]]
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 0 -3 -6
## [3,] 0 0 0