\(~\)
\(~\)
# Creating matrix 4 x 3 with random numbers
<- matrix(c(1:12), nrow = 4, byrow = T)
matrixA 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
<- t(matrixA)
matrixAT 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
<- matrixA %*% matrixAT
ATA 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
<- matrixAT %*% matrixA
AAT AAT
## [,1] [,2] [,3]
## [1,] 166 188 210
## [2,] 188 214 240
## [3,] 210 240 270
\(~\)
# Create matrix for A
<- matrix(c(1:9), nrow = 3, ncol = 3)
A A
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
# Create matrix for AT
<- matrix(c(1:9), nrow = 3, ncol = 3)
AT AT
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
\(~\)
# Multiply matrix A with matrix AT
%*% AT A
## [,1] [,2] [,3]
## [1,] 30 66 102
## [2,] 36 81 126
## [3,] 42 96 150
# Multiply matrix AT with matrix A
%*% A AT
## [,1] [,2] [,3]
## [1,] 30 66 102
## [2,] 36 81 126
## [3,] 42 96 150
\(~\)
%*% AT == AT %*% A 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
<- function(A) {
LU
# Upper triangular
<- A
U
# Setting matrix dimension
<- dim(A)[1]
n
# Lower triangular
<- diag(n)
L
if (n == 1) {
return(list(L, U))
}
for(a in 2:n) {
for(t in 1:(a - 1)) {
<- -U[a, t] / U[t, t]
multiplier <- multiplier * U[t, ] + U[a, ]
U[a, ] <- -multiplier
L[a, t]
}
}return(list(L,U))
}
\(~\)
# Application to the function
# Testing with a 3x3 matrix
<- matrix(1:9, nrow = 3, byrow = T)
A A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
<- LU(A)
LU_2
# Multiply the upper and lower matrix
<- LU_2[[1]]
lower_multiply lower_multiply
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 4 1 0
## [3,] 7 2 1
<- LU_2[[2]]
upper_multiply upper_multiply
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 0 -3 -6
## [3,] 0 0 0
== lower_multiply %*% upper_multiply A
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
\(~\)