1. Problem set 1

  1. Show that ATA != AAT in general. (Proof and demonstration.)
(A <- matrix(c(3,2,1,4,3,1,-4,2,-5), nrow=3, byrow=TRUE))
##      [,1] [,2] [,3]
## [1,]    3    2    1
## [2,]    4    3    1
## [3,]   -4    2   -5
#A Transpose
(At <- t(A))
##      [,1] [,2] [,3]
## [1,]    3    4   -4
## [2,]    2    3    2
## [3,]    1    1   -5
A%*%At
##      [,1] [,2] [,3]
## [1,]   14   19  -13
## [2,]   19   26  -15
## [3,]  -13  -15   45
At%*%A
##      [,1] [,2] [,3]
## [1,]   41   10   27
## [2,]   10   17   -5
## [3,]   27   -5   27

To check for the given condition using logical evaluation

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

This shows that AAt != AtA

(2) For a special type of square matrix A, we get ATA = AAT . Under what conditions

could this be true? (Hint: The Identity matrix I is an example of such a matrix).

Answer

We get ATA = AAT for a square matrix A if A is symmetrical along the diagonal. This implies that multiplying a matrix A by its transpose AT is like multiplying the matrix by itself.

for instance:

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

AT will then be

(At <- t(A))
##      [,1] [,2] [,3]
## [1,]    1    2    0
## [2,]    2    1    0
## [3,]    0    0    1

Checking the statement above:

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

2. 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 fights 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 fight using radars. Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer

Solution:

Choosing the LU version =>

luDecomposer <- function(A) 
{
  # if A is not a square matrix, return null
  if (dim(A)[1]!=dim(A)[2]) 
  {
    return(NULL)
  }
  
  M <- A
  dimOfA <- dim(A)[1]
  diagA <- diag(dimOfA)
  
  if (dimOfA == 1) 
  {
    return(list(diagA, M))
  }
  
  #Using a nested for-loop
  for(a in 2:dimOfA)
  {
    for(b in 1:(a-1)) 
    {
      mx <- (-M[a,b] / M[b,b]) #devicing a multiplier
      M[a, ] <- mx * M[b, ] + M[a, ]
      diagA[a,b] <- -mx
    }
  }
  return(list(diagA,M))
}

Verifications

(A <- matrix(c(4,1,1,1,1,2,5,4,3), nrow=3, byrow=TRUE))
##      [,1] [,2] [,3]
## [1,]    4    1    1
## [2,]    1    1    2
## [3,]    5    4    3
luD <- luDecomposer(A)
(l<-luD[[1]])
##      [,1]     [,2] [,3]
## [1,] 1.00 0.000000    0
## [2,] 0.25 1.000000    0
## [3,] 1.25 3.666667    1
(u<-luD[[2]])
##      [,1] [,2]      [,3]
## [1,]    4 1.00  1.000000
## [2,]    0 0.75  1.750000
## [3,]    0 0.00 -4.666667

How did it fare?

A == l %*% u
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE