(1) Show that \(A^{T}A \neq AA^{T}\) in general. (Proof and demonstration.)

For any given square matrix \(A\),

\[ A^TA = \begin{bmatrix} A_{11} & A_{12} & \cdot & \cdot & \cdot \\ A_{21} & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot &\cdot & \cdot & A_{n-1m} \\ \cdot & \cdot & \cdot & A_{nm-1} & A_{nm} \end{bmatrix}^T \times \begin{bmatrix} A_{11} & A_{12} \cdot & \cdot & \cdot\\ A_{21} & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & A_{n,m-1} \\ \cdot & \cdot & \cdot & A_{n-1,m} & A_{nm} \end{bmatrix}\]

\[ A^TA = \begin{bmatrix} A_{11} & A_{21} & \cdot & \cdot & \cdot \\ A_{12} & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot &\cdot & \cdot & A_{nm-1} \\ \cdot & \cdot & \cdot & A_{n-1m} & A_{nm} \end{bmatrix} \times \begin{bmatrix} A_{11} & A_{12} \cdot & \cdot & \cdot\\ A_{21} & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & A_{n-1m} \\ \cdot & \cdot & \cdot & A_{nm-1} & A_{nm} \end{bmatrix}\]

\[\hspace{12mm} = \begin{bmatrix} \sum_{i=1}^{n}A_{i,1}A_{i,1} & \sum_{i=1}^{n}A_{1,i}A_{2,2} & \cdot & \cdot & \cdot \\ \sum_{i=1}^{n}A_{i,2}A_{i,2} & \cdot & \cdot & \cdot & \cdot\\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \sum_{i=1}^{n}A_{i,m-1}A_{i,m-1} \\ \cdot & \cdot & \cdot & \sum_{i=1}^{n}A_{i,m}A_{i,m-1} & \sum_{i=1}^{n}A_{i,m}A_{i,m} \end{bmatrix}\]


\[ AA^T = \begin{bmatrix} A_{11} & A_{12} & \cdot & \cdot & \cdot \\ A_{21} & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot &\cdot & \cdot & A_{n-1m} \\ \cdot & \cdot & \cdot & A_{nm-1} & A_{nm} \end{bmatrix} \times \begin{bmatrix} A_{11} & A_{12} \cdot & \cdot & \cdot\\ A_{21} & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & A_{n-1m} \\ \cdot & \cdot & \cdot & A_{nm-1} & A_{nm} \end{bmatrix}^T\]

\[ AA^T = \begin{bmatrix} A_{11} & A_{12} & \cdot & \cdot & \cdot \\ A_{21} & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot &\cdot & \cdot & A_{n-1m} \\ \cdot & \cdot & \cdot & A_{nm-1} & A_{nm} \end{bmatrix} \times \begin{bmatrix} A_{11} & A_{21} \cdot & \cdot & \cdot\\ A_{12} & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & A_{nm-1} \\ \cdot & \cdot & \cdot & A_{n-1m} & A_{nm} \end{bmatrix}\]

\[\hspace{12mm} = \begin{bmatrix} \sum_{i=1}^{n}A_{1,i}A_{1,i} & \sum_{i=1}^{n}A_{1,i}A_{2,i} & \cdot & \cdot & \cdot \\ \sum_{i=1}^{n}A_{2,i}A_{2,i} & \cdot & \cdot & \cdot & \cdot\\ \cdot & \cdot & \cdot & \cdot & \cdot \\ \cdot & \cdot & \cdot & \cdot & \sum_{i=1}^{n}A_{n-1,i}A_{n-1,i} \\ \cdot & \cdot & \cdot & \sum_{i=1}^{n}A_{n,i}A_{n-1,i} & \sum_{i=1}^{n}A_{i,m}A_{i,m} \end{bmatrix}\]


Comparing the indicies of the two resultant matricies, the sum of the products would not be equal. Therefore, excluding special conditions, \(A^TA \neq AA^T\)

(2) For a special type of square matrix \(A\), we get \(A^TA = AA^T\). Under what conditions could this be true?

For all indices \(i\) and \(j\), if \(A_{i,j} = A_{j,i}\) then \(A^TA = AA^T\). The elements of the matrix would have the property of being symmetrical about the diagonal, i.e. a symmetric matrix.

Part II - Write an R function to factorize a square matrix \(A\) into \(LU\) or \(LDU\).

Using the LU factorization method:

LU_fact <- function(A, L, seed) {

    if (seed >= ncol(A)) {
        return(list(L,A))    
    }
    
    else {
        for(i in (seed+1):nrow(A)) {
            l <- -A[i,seed]/A[seed,seed]
            L[i,seed] <- -l
            A[i,] <- A[i,] + l*A[seed,]
            }
        }
    LU_fact(A, L, seed + 1)
}

#***---MAIN---***
A_base <- c(
    1, 1, 3,
    2, -1, 5,
    -1, -2, 4)

n <- sqrt(length(A_base))
A <- matrix(A_base, nrow = n, byrow = TRUE)

L_base <- diag(n)
LU <- LU_fact(A, L_base, 1)

L <- LU[[1]]
U <- LU[[2]]

# Test that A == LU:
all.equal(L%*%U, A)
## [1] TRUE
(L)
##      [,1]      [,2] [,3]
## [1,]    1 0.0000000    0
## [2,]    2 1.0000000    0
## [3,]   -1 0.3333333    1
(U)
##      [,1] [,2]      [,3]
## [1,]    1    1  3.000000
## [2,]    0   -3 -1.000000
## [3,]    0    0  7.333333