1. Problem set 1

\[ (1)\:Show \: that \:A^TA \neq AA^T \:in\: general. (Proof\: and\: demonstration.) \]

\[ Suppose\: A\: is\: an\: m×n\: matrix, where\: m, n \geqslant 1\: and\: m \neq n \]

\[ A = \begin{bmatrix} a_11 & .. & .. & a_1n \\ a_21 & .. & .. & a_2n \\ .. & .. & .. & .. \\ a_m1 & .. & .. & a_mn \end{bmatrix} \]

The transpose of A is an n x m matrix \[ A^T = \begin{bmatrix} a_11 & a_21 & .. & a_m1 \\ .. & .. & .. & .. \\ .. & .. & .. & .. \\ a_1n & a_2n & .. & a_mn \end{bmatrix} \]

\[ AA^T is\: an\: m×m\: matrix \] \[ A^TA is\: an\: n×n\: matrix \]

We can see that these are two matrices of different sizes, and we can conclude that:

\[ \therefore A^TA \neq AA^T \]

\[ Suppose\: A\: is\: an\: m×n\: matrix, where\: m, n \geqslant 1\: and\: m = n \]

\[ A = \begin{bmatrix} a_11 & .. & .. & a_1n \\ a_21 & .. & .. & a_2n \\ .. & .. & .. & .. \\ a_m1 & .. & .. & a_mn \end{bmatrix} \]

The transpose of A is an n x m matrix \[ A^T = \begin{bmatrix} a_11 & a_21 & .. & a_m1 \\ .. & .. & .. & .. \\ .. & .. & .. & .. \\ a_1n & a_2n & .. & a_mn \end{bmatrix} \]

We can change the elements’ name of transpose A and rename it as matrix B (n x p matrix)

\[ B = \begin{bmatrix} b_11 & .. & .. & b_1p \\ b_21 & .. & .. & b_2p \\ .. & .. & .. & .. \\ b_n1 & .. & .. & b_np \end{bmatrix} \]

By using theorem EMP (Entries of Matrix Products), the jth column of AB and BA is:

\[ [AB]_{ij} = \displaystyle\sum\limits_{k=1}^n a_{ik}bk_{j}\] \[ [BA]_{ij} = \displaystyle\sum\limits_{k=1}^n b_{ik}ak_{j}\]

This shows the they are not equal and we can conclude that: (substitute B with A transpose) \[ [AA^T]_{ij} = \displaystyle\sum\limits_{k=1}^n a_{ik}bk_{j}\] \[ [A^TA]_{ij} = \displaystyle\sum\limits_{k=1}^n b_{ik}ak_{j}\]

\[ \therefore A^TA \neq AA^T \]

# Example 
set.seed(2)
A <- matrix(sample(48,16, replace=F), nrow=4, byrow=T)
A
##      [,1] [,2] [,3] [,4]
## [1,]   21   15    6   46
## [2,]   32    8   17   29
## [3,]   42   12   11    1
## [4,]    3   16   44   43
AT <- t(A)
AT
##      [,1] [,2] [,3] [,4]
## [1,]   21   32   42    3
## [2,]   15    8   12   16
## [3,]    6   17   11   44
## [4,]   46   29    1   43
ATA <- AT %*% A
ATA
##      [,1] [,2] [,3] [,4]
## [1,] 3238 1123 1264 2065
## [2,] 1123  689 1062 1622
## [3,] 1264 1062 2382 2672
## [4,] 2065 1622 2672 4807
AAT <- A %*% AT
AAT
##      [,1] [,2] [,3] [,4]
## [1,] 2818 2228 1174 2545
## [2,] 2228 2218 1656 2219
## [3,] 1174 1656 2030  845
## [4,] 2545 2219  845 4050
ATA == AAT
##       [,1]  [,2]  [,3]  [,4]
## [1,] FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE

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

Any symmetric matrix will satisfies this condition.Identity matrix is always symmetric

Matrix A is symmetric if \[ A = A^T \] therefore \[ A^TA = AA\: and\: AA^T = AA\] \[ A^T A = AA^T\]

  1. 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, E.g. LFulton_Assignment2_PS2.png 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. If you doing the entire assignment in R, then please submit only one markdown document for both the problems

library(matrixcalc)

lu_factorization <- function(A) {
    lu_result <- lu.decomposition(A)
    L <- lu_result$L
    U <- lu_result$U
    return(list(L = L, U = U))
}

# Example

M2 <- matrix(c(1,2,3,1,1,1,2,0,1), nrow=3)
M2
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    2    1    0
## [3,]    3    1    1
LU_matrix <- lu_factorization(M2)

print("Lower triangular matrix:")
## [1] "Lower triangular matrix:"
print(LU_matrix$L)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    2    1    0
## [3,]    3    2    1
print("Upper triangular  matrix:")
## [1] "Upper triangular  matrix:"
print(LU_matrix$U)
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    0   -1   -4
## [3,]    0    0    3
# LU Decomposition using Gaussian elimination

decomposeU <- function(A2) {
n <- nrow(A2)
L2 <- diag(n)

for (k in 1:(n - 1)) {
  for (i in (k + 1):n) {
    factor <- A2[i, k] / A2[k, k]
    A2[i, (k + 1):n] <- A2[i, (k + 1):n] - factor * A2[k, (k + 1):n]
    L2[i, k] <- factor
  }
}

U2 <- A2
U2[lower.tri(U2)] <- 0
cat("Upper triangular Matrix:\n")
print(U2)
cat("\nLower triangular Matrix:\n")
print(L2)
}

A <- matrix(c(1,2,3,1,1,1,2,0,1), nrow=3)
decomposeU(A)
## Upper triangular Matrix:
##      [,1] [,2] [,3]
## [1,]    1    1    2
## [2,]    0   -1   -4
## [3,]    0    0    3
## 
## Lower triangular Matrix:
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    2    1    0
## [3,]    3    2    1