ASSIGNMENT 2

Problem set 1 (1) Show that A^T (A) =/= (A)A^T in general. (Proof and demonstration.) (2)For a special type of square matrix A, we get A^T (A) = (A)A^T ; Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).

The proof can be found in the laws of matrix, AB =/= BA this same rule applies to part one of this problem set. The rules of matrix multiplication states that the order in which matrices are multiplied matters.

#create a matrix  A
A <- matrix(c(2, 3, 4, 5), nrow = 2)

#calculate A^T A; The transposition(t) is needed for the dimensions align 
A.T_A <- t(A) %*% A

#calculate A A^T; I used the decimal to remind myself its to the power of T
A_A.T <- A %*% t(A)

# Compare the results
if(all(A.T_A == A_A.T)) {
  cat("A^T A is equal to A A^T\n")
} else {
  cat("A^T A is not equal to A A^T\n")
}
## A^T A is not equal to A A^T

Problem set 1 (2)For a special type of square matrix A, we get A^T (A) = (A)A^T ; Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).

Only if the square matrix is symmetrical would it hold to be true, as per the hint, the identity matrix is symmetrical which is why this type of matrix allows A^T (A) to be equal to (A)A^T

#I will do a quick example to prove if it works & create a symmetric matrix B and update the variablesin case it messes up the code to leave the same variable A. 
B <- matrix(c(3, 2, 2, 5), nrow = 2)

B.T_B <- t(B) %*% B

B_B.T <- B %*% t(B)

# Compare the results; I'll leave the print out the same so it can easily answer the question above
if(all(B.T_B == B_B.T)) {
  cat("A^T A is equal to A A^T for a symmetric matrix\n")
} else {
  cat("A^T A is not equal to A A^T for a symmetric matrix\n")
}
## A^T A is equal to A A^T for a symmetric matrix

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

#Write an R function to factorize a square matrix A into LU (lower triangular matrix & upper)

lufact <- function(a) {
  n <- nrow(a)
  U <- matrix(0, ncol = n, nrow = n) 
  L <- diag(x = 1, ncol = n, nrow = n) # lower has to have 1's on diagonal
  
  
  for (i in 1:n) {
    i_p_1 <- i + 1  #create variables for i+1 and i-1
    i_m_1 <- i - 1
    for (j in 1:n) {
      U[i,j] <- a[i,j]  #loop and copy a to U
      # if i-1 more than zero
      if (i_m_1 > 0) {
        for (k in 1:i_m_1) {
          U[i,j] <- U[i,j] - L[i,k] * U[k,j]
        }
      }
    }
    if (i_p_1 <= n) {
      for (j in i_p_1:n) {  # loop through j i+1 to n 
        L[j,i] <- a[j,i]  # copy lower of a to L
        if (i_m_1 > 0) {
          for (k in 1:i_m_1) {
            L[j,i] <- L[j,i] - L[j,k] * U[k,i]
          }
        }
        L[j,i] <- L[j,i]/ U[i,i]
      }
    } 
  }
  result <- list(L=L, U=U)
  return(result)
}

# try on 3 by 3  matrix 
A <- matrix(c(3, 1, 2, 3, 2, 1, 3, 2, 1), nrow = 3)
LU <- lufact(A)
L <- LU$L
U <- LU$U

print("Matrix A:")
## [1] "Matrix A:"
print(A)
##      [,1] [,2] [,3]
## [1,]    3    3    3
## [2,]    1    2    2
## [3,]    2    1    1
print("Lower Triangular Matrix L:")
## [1] "Lower Triangular Matrix L:"
print(L)
##           [,1] [,2] [,3]
## [1,] 1.0000000    0    0
## [2,] 0.3333333    1    0
## [3,] 0.6666667   -1    1
print("Upper Triangular Matrix U:")
## [1] "Upper Triangular Matrix U:"
print(U)
##      [,1] [,2] [,3]
## [1,]    3    3    3
## [2,]    0    1    1
## [3,]    0    0    0

Ideally I’d want to check the results using matrixcalc package.

note to self - go back to latex - tidy typesetting (of mathematical formulas)

Inline math equations are put in between single dollar signs \((A \ci B)\). Math equation blocks are framed with double dollar signs \[\begin{equation}... \end{equation}\] (Medium article but the method didnt work - try again) or follow up with Larry.