Problem Set 1

1) Show that A^T A != AA^T in general. (Proof and demonstration.)

#create 3 x 3 matrix with any numbers
A = matrix(c(1:9),nrow = 3)
A
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
#transpose the matrix A
trans_A = t(A)
trans_A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Check if A^T A != AA^T

#multiply A by transpose A
AT_A = A %*% trans_A
AT_A
##      [,1] [,2] [,3]
## [1,]   66   78   90
## [2,]   78   93  108
## [3,]   90  108  126
#multiply transpose A by A
A_AT = trans_A %*% A
A_AT
##      [,1] [,2] [,3]
## [1,]   14   32   50
## [2,]   32   77  122
## [3,]   50  122  194
AT_A == A_AT
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE

As shown above, A multiplied by transpose A is not equal to transpose A multiplied by A.

2) For a special type of square matrix A, we get AT A = AAT. Under what conditions could this be true? (Hint: The Identity matrix I is an example of such a matrix).

#create matrix A2 and AT2 that are equivalent
A2 = matrix(c(1:3),nrow = 3, ncol = 3)
AT2 = matrix(c(1:3),nrow = 3, ncol = 3)

#multiply A2 by transpose A2
A2_AT2 = A2 %*% AT2
A2_AT2
##      [,1] [,2] [,3]
## [1,]    6    6    6
## [2,]   12   12   12
## [3,]   18   18   18
#multiply transpose A2 by A2
AT2_A2 = AT2 %*% A2
AT2_A2
##      [,1] [,2] [,3]
## [1,]    6    6    6
## [2,]   12   12   12
## [3,]   18   18   18
AT2_A2 == A2_AT2
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

As shown above, the conditions can be true if the matrix and the transpose of the matrix are equivalent.

Problem Set 2

LU = function(A){

#Upper triangular
  U = A 
  
#Matrix dimensions
  n = dim(A)[1]
  
#Lower triangular
  L = diag(n)
  
  if(n == 1){
  return(list(L,U))
  }
  
  for(x in 2:n){
    for(y in 1:(x-1)){
      multiply = -U[x, y] / U[y, y]
      U[x,] = multiply * U[y,] + U[x,]
      L[x, y] = -multiply
    }
  }
  return(list(L, U))
}
#Create a random 3 x 3 matrix
A = matrix(c(3:11), nrow = 3, ncol = 3)
A
##      [,1] [,2] [,3]
## [1,]    3    6    9
## [2,]    4    7   10
## [3,]    5    8   11
LUA = LU(A)

#multiply upper matrix
upper_multi = LUA[[2]]
upper_multi
##      [,1] [,2] [,3]
## [1,]    3    6    9
## [2,]    0   -1   -2
## [3,]    0    0    0
#multiply lower matrix
lower_multi = LUA[[1]]
lower_multi
##          [,1] [,2] [,3]
## [1,] 1.000000    0    0
## [2,] 1.333333    1    0
## [3,] 1.666667    2    1
#proof matrix A is equal to lower matrix multiplied by upper matrix
A == lower_multi %*% upper_multi
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE