Let’s first define our matrix A.
A = matrix(c(5,4,7,9), ncol=2, byrow=T)
A
## [,1] [,2]
## [1,] 5 4
## [2,] 7 9
Define our \(A^T\)
AT = t(A)
AT
## [,1] [,2]
## [1,] 5 7
## [2,] 4 9
If we times AT by A we get:
ATA = AT %*% A
ATA
## [,1] [,2]
## [1,] 74 83
## [2,] 83 97
Hence, \(A^TT\neq AA^T\)
ATA == AT
## [,1] [,2]
## [1,] FALSE FALSE
## [2,] FALSE FALSE
A <- matrix(sample(1:100,25), nrow = 5, byrow = TRUE)
A
## [,1] [,2] [,3] [,4] [,5]
## [1,] 72 23 13 59 45
## [2,] 8 77 51 91 84
## [3,] 89 64 25 24 26
## [4,] 3 93 7 73 95
## [5,] 74 42 32 33 79
ATA <- t(A) %*% A
ATA
## [,1] [,2] [,3] [,4] [,5]
## [1,] 18654 11355 5958 9773 12357
## [2,] 11355 20967 7821 18075 21320
## [3,] 5958 7821 4468 7575 8712
## [4,] 9773 18075 7575 18756 20465
## [5,] 12357 21320 8712 20465 25023
m1 <- ATA %*% t(ATA)
m1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 760612483 936592188 408252990 868867410 1033715830
## [2,] 936592188 1410967180 589238190 1324523515 1558861962
## [3,] 408252990 589238190 249908398 553804989 652314693
## [4,] 868867410 1324523515 553804989 1250201540 1468054596
## [5,] 1033715830 1558861962 652314693 1468054596 1728103547
m2 <- t(ATA) %*% ATA
m2
## [,1] [,2] [,3] [,4] [,5]
## [1,] 760612483 936592188 408252990 868867410 1033715830
## [2,] 936592188 1410967180 589238190 1324523515 1558861962
## [3,] 408252990 589238190 249908398 553804989 652314693
## [4,] 868867410 1324523515 553804989 1250201540 1468054596
## [5,] 1033715830 1558861962 652314693 1468054596 1728103547
Hence, for this special case \(A^T A = AA^T\)
m1 == m2
## [,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
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 flights 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 flight using radars.
Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer. Please submit your response in an R Markdown document using our class naming convention, E.g. LFulton_Assignment2_PS2.png
LU_convert <- function(A){
#Check for Square matrix
#stopifnot(nrow(A) == ncol(A))
if(nrow(A) == ncol(A)){
print('Square Matrix')
} else{
warning('Not a Sqaure Matrix')
return(A)
}
size <- nrow(A)
#Upper Triangle
U <- A
L <- diag(nrow(A))
for (j in 1:(size-1)){
i <- j+1
for (i in (j+1):size){
x <- -U[i,j]/U[j,j]
U[i,j] <- 0
j2 <- j+1
for (j2 in (j+1):size){
U[i,j2] <- U[i,j2] + x*U[j,j2]
}
#Lower Triangle
L[i,j] = -x
}
}
print("Original Matrix")
print(A)
print("Upper Triangle")
print(U)
print("Lower Triangle")
print(L)
print("A = LU")
print(A == (L %*% U))
print(L %*% U)
}
A2X2 <- matrix(c(5,6,9,8), ncol = 2)
LU_convert(A2X2)
## [1] "Square Matrix"
## [1] "Original Matrix"
## [,1] [,2]
## [1,] 5 9
## [2,] 6 8
## [1] "Upper Triangle"
## [,1] [,2]
## [1,] 5 9.0
## [2,] 0 -2.8
## [1] "Lower Triangle"
## [,1] [,2]
## [1,] 1.0 0
## [2,] 1.2 1
## [1] "A = LU"
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [,1] [,2]
## [1,] 5 9
## [2,] 6 8
A3X3 <- matrix(c(5,6,9,8,11,12,15,16,1), ncol =3 )
LU_convert(A3X3)
## [1] "Square Matrix"
## [1] "Original Matrix"
## [,1] [,2] [,3]
## [1,] 5 8 15
## [2,] 6 11 16
## [3,] 9 12 1
## [1] "Upper Triangle"
## [,1] [,2] [,3]
## [1,] 5 8.0 15.00000
## [2,] 0 1.4 -2.00000
## [3,] 0 0.0 -29.42857
## [1] "Lower Triangle"
## [,1] [,2] [,3]
## [1,] 1.0 0.000000 0
## [2,] 1.2 1.000000 0
## [3,] 1.8 -1.714286 1
## [1] "A = LU"
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [,1] [,2] [,3]
## [1,] 5 8 15
## [2,] 6 11 16
## [3,] 9 12 1