Problem set 1

1. Show that (t(A))A does not equal A(t(A)) in general. (Proof and demonstration.)

A <- matrix(seq(from=1,to=6), nrow=2, byrow=T)

A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
t(A)
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6
t(A) %*% A
##      [,1] [,2] [,3]
## [1,]   17   22   27
## [2,]   22   29   36
## [3,]   27   36   45
A %*% t(A)
##      [,1] [,2]
## [1,]   14   32
## [2,]   32   77

Generally, the two examples above will not be equal because a 3X2 matrix multiplied by a 2X3 matrix will produce a 3X3 matrix, while a 2X3 matrix multiplied by a 3X2 matrix will produce a 2X2 matrix. An instance where this general rule would not apply is a square matrix, as the numbers of rows and columns would be the same in the result, regardless of the order of transposition.

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

I <- matrix(c(1,0,0,0,1,0,0,0,1), nrow = 3)
I
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
t(I)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
I %*% t(I)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
t(I) %*% I
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

Transposition takes the each row and changes is it into the corresponding column. In the case of the identity matrix the first 1 at position I1,1 remains the same upon transposition, just as the ones at I2,2 and I3,3 and, as a result, multiplying the matrices results in the same matrix regardless of the order of transposition.

S <- matrix(c(2,2,2,2,2,2,2,2,2), nrow = 3)
S
##      [,1] [,2] [,3]
## [1,]    2    2    2
## [2,]    2    2    2
## [3,]    2    2    2
t(S)
##      [,1] [,2] [,3]
## [1,]    2    2    2
## [2,]    2    2    2
## [3,]    2    2    2
S %*% t(S)
##      [,1] [,2] [,3]
## [1,]   12   12   12
## [2,]   12   12   12
## [3,]   12   12   12
t(S) %*% S
##      [,1] [,2] [,3]
## [1,]   12   12   12
## [2,]   12   12   12
## [3,]   12   12   12

Another case where this is true is if every single number in the matrix were the same, because transposition does not have any impact on which numbers are where.

Problem set 2

Matrix factorization is a very important problem. There are supercomputers built just to do matrix factorizations. Every second you are on an airplane, matrices are being factorized. Radars that track flights use a technique called Kalman filtering. At the heart of Kalman Filtering is a Matrix Factorization operation. Kalman Filters are solving linear systems of equations when they track your flight using radars. Write an R Function to factorize a square matrix A into LU or LDU, whichever you prefer. Please submit your response in an R Markdown document using our class naming convention. 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

I had trouble doing this without the help of the matrix package. I may try more, but thought I should get the assignment in.

randSM <- function(range){
  #random square matrix with 3 to 5 rows/columns
  x = sample(3:5, 1)
  m = matrix(sample.int(range,x**2,replace=T),nrow = x, ncol = x)
  return (m)
}



A = randSM(10)
A
##      [,1] [,2] [,3] [,4]
## [1,]    2    8    1    2
## [2,]   10   10    1   10
## [3,]    1    2    8    2
## [4,]    5    9    3    2
require(Matrix)
## Loading required package: Matrix
decom <- function(A){
  L = expand(lu(A))$L
  U = expand(lu(A))$U
  P = expand(lu(A))$P 
  
  TEST = P%*% L %*% U
  
  
  print("The initial matrix is")
  print (A)
  print("The lower triangular matrix is")
  print(L)
  print("The upper triangular matrix is")
  print (U)
  print("The permutation calculation is")
  print (P)
  
  print(TEST)
  
  print("Is P *%* L %*% U equal to A?")
  print(TEST==A)
  
}

decom(A)
## [1] "The initial matrix is"
##      [,1] [,2] [,3] [,4]
## [1,]    2    8    1    2
## [2,]   10   10    1   10
## [3,]    1    2    8    2
## [4,]    5    9    3    2
## [1] "The lower triangular matrix is"
## 4 x 4 Matrix of class "dtrMatrix" (unitriangular)
##      [,1]      [,2]      [,3]      [,4]     
## [1,] 1.0000000         .         .         .
## [2,] 0.2000000 1.0000000         .         .
## [3,] 0.1000000 0.1666667 1.0000000         .
## [4,] 0.5000000 0.6666667 0.2532189 1.0000000
## [1] "The upper triangular matrix is"
## 4 x 4 Matrix of class "dtrMatrix"
##      [,1]      [,2]      [,3]      [,4]     
## [1,] 10.000000 10.000000  1.000000 10.000000
## [2,]         .  6.000000  0.800000  0.000000
## [3,]         .         .  7.766667  1.000000
## [4,]         .         .         . -3.253219
## [1] "The permutation calculation is"
## 4 x 4 sparse Matrix of class "pMatrix"
##             
## [1,] . | . .
## [2,] | . . .
## [3,] . . | .
## [4,] . . . |
## 4 x 4 Matrix of class "dgeMatrix"
##      [,1] [,2] [,3] [,4]
## [1,]    2    8    1    2
## [2,]   10   10    1   10
## [3,]    1    2    8    2
## [4,]    5    9    3    2
## [1] "Is P *%* L %*% U equal to A?"
## 4 x 4 Matrix of class "lgeMatrix"
##      [,1] [,2] [,3] [,4]
## [1,] TRUE TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE TRUE