Problem Set 1

Question (1): Show that \(A \cdot A^\intercal \neq A^\intercal \cdot A\) in general.


Matrix multiplication is not usually commutative. The order of the matrices determines the size (and content) of the dot product.

\[\text{Let }A = \begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6 \end{bmatrix} \qquad A^\intercal = \begin{bmatrix} 1 & 4\\ 2 & 5\\ 3 & 6 \end{bmatrix}\]


\(B\) is a matrix with the dot product of \(A \cdot A^\intercal\).

\[\begin{align} &B_{1,1} = 1^2 + 2^2 + 3^2 = 14& \\ &B_{1,2} = (1 \times 4) + (2 \times 5) + (3 \times 6) = 32& \\ &B_{2,1} = (4 \times 1) + (5 \times 2) + (6 \times 3) = 32& \\ &B_{2,2} = 4^2 + 5^2 + 6^2 = 77& \end{align} \qquad B = \begin{bmatrix} 14 & 32\\ 32 & 77 \end{bmatrix} \]


\(C\) is a matrix with the dot product of \(A^\intercal \cdot A\).

\[\begin{align} &C_{1,1} = 1^2 + 4^2 = 17 & &C_{1,2} = (1 \times 2) + (4 \times 5) = 22 & &C_{1,3} = (1 \times 3) + (4 \times 6) = 27 &\\ &C_{2,1} = (2 \times 1) + (5 \times 4) = 22 & &C_{2,2} = 2^2 + 5^2 = 29 & &C_{2,3} = (2 \times 3) + (5 \times 6) = 36 &\\ &C_{3,1} = (3 \times 1) + (6 \times 4) = 27 & &C_{3,2} = (3 \times 2) + (6 \times 5) = 36 & &C_{3,3} = 3^2 + 6^2 = 45 & \end{align} \]
\[ C = \begin{bmatrix} 17 & 22 & 27\\ 22 & 29 & 36\\ 27 & 36 & 45 \end{bmatrix} \]

\(B \neq C\), therefore \(A \cdot A^\intercal \neq A^\intercal \cdot A\).




Question (2): For a special type of square matrix \(A\), we get \(A \cdot A^\intercal = A^\intercal \cdot A\). Under what conditions could this be true?

Matrix multiplication is commutative when the matrix is symmetric — when the entries mirror along the main diagonal. Transposing a symmetric square matrix \(A\) will return the same matrix, so \(A = A^\intercal\), and \(A \cdot A^\intercal = A^\intercal \cdot A\).




Problem Set 2

Write an R function to factorize a square matrix \(A\) into \(LU\).

# Test matrix
A <- matrix(c(1,2,3,1,1,1,2,0,1), nrow=3)

factorize <- function(A) {
  
  # Assign the dimensions of A to variables
  i <- nrow(A)
  j <- ncol(A)
  dim(A) <- c(i, j)
  
  # Check whether A is a square matrix
  if (is.square.matrix(A) == FALSE) {
    return("Use a square matrix")
  } else {
  
    # Initialize matrix L as an identity matrix
    L <- diag(1, dim(A))
    
    # Initialize matrix U as a copy of A
    U <- A
    
    # Loop through each row
    for (i in 2:i){
      
      # Loop through each column
      for (j in 1:(i-1)) {
        
        # Create elimination matrix E and populate it with the multiplier
        E <- diag(1, dim(A))
        E[i, j] <- -(U[i,j]/U[j,j])
        
        # Apply the elimination matrix to U
        U <- E %*% U
        
        # Update L to be the inverse of the multiplier
        L <- L %*% solve(E)
    
      }
    }
      print("----Upper Diagonal Matrix----")
      print(U)
      print("----Lower Diagonal Matrix----")
      print(L)
      print("----------TEST L*U=A----------")
      print(L%*%U == A)
  }
}

factorize(A)
## [1] "----Upper Diagonal Matrix----"
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    0   -1   -4
## [3,]    0    0    3
## [1] "----Lower Diagonal Matrix----"
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    2    1    0
## [3,]    3    2    1
## [1] "----------TEST L*U=A----------"
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
# Test accuracy of decomposition using function from "matrixcalc" package
lu.decomposition(A)
## $L
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    2    1    0
## [3,]    3    2    1
## 
## $U
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    0   -1   -4
## [3,]    0    0    3
# The Upper and Lower Diagonal Matrices match, so my function is accurate.