\(~\)
\(~\)
# Creating matrix 4 x 3 with random numbers
matrixA <- matrix(c(1:12), nrow = 4, byrow = T)
matrixA## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
\(~\)
# Transpose "matrixA"; it creates a 3 x 4 matrix
matrixAT <- t(matrixA)
matrixAT## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
\(~\)
# Checking to see if A^TA ≠ AA^T
# A^TA; matrixA multiplied by matrixAT
ATA <- matrixA %*% matrixAT
ATA## [,1] [,2] [,3] [,4]
## [1,] 14 32 50 68
## [2,] 32 77 122 167
## [3,] 50 122 194 266
## [4,] 68 167 266 365
#AA^T; matrixAT multiplied by matrixA
AAT <- matrixAT %*% matrixA
AAT## [,1] [,2] [,3]
## [1,] 166 188 210
## [2,] 188 214 240
## [3,] 210 240 270
\(~\)
# Create matrix for A
A <- matrix(c(1:9), nrow = 3, ncol = 3)
A## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
# Create matrix for AT
AT <- matrix(c(1:9), nrow = 3, ncol = 3)
AT## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
\(~\)
# Multiply matrix A with matrix AT
A %*% AT## [,1] [,2] [,3]
## [1,] 30 66 102
## [2,] 36 81 126
## [3,] 42 96 150
# Multiply matrix AT with matrix A
AT %*% A## [,1] [,2] [,3]
## [1,] 30 66 102
## [2,] 36 81 126
## [3,] 42 96 150
\(~\)
A %*% AT == AT %*% A## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
\(~\)
After transposing ATA matrix the results were a 3x3 AAT matrix, originally a 4x3, therefore in general, \(A^TA \ne AA^T\). For part two, when the matrix is symmetrical the transposing of the matrices equals to the matrix itself, therefore \(A^TA = AA^T\).
\(~\)
\(~\)
# Create the function
LU <- function(A) {
# Upper triangular
U <- A
# Setting matrix dimension
n <- dim(A)[1]
# Lower triangular
L <- diag(n)
if (n == 1) {
return(list(L, U))
}
for(a in 2:n) {
for(t in 1:(a - 1)) {
multiplier <- -U[a, t] / U[t, t]
U[a, ] <- multiplier * U[t, ] + U[a, ]
L[a, t] <- -multiplier
}
}
return(list(L,U))
}\(~\)
# Application to the function
# Testing with a 3x3 matrix
A <- matrix(1:9, nrow = 3, byrow = T)
A## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
LU_2 <- LU(A)
# Multiply the upper and lower matrix
lower_multiply <- LU_2[[1]]
lower_multiply## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 4 1 0
## [3,] 7 2 1
upper_multiply <- LU_2[[2]]
upper_multiply## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 0 -3 -6
## [3,] 0 0 0
A == lower_multiply %*% upper_multiply## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
\(~\)