Problem 1 Create matrix
set.seed(500)
A<-matrix(sample(30,16, replace=F), nrow=4,byrow=T)
A
## [,1] [,2] [,3] [,4]
## [1,] 7 27 15 21
## [2,] 18 9 12 28
## [3,] 10 26 4 19
## [4,] 22 24 6 29
transpose A
AT<-t(A)
AT
## [,1] [,2] [,3] [,4]
## [1,] 7 18 10 22
## [2,] 27 9 26 24
## [3,] 15 12 4 6
## [4,] 21 28 19 29
matrix multiplication
ATA<-AT %*% A
ATA
## [,1] [,2] [,3] [,4]
## [1,] 957 1139 493 1479
## [2,] 1139 2062 761 2009
## [3,] 493 761 421 901
## [4,] 1479 2009 901 2427
AAT<- A %*% AT
AAT
## [,1] [,2] [,3] [,4]
## [1,] 1444 1137 1231 1501
## [2,] 1137 1333 994 1496
## [3,] 1231 994 1153 1419
## [4,] 1501 1496 1419 1937
compare the result
ATA == AAT
## [,1] [,2] [,3] [,4]
## [1,] FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE
Result shows that the AAT is not equal to ATA In linear Algebra, a symmetric matrix is a square matrix that is equal to its transpose A=AT, ATA=AAT every square diagnomal matrix is symmetric
a<-diag(5)
a
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 0
## [2,] 0 1 0 0 0
## [3,] 0 0 1 0 0
## [4,] 0 0 0 1 0
## [5,] 0 0 0 0 1
at<-t(a)
at
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 0
## [2,] 0 1 0 0 0
## [3,] 0 0 1 0 0
## [4,] 0 0 0 1 0
## [5,] 0 0 0 0 1
How can we see that at and a is equal, there for, it is going to make the AAT and ATA equal.
aat<-a %*% at
aat
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 0
## [2,] 0 1 0 0 0
## [3,] 0 0 1 0 0
## [4,] 0 0 0 1 0
## [5,] 0 0 0 0 1
ata<- at %*% a
ata
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0 0 0 0
## [2,] 0 1 0 0 0
## [3,] 0 0 1 0 0
## [4,] 0 0 0 1 0
## [5,] 0 0 0 0 1
compare it now
ata == aat
## [,1] [,2] [,3] [,4] [,5]
## [1,] TRUE TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE TRUE
## [5,] TRUE TRUE TRUE TRUE TRUE
letโs try another symmetric matrix with different numbers
b<-matrix(c(2,0,0,0,22,0,0,0,6),nrow = 3, byrow = T)
b
## [,1] [,2] [,3]
## [1,] 2 0 0
## [2,] 0 22 0
## [3,] 0 0 6
bt<-t(b)
bt
## [,1] [,2] [,3]
## [1,] 2 0 0
## [2,] 0 22 0
## [3,] 0 0 6
bbt<- b %*% bt
bbt
## [,1] [,2] [,3]
## [1,] 4 0 0
## [2,] 0 484 0
## [3,] 0 0 36
btb<- bt %*% b
btb
## [,1] [,2] [,3]
## [1,] 4 0 0
## [2,] 0 484 0
## [3,] 0 0 36
compare it again
bbt == btb
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
With trying other numbers, it still works so from the left to right bottom has to be the same numbers in order to make AAT and ATA equal, rest of the numbers shold be 0
Problem 2
# Check if matrix is square
LU <- function(A){
if (dim(A)[1]!=dim(A)[2]){
return("matrix is not square")
}
r<-nrow(A)
U<-A
L<-diag(r)
for (x in c(1:r)){
for(y in c(2:r)){
if(y>x){
row<- U[x,]
multiplier <- U[y,x]/row[x]
U[y,]<- U[y,] - (multiplier*row)
L[y,x] <- multiplier
}
}
}
return(list(L=L, U=U))
}
get the matrix A
set.seed(1000)
A <- matrix((sample(9,9, replace = F)), nrow = 3, byrow = TRUE)
A
## [,1] [,2] [,3]
## [1,] 4 3 6
## [2,] 8 5 7
## [3,] 1 2 9
test it with the samples
LUA<-LU(A)
LUA
## $L
## [,1] [,2] [,3]
## [1,] 1.00 0.00 0
## [2,] 2.00 1.00 0
## [3,] 0.25 -1.25 1
##
## $U
## [,1] [,2] [,3]
## [1,] 4 3 6.00
## [2,] 0 -1 -5.00
## [3,] 0 0 1.25
Use lu.decomposition() function to see if any difference
library(matrixcalc)
A<-matrix(c(4,3,6,8,5,7,1,2,9), nrow = 3,byrow = T)
A
## [,1] [,2] [,3]
## [1,] 4 3 6
## [2,] 8 5 7
## [3,] 1 2 9
LUDA<-lu.decomposition(A)
LUDA
## $L
## [,1] [,2] [,3]
## [1,] 1.00 0.00 0
## [2,] 2.00 1.00 0
## [3,] 0.25 -1.25 1
##
## $U
## [,1] [,2] [,3]
## [1,] 4 3 6.00
## [2,] 0 -1 -5.00
## [3,] 0 0 1.25
LUDA$L == LUA$L
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
LUDA$U == LUA$U
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
according the comparison, LUDA and LUA are equal and function works