Show that ATA = AAT in general. (Proof and demonstration.)
library(matrixcalc)
## Let A =
A <- matrix(c(3,1,2,4,5,1,2,3,7),nrow=3,ncol=3,byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 3 1 2
## [2,] 4 5 1
## [3,] 2 3 7
## The transpose of A is then:
Transpose_A <- t(A)
Transpose_A
## [,1] [,2] [,3]
## [1,] 3 4 2
## [2,] 1 5 3
## [3,] 2 1 7
## Matrix Multiply AT %*% A
ATA <- Transpose_A %*% A
ATA
## [,1] [,2] [,3]
## [1,] 29 29 24
## [2,] 29 35 28
## [3,] 24 28 54
## Matrix Multiple A %*% At
AAT <- A %*% Transpose_A
AAT
## [,1] [,2] [,3]
## [1,] 14 19 23
## [2,] 19 42 30
## [3,] 23 30 62
Thus we can see that ATA != AAT
## we get false values here
AAT == ATA
## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
For a special type of square matrix A, we get ATA = AAT.Under what conditions could this be true?
A particular type of square matrix that can hold under this condition is when the values of the lower and upper triangles are the same. Regardless if the values are swapped over the diagonals, the matrix is still the same. An example of such a matrix would be taking the product of our previous matrix from problem one and applying its transpose. Then, matrix multiply the matrix and its transpose, and vice-versa we can see that the values are the same, which makes AAT = ATA accurate.
## Let our new matrix be the product of A*AT
a <- AAT
a
## [,1] [,2] [,3]
## [1,] 14 19 23
## [2,] 19 42 30
## [3,] 23 30 62
## Let us take the transpose of A*AT now
Ta <- t(a)
Ta
## [,1] [,2] [,3]
## [1,] 14 19 23
## [2,] 19 42 30
## [3,] 23 30 62
## Let us matrix multiple a and its transpose first!
aaT <- a %*% Ta
aaT
## [,1] [,2] [,3]
## [1,] 1086 1754 2318
## [2,] 1754 3025 3557
## [3,] 2318 3557 5273
## Let us matrix multiply its transpose and a next!
aTa <- Ta %*% a
aTa
## [,1] [,2] [,3]
## [1,] 1086 1754 2318
## [2,] 1754 3025 3557
## [3,] 2318 3557 5273
##verify that aaT == aTa
aaT == aTa
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer.
a <- matrix(c(2,10,4,1),nrow=2,byrow=TRUE)
Lu <- function(A){
dimens <- dim(A)
U <- A
L <- diag(dimens)
for (i in 2:dimens[1]){
for(j in 1:(i-1)){
multipler <- -U[i,j] / U[j,j]
U[i,] <- multipler * U[j,] + U[i,] ## performing row operations
L[i,j] <- -multipler
}
}
print(L)
print(U)
}
Lu(a)
## [,1] [,2]
## [1,] 2 0
## [2,] 2 2
## [,1] [,2]
## [1,] 2 10
## [2,] 0 -19
##Checking my code with lu function
lu.decomposition(a)
## $L
## [,1] [,2]
## [1,] 1 0
## [2,] 2 1
##
## $U
## [,1] [,2]
## [1,] 2 10
## [2,] 0 -19