R Markdown

  1. Problem set 1
  1. Show that AT A # AAT in general. (Proof and demonstration.)
A <- matrix(c(1, 4, 7, 2, 5, 8, 3, 6, 9), nrow=3, ncol=3) 
AT <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow=3, ncol=3)

\[\mathbf{A} = \left[\begin{array} {rrr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{array}\right] \]

\[\mathbf{AT} = \left[\begin{array} {rrr} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{array}\right] \]

The below clearly shows that A*AT # AT.A

##      [,1] [,2] [,3]
## [1,]   14   32   50
## [2,]   32   77  122
## [3,]   50  122  194
##      [,1] [,2] [,3]
## [1,]   66   78   90
## [2,]   78   93  108
## [3,]   90  108  126

Problem 1 Part 2

  1. For a special type of square matrix A, we get AT A = AAT . Under what conditions could this be true?

The Matrix multipication of a matrix and an identity matrix is always equal to the matrix multiplication of the identity matrix into the matrix

Since the transpose of an identity matrix is same as the matrix. matrix multipication is the same

A <- matrix(c(1, 0, 0, 0, 1, 0, 0, 0, 1), nrow=3, ncol=3) 
AT <- matrix(c(1, 0, 0, 0, 1, 0, 0, 0, 1), nrow=3, ncol=3) 
A%*%AT
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
AT%*%A
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Problem 2

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

LUDecompose<- function(A)
{
  r <- dim(A)[1]
  c <- dim(A)[2]
  U <- A
  
  #Create Upper Traingular Matrix
  (L <- A)
  upper.tri(L)
  L[upper.tri(L)] <- 0
  diag(L) <- 1

  if(r==c)
  {
    for(i in 2:c)
    {
      for(j in 1:(i-1))
      {
        
        for(k in j:c)
        {
          if(j==(k-1))
          {
            multiplier =  (U[i,j] / U[j,j]) * (-1)
            multiplierrow = j
          }
          
        }
        for(k in j:c)
        {
          U[i,k] = U[i,k] + (U[multiplierrow,k] * multiplier)  

        }
        L[i,j] = multiplier * -1
      }
    }
    retList <- list(L,U)
  }
  else
    retList <- List("Invalid Square Matrix passed.")
  
  return(retList)
}
A <- matrix(c(1, 4, 7, 2, 5, 8, 3, 6, 9), nrow=3, ncol=3) 
#A <- matrix(c(1, -2, 3, 4, 8, 4, -3, 5, 7), nrow=3, ncol=3) 
LUList = LUDecompose(A)
LUList[1]
## [[1]]
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    4    1    0
## [3,]    7    2    1
LUList[2]
## [[1]]
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    0   -3   -6
## [3,]    0    0    0