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
\[ \left(\begin{array}{cc} 1 & 2 & 3\\ -1 & 0 & 4 \end{array}\right) \]
write code in R to compute= \(X = AA^{T} ; Y = A^{T}A\)
A = matrix(c(1,2,3,-1,0,4), nrow=2,byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
X = A %*% t(A)
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
Y = t(A) %*% A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commands in R.
#eigen command can find values and vectors
valX = eigen(X)$values
vecX = eigen(X)$vectors
eigen(X)
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
valY = eigen(Y)$values
vecY = eigen(Y)$vectors
eigen(Y)
## $values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
##
## $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
singVal = svd(A)$d
leftVec = svd(A)$u
rightVec = svd(A)$v
Then, compute the left-singular, singular values, and right-singular vectors of A using the svd command.
#svd command
singVal = svd(A)$d
leftVec = svd(A)$u
rightVec = svd(A)$v
singVal
## [1] 5.157693 2.097188
leftVec
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
rightVec
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y.
#eigenvector calculation compared to the SVD calculation. They are equal.
vecX
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
leftVec
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
vecY
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
rightVec
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
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.
valX
## [1] 26.601802 4.398198
singVal^2
## [1] 26.601802 4.398198
valY[1:2]
## [1] 26.601802 4.398198
singVal^2
## [1] 26.601802 4.398198
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)
randSM <- function(x){
#random square matrix with 3 rows/columns
m = matrix(sample.int(10,x**2,replace=T),nrow = x, ncol = x)
return (m)
}
A = randSM(3)
A
## [,1] [,2] [,3]
## [1,] 3 3 2
## [2,] 10 8 10
## [3,] 2 10 6
det(A)
## [1] -108
#function to find the inverse
myinverse <- function(A) {
# of rows
n = nrow(A)
#function removes the corresponding row/column for each iteration and find the determinate value of the submatrix
determS = function(A, i, j) {
det(A[-i,-j])
}
#supposed to change the sign on every other calculation, but isn't working properly. Can you take a look?
cofactors <- function(A, i, j) {
-1^(i+j) * determS(A, i, j)
}
#blank placeholder for cofactor matrix
cofac = matrix(0,n,n)
for (i in 1:n){
for (j in 1:n){
cofac[i,j] = cofactors(A, i, j)
}
}
return (t(cofac)/det(A))
}
B = myinverse(A)
B
## [,1] [,2] [,3]
## [1,] -0.4814815 -0.01851852 0.12962963
## [2,] 0.3703704 0.12962963 0.09259259
## [3,] 0.7777778 0.22222222 -0.05555556
solve(A)
## [,1] [,2] [,3]
## [1,] 0.4814815 -0.01851852 -0.12962963
## [2,] 0.3703704 -0.12962963 0.09259259
## [3,] -0.7777778 0.22222222 0.05555556
A %*% B
## [,1] [,2] [,3]
## [1,] 1.222222 0.7777778 0.5555556
## [2,] 5.925926 3.0740741 1.4814815
## [3,] 7.407407 2.5925926 0.8518519
As you can see above, some of my signs appear to be wrong and only some of the off diagonal values approach 0. Unsure where my issue is.