1. Problem set 1

  1. Show that AT A 6= AAT in general. (Proof and demonstration.)
## Proof:
##(AT A) T = AT (AT ) T (By Algebraic Rule 4 for Transpose)                                                                                                  
##         = AT A. (By Algebraic Rule 1 for Transpose)                                                                 
##By the definition of symmetry, AT A is symmetric.                                                                                                           
## (AAT ) T = (AT ) T AT (By Algebraic Rule 4 for Transpose)                                                            
##          = AAT . (By Algebraic Rule 1 for Transpose)                                                                                               
##By the definition of symmetry, AAT is symmetric. 


A <- matrix(c(1,4,1,3,1,0,2,3,1), nrow=3, byrow=TRUE)
AT <- t(A)

Matrix A is

A
##      [,1] [,2] [,3]
## [1,]    1    4    1
## [2,]    3    1    0
## [3,]    2    3    1

Transpose of Matrix A is

AT
##      [,1] [,2] [,3]
## [1,]    1    3    2
## [2,]    4    1    3
## [3,]    1    0    1

Multiply A by its Transpose AT

A %*% AT
##      [,1] [,2] [,3]
## [1,]   18    7   15
## [2,]    7   10    9
## [3,]   15    9   14

Multiply Transpose A by A

AT %*% A
##      [,1] [,2] [,3]
## [1,]   14   13    3
## [2,]   13   26    7
## [3,]    3    7    2

Logical comparison

A %*% AT == AT %*% A
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE

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

When Matrix is simliar along the digonal then A = A.AT. For symettrical matrix its transpose is equal.

Demonstration

A <- matrix(c(1,2,0,2,1,0,0,0,1), nrow=3, byrow=TRUE)
AT <- t(A)

Matrix A is

A
##      [,1] [,2] [,3]
## [1,]    1    2    0
## [2,]    2    1    0
## [3,]    0    0    1

Transpose of Matrix A is which is equal to A

AT
##      [,1] [,2] [,3]
## [1,]    1    2    0
## [2,]    2    1    0
## [3,]    0    0    1
A == t(A)
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

Logical comparison

A %*% AT == AT %*% A
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE

###2. Problem set 2 Write an R function to factorize a square matrix A into LU or LDU, whichever you prefer.

factor_LU <- function(M) {
  # Check wheter matrix A is square
  if (dim(M)[1]!=dim(M)[2]) {
    return(NA)
  }
  
  U <- M
  n <- dim(M)[1]
  L <- diag(n)
  
  
  if (n==1) {
    return(list(L,U))
  }
  
  
  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))
}

Examples

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

A
##      [,1] [,2] [,3]
## [1,]    3    2    2
## [2,]    2    2    4
## [3,]    8    4    5
L
##           [,1] [,2] [,3]
## [1,] 1.0000000    0    0
## [2,] 0.6666667    1    0
## [3,] 2.6666667   -2    1
U
##      [,1]      [,2]     [,3]
## [1,]    3 2.0000000 2.000000
## [2,]    0 0.6666667 2.666667
## [3,]    0 0.0000000 5.000000
A == L %*% U
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE