Problem set 1

Part (1)

Show That \(\begin{aligned} A^tA \neq AA^t \end{aligned}\) in general. (Proof and demonstration.)

Demonstration:

\[ \begin{aligned} \text{Lets Consider the Matrix A = } \begin{bmatrix} 1 & 0 & 3 & \\2 & -4 & 2\\1 & -3 & 2\end{bmatrix} \text{We know that } A^T = \begin{bmatrix} 1 & 2 & 1 & \\0 & -4 & -3\\3 & 2 & 2\end{bmatrix} \end{aligned} \]

A <- matrix(c(1,0,3,2,-4,2,1,-3,2), nrow=3, byrow=TRUE)
At <- t(A)
A
##      [,1] [,2] [,3]
## [1,]    1    0    3
## [2,]    2   -4    2
## [3,]    1   -3    2
At
##      [,1] [,2] [,3]
## [1,]    1    2    1
## [2,]    0   -4   -3
## [3,]    3    2    2
# Multiplying A by its transpose we get 3x3
A %*% At
##      [,1] [,2] [,3]
## [1,]   10    8    7
## [2,]    8   24   18
## [3,]    7   18   14
# Multiplying transpose At by A we get 3x3
At %*% A 
##      [,1] [,2] [,3]
## [1,]    6  -11    9
## [2,]  -11   25  -14
## [3,]    9  -14   17
\[\begin{aligned} \text{Then} A^TA = \begin{bmatrix} 10 & 8 & 7 \\8 & 24 & 18\\7 & 18 & 14\end{bmatrix} \text{and} AA^T = \begin{bmatrix} 6 & -11 & 9 \\-11 & 25 & -14\\9 & -14 & 17\end{bmatrix} \end{aligned}\]

From the above it is obvious that \(\begin{aligned} A^tA \neq AA^t \end{aligned}\)

Prof:

\[\begin{aligned} \text{Lets Consider the Matrix A with i rows and j columns where } \begin{aligned} i\neq j \end{aligned} = \begin{bmatrix} a_{1,1} & a_{1,2} &...... &a_{1,j} \\ a_{2,1} & a_{2,2} &...... & a_{2,j}\\ a_{3,1} & a_{3,2} &...... & a_{3,j}\\ . & . &...... & .\\ . & . &...... & .\\ a_{i,1} & a_{i,2} &...... & a_{i,j}\\ \end{bmatrix} \end{aligned}\] \[\begin{aligned} \text{Transpose of Matrix A with j rows and i columns where } \begin{aligned} j\neq i is A^T_{(ji)} \end{aligned} = \begin{bmatrix} a_{1,1} & a_{2,1} &...... &a_{i,1} \\ a_{1,2} & a_{2,2} &...... & a_{i,2}\\ a_{1,3} & a_{2,3} &...... & a_{i,3}\\ . & . &...... & .\\ . & . &...... & .\\ a_{1,j} & a_{2,j} &...... & a_{j,i}\\ \end{bmatrix} \end{aligned}\]

\[ \begin{aligned} A{(ij)}A^t{(ji)} \end{aligned}\text{will result the matrix} \begin{aligned} AA^T{(ij)} \end{aligned} \] \[ \begin{aligned} A^t{(ji)} A{(ij)} \end{aligned}\text{will result the matrix} \begin{aligned} A^TA{(ji)} \end{aligned} \] It is obvious that => \(\begin{aligned} A{(ij)} A^t{(ji)} \neq A^t{(ji)}A{(ij)} \end{aligned}\)

Example consider 4 x 5 matrix

A = matrix(c(1,3,2,1, 1,2,1,1, 1,1,0,1, 1,2,2,1, 3,4,1,3), nrow=4, ncol=5)
A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    1    1    1    3
## [2,]    3    2    1    2    4
## [3,]    2    1    0    2    1
## [4,]    1    1    1    1    3
#transpose of matrix A, generates 5 X 4
tA = t(A)
tA
##      [,1] [,2] [,3] [,4]
## [1,]    1    3    2    1
## [2,]    1    2    1    1
## [3,]    1    1    0    1
## [4,]    1    2    2    1
## [5,]    3    4    1    3
#Create 4 X 4 matrix
A%*%tA
##      [,1] [,2] [,3] [,4]
## [1,]   13   20    8   13
## [2,]   20   34   16   20
## [3,]    8   16   10    8
## [4,]   13   20    8   13
#Create 5 X 5 matrix
tA%*%A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   15   10    5   12   20
## [2,]   10    7    4    8   15
## [3,]    5    4    3    4   10
## [4,]   12    8    4   10   16
## [5,]   20   15   10   16   35

Outputs are different

Part (2)

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

When the matrix is symmetrical square matrix \(\begin{aligned} A^tA = AA^t \end{aligned}\)

Prof:

# Symmetrical 4 X 4 matrix 
A = matrix(c(9,1,3,6, 1,11,7,6, 3,7,4,1, 6,6,1,10), nrow=4, ncol=4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    9    1    3    6
## [2,]    1   11    7    6
## [3,]    3    7    4    1
## [4,]    6    6    1   10
#transpose of matrix A  is similar to A
tA = t(A)
tA
##      [,1] [,2] [,3] [,4]
## [1,]    9    1    3    6
## [2,]    1   11    7    6
## [3,]    3    7    4    1
## [4,]    6    6    1   10
#matrix multiplication
A%*%tA
##      [,1] [,2] [,3] [,4]
## [1,]  127   77   52  123
## [2,]   77  207  114  139
## [3,]   52  114   75   74
## [4,]  123  139   74  173
#matrix multiplication
tA%*%A
##      [,1] [,2] [,3] [,4]
## [1,]  127   77   52  123
## [2,]   77  207  114  139
## [3,]   52  114   75   74
## [4,]  123  139   74  173

It is obvious that for Symmetrical matrix => \(\begin{aligned} A{(ii)} A^t{(ii)} = A^t{(ii)}A{(ii)} \end{aligned}\)

When the matrix is identical \(\begin{aligned} I^tI = II^t \end{aligned}\)

Prof:

# Identical 4 X 4 matrix 
A = diag(4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
# Identical 4 X 4 matrix 
At = t(A)
At
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
#matrix multiplication
A%*%At
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
#matrix multiplication
At%*%A
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1

It is obvious that for Identical matrix => \(\begin{aligned} A{(ij)} A^t{(ji)} = A^t{(ji)}A{(ij)} \end{aligned}\)

Problem set 2

Part (1)

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 ights 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 Fight using radars.

Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer. The function factorize checks for the size of a square matrix, and then factorizes it into LU.

factorizeLU <- function(A) {
  # Check that A is square
  if (dim(A)[1]!=dim(A)[2]) {
    return(NA)
  }
  
  U <- A
  n <- dim(A)[1]
  L <- diag(n)
  
  # If dimension is 1, then U=A and L=[1]
  if (n==1) {
    return(list(L,U))
  }
  
  # Loop through the lower triangle (by rows and columns)
  # Determine multiplier for each position and add it to L
  for(i in 2:n) {
    for(j in 1:(i-1)) {
      multiplier <- -U[i,j] / U[j,j]
      U[i, ] <- multiplier * U[j, ] + U[i, ]
      L[i,j] <- -multiplier
    }
  }
  return(list(L,U))
}

Demonstration

Example 1

A <- matrix(c(1,4,-3,-2,8,5,3,4,7), nrow=3, byrow=TRUE)
LU <- factorizeLU(A)
L<-LU[[1]]  
U<-LU[[2]]

A
##      [,1] [,2] [,3]
## [1,]    1    4   -3
## [2,]   -2    8    5
## [3,]    3    4    7
L
##      [,1] [,2] [,3]
## [1,]    1  0.0    0
## [2,]   -2  1.0    0
## [3,]    3 -0.5    1
U
##      [,1] [,2] [,3]
## [1,]    1    4 -3.0
## [2,]    0   16 -1.0
## [3,]    0    0 15.5
L %*% U
##      [,1] [,2] [,3]
## [1,]    1    4   -3
## [2,]   -2    8    5
## [3,]    3    4    7
A == L %*% U
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
A <- matrix(c(2,1,6,8),nrow=2, byrow = T)

LU <- factorizeLU(A)
L<-LU[[1]]  
U<-LU[[2]]
A
##      [,1] [,2]
## [1,]    2    1
## [2,]    6    8
L
##      [,1] [,2]
## [1,]    1    0
## [2,]    3    1
U
##      [,1] [,2]
## [1,]    2    1
## [2,]    0    5
A == L %*% U
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE