Problem set 1

(1) Show that ATA != AAT in general. (Proof and demonstration)

# Take A Matrix and allocate the values
A<- matrix(c(1,2,1,2,1,2,1,1,1,1,6,1,1,1,8,7), 4,4,byrow = T)

# Find transpose of the Matrix
AT<- t(A)

# Proof
ATA_LHS <- AT %*% A
AAT_RHS <- A%*% AT
ATA_LHS==AAT_RHS
##       [,1]  [,2]  [,3]  [,4]
## [1,] FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE

By this we can demonstrate ATA != AAT

(2) For a special type of square matrix A, we get ATA = AAT. Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).

#METHOD-1:if the matrix is identity matrix will have ATA=AAT
A <- matrix(c(1,0,0,0,1,0,0,0,1), 3,3, byrow = T)
AT<- t(A)
ATA_LHS <- AT %*% A
AAT_RHS <- A%*% AT
ATA_LHS==AAT_RHS
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
#METHOD-2:different diagnol elements in the matrix and remaining elements are zero.
A<- matrix(c(3,0,0,0,8,0,0,0,2), 3,3, byrow = T)
AT<-t(A)
ATA_LHS <- AT %*% A
AAT_RHS <- A%*% AT
ATA_LHS==AAT_RHS
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
#METHOD-3: scalar multiple of identity matrix
A<- matrix(c(1,0,0,0,1,0,0,0,1), 3,3, byrow = T)
scalar_multiple<- 3*A
AT<-t(scalar_multiple)
ATA_LHS <- AT %*% scalar_multiple
AAT_RHS <-scalar_multiple %*% AT
ATA_LHS==AAT_RHS
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

Problem set 2

Write an R function to factorize a square matrix A into LU or LDU.

#METHOD-1:

library(matrixcalc)
(A <- matrix( c(1,2,3,4,5,6,7,8,9), 3,3, byrow=T))
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
luA <- lu.decomposition(A)
(L <- luA$L)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    4    1    0
## [3,]    7    2    1
(U <- luA$U)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    0   -3   -6
## [3,]    0    0    0
( L %*% U )
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
#METHOD-2:
library(Matrix)
(A <- matrix(c(5, 3, -9, 5, 2, -6, 6, 1, -1, 2, -5, 6, 3, 5, -2, -3), 4,4,byrow = T))
##      [,1] [,2] [,3] [,4]
## [1,]    5    3   -9    5
## [2,]    2   -6    6    1
## [3,]   -1    2   -5    6
## [4,]    3    5   -2   -3
luA <- lu(A)
elu <- expand(luA)
(L <- elu$L)
## 4 x 4 Matrix of class "dtrMatrix" (unitriangular)
##      [,1]       [,2]       [,3]       [,4]      
## [1,]  1.0000000          .          .          .
## [2,]  0.4000000  1.0000000          .          .
## [3,]  0.6000000 -0.4444444  1.0000000          .
## [4,] -0.2000000 -0.3611111 -0.4347826  1.0000000
(U <- elu$U)
## 4 x 4 Matrix of class "dtrMatrix"
##      [,1]      [,2]      [,3]      [,4]     
## [1,]  5.000000  3.000000 -9.000000  5.000000
## [2,]         . -7.200000  9.600000 -1.000000
## [3,]         .         .  7.666667 -6.444444
## [4,]         .         .         .  3.836957
(L%*%U)
## 4 x 4 Matrix of class "dgeMatrix"
##      [,1] [,2] [,3] [,4]
## [1,]    5    3   -9    5
## [2,]    2   -6    6    1
## [3,]    3    5   -2   -3
## [4,]   -1    2   -5    6

SOLVING MATRIX:

(B <- matrix(c(13, -18, 29, 19), nrow = 4))
##      [,1]
## [1,]   13
## [2,]  -18
## [3,]   29
## [4,]   19
(P <- elu$P)
## 4 x 4 sparse Matrix of class "pMatrix"
##             
## [1,] | . . .
## [2,] . | . .
## [3,] . . . |
## [4,] . . | .
(Y <- solve(L) %*% P %*% B)
## 4 x 1 Matrix of class "dgeMatrix"
##             [,1]
## [1,]  13.0000000
## [2,] -23.2000000
## [3,]   0.8888889
## [4,]  23.6086957
(X <- solve(U) %*% Y)
## 4 x 1 Matrix of class "dgeMatrix"
##           [,1]
## [1,] 0.3144476
## [2,] 9.4183192
## [3,] 5.2880076
## [4,] 6.1529745