Show that (Transpose of A)A NOT Equal To A (Transpose of A) in general

A <- matrix(c(2,1,3,6,0,-1,3,1,-2), nrow=3, byrow=TRUE)
At <- t(A)
At
##      [,1] [,2] [,3]
## [1,]    2    6    3
## [2,]    1    0    1
## [3,]    3   -1   -2
A
##      [,1] [,2] [,3]
## [1,]    2    1    3
## [2,]    6    0   -1
## [3,]    3    1   -2
At
##      [,1] [,2] [,3]
## [1,]    2    6    3
## [2,]    1    0    1
## [3,]    3   -1   -2

Multiplying A by its transpose At

A %*% At
##      [,1] [,2] [,3]
## [1,]   14    9    1
## [2,]    9   37   20
## [3,]    1   20   14

Multiplying transposed A by A

At %*% A
##      [,1] [,2] [,3]
## [1,]   49    5   -6
## [2,]    5    2    1
## [3,]   -6    1   14

Logical comparison

A %*% At == At %*% A
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE  TRUE

(2) For a special type of square matrix A, we get (Transpose of A)A Equal To A (Transpose of A)

Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).Please typeset your response using LaTeX mode in RStudio.

Answer: For a matrix which is symmetrical along the diagonal, this is true because a symmetric matrix and its transpose are equal, means you are just multiplying a matrix by itself.

Demonstration

A1 <- matrix(c(1,6,0,6,1,0,0,0,1), nrow=3, byrow=TRUE)
A1
##      [,1] [,2] [,3]
## [1,]    1    6    0
## [2,]    6    1    0
## [3,]    0    0    1

transpose of the matrix A1 is A1 itself

t(A1)
##      [,1] [,2] [,3]
## [1,]    1    6    0
## [2,]    6    1    0
## [3,]    0    0    1
A == t(A)
##       [,1]  [,2]  [,3]
## [1,]  TRUE FALSE  TRUE
## [2,] FALSE  TRUE FALSE
## [3,]  TRUE FALSE  TRUE
(A %*% t(A)) == (t(A) %*% A)
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE  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

factorize_LU <- function(A) {
  # Check wheter matrix A is square
  if (dim(A)[1]!=dim(A)[2]) {
    return(NA)
  }
  
  U <- A
  n <- dim(A)[1]
  L <- diag(n)
  
  
  if (n==1) {
    return(list(L,U))
  }
  
  
  for(i in 2:n) {
    for(j in 1:(i-1)) {
      multiplier <- -U[i,j] / U[j,j]
      U[i, ] <- multiplier * U[j, ] + U[i, ]
      L[i,j] <- -multiplier
    }
  }
  return(list(L,U))
}

Example

D <- matrix(c(3,1,1,1,1,3,7,2,1), nrow=3, byrow=TRUE)
LU <- factorize_LU(D)
L<-LU[[1]]  
U<-LU[[2]]
D
##      [,1] [,2] [,3]
## [1,]    3    1    1
## [2,]    1    1    3
## [3,]    7    2    1
L
##           [,1] [,2] [,3]
## [1,] 1.0000000  0.0    0
## [2,] 0.3333333  1.0    0
## [3,] 2.3333333 -0.5    1
U
##      [,1]          [,2]     [,3]
## [1,]    3  1.000000e+00 1.000000
## [2,]    0  6.666667e-01 2.666667
## [3,]    0 -5.551115e-17 0.000000
D == L %*% U
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE