Problem set 1

1.1 Not equal

Show that \(A^TA \neq AA^T\) in general. \[\mathbf{A} = \left[\begin{array} {rrr} a_{11} & a_{12} \\ a_{21} & a_{22} \end{array}\right] \] \[\mathbf{A^T} = \left[\begin{array} {rrr} a_{11} & a_{21} \\ a_{12} & a_{22} \end{array}\right] \] Let \(E_{ij}\) be the elements of resulting matrix. It can be seen that the elements will not be equal:

\(E_{11}\)
\[(a_{11}* a_{11}) + (a_{21} * a_{21}) \neq (a_{11}* a_{11}) + (a_{12} * a_{12})\]

\(E_{21}\)
\[(a_{12}* a_{11}) + (a_{22} * a_{21}) \neq (a_{21}* a_{11}) + (a_{22} * a_{12})\]

\(E_{12}\)
\[(a_{11}* a_{12}) + (a_{21} * a_{22}) \neq (a_{11}* a_{21}) + (a_{12} * a_{22})\]

\(E_{22}\)
\[(a_{12}* a_{12}) + (a_{22} * a_{22}) \neq (a_{21}* a_{21}) + (a_{22} * a_{22})\]

An example:

A <- matrix(c(7, 2, 6, 4), nrow=2, ncol=2) ;A
##      [,1] [,2]
## [1,]    7    6
## [2,]    2    4

\(A^TA\)

t(A)%*%A
##      [,1] [,2]
## [1,]   53   50
## [2,]   50   52

\(AA^T\):

A%*%t(A)
##      [,1] [,2]
## [1,]   85   38
## [2,]   38   20

1.2 Equal

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

They will be equal if the elements above and below the diagonal mirror each other.

An example:

A <- matrix(c(2, 7, 3, 7, 9, 4, 3, 4, 7), nrow=3, ncol=3) ;A
##      [,1] [,2] [,3]
## [1,]    2    7    3
## [2,]    7    9    4
## [3,]    3    4    7

\(A^TA\)

t(A)%*%A
##      [,1] [,2] [,3]
## [1,]   62   89   55
## [2,]   89  146   85
## [3,]   55   85   74

\(AA^T\):

A%*%t(A)
##      [,1] [,2] [,3]
## [1,]   62   89   55
## [2,]   89  146   85
## [3,]   55   85   74

Problem set 2

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

2.1 Create function

find_LU <- function(m){

  L <- diag(nrow(m))
  U <- m

  for (cl in 1:(nrow(m) - 1)) {
    for (rw in (cl+1):nrow(m)) {
    L[rw,cl] <- U[rw,cl]/U[cl,cl]
    U[rw,] <- U[rw,] - (U[cl,] * (U[rw,cl]/U[cl,cl]))
      
  }
  }

  m_LU <- list("L" = L, "U" = U)
  return(m_LU)

}

2.2 Test function with 2x2 matrix

A <- matrix(c(1, 4, 3, -2), nrow=2, ncol=2)

LU <- find_LU(A)

# original matrix
A
##      [,1] [,2]
## [1,]    1    3
## [2,]    4   -2
# factors of matrix
LU
## $L
##      [,1] [,2]
## [1,]    1    0
## [2,]    4    1
## 
## $U
##      [,1] [,2]
## [1,]    1    3
## [2,]    0  -14
# multipy factors, returns the original matrix
LU$L %*% LU$U
##      [,1] [,2]
## [1,]    1    3
## [2,]    4   -2

2.3 Test function with 3x3 matrix

A <- matrix(c(1, 10, -1, 6, -1, -2, 3, -2, 4), nrow=3, ncol=3)

LU <- find_LU(A)

# original matrix
A
##      [,1] [,2] [,3]
## [1,]    1    6    3
## [2,]   10   -1   -2
## [3,]   -1   -2    4
# factors of matrix
LU
## $L
##      [,1]        [,2] [,3]
## [1,]    1  0.00000000    0
## [2,]   10  1.00000000    0
## [3,]   -1 -0.06557377    1
## 
## $U
##      [,1] [,2]       [,3]
## [1,]    1    6   3.000000
## [2,]    0  -61 -32.000000
## [3,]    0    0   4.901639
# multipy factors, returns the original matrix
LU$L %*% LU$U
##      [,1] [,2] [,3]
## [1,]    1    6    3
## [2,]   10   -1   -2
## [3,]   -1   -2    4

2.4 Test function with 4x4 matrix

A <- matrix(c(1, 10, -1, -2, 8, 6, -1, -2, 3,-1, -2, 4), nrow=4, ncol=4)

LU <- find_LU(A)

# original matrix
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    8    3    1
## [2,]   10    6   -1   10
## [3,]   -1   -1   -2   -1
## [4,]   -2   -2    4   -2
# factors of matrix
LU
## $L
##      [,1]        [,2]     [,3] [,4]
## [1,]    1  0.00000000  0.00000    0
## [2,]   10  1.00000000  0.00000    0
## [3,]   -1 -0.09459459  1.00000    0
## [4,]   -2 -0.18918919 -2.13986    1
## 
## $U
##      [,1] [,2]       [,3] [,4]
## [1,]    1    8   3.000000    1
## [2,]    0  -74 -31.000000    0
## [3,]    0    0  -1.932432    0
## [4,]    0    0   0.000000    0
# multipy factors, returns the original matrix
LU$L %*% LU$U
##      [,1] [,2] [,3] [,4]
## [1,]    1    8    3    1
## [2,]   10    6   -1   10
## [3,]   -1   -1   -2   -1
## [4,]   -2   -2    4   -2