### Elina Azrilyan

September 5th, 2019

#### Problem set 1

1. Show that A x t(A) does not equal t(A) x A in general. (Proof and demonstration.)
##### Proof:

One of the basic laws of matrixes is that AB != BA, matrix multiplication is not commutative — order matters. This is generally true with some exceptions (see question 2 in this problem set).

##### Demonstration for nxn matrix:
A <- matrix(c(2, 2, 1, 4, 2, 3, 1, 0, 1),nrow=3)
B <- t(A)

A %*% B == B %*% A
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
##### Demonstration for matrixes other than nxn - we can’t even calculate a product for those.
A <- matrix(c(2, 2, 1, 4, 2, 3, 1, 0, 1, 2, 3, 1),nrow=3)
B <- t(A)

#We get an error since the dimansions are incompatible

#A %*% B == B %*% A

#Error in B * A : non-conformable arrays
#Error in A * B : non-conformable arrays
1. For a special type of square matrix A, we get AT A = AAT . Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).

t(A) x A and A x t(A) are equal when the matrix is symetric. A symmetric matrix is a square matrix that is equal to its transpose.

##### Example:
A <- matrix(c(2, 1, 1, 1, 2, 1, 1, 1, 2),nrow=3)
B <- t(A)

A
##      [,1] [,2] [,3]
## [1,]    2    1    1
## [2,]    1    2    1
## [3,]    1    1    2
B
##      [,1] [,2] [,3]
## [1,]    2    1    1
## [2,]    1    2    1
## [3,]    1    1    2
(A %*% B) == (B %*% A)
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

#### Problem set 2

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

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.

##### The code below creates the function:
factorization <- function(A) {
#Let's assign our matrix to 'upper' so that we can calculate U from that

upper <- A
#Let's create an identity matrix of the same dimension and call it 'lower' so that we can use that as a starting point for L calculation

lower <- diag(nrow = dim(A))

#The following loops move through the matrix and calculate the Upper and Lower Triangular Matrixes:

for (i in 2:dim(A)){ #
for (j in 1:(i-1)){
lower[i,j] <- upper[i,j] / upper[j,j]
upper[i, ] <- -lower[i,j] * upper[j, ] + upper[i, ]
}
}

print("Upper Triangular Martix:")
print(upper)
print("Lower Triangular Martix:")
print(lower)
print("Original Matrix A:")
print(A)
print("LU:")
print(lower%*%upper)
print("Comparing A and LU")
print(A==lower%*%upper)

#If we want to return the U and L rather than print - we would use the following code:
#return(list(lower,upper))
} 
##### We can test out the function with some examples:
A <- matrix(c(2, 4, -4, 1, -4, 3, -6, -9, 5), nrow=3, ncol=3, byrow = TRUE)
factorization(A)
##  "Upper Triangular Martix:"
##      [,1] [,2] [,3]
## [1,]    2    4 -4.0
## [2,]    0   -6  5.0
## [3,]    0    0 -4.5
##  "Lower Triangular Martix:"
##      [,1] [,2] [,3]
## [1,]  1.0  0.0    0
## [2,]  0.5  1.0    0
## [3,] -3.0 -0.5    1
##  "Original Matrix A:"
##      [,1] [,2] [,3]
## [1,]    2    4   -4
## [2,]    1   -4    3
## [3,]   -6   -9    5
##  "LU:"
##      [,1] [,2] [,3]
## [1,]    2    4   -4
## [2,]    1   -4    3
## [3,]   -6   -9    5
##  "Comparing A and LU"
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
B <- matrix(c(-1, 3, 4, 2, 4, 2, -9, 3, -1), nrow=3, ncol=3, byrow = TRUE)
factorization(B)
##  "Upper Triangular Martix:"
##      [,1] [,2] [,3]
## [1,]   -1    3    4
## [2,]    0   10   10
## [3,]    0    0  -13
##  "Lower Triangular Martix:"
##      [,1] [,2] [,3]
## [1,]    1  0.0    0
## [2,]   -2  1.0    0
## [3,]    9 -2.4    1
##  "Original Matrix A:"
##      [,1] [,2] [,3]
## [1,]   -1    3    4
## [2,]    2    4    2
## [3,]   -9    3   -1
##  "LU:"
##      [,1] [,2] [,3]
## [1,]   -1    3    4
## [2,]    2    4    2
## [3,]   -9    3   -1
##  "Comparing A and LU"
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE