1.1 Show that AT A ̸= AAT in general. (Proof and demonstration.)
\[ A = \begin{bmatrix} a11 & a12 \\ a21 & a22 \end{bmatrix} , B = \begin{bmatrix} a11 & a21 \\ a12 & a22 \end{bmatrix} \]
Proof:
D11: (a11∗a11)+(a21∗a21)≠(a11∗a11)+(a12∗a12)
D21: (a12∗a11)+(a22∗a21)≠(a21∗a11)+(a22∗a12)
D12: (a11∗a12)+(a21∗a22)≠(a11∗a21)+(a12∗a22)
D22: (a12∗a12)+(a22∗a22)≠(a21∗a21)+(a22∗a22)
Demonstration:
A <- matrix(c(5, 8, 0, -33), nrow=2, ncol=2)
A
## [,1] [,2]
## [1,] 5 0
## [2,] 8 -33
t(A)%*%A
## [,1] [,2]
## [1,] 89 -264
## [2,] -264 1089
A%*%t(A)
## [,1] [,2]
## [1,] 25 40
## [2,] 40 1153
1.2 For a special type of square matrix A, we get AT A = AAT . Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).
ATA=AAT only if a matrix is symmetrical (aij=aji).
A <- matrix(c(4,-3,0,-3,8,12,0,12,-2), nrow=3, ncol=3)
A
## [,1] [,2] [,3]
## [1,] 4 -3 0
## [2,] -3 8 12
## [3,] 0 12 -2
t(A)%*%A
## [,1] [,2] [,3]
## [1,] 25 -36 -36
## [2,] -36 217 72
## [3,] -36 72 148
A%*%t(A)
## [,1] [,2] [,3]
## [1,] 25 -36 -36
## [2,] -36 217 72
## [3,] -36 72 148
Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer. You don’t have to worry about permuting rows of A and you can assume that A is less than 5x5, if you need to hard-code any variables in your code.
LU <- function(mtrx){
L <- diag(nrow(mtrx))
U <- mtrx
for (i in 1:(nrow(mtrx) - 1)) {
for (j in (i+1):nrow(mtrx)) {
E = U[j,i]/U[i,i]
# updating lower triangular matrix
L[j,i] <- E
# updating upper triangular matrix
U[j,] <- U[j,] - (U[i,] * (E))
}
}
LU_list<- list("L" = L, "U" = U, "L*U" = L%*%U )
return(LU_list)
}
Test
A<-matrix(c(-1,13,6,0,9,-5,3,7,5,-1,3,10,26,-12,0,17),ncol=4,nrow=4)
A
## [,1] [,2] [,3] [,4]
## [1,] -1 9 5 26
## [2,] 13 -5 -1 -12
## [3,] 6 3 3 0
## [4,] 0 7 10 17
LU(A)
## $L
## [,1] [,2] [,3] [,4]
## [1,] 1 0.0000000 0 0
## [2,] -13 1.0000000 0 0
## [3,] -6 0.5089286 1 0
## [4,] 0 0.0625000 14 1
##
## $U
## [,1] [,2] [,3] [,4]
## [1,] -1 9 5.0000000 26.000000
## [2,] 0 112 64.0000000 326.000000
## [3,] 0 0 0.4285714 -9.910714
## [4,] 0 0 0.0000000 135.375000
##
## $`L*U`
## [,1] [,2] [,3] [,4]
## [1,] -1 9 5 26
## [2,] 13 -5 -1 -12
## [3,] 6 3 3 0
## [4,] 0 7 10 17