If A is an mXn matrix where m = no of rows and n = no of columns and m != n then AAT will be mXm matrix and ATA will be nxn matrix. since m !=n these two results can not be same
tryCatch(
{
A <- matrix(c(3,5,7,6,4,2), nrow=2, byrow=TRUE)
At <- t(A)
AAT = A %*% At
ATA = At %*% A
AAT== ATA
},error = function(e){(print (e))
})
## <simpleError in AAT == ATA: non-conformable arrays>
## <simpleError in AAT == ATA: non-conformable arrays>
This can be true for a matrix where m = n and AT = A
A <- matrix(c(4,5,3,5,4,3,3,3,8), nrow=3, byrow=TRUE)
At <- t(A)
AAT = A %*% At
ATA = At %*% A
print(AAT == ATA)
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
MatrixDecomposition <- function(A) {
U <- A
n <- dim(A)[1]
L <- diag(n)
# For loops to construct LTM and UTM
for(i in 2:n) {
for(j in 1:(i-1)) {
multiplier <- -U[i,j] / U[j,j]
U[i, ] <- multiplier * U[j, ] + U[i, ]
L[i,j] <- -multiplier
}
}
return(list(L,U))
}
A <- matrix(c(5,8,-1,2,12,9,7,8,11), nrow=3, byrow=TRUE)
LU <- MatrixDecomposition(A)
L<-LU[[1]]
U<-LU[[2]]
A == L %*% U
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE