Assignment 2

Problem set 1

Answer 1

knitr::include_graphics("C:\\Users\\sergioor\\Desktop\\Old PC\\CUNY\\DATA605\\hw1ex1.jpg")

Demonstration:

\(Consider\quad a\quad matrix\quad A\quad i,j:\\ \\ \begin{bmatrix} { a }_{ 1,1 } & { a }_{ 1,2 } & { a }_{ 1,3 } & . & . & . & { a }_{ 1,j } \\ { a }_{ 2,1 } & { a }_{ 2,2 } & { a }_{ 2,3 } & . & . & . & . \\ { a }_{ 3,1 } & { a }_{ 3,2 } & { a }_{ 3,3 } & . & . & . & . \\ . & . & . & . & . & . & . \\ . & . & . & . & . & . & . \\ . & . & . & . & . & . & . \\ { a }_{ i,1 } & { a }_{ i,2 } & { a }_{ i,3 } & . & . & . & { a }_{ i,j } \end{bmatrix}\)

Assuming \(i\neq j\)

\({ A }_{ (j,i) }^{ T }\) will result in j rows and i columns

\(Transpose\quad of\quad A,{ A }^{ T\quad }=\quad \begin{bmatrix} { a }_{ 1,1 } & { a }_{ 2,1 } & { a }_{ 3,1 } & . & . & . & { a }_{ i,1 } \\ { a }_{ 1,2 } & { a }_{ 2,2 } & { a }_{ 3,2 } & . & . & . & { a }_{ 2,j } \\ { a }_{ 1,3 } & { a }_{ 2,3 } & { a }_{ 3,3 } & . & . & . & { a }_{ 3,j } \\ . & . & . & . & . & . & . \\ . & . & . & . & . & . & . \\ . & . & . & . & . & . & . \\ { a }_{ 1,j } & { a }_{ 2,j } & { a }_{ 3,j } & . & . & . & { a }_{ j,i } \end{bmatrix}\)

\({ A }_{ (i,j) }.{ A }_{ (j,i) }^{ T }\quad will\quad result\quad in\quad matrix\quad of\quad i\quad rows\quad and\quad i\quad columns,\quad A.{ A }_{ (i,i) }^{ T }\\ { A }_{ (j,i) }.{ A }_{ (i,j) }^{ T }\quad will\quad result\quad in\quad matrix\quad of\quad j\quad rows\quad and\quad j\quad columns,\quad { A }_{ }^{ T }{ A }_{ (j,j) }\\ Hence,\quad { A }_{ (i,j) }{ A }_{ (j,i) }^{ T }\quad \neq \quad { A }_{ (j,i) }^{ T }{ A }_{ (i,j) }\quad \\ A{ A }_{ (i,i) }^{ T }\quad \neq \quad { A }_{ }^{ T }{ A }_{ (j,j) }\)

Consider the 4x5 matrix

A = matrix(c(1,3,2,2, 3,2,1,1, 2,1,0,1, 1,-1,2,1, 1,4,1,3), nrow=4, ncol=5)
A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    2    1    1
## [2,]    3    2    1   -1    4
## [3,]    2    1    0    2    1
## [4,]    2    1    1    1    3

Generate traspose of A:

tA = t(A)

tA
##      [,1] [,2] [,3] [,4]
## [1,]    1    3    2    2
## [2,]    3    2    1    1
## [3,]    2    1    0    1
## [4,]    1   -1    2    1
## [5,]    1    4    1    3

Transpose now is a 5x4 matrix

proving original \(\quad \\ A{ A }_{ (i,i) }^{ T }\quad \neq \quad { A }_{ }^{ T }{ A }_{ (j,j) }\):

A%*%tA
##      [,1] [,2] [,3] [,4]
## [1,]   16   14    8   11
## [2,]   14   31   10   20
## [3,]    8   10   10   10
## [4,]   11   20   10   16
#geenrates 4x4 matrix
tA%*%A
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   18   13    7    4   21
## [2,]   13   15    9    4   15
## [3,]    7    9    6    2    9
## [4,]    4    4    2    7    2
## [5,]   21   15    9    2   27
#geenrates 5x5 matrix

Hence \(\quad \\ A{ A }_{ (i,i) }^{ T }\quad \neq \quad { A }_{ }^{ T }{ A }_{ (j,j) }\) is correct.

Answer number 2

When a matrix is symmetrical square matrix or identity matrix then \({ A }_{ (i,j) }{ A }_{ (j,i) }^{ T }\quad =\quad { A }_{ (j,i) }^{ T }{ A }_{ (i,j) }\)

#4 X 4 matrix 
A = matrix(c(9,1,3,6, 1,11,7,6, 3,7,4,1, 6,6,1,10), nrow=4, ncol=4)

A
##      [,1] [,2] [,3] [,4]
## [1,]    9    1    3    6
## [2,]    1   11    7    6
## [3,]    3    7    4    1
## [4,]    6    6    1   10
#transpose of matrix A
#it is similar to A
tA = t(A)

tA
##      [,1] [,2] [,3] [,4]
## [1,]    9    1    3    6
## [2,]    1   11    7    6
## [3,]    3    7    4    1
## [4,]    6    6    1   10
#matrix multiplication
A%*%tA
##      [,1] [,2] [,3] [,4]
## [1,]  127   77   52  123
## [2,]   77  207  114  139
## [3,]   52  114   75   74
## [4,]  123  139   74  173
tA%*%A
##      [,1] [,2] [,3] [,4]
## [1,]  127   77   52  123
## [2,]   77  207  114  139
## [3,]   52  114   75   74
## [4,]  123  139   74  173

Symmetric Square Matrix is one exception to the rule, where (i = j) \({ A }_{ (i,j) }{ A }_{ (j,i) }^{ T }\quad =\quad { A }_{ (j,i) }^{ T }{ A }_{ (i,j) }\quad =\quad A{ A }_{ (i,i) }^{ T }\quad =\quad { A }^{ T }{ A }_{ (j,j) }\)

Similarly for identity matrix

#4 X 4 identity matrix 
A = diag(4)

A
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
#transpose of matrix A
#it is similar to A
tA = t(A)

tA
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
#matrix multiplication
A%*%tA
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
tA%*%A
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1

Identity matrix is another exception to the rule, where (i = j),

\({ A }_{ (i,j) }{ A }_{ (j,i) }^{ T }\quad =\quad { A }_{ (j,i) }^{ T }{ A }_{ (i,j) }\quad =\quad A{ A }_{ (i,i) }^{ T }\quad =\quad { A }^{ T }{ A }_{ (j,j) }\)

Problem set 2

knitr::include_graphics("C:\\Users\\sergioor\\Desktop\\Old PC\\CUNY\\DATA605\\hw2ex2.jpg")

Following function accepts a 5 X 5 square matrix and diagonal value to create identity matrix. It returns two matrices lower(L) and upper(U) decomposition

lud_function <- function(a,diagonal){
  #initiate identity matrix
  l = diag(diagonal)
  u = a
  
  #check if a[1,1] equal zero move it following row
  for (i in 1:nrow(u)){
    if (u[1,1] == 0){
      if (i+1 <= nrow(u)){
        swap = u[i+1,]
        u[i+1,] = u[1,]
        u[1,] = swap
        if (u[1,1] != 0){
          break
        }        
      }
    }
  }
  
  #Loop through every column and every row.
  #While working with column number 1 row number should be 2 (row > column)
  #For the first row, leave values as is
  #For the second row make first value zero
  #For the third row make first and second column values zero
  #Basically, we are doing row reduction echelon form, converting numbers on the lower side of diagonal to zero.
  #And saving factor value on lower side of identity matrix.
  
  k = 0
  #read columns
  for (j in 1:ncol(a)){
    k = k + 1
    #read rows
    for (i in 1:nrow(u)){
      #if row > column and value for the row is not equal to zero
      if (i > j){
        if (u[i,j] != 0){
          #initiate the sign of the factor
          s = -1
          if (u[i,j] < 0){
            if (u[k,j] < 0){
              #when both numbers are negative keep sign positive
              s = 1
            }
          }
          #calculate the factor
          f = s *  u[i,j]/u[k,j]
          #multiply upper row with the factor and sign
          swap1 = s * (u[k,] * u[i,j])/u[k,j]
          #get current row
          swap2 = u[i,]
          #add modified upper row to current row.
          #this should make row,column value to zero when addition is performed
          u[i,] = swap1 + swap2
          #record the factor
          l[i,j] = -1*f
        } 
      }
    }
  }
  return(list(l=l,u=u))
}

#get matrix
#a = matrix(c(1,-2,3, 4,8,4, -3,5,7), nrow=3, ncol=3)
#l = diag(3)
#a = matrix(c(1,2,0,0, 5,12,4,0, 0,5,13,6, 0,0,5,11), nrow=4, ncol=4)
#l = diag(4)

a = matrix(c(2,6,6,4,2, 3,1,3,2,1, 4,3,1,4,2, 1,1,2,7,4, 3,2,5,8,2), nrow=5, ncol=5)
m = lud_function(a,5)

Lower Matrix

#Lower matrix
m$l
##      [,1]  [,2]       [,3]     [,4] [,5]
## [1,]    1  0.00  0.0000000 0.000000    0
## [2,]    3  1.00  0.0000000 0.000000    0
## [3,]    3 -0.75  1.0000000 0.000000    0
## [4,]    2 -0.50 -0.4788732 1.000000    0
## [5,]    1 -0.25 -0.2394366 0.678392    1

Upper Matric

#Upper matrix
m$u
##      [,1]       [,2]       [,3]          [,4]       [,5]
## [1,]    2   3.000000   4.000000  1.000000e+00  3.0000000
## [2,]    0  -8.000000  -9.000000 -2.000000e+00 -7.0000000
## [3,]    0 -12.000000 -17.750000 -2.500000e+00 -9.2500000
## [4,]    0 -13.746479 -17.000000  2.802817e+00 -5.9295775
## [5,]    0   2.452261   3.032663 -2.220446e-16 -0.9422111

Original Matrix for proof

a
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    3    4    1    3
## [2,]    6    1    3    1    2
## [3,]    6    3    1    2    5
## [4,]    4    2    4    7    8
## [5,]    2    1    2    4    2

Multiplying L and U and we have the original Matrix back

#multipication of L and U
m$l%*%m$u
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    2    3    4    1    3
## [2,]    6    1    3    1    2
## [3,]    6    3    1    2    5
## [4,]    4    2    4    7    8
## [5,]    2    1    2    4    2