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
\[A = \begin{bmatrix} 1 & 2 & 3 \\ -1 & 0 & 4\\ \end{bmatrix}\]
write code in R to compute \(X = A*A^T\) and \(Y = A^T*A\). Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commans 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.
#build matrix A
A <- matrix(c(1,2,3,-1,0,4),nrow=2, ncol=3, byrow = TRUE)
print(A)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
#build transpose of matrix A
transpose_A <- t(A)
print(transpose_A)
## [,1] [,2]
## [1,] 1 -1
## [2,] 2 0
## [3,] 3 4
#calculate X
X <- A%*%t(A)
print(X)
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
#calculate Y
Y <- t(A)%*%A
print(Y)
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
#calculate eigenvalues
eigen_value_X <- eigen(X)$value
print(eigen_value_X )
## [1] 26.601802 4.398198
eigen_value_Y <- eigen(Y)$value
print(eigen_value_Y)
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
#calculate eigenvectors
eigen_vector_X <- eigen(X)$vector
print(eigen_vector_X)
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
eigen_vector_Y <- eigen(Y)$vector
print(eigen_vector_Y)
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
#calculate left-singular vector
left_singular_vector <- svd(A)$u
print(left_singular_vector)
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
#calculate right-singular vectors
right_singular_vector <- svd(A)$v
print(right_singular_vector)
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
#calculate singular values vector
singular_values_vector <- svd(A)$d
print(singular_values_vector)
## [1] 5.157693 2.097188
Let’s examine the two sets of singular vectors and show that scalars of singular vectors are equal to scalars of eigenvectors.
#compare absolute values of left singular vector with absolute values of eigenvector of X
abs(left_singular_vector)
## [,1] [,2]
## [1,] 0.6576043 0.7533635
## [2,] 0.7533635 0.6576043
abs(eigen_vector_X)
## [,1] [,2]
## [1,] 0.6576043 0.7533635
## [2,] 0.7533635 0.6576043
#compare absolute values of right singular vector with absolute values of eigenvector of Y
abs(right_singular_vector)
## [,1] [,2]
## [1,] 0.01856629 0.6727903
## [2,] 0.25499937 0.7184510
## [3,] 0.96676296 0.1765824
abs(eigen_vector_Y)
## [,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 squired eigenvalues of X and Y equal to singular values of A.
singular_values_vector
## [1] 5.157693 2.097188
round(sqrt(eigen_value_X),6)
## [1] 5.157693 2.097188
round(sqrt(eigen_value_Y),6)
## [1] 5.157693 2.097188 0.000000
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.
myinverse <- function(A){
#initialize co-factors matrix
co_factors_A <- matrix(c(0:0),nrow=nrow(A), ncol=ncol(A), byrow = TRUE)
print("initial co-factors matrix")
print(co_factors_A)
#loop through all elements of matrix
for(i in 1:nrow(co_factors_A))
{
for(j in 1:ncol(co_factors_A))
{
co_factors_A[i,j] <- (-1)^(i+j)*det(A[-i,-j]) # eliminate the row and the column of current element when calculating the co-factor
}
}
print("co-factors matrix")
print(co_factors_A)
#calculate inverse of matrix A by dividing the transpose of matrix A by determinant of matrix A
inverse_A<-t(co_factors_A)/det(A)
print("inverse matrix")
print(inverse_A)
#identity matrix should have all 1s in main diaginal and all other elements should be close to 0
print("Identity matrix")
print(round(inverse_A%*%A,0))
}
my_matrix <- matrix(c(1,2,3,-1,0,4,8,9,7),nrow=3, ncol=3, byrow = TRUE)
myinverse(my_matrix)
## [1] "initial co-factors matrix"
## [,1] [,2] [,3]
## [1,] 0 0 0
## [2,] 0 0 0
## [3,] 0 0 0
## [1] "co-factors matrix"
## [,1] [,2] [,3]
## [1,] -36 39 -9
## [2,] 13 -17 7
## [3,] 8 -7 2
## [1] "inverse matrix"
## [,1] [,2] [,3]
## [1,] -2.4 0.8666667 0.5333333
## [2,] 2.6 -1.1333333 -0.4666667
## [3,] -0.6 0.4666667 0.1333333
## [1] "Identity matrix"
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1