1 Show that A^T A 6 \(\neq\) AA^T in general.

Let’s say


A=1ab1AAT=1ab1  1ba1 =1+a2b+ab+ab2+1ATA=1ba1  1ab1 =1+b2a+ba+ba2+1AATATA

Using R

A=matrix(c(1,2,3,4), nrow=2, ncol=2) 
A
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
t(A)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
A %*% t(A)
##      [,1] [,2]
## [1,]   10   14
## [2,]   14   20
t(A) %*% A
##      [,1] [,2]
## [1,]    5   11
## [2,]   11   25
2. For a special type of square matrix A, we get AT A = AAT.Under what conditions could this be true?

For the identity matrix and its variants, and symetric matrices \(A^TA = AA^T\). Because a symmetric matrix and its transpose are equal, that is multiplying a matrix by itself.

3. Write an R function to factorize a square matrix \(A\) into LU or LDU

This function will factorize matrix into LU

factorize <- function(A) 
{
  if (nrow(A) == ncol(A)) 
  {
      size <- nrow(A)
      L <- diag(size) #construct a diagonal matrix.
      
      
      for (i in 1:(size - 1)) 
      {
          for (j in (i + 1):size) 
          {
              L[j, i] <- A[j, i] / A[i, i]
              A[j, ]  <- A[j, ] - L[j, i] * A[i, ]
          }
      }
      print("check if below is same as original matrix")
      print("******L %*% A********")
      print(L %*% A)
      print("******L********")
      print(L)
      print("******U********")
      print(A)
  } 
}

Check with a 2x2 and a 3x3 matrix

# 2x2
A2x2 <- matrix(c(2,3,4,5), nrow = 2)
A2x2
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    5
factorize(A2x2)
## [1] "check if below is same as original matrix"
## [1] "******L %*% A********"
##      [,1] [,2]
## [1,]    2    4
## [2,]    3    5
## [1] "******L********"
##      [,1] [,2]
## [1,]  1.0    0
## [2,]  1.5    1
## [1] "******U********"
##      [,1] [,2]
## [1,]    2    4
## [2,]    0   -1
# 3x3
A3x3 <- matrix(c(8,9,10,0,1,2,11,12,13), nrow = 3)
A3x3
##      [,1] [,2] [,3]
## [1,]    8    0   11
## [2,]    9    1   12
## [3,]   10    2   13
factorize(A3x3)
## [1] "check if below is same as original matrix"
## [1] "******L %*% A********"
##      [,1] [,2] [,3]
## [1,]    8    0   11
## [2,]    9    1   12
## [3,]   10    2   13
## [1] "******L********"
##       [,1] [,2] [,3]
## [1,] 1.000    0    0
## [2,] 1.125    1    0
## [3,] 1.250    2    1
## [1] "******U********"
##      [,1] [,2]   [,3]
## [1,]    8    0 11.000
## [2,]    0    1 -0.375
## [3,]    0    0  0.000