HW4

Problem Set 1

A <- matrix(c(1,-1,2,0,3,4),
            nrow = 2, ncol = 3)
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
#X = A(A^T)
X <- A %*% t(A)
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
#Y =(A^T)A
Y <- t(A) %*% A
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25
#Eigen Vectors and Values 
eigX <- eigen(X)
eigX
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
eigY <- eigen(Y)
eigX
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
#SVD  
svd_A <- svd(A)
#Left Singular Vector
svd_A$u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
eigX$vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
#Right Singular Vector
svd_A$v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
eigY$vectors
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001

When comparing the vectors produced using the svd command to eigen vectors X and Y we see that the first column in u (the left singluar vector) is -1 * the first column of the eigen vecor of X. THe second column in u is equal to the second column of the eignvector of X. The same trend is seen when comparing v (the right singular vector) and eigenvector Y.

sqrt(eigen(X)$values)
## [1] 5.157693 2.097188
svd_A$d
## [1] 5.157693 2.097188

This shows that the two non zero eigenvalues of X and Y are squares of the two non zero singularvalues of A (d).

Problem Set 2

#Function
A <- matrix(c(1,4,5,2,5,9,3,2,7),nrow=3)
myinverse <- function(A){
  #Create identiy matrix with rows and columns equal to A
  C <- diag(1,nrow(A),ncol(A))
   for (i in 1:nrow(A)) { 
        for (j in 1:ncol(A)){ 
          #Calucate the value for each element 
          Pij <- A[-i,-j]
          C[i,j] <- ((-1)^(i+j))*det(Pij)
        }
   }
  #Use the transpose of the created matrix C and the determinant of the input matrix A to get the inverse
  return(t(C)/det(A))
}

#Test the Function
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    2
## [3,]    5    9    7
myinverse(A)
##            [,1]        [,2]       [,3]
## [1,]  1.2142857  0.92857143 -0.7857143
## [2,] -1.2857143 -0.57142857  0.7142857
## [3,]  0.7857143  0.07142857 -0.2142857
A%*%myinverse(A)
##              [,1]         [,2]         [,3]
## [1,] 1.000000e+00 5.551115e-16 2.220446e-16
## [2,] 4.440892e-15 1.000000e+00 4.440892e-16
## [3,] 6.217249e-15 2.442491e-15 1.000000e+00

We see that the dot product of A and the result from myinverse(A) gives us the identity matrix.