If \(A\) is an \(m\times n\) matrix then \({ A }^{ T }\) is an \(n\times m\) matrix.
\({ A }^{ T }A\) will be \(n\times m\) and \(A{ A }^{ T }\) will be an \(m\times n\) matrix. For non-square matrices, \({ A }^{ T }A\) and \(A{ A }^{ T }\) will have different numbers of rows and columns. For two matrices to be equivilent, each entry must be equivilent. Thereore \(A{ A }^{ T }\neq{ A }^{ T }A\)
For square matrices, let
\(A\) = \(\begin{bmatrix} a_{ 11 } & { a }_{ 12 } & ...{ a }_{ 1i } \\ { a }_{ 21 } & { a }_{ 22 } & ...{ a }_{ 2i } \\ { a }_{ i1 } & { a }_{ i2 } & { ...a }_{ ii } \end{bmatrix}\)
\({ A }^{ T }\) = \(\begin{bmatrix} a_{ 11 } & { a }_{ 21 } & ...{ a }_{ i1 } \\ { a }_{ 12 } & { a }_{ 22 } & ...{ a }_{ i2 } \\ { a }_{ 1i } & { a }_{ 2i } & { ...a }_{ ii } \end{bmatrix}\)
Each entry \({ A }_{ mn }\) of \(A{ A }^{ T }\) becomes \({ A }_{ mn }=\sum _{ }^{ i }{ { a }_{ mi }{ a }_{ ni } }\)
Each entry of \({ A }^{ T }A\) is \({ A }_{ mn }=\sum _{ }^{ i }{ { a }_{ im }{ a }_{ in } }\)
We can see that expressions are generally not equivilent.
If A is symmetric, \({ a }_{ in }\) = \({ a }_{ ni }\) by definition, so \(\sum _{ }^{ i }{ { a }_{ mi }{ a }_{ ni } } = \sum _{ }^{ i }{ { a }_{ im }{ a }_{ in } }\)
In this case \(A{ A }^{ T }={ A }^{ T }A\)
LU factorization function
LU <- function(matrix){
U <- matrix
rows <- nrow(U)
cols <- ncol(U)
if (rows == cols){
L <- diag(rows)
r <- 1
for (j in 1:(cols - 1)){
if(r > rows){
break
}
cur_row <- U[r, ]
factor <- U[c(1:rows) > r, j]/U[r,j] #Make sure to choose diagonal entries because for an invertable matrix, these will be nonzero
U[c(1:rows) > r, ] <- U[c(1:rows) > r, ] -
matrix(rep(cur_row, each = rows - r), ncol = cols, nrow = rows - r) * factor
L[c(1:rows) > r, j] <- factor
r <- r + 1
}
return(list(L, U))
}else{
print("Can't factorize. Not a square matrix.")
}
}
This will only cover invertable matrices because a permutation matrix is not used.
Test
A <- matrix(c(1,-2,3,4,8,4,-3,5,7), nrow = 3)
LU(A)
## [[1]]
## [,1] [,2] [,3]
## [1,] 1 0.0 0
## [2,] -2 1.0 0
## [3,] 3 -0.5 1
##
## [[2]]
## [,1] [,2] [,3]
## [1,] 1 4 -3.0
## [2,] 0 16 -1.0
## [3,] 0 0 15.5