Problem Set 1

\(A\)

matA <- matrix(c(1,2,3,-1,0,4), nrow = 2, byrow = T)
matA
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4

\(A^T\)

#transpose of A
matA.transpose <- t(matA)
matA.transpose
##      [,1] [,2]
## [1,]    1   -1
## [2,]    2    0
## [3,]    3    4

\(X=AA^T\)

mat.X <- matA%*%matA.transpose
mat.X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17

\(Y=A^TA\)

mat.Y <- matA.transpose%*%matA
mat.Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25

Eigenvalues of X

eigen.xvalues <- round(eigen(mat.X)$values, 2)
eigen.xvalues
## [1] 26.6  4.4

Eigenvectors of X

eigen.xvectors <- round(eigen(mat.X)$vectors, 3)
eigen.xvectors
##       [,1]   [,2]
## [1,] 0.658 -0.753
## [2,] 0.753  0.658

Eigenvalues of Y

eigen.yvalues <- round(eigen(mat.Y)$values, 2)
eigen.yvalues
## [1] 26.6  4.4  0.0

Eigenvectors of Y

eigen.yvectors <- round(eigen(mat.Y)$vectors, 3)
eigen.yvectors
##        [,1]   [,2]   [,3]
## [1,] -0.019 -0.673  0.740
## [2,]  0.255 -0.718 -0.647
## [3,]  0.967  0.177  0.185

Left singular value vectors from the SVD of A

left.singular <- round(svd(matA)$d, 3)
left.singular 
## [1] 5.158 2.097

Singular value vectors from the SVD of A

single.value <- round(svd(matA)$u, 3)
single.value
##        [,1]   [,2]
## [1,] -0.658 -0.753
## [2,] -0.753  0.658

Right singular value vectors from the SVD of A For some reason, using the svd() method only produced 2 vectors. I had to add the argument nv=3 to force it to make 3 vectors.

right.singular <- round(svd(matA, nv = 3)$v, 3)
right.singular
##        [,1]   [,2]   [,3]
## [1,]  0.019 -0.673 -0.740
## [2,] -0.255 -0.718  0.647
## [3,] -0.967  0.177 -0.185

Eigenvectors of X = Single Value vectors from the SVD of A. You can tell from the matrices below that the vectors are scalars of each other. Therefore, the single value vectors are eigenvectors of X

eigen.xvectors
##       [,1]   [,2]
## [1,] 0.658 -0.753
## [2,] 0.753  0.658
single.value
##        [,1]   [,2]
## [1,] -0.658 -0.753
## [2,] -0.753  0.658

Eigenvectors of Y = Right-Singular Value vectors from the SVD of A.

eigen.yvectors
##        [,1]   [,2]   [,3]
## [1,] -0.019 -0.673  0.740
## [2,]  0.255 -0.718 -0.647
## [3,]  0.967  0.177  0.185
right.singular
##        [,1]   [,2]   [,3]
## [1,]  0.019 -0.673 -0.740
## [2,] -0.255 -0.718  0.647
## [3,] -0.967  0.177 -0.185

Problem Set 2

Start with a known full-rank 3x3 Matrix

A <- matrix(c(1,2,3,3,2,1,2,1,3), ncol = 3, byrow = T )
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    3    2    1
## [3,]    2    1    3
myInverse <- function(n) {
  
  i <- 1
  j <- 1
  cofactors <- c()
  minors <- c()
  det <- 0
  
  #Find the co-factors
  while(i <= dim(n)[1]){  # <- while less than or equal to the number of rows
    j <- 1
    
    while(j <= dim(n)[2]){ # <- while less than or equal to the number of columns
      
      #create a submatrix by eliminating the rows that correspond with i and j 
      #(ex: i,j = 1, then col and row 1 will be eliminated)
      A.sub <- n[-(i),-(j)]
      #Find the determinant and enter it into the minor vector
      minors <- c(minors, det(A.sub))
      #Applying the sign changes for the cofactor matrix and adding it to the cofactor vector
      cofactors <- c(cofactors, ((-1)^(i+j))*det(A.sub))
      
      j <- j + 1

    } #end while(j) loop
  i <- i + 1
  } #end while(i) loop
  
  #converts the vectors into a matrices
  mat.cofactor <- matrix(cofactors, nrow = dim(n), byrow = T)
  mat.minors <- matrix(minors, nrow = dim(n), byrow = T)

  #Find the determinant of our original matrix. Match the values of the first rows of the minors matrix 
  #and the first row of the original matrix and multiply them, switching signs each time.
  x <- 1
  while(x <= dim(n)[2])
  {
    det <- det + ((-1)^(x+1))*(A[1,x] * mat.minors[1,x])
    x <- x + 1
  }
  
  #transpose the matrix
  mat.cofactor <- t(mat.cofactor)
  
  #Multiply the transpose matrix by the 1/determinant so the diagonal values of I will be close to 1
  mat.cofactor <- mat.cofactor*(1/det)
  
  return(mat.cofactor)
  
} #end function myInverse

Testing PS2

Finding \(B\)

B <- myInverse(A)
B
##             [,1]  [,2]       [,3]
## [1,] -0.41666667  0.25  0.3333333
## [2,]  0.58333333  0.25 -0.6666667
## [3,]  0.08333333 -0.25  0.3333333

\(A \times B = I\)

round(A%*%B, 2)
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1