# create as sample matrix
M <- matrix(c(7,1,5,8,2,3,9,0,4),ncol =3)
# Display the sample matrix
M
## [,1] [,2] [,3]
## [1,] 7 8 9
## [2,] 1 2 0
## [3,] 5 3 4
# calculate as sample matrix transpose
Mt <- t(M)
# Display the sample matrix transpose
Mt
## [,1] [,2] [,3]
## [1,] 7 1 5
## [2,] 8 2 3
## [3,] 9 0 4
# I will now multiply M by its transpose t(M) and display the product
Mt %*% M
## [,1] [,2] [,3]
## [1,] 75 73 83
## [2,] 73 77 84
## [3,] 83 84 97
# I will now perform a check (prove correctness), that in fact ATA != AAT
Mt %*% M == M %*% Mt
## [,1] [,2] [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
We can see from the result above that in fact ATA != AAT is true.
This holds true under the conditions that a particular matrix is symmetrical on the diagonal. Symmetry along the diagonal would indicate that a particular matrix and its transpose are in fact equal. Below is a depiction of this:
# Create a sample Matrix M
M <- matrix(c(5,2,0,2,5,0,0,0,1),ncol =3)
# Display the sample matrix
M
## [,1] [,2] [,3]
## [1,] 5 2 0
## [2,] 2 5 0
## [3,] 0 0 1
# calculate as sample matrix transpose
Mt <- t(M)
# Display the sample matrix transpose
Mt
## [,1] [,2] [,3]
## [1,] 5 2 0
## [2,] 2 5 0
## [3,] 0 0 1
# Prove correctness that this matrix is symmetrical on the diagonal by showing that this matrix is equal to it s transpose
# Since, we assume a symmetric matrix, then: A == t(A). This substitution, will give us the following:
(M %*% t(M)) == (t(M) %*% M)
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
We can see from the result above that in fact ATA == AAT under the condition of a matrix being symmetrical on diagonal.
Matrix factorization is a very important problem. There are supercomputers built just to do matrix factorization. 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 You donโt have to worry about permuting rows of A and you can assume that A is less than 5x5, if you need to hard-code any variables in your code. If you doing the entire assignment in R, then please submit only one markdown document for both the problems.
#sample square matrix A:
A <- matrix (c(6, 7, 8, 1, 4, 5, 5, 2, 1), ncol = 3)
#Function which factorizes a square matrix into Lower and Upper decomposition
SM_Factorize <- function(A){
#Number of rows in matrix A
number_of_rows <- nrow(A)
#Lower triangular matrix represents diagonal matrix (where all elements above and below main diagonal are 0)
Lower <- diag(number_of_rows)
#Upper triangular matrix represents the matrix itself
Upper<- A
#looping through two vectors - 1 representing the rows, 2 representing the columns
for (z in c(1:number_of_rows )){
for(x in c(2:number_of_rows )){
#algorithm starts when column > row, meaning, in A 3X3 square matrix we look at the row of elements in positions:A12,A13,A23
if(x > z){
# current row we look at is the first row
current_row <- Upper[z,];
#retrieve the multiplier by dividing the appropriate elements by the current row
multiplier <- Upper[x, z] / current_row[z]
#have multiplier swap rows to ensure that a triangle of zeros is formed below the diagonal
Upper[x,] <- Upper[x,] - (multiplier * current_row)
#have multiplier swap rows to ensure that a triangle of zeros is formed above the diagonal
Lower[x,z] <- multiplier
}
}
}
#printing the results
#upper triangular matrix
print(Upper)
print (Lower)
print (A)
}
#Run the function
SM_Factorize(A)
## [,1] [,2] [,3]
## [1,] 6 1.000000 5.0000000
## [2,] 0 2.833333 -3.8333333
## [3,] 0 0.000000 -0.7058824
## [,1] [,2] [,3]
## [1,] 1.000000 0.000000 0
## [2,] 1.166667 1.000000 0
## [3,] 1.333333 1.294118 1
## [,1] [,2] [,3]
## [1,] 6 1 5
## [2,] 7 4 2
## [3,] 8 5 1