Problem Set 1:
1. Show that \(A^TA\neq AA^T\)
# First create matrix A
A <- matrix(c(1,2,3,4,5,6,7,8,9), nrow = 3)
A
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
# Then transpose matrix A using t()
AT <- t(A)
AT
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
# Multiply the transposed matrix A by matrix A
ATA <- AT %*% A
ATA
## [,1] [,2] [,3]
## [1,] 14 32 50
## [2,] 32 77 122
## [3,] 50 122 194
# Multiply A by transposed matrix A (AA^T)
AAT <- A %*% AT
AAT
## [,1] [,2] [,3]
## [1,] 66 78 90
## [2,] 78 93 108
## [3,] 90 108 126
# You can see that the matrices are not identical
ATA != AAT
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
2. For a special type of square matrix \(A\), we get \(A^TA = AA^T\). Under what conditions could this be true?
## The only time this would work is if the item is an identity matrix; it can also include a constant multiplier (i.e. diag * c)
# Using Identity Matrix of 3x3
I <- diag(3)
I
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
# Transpose the id matrix
IT <- t(I)
# try both
I1 <- IT %*% I
I2 <- I %*% IT
# check using logic
I1 != I2
## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
Matrix Factorization L U
A = matrix(c(1,2,-1,1,-1,-2,3,5,4), nrow=3, ncol=3)
b = matrix(c(1, 2, 6), nrow = 3)
calc <- function (A,b){
full = cbind(A,b)
k=0
#initialize Lower
L = diag(dim(A)[2])
while (k < ncol(A)){
k = k+1
i = k
j = k
while (i <= nrow(full)-1){
c = full[i+1,j]/full[j,j]
full[i+1,] = full[i+1,]-c*full[j,]
L[i+1,j] = c
i = 1+i
}
}
if (nrow(A) == 3){
j=nrow(A)-1
x3 = full[j+1,j+2]/full[j+1,j+1]
x2 = (full[j,j+2] - full[j,j+1]*x3)/full[j,j]
x1 = (full[j-1,j+2] - full[j-1,j+1]*x3 - full[j-1,j]*x2 )/full[j-1,j-1]
}
sol = cbind(x1, x2, x3)
print("The following is the upper matrix")
print(full[,1:3])
print("The following is the lower matrix")
print(L)
}
calc(A,b)
## [1] "The following is the upper matrix"
## [,1] [,2] [,3]
## [1,] 1 1 3.000000
## [2,] 0 -3 -1.000000
## [3,] 0 0 7.333333
## [1] "The following is the lower matrix"
## [,1] [,2] [,3]
## [1,] 1 0.0000000 0
## [2,] 2 1.0000000 0
## [3,] -1 0.3333333 1