Problem 1
- Show that \(A^{T}A \neq AA^{T}\) in general. (Proof and demostration)
- explore mxn matrix where \(m \neq n\)
# 1. 2x3 matrix will have 3x2 transpose. The dimension of AA.T is 2x2, however, the dimension of A.TA is 3x3. Therefore, in general, a mxn matrix will have nxm transpose, the poduct of itself and its transpose and the product of its transpose and itself will have different dimension, so that these two product are not going to be the same.
A = matrix(c(1, 2, 3, 4, 5,6), 2,3)
AT = t(A)
AAT = A%*%AT
ATA = AT %*%A
dim(AAT)## [1] 2 2
dim(ATA)## [1] 3 3
identical(ATA, AAT)## [1] FALSE
- explore mxn matrix where m = n
# 3x3 matrix will have 3x3 transpose. Even though the product dimension is going to be the same, the values of both product are different.
B = matrix(c(1,2,3,4,5,6,7,8,9), 3,3)
BT = t(B)
BBT = B %*% BT
BTB = BT %*% B
dim(BBT)## [1] 3 3
dim(BTB)## [1] 3 3
identical(BTB, BBT)## [1] FALSE
- For a special type of square matrix A, we get \(A^{T}A = AA^{T}\) . Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).
- \(A = A^{T} => A^{T}A = AA^{T} = I\)
# example
C = matrix(c(-1,0,0,1),2,2)
CT = t(C)
identical(C,CT)## [1] TRUE
# product checking
CCT = C%*%CT
CTC = CT%*%C
identical(CCT, CTC)## [1] TRUE
Problem 2
Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer.
lu <- function(matrix){
if(nrow(matrix) != ncol(matrix)){
paste("this is not a square matrix")
break
}else{
n <- nrow(matrix)
L <- diag(n) # L: matrix with diagonal = 1
U <- matrix # U = copy of matrix
for(i in 1:(n-1)){
for (j in (i+1) : n) {
L[j,i] <- U[j,i]/U[i,i]
U[j,] <- U[j,]-L[j,i]*U[i,]
}
}
LU <- list('L' = L, 'U' = U)
}
}# test the code. The matrix is from the video provided in BB
A <- matrix(c(1,4,-3,-2,8,5,3,4,7), nrow = 3, byrow = T)
A## [,1] [,2] [,3]
## [1,] 1 4 -3
## [2,] -2 8 5
## [3,] 3 4 7
lu(A)$L## [,1] [,2] [,3]
## [1,] 1 0.0 0
## [2,] -2 1.0 0
## [3,] 3 -0.5 1
lu(A)$U## [,1] [,2] [,3]
## [1,] 1 4 -3.0
## [2,] 0 16 -1.0
## [3,] 0 0 15.5
identical(lu(A)$L %*% lu(A)$U, A)## [1] TRUE