Homework 02

Problem Set 1
Week02 Problem Set -1
Part (1)

\[\mathbf{Let}\] \[\mathbf{A}=\left[\begin{matrix} a & b & c \\ d & e & f \\ g & h & i \end{matrix}\right]; \mathbf{A^T}=\left[\begin{matrix} a & d & g \\ b & e & h \\ c & f & i \end{matrix}\right] \]

\[\mathbf{A.A^T} = \left[\begin{matrix} a & b & c \\ d & e & f \\ g & h & i \end{matrix}\right]. \left[\begin{matrix} a & d & g \\ b & e & h \\ c & f & i \end{matrix}\right] = \left[\begin{matrix} a^2+b^2+c^2 & ad+be+cf & ag+bh+ci \\ ad+be+cf & d^2+e^2+f^2 & dg+eh+fi \\ ag+bh+ci & dg+eh+fi & g^2+h^2+i^2 \end{matrix}\right] \]

\[\mathbf{A^T.A} = \left[\begin{matrix} a & d & g \\ b & e & h \\ c & f & i \end{matrix}\right].\left[\begin{matrix} a & b & c \\ d & e & f \\ g & h & i \end{matrix}\right] = \left[\begin{matrix} a^2+d^2+g^2 & ab+de+gh & ac+df+gi \\ ab+de+gh & b^2+e^2+h^2 & bc+ef+hi \\ ac+df+gi & bc+ef+hi & c^2+f^2+i^2 \end{matrix}\right] \]

\[\mathbf{Therefore}\] \[\mathbf{A^T.A} \neq \mathbf{A.A^T}\]

\[\mathbf{A}=\left[\begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix}\right]; \mathbf{A^T}=\left[\begin{matrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{matrix}\right] \]

\[\mathbf{A.A^T} = \left[\begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix}\right]. \left[\begin{matrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{matrix}\right] = \left[\begin{matrix} 14 & 32 & 50 \\ 32 & 77 & 122 \\ 50 & 122 & 194 \end{matrix}\right] \]

\[\mathbf{A^T.A} = \left[\begin{matrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{matrix}\right].\left[\begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{matrix}\right] = \left[\begin{matrix} 66 & 78 & 90 \\ 78 & 93 & 108 \\ 90 & 108 & 126 \end{matrix}\right] \]

\[\mathbf{Therefore}\] \[\mathbf{A^T.A} \neq \mathbf{A.A^T}\]

A <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
print (A)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
print (t(A))
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
AAT =  A %*% t(A)
ATA =  t(A) %*% A
print (AAT)
##      [,1] [,2] [,3]
## [1,]   14   32   50
## [2,]   32   77  122
## [3,]   50  122  194
print (ATA)
##      [,1] [,2] [,3]
## [1,]   66   78   90
## [2,]   78   93  108
## [3,]   90  108  126
print (AAT==ATA)
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
Part (2)

\[\mathbf{A^T.A} = \mathbf{A.A^T} \] 1) If A is an identity matrix
\[\mathbf{A} = \mathbf{I}\]

\[\mathbf{A}=\left[\begin{matrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{matrix}\right]; \mathbf{A^T}=\left[\begin{matrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{matrix}\right] \]

\[\mathbf{then}\] \[\mathbf{A^T.A} = \mathbf{A.A^T} = \left[\begin{matrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{matrix}\right]\] 2) If A is a square matrix and every element in A is same

\[\mathbf{A}=\left[\begin{matrix} 2 & 2 & 2 \\ 2 & 2 & 2 \\ 2 & 2 & 2 \end{matrix}\right]; \mathbf{A^T}=\left[\begin{matrix} 2 & 2 & 2 \\ 2 & 2 & 2 \\ 2 & 2 & 2 \end{matrix}\right] \]

\[\mathbf{then}\] \[\mathbf{A^T.A} = \mathbf{A.A^T} = \left[\begin{matrix} 12 & 12 & 12 \\ 12 & 12 & 12 \\ 12 & 12 & 12 \end{matrix}\right]\]

A <- matrix(c(1, 0, 0, 0, 1, 0, 0, 0, 1), nrow = 3, ncol = 3, byrow = TRUE)
print (A)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
print (t(A))
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
AAT =  A %*% t(A)
ATA =  t(A) %*% A
print (AAT)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
print (ATA)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
print (AAT==ATA)
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
A <- matrix(c(2, 2, 2, 2, 2, 2, 2, 2, 2), nrow = 3, ncol = 3, byrow = TRUE)
print (A)
##      [,1] [,2] [,3]
## [1,]    2    2    2
## [2,]    2    2    2
## [3,]    2    2    2
print (t(A))
##      [,1] [,2] [,3]
## [1,]    2    2    2
## [2,]    2    2    2
## [3,]    2    2    2
AAT =  A %*% t(A)
ATA =  t(A) %*% A
print (AAT)
##      [,1] [,2] [,3]
## [1,]   12   12   12
## [2,]   12   12   12
## [3,]   12   12   12
print (ATA)
##      [,1] [,2] [,3]
## [1,]   12   12   12
## [2,]   12   12   12
## [3,]   12   12   12
print (AAT==ATA)
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
Problem Set 2
Week02 Problem Set -2
# Use a recursive function to factorize matrix
factorizeMatrix <- function(A, diagonalMatrix, index) {

    columns = ncol(A)
    rows    = nrow(A)
  
    if (index >= columns) {
      return(list(diagonalMatrix, A))    
    } else {
      for(i in (index+1):rows) {
        multiplier <- (-A[i, index] / A[index, index])
        diagonalMatrix[i, index] <- round(-multiplier, 2)
        A[i, ] <- A[i, ] + (multiplier * A[index, ])
      }
      print (A)
    }
    factorizeMatrix(A, diagonalMatrix, index + 1)
}

# Construct a diagonal matrix with the same number of rows as input matrix
A <- matrix(c(2, 4, -4, 1, -4, 3, -6, -9, 5), nrow=3, ncol=3, byrow = TRUE)
luMatrix <- factorizeMatrix(A, diag(nrow(A)), 1)
##      [,1] [,2] [,3]
## [1,]    2    4   -4
## [2,]    0   -6    5
## [3,]    0    3   -7
##      [,1] [,2] [,3]
## [1,]    2    4 -4.0
## [2,]    0   -6  5.0
## [3,]    0    0 -4.5
lowerMatrix <- luMatrix[[1]]
upperMatrix <- luMatrix[[2]]

# Print the input matrix
print (A)
##      [,1] [,2] [,3]
## [1,]    2    4   -4
## [2,]    1   -4    3
## [3,]   -6   -9    5
# Print the lower matrix
print (lowerMatrix)
##      [,1] [,2] [,3]
## [1,]  1.0  0.0    0
## [2,]  0.5  1.0    0
## [3,] -3.0 -0.5    1
# Print the upper matrix
print (upperMatrix)
##      [,1] [,2] [,3]
## [1,]    2    4 -4.0
## [2,]    0   -6  5.0
## [3,]    0    0 -4.5
# Check if A = L.U
print(A == lowerMatrix %*% upperMatrix)
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE