| 5 | 7 |
| 6 | 8 |
A <- matrix(c(5, 6, 7, 8), ncol = 2)
t(A)
## [,1] [,2]
## [1,] 5 6
## [2,] 7 8
t <- 2
ATA <- A^t %*% A
AAT <- A %*% A^t
identical(ATA, AAT)
## [1] FALSE
Answer: Here, We have a matrix A
A <- matrix(c(1, 1, 1, 1, 2, 1, 1, 1, 1), nrow = 3, byrow = T)
A
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 2 1
## [3,] 1 1 1
# let's transpose the value of A
t(A)
## [,1] [,2] [,3]
## [1,] 1 1 1
## [2,] 1 2 1
## [3,] 1 1 1
#Write equations for $A^T A = AA^T$
ATA <- t(A) %*% A
AAT <- A %*% t(A)
AAT == ATA
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
Hence, we can state that it is true when the matrix is symmetrical.
Answer:
# to freeze the randomnly selected numbers we need to freeze it and set.seed helps us to do that.
set.seed(5)
n <- 5
X <- matrix(sample.int(5, size = n^2, replace = TRUE), ncol = n)
X
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2 4 2 2 5
## [2,] 4 3 3 2 4
## [3,] 5 5 2 5 2
## [4,] 2 5 3 3 2
## [5,] 1 1 2 5 1
# let's take the values for the matrix.
matrixLDU <- function(X){
U = X
# to populate cells we need a diagonal
L = diag(x = 1, ncol = ncol(X), nrow = nrow(X))
# here we are using loop to populate elements for L & U
for (i in 1:(nrow(U) - 1)){
for (j in (i + 1):nrow(U)){
if (U[i, i] != 0){
multiplier = (U[j, i] / U[i, i])
L[j, i] = multiplier
U[j,] = (U[j,] - multiplier*U[i,])
}
}
}
return(list('L'= L, 'U'= U))
}
Y <- matrixLDU(X)
Y$L
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1.0 0.0 0.0 0 0
## [2,] 2.0 1.0 0.0 0 0
## [3,] 2.5 1.0 1.0 0 0
## [4,] 1.0 -0.2 -0.4 1 0
## [5,] 0.5 0.2 -0.6 4 1
Y$U
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2 4 2 2.0 5.0
## [2,] 0 -5 -1 -2.0 -6.0
## [3,] 0 0 -2 2.0 -4.5
## [4,] 0 0 0 1.4 -6.0
## [5,] 0 0 0 0.0 21.0
# to calculate the outer product for dimensions
Y$L%*%Y$U
## [,1] [,2] [,3] [,4] [,5]
## [1,] 2 4 2 2 5
## [2,] 4 3 3 2 4
## [3,] 5 5 2 5 2
## [4,] 2 5 3 3 2
## [5,] 1 1 2 5 1
# to create another matrix and check
Z <- matrix(c(1:16), ncol = 4)
Y1 <- matrixLDU(Z)
Y1$L
## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 0
## [2,] 2 1 0 0
## [3,] 3 2 1 0
## [4,] 4 3 0 1
Y1$U
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 0 -4 -8 -12
## [3,] 0 0 0 0
## [4,] 0 0 0 0
# to check the values of the outer product and dimensions
all.equal(Y1$L %*% Y1$U, Z)
## [1] TRUE