In this problem, we’ll verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a 3 × 2 matrix A:
\[ \begin{bmatrix} 1 & 2 & 3 \\ -1 & 0 & 4 \\ \end{bmatrix} \]
Write code in R to compute X = AA^T and Y = A^TA. Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commands in R. Then, compute the left-singular, singular values, and right-singular vectors of A using the svd command. Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y. In addition, the two non-zero eigenvalues (the 3rd value will be very close to zero, if not zero) of both X and Y are the same and are squares of the non-zero singular values of A. Your code should compute all these vectors and scalars and store them in variables. Please add enough comments in your code to show me how to interpret your steps.
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
## [1] 5.157693 2.097188
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
The eigenvectors of X and Y are similar to the left and right singular vectors of A.
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
The square roots of the eigenvalues of X and Y are equal to the singular vectors of X and Y.
## [1] 5.157693 2.097188
## [1] 5.157693e+00 2.097188e+00 7.393046e-09
Using the procedure outlined in section 1 of the weekly handout, write a function to compute the inverse of a well-conditioned full-rank square matrix using co-factors. In order to compute the co-factors, you may use built-in commands to compute the determinant. Your function should have the following signature: B = myinverse(A) where A is a matrix and B is its inverse and A×B = I. The off-diagonal elements of I should be close to zero, if not zero. Likewise, the diagonal elements should be close to 1, if not 1. Small numerical precision errors are acceptable but the function myinverse should be correct and must use co-factors and determinant of A to compute the inverse.
A co-factor is the determinant of this sub-matrix along with the appropriate sign (+ or -) that goes with it.
## [,1] [,2] [,3] [,4]
## [1,] 10 8 20 14
## [2,] 2 13 13 20
## [3,] 20 10 2 1
## [4,] 4 8 13 3
myinverse <- function(matrix){
# A matrix with a determinant of 0 has no inverse
if (det(matrix) == 0){
print("This matrix has no inverse, its determinant is 0.")
} else {
C <- matrix
for (i in 1: nrow(matrix)){
for (j in 1: ncol(matrix)){
# find the determinant of the matrix after removing a row and column
det_matrix <- det(matrix[-i, -j])
# find the appropriate sign
C[i, j] <- (-1) ^ (i+j) * det_matrix
}
}
# the inverse is the transpose of C divided by the determinant
B = t(C)/det_matrix
return(B)
}
}
print(myinverse(A))## [,1] [,2] [,3] [,4]
## [1,] -0.5018460 0.2900844 -0.41191983 0.5453586
## [2,] 1.1334388 -0.6165612 -0.28850211 -1.0828059
## [3,] -0.4525316 0.3829114 0.29588608 -0.5395570
## [4,] -0.3924051 -0.4018987 0.03639241 1.0000000