(A <- matrix(c(3,2,1,4,3,1,-4,2,-5), nrow=3, byrow=TRUE))
## [,1] [,2] [,3]
## [1,] 3 2 1
## [2,] 4 3 1
## [3,] -4 2 -5
#A Transpose
(At <- t(A))
## [,1] [,2] [,3]
## [1,] 3 4 -4
## [2,] 2 3 2
## [3,] 1 1 -5
A%*%At
## [,1] [,2] [,3]
## [1,] 14 19 -13
## [2,] 19 26 -15
## [3,] -13 -15 45
At%*%A
## [,1] [,2] [,3]
## [1,] 41 10 27
## [2,] 10 17 -5
## [3,] 27 -5 27
A %*% At == At %*% A
## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
could this be true? (Hint: The Identity matrix I is an example of such a matrix).
for instance:
(A <- matrix(c(1,2,0,2,1,0,0,0,1), nrow=3, byrow=TRUE))
## [,1] [,2] [,3]
## [1,] 1 2 0
## [2,] 2 1 0
## [3,] 0 0 1
AT will then be
(At <- t(A))
## [,1] [,2] [,3]
## [1,] 1 2 0
## [2,] 2 1 0
## [3,] 0 0 1
Checking the statement above:
A == At
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
A %*% At == At %*% A
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
#Matrix factorization is a very important problem. There are supercomputers built just to do matrix factorizations. Every second you are on an airplane, matrices are being factorized. Radars that track fights use a technique called Kalman filtering. At the heart of Kalman Filtering is a Matrix Factorization operation. Kalman Filters are solving linear systems of equations when they track your fight using radars. Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer
Choosing the LU version =>
luDecomposer <- function(A)
{
# if A is not a square matrix, return null
if (dim(A)[1]!=dim(A)[2])
{
return(NULL)
}
M <- A
dimOfA <- dim(A)[1]
diagA <- diag(dimOfA)
if (dimOfA == 1)
{
return(list(diagA, M))
}
#Using a nested for-loop
for(a in 2:dimOfA)
{
for(b in 1:(a-1))
{
mx <- (-M[a,b] / M[b,b]) #devicing a multiplier
M[a, ] <- mx * M[b, ] + M[a, ]
diagA[a,b] <- -mx
}
}
return(list(diagA,M))
}
(A <- matrix(c(4,1,1,1,1,2,5,4,3), nrow=3, byrow=TRUE))
## [,1] [,2] [,3]
## [1,] 4 1 1
## [2,] 1 1 2
## [3,] 5 4 3
luD <- luDecomposer(A)
(l<-luD[[1]])
## [,1] [,2] [,3]
## [1,] 1.00 0.000000 0
## [2,] 0.25 1.000000 0
## [3,] 1.25 3.666667 1
(u<-luD[[2]])
## [,1] [,2] [,3]
## [1,] 4 1.00 1.000000
## [2,] 0 0.75 1.750000
## [3,] 0 0.00 -4.666667
How did it fare?
A == l %*% u
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE