\[\text{Let }A = \begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{bmatrix} \qquad A^\intercal = \begin{bmatrix} 1 & 4\\ 2 & 5\\ 3 & 6 \end{bmatrix}\]
\[\begin{align} &B_{1,1} = 1^2 + 2^2 + 3^2 = 14& \\ &B_{1,2} = (1 \times 4) + (2 \times 5) + (3 \times 6) = 32& \\ &B_{2,1} = (4 \times 1) + (5 \times 2) + (6 \times 3) = 32& \\ &B_{2,2} = 4^2 + 5^2 + 6^2 = 77& \end{align} \qquad B = \begin{bmatrix} 14 & 32\\ 32 & 77 \end{bmatrix} \]
\[\begin{align}
&C_{1,1} = 1^2 + 4^2 = 17 &
&C_{1,2} = (1 \times 2) + (4 \times 5) = 22 &
&C_{1,3} = (1 \times 3) + (4 \times 6) = 27 &\\
&C_{2,1} = (2 \times 1) + (5 \times 4) = 22 &
&C_{2,2} = 2^2 + 5^2 = 29 &
&C_{2,3} = (2 \times 3) + (5 \times 6) = 36 &\\
&C_{3,1} = (3 \times 1) + (6 \times 4) = 27 &
&C_{3,2} = (3 \times 2) + (6 \times 5) = 36 &
&C_{3,3} = 3^2 + 6^2 = 45 &
\end{align} \]
\[
C = \begin{bmatrix}
17 & 22 & 27\\
22 & 29 & 36\\
27 & 36 & 45
\end{bmatrix}
\]
\(B \neq C\), therefore \(A \cdot A^\intercal \neq A^\intercal \cdot A\).
Matrix multiplication is commutative when the matrix is symmetric — when the entries mirror along the main diagonal. Transposing a symmetric square matrix \(A\) will return the same matrix, so \(A = A^\intercal\), and \(A \cdot A^\intercal = A^\intercal \cdot A\).
# Test matrix
A <- matrix(c(1,2,3,1,1,1,2,0,1), nrow=3)
factorize <- function(A) {
# Assign the dimensions of A to variables
i <- nrow(A)
j <- ncol(A)
dim(A) <- c(i, j)
# Check whether A is a square matrix
if (is.square.matrix(A) == FALSE) {
return("Use a square matrix")
} else {
# Initialize matrix L as an identity matrix
L <- diag(1, dim(A))
# Initialize matrix U as a copy of A
U <- A
# Loop through each row
for (i in 2:i){
# Loop through each column
for (j in 1:(i-1)) {
# Create elimination matrix E and populate it with the multiplier
E <- diag(1, dim(A))
E[i, j] <- -(U[i,j]/U[j,j])
# Apply the elimination matrix to U
U <- E %*% U
# Update L to be the inverse of the multiplier
L <- L %*% solve(E)
}
}
print("----Upper Diagonal Matrix----")
print(U)
print("----Lower Diagonal Matrix----")
print(L)
print("----------TEST L*U=A----------")
print(L%*%U == A)
}
}
factorize(A)
## [1] "----Upper Diagonal Matrix----"
## [,1] [,2] [,3]
## [1,] 1 1 2
## [2,] 0 -1 -4
## [3,] 0 0 3
## [1] "----Lower Diagonal Matrix----"
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 2 1 0
## [3,] 3 2 1
## [1] "----------TEST L*U=A----------"
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
# Test accuracy of decomposition using function from "matrixcalc" package
lu.decomposition(A)
## $L
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 2 1 0
## [3,] 3 2 1
##
## $U
## [,1] [,2] [,3]
## [1,] 1 1 2
## [2,] 0 -1 -4
## [3,] 0 0 3
# The Upper and Lower Diagonal Matrices match, so my function is accurate.