Problem Set 1

  1. Show that \(A^TA \neq AA^T\) in general. (Proof and demonstration).
If we have a 2 x 3 matrix A: \[\begin{bmatrix} -1 & -3 & 0\\ 2 & 2 & 3 \end{bmatrix}\] And a 3 x 2 matrix \(A^T\) \[\begin{bmatrix} -1 & 2\\ -3 & 2\\ 0 & 3 \end{bmatrix}\]

\(A^TA\) will result in a 3 x 3 matrix and \(AA^T\) will result in a 2 x 2 matrix based on the resultant dimensions of matrix multiplication are the size of the rows

We can prove this by using the left multiplication function %*% in R which will return two different resuktant matrices.

A

Transpose A

trans_A <- t(A)

\(A^TA\)

t(A) %*% A
##      [,1] [,2] [,3]
## [1,]    5    7    6
## [2,]    7   13    6
## [3,]    6    6    9

\(AA^T\)

A %*% t(A)
##      [,1] [,2]
## [1,]   10   -8
## [2,]   -8   17
  1. For a special type of square matrix \(A\), we get \(A^TA\) = \(AA^T\) . Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).

Another special square matrix is a symmetrical matrix which is equal to it’s transpose. Since it is equal to is transpose which ever matrix we use first, either will result in the same resultant matrix. We can see this below

# Square matrix - symmetric matrix
C <- matrix(c(1,1,-1,1,2,0,-1,0,5), nrow = 3, ncol = 3, byrow = T)
C
##      [,1] [,2] [,3]
## [1,]    1    1   -1
## [2,]    1    2    0
## [3,]   -1    0    5
t(C)
##      [,1] [,2] [,3]
## [1,]    1    1   -1
## [2,]    1    2    0
## [3,]   -1    0    5
C %*% t(C) == t(C) %*% C
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

Problem Set 2

  1. Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer.
mat_factorize <- function(mat){
E21 <- matrix(c(1,(-1 * D[2,1]),0,0,1,0,0,0,1),nrow=3)
E21
U <- E21 %*% D
U

E31 <- matrix(c(1,0,(-1 * U[3,1]),0,1,0,0,0,1),nrow=3)
E31
U <- E31 %*% E21 %*% D
U

E32 <- matrix(c(1,0,0,0,1,(-1 * U[3,2]),0,0,1),nrow=3)
E32
E32 %*% E31 %*% E21 %*% D
U <- E32 %*% E31 %*% E21 %*% D
U

L <- solve(E21) %*% solve(E31) %*% solve(E32)
L

L %*% U == D
}

Create a 3x3 matrix and pass it into the function

D <- matrix(c(1,-2,1,4,-5,1,0,3,1), nrow = 3, ncol = 3)

mat_factorize(D)
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

Generating a random 3 x 3 matrix and passing it into the function

E <- matrix(runif(9), nrow = 3)
E
##             [,1]      [,2]      [,3]
## [1,] 0.006100453 0.8498592 0.9598798
## [2,] 0.488038664 0.3246678 0.6909036
## [3,] 0.951750305 0.4987963 0.5649034
mat_factorize(E)
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE