Grando 2 Homework

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

Answer:

First, it is easily shown that if the matrix is not square, then \({ A }^{ T }A\neq A{ A }^{ T }\) is generally not true because a when a matrix is transposed, it’s dimensions are changed.

\[\begin{bmatrix} { a }_{ 11 } & { a }_{ 12 } \\ { a }_{ 21 } & { a }_{ 22 } \\ { a }_{ 31 } & { a }_{ 32 } \end{bmatrix}*\begin{bmatrix} b_{ 11 } & { b }_{ 12 } & b_{ 13 } \\ b_{ 21 } & b_{ 22 } & b_{ 23 } \end{bmatrix}=\begin{bmatrix} c_{ 11 } & c_{ 12 } & c_{ 13 } \\ c_{ 21 } & c_{ 22 } & c_{ 23 } \\ c_{ 31 } & c_{ 32 } & c_{ 33 } \end{bmatrix}\]

Example:

A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3)
mtx1 <- A %*% t(A)
mtx2 <- t(A) %*% A
all.equal(mtx1, mtx2)
## [1] "Attributes: < Component \"dim\": Mean relative difference: 0.3333333 >"
## [2] "Numeric: lengths (9, 4) differ"

For square matrices, it is generally not true because the vector multiplication of \({ A }^{ T }A\) would have to be the same as that for \(A{ A }^{ T }\). Let’s take the first calculation [1,1] of the resulting matrix for example.

A <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3)
Arow1 = A[1, ]
ATransposeColumn1 = t(A)[, 1]
val1 = Arow1 %*% ATransposeColumn1

ATransposeRow1 = t(A)[1, ]
AColumn1 = A[, 1]
val2 = ATransposeRow1 %*% AColumn1

val1 == val2
##       [,1]
## [1,] FALSE

So, for this to be true, the vector multiplciation results of both matrices would have to be equal, which means that \({ A }^{ T }A=A{ A }^{ T }\) would only work for a matrix which does not change when it is transpose is taken. Note, the proof is in the next answer to the question.

1.2 For a special type of square matrix A, we get \({ A }^{ T }A=A{ A }^{ T }\). Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).

Answer:

As stated in the previous answer, for this statement to be true, \(A\) would have to be identical to \({ A }^{ T }\). A matrix with this property is called a symmetrical matrix.

A <- matrix(c(1, 2, 3, 2, 4, 2, 3, 2, 5), nrow = 3)
mtx1 <- A %*% t(A)
mtx2 <- t(A) %*% A
all.equal(mtx1, mtx2)
## [1] TRUE

Going back to question 1.1, we can show the following:

For a symmetric matrix:

\[A={A}^{T}\] We can rewrite the equation provided to us in the assignment:

\[{ A }^{ T }A=A{ A }^{ T }\]

By substituting \({A}^{T}\) for \(A\), which is:

\[AA=AA\]

Here, we can see that order does not matter only because we are multiplying identical matrices on both sides of the equation.

2.1 Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer. You don’t have to worry about permuting rows of A and you can assume that A is less than 5x5, if you need to hard-code any variables in your code.

Answer:

For this answer, we will use the matrix provided in the reading to test the function

lower_matrix <- function(A) {
    mtx <- A
    Imtx <- diag(dim(A)[1])
    mtxs <- list()
    i = 1
    fixrow = 0
    adj_mtx <- mtx
    for (col in 1:as.integer(dim(mtx)[2])) {
        for (row in 1:as.integer(dim(mtx)[1])) {
            if (row == col & mtx[row, col] == 0) {
                for (subrow in (row + 1):as.integer(dim(mtx)[1])) {
                  if (subrow > dim(mtx)[1]) {
                    stop("A more complex matrix solver is necessary")
                  }
                  if (mtx[subrow, col] != 0) {
                    tmprow = mtx[row, ]
                    mtx[row, ] = mtx[subrow, ]
                    mtx[subrow, ] = tmprow
                    fixrow = 1
                    adj_mtx <- mtx
                  }
                }
                if (fixrow == 0) {
                  stop("A more complex matrix solver is necessary")
                }
            }
            if (row > col) {
                Imtxtmp <- Imtx
                Imtxtmp[row, col] <- -1 * mtx[row, col]/mtx[col, col]
                mtxs[[i]] <- Imtxtmp
                mtx <- Imtxtmp %*% mtx
                i = i + 1
            }
        }
    }
    L <- solve(mtxs[[1]])
    for (j in 2:(i - 1)) {
        solve(mtxs[[j]])
        L <- L %*% solve(mtxs[[j]])
    }
    return(list(A = adj_mtx, U = mtx, L = L))
}

Solution example

## [1] "A Matrix:"
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    2    1    0
## [3,]    3    1    1
## [1] "U Matrix"
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    0   -1   -4
## [3,]    0    0    3
## [1] "L Matrix"
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    2    1    0
## [3,]    3    2    1
## [1] "Adjusted A Matrix"
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    2    1    0
## [3,]    3    1    1
## [1] "Result of LU, which should be equal to A_adj"
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    2    1    0
## [3,]    3    1    1
## [1] "Comparison of LU and A_adj"
## [1] TRUE

Example with zeros in the pivot points

## [1] "A Matrix:"
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    0    2    3    9    5
## [2,]    2    1    1    1    6
## [3,]    3    4    1    2    7
## [4,]    1    7    6    3    8
## [5,]    4    2    7    4    9
## [1] "U Matrix"
##      [,1] [,2] [,3]  [,4]      [,5]
## [1,]    4    2  7.0  4.00   9.00000
## [2,]    0    2  3.0  9.00   5.00000
## [3,]    0    0 -2.5 -1.00   1.50000
## [4,]    0    0  0.0 -9.05 -10.80000
## [5,]    0    0  0.0  0.00  16.09392
## [1] "L Matrix"
##      [,1] [,2] [,3]     [,4] [,5]
## [1,] 1.00 0.00  0.0 0.000000    0
## [2,] 0.00 1.00  0.0 0.000000    0
## [3,] 0.50 0.00  1.0 0.000000    0
## [4,] 0.75 1.25  3.2 1.000000    0
## [5,] 0.25 3.25  2.2 2.767956    1
## [1] "Adjusted A Matrix"
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    4    2    7    4    9
## [2,]    0    2    3    9    5
## [3,]    2    1    1    1    6
## [4,]    3    4    1    2    7
## [5,]    1    7    6    3    8
## [1] "Result of LU, which should be equal to A_adj"
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    4    2    7    4    9
## [2,]    0    2    3    9    5
## [3,]    2    1    1    1    6
## [4,]    3    4    1    2    7
## [5,]    1    7    6    3    8
## [1] "Comparison of LU and A_adj"
## [1] TRUE

Note, because in some cases the matrix rows will be reorganized by my function, I compare \(LU\) to the adjusted matrix \(A\_adj\) which only varies form \(A\) in row order.