1. Problem set 1

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

\[ To\ Proof And\ Demonstrate\ That: \ A^{T}A \neq AA^{T}\]

\[A = \begin{pmatrix} a & b & c\\ d & e & f \end{pmatrix} \]

\[A^{T} = \begin{pmatrix} a & d\\ b & e \\ c & f \end{pmatrix} \]

\[AA^{T} = \begin{pmatrix} a^2 + b^2 + c^2 & ad+be+cf\\ ad+be+cf & d^2+e^2+f^2 \end{pmatrix} \]

\[A^{T}A = \begin{pmatrix} a^2 + d^2 & ab+de & ac+df\\ ab+de & b^2+e^2 & bc+ef\\ ac+df & bc+ef & c^2+f^2 \end{pmatrix} \]

\[ \begin{pmatrix} a^2 + d^2 & ab+de & ac+df\\ ab+de & b^2+e^2 & bc+ef\\ ac+df & bc+ef & c^2+f^2 \end{pmatrix} \neq \begin{pmatrix} a^2 + b^2 + c^2 & ad+be+cf\\ ad+be+cf & d^2+e^2+f^2 \end{pmatrix} \]

\[Generally, A^{T}A \neq AA^{T} as\ one\ product\ is\ a\ 2x2\ matrix\, while\ the\ other\ is\ a\ 3x3\ matrix.\]

Demonstration:

# Original Matrix A
A = matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=FALSE)

# Transpose Matrix A_t
A_t = t(A)

# Display A and A_t
A; A_t
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## [3,]    5    6

\[AA^{T}\]

# A and A_t multiplication
A%*%A_t
##      [,1] [,2]
## [1,]   35   44
## [2,]   44   56

\[A^{T}A\]

# A_t and A multiplication
A_t%*%A
##      [,1] [,2] [,3]
## [1,]    5   11   17
## [2,]   11   25   39
## [3,]   17   39   61
#To Check if the two matrices are equal
EqMat <- identical(A_t, A)

EqMat
## [1] FALSE

\[(2)\ For\ a\ special\ type\ of\ square\ matrix\ A, we\ get\ A^{T}A = AA^{T}\ Under\ what\ conditions\ could\ this\ be\ true?\].

\[A^{T}A = AA^{T} when\ the\ matrix\ is\ symmetric\]

# Create a symmetric matrix
sym = matrix(c(1,2,3,4,2,1,5,6,3,5,1,7,4,6,7,1),nrow=4,ncol=4,byrow=FALSE)

print(sym)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    3    4
## [2,]    2    1    5    6
## [3,]    3    5    1    7
## [4,]    4    6    7    1
# Check if the matrix is symmetric
isSymmetric(sym)
## [1] TRUE

2. Problem set 2

Write an R function to factorize a square matrix A into LU or LDU

lu_decomposition <- function(A) {
  n <- nrow(A)
  if (n != ncol(A)) {
    stop("Stop!!! Provide square matrix.")
  }

  # Initialize matrices L and U
  L=diag(n)
  U=A
  print("Initial matrix:")
  print(A)
  
for(i in 2:n) {
    for(j in 1:(i-1)) {
      factor <- -U[i,j] / U[j,j]
      U[i, ] <- factor * U[j, ] + U[i, ]
      L[i,j] <- -factor
    }
  }
  
  return(list(L = L, U = U))
}
# Example: Use nrow and ncol variables to control the Matrix dimension
A <- matrix(c(2, -1, 1, 5,7,8,1, 11,3,-1,3,5,12,2, 8, 1, 1, 4, 9, 3,7,9,5,1,6),
            nrow = 4,ncol=4)
## Warning in matrix(c(2, -1, 1, 5, 7, 8, 1, 11, 3, -1, 3, 5, 12, 2, 8, 1, : data
## length [25] is not a sub-multiple or multiple of the number of rows [4]
result <- lu_decomposition(A)
## [1] "Initial matrix:"
##      [,1] [,2] [,3] [,4]
## [1,]    2    7    3   12
## [2,]   -1    8   -1    2
## [3,]    1    1    3    8
## [4,]    5   11    5    1
print(result$L)
##      [,1]       [,2]      [,3] [,4]
## [1,]  1.0  0.0000000  0.000000    0
## [2,] -0.5  1.0000000  0.000000    0
## [3,]  0.5 -0.2173913  1.000000    0
## [4,]  2.5 -0.5652174 -1.378378    1
print(result$U)
##      [,1]          [,2]     [,3]      [,4]
## [1,]    2  7.000000e+00 3.000000  12.00000
## [2,]    0  1.150000e+01 0.500000   8.00000
## [3,]    0  0.000000e+00 1.608696   3.73913
## [4,]    0 -8.881784e-16 0.000000 -19.32432