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 = \left[\begin{array}{cc} 1 & 2 & 3\\ -1 & 0 & 4\\ \end{array}\right] \]
write code in R to compute X = AAT and Y = ATA. 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.
output = function(A) {
print("Input Matrix(A):\n")
print(A)
##Part 1
print("Transpose Matrix AA^T (X)")
AAT = A %*% t(A)
print(AAT)
eigen_aat = eigen(AAT)
print("Eigen Value for X:")
print(eigen_aat$values)
print("Eigen Vector for X:")
print(eigen_aat$vectors)
print("SVD for X:")
print("U matrix for X:")
print(svd(AAT)$u)
print("Diagonal for X:")
print(svd(AAT)$d)
print("V matrix for X:")
print(svd(AAT)$v)
print("Final SVD")
print(svd(AAT))
##Part 2
print("Transpose Matrix AA^T (Y)")
ATA = t(A) %*% A
print(ATA)
eigen_ata = eigen(ATA)
print("Eigen Value for Y:")
print(eigen_ata$values)
print("Eigen Vector for Y:")
print(eigen_ata$vectors)
print("SVD for Y:")
print("U matrix for Y:")
print(svd(ATA)$u)
print("Diagonal for Y:")
print(svd(ATA)$d)
print("V matrix for Y:")
print(svd(ATA)$v)
print("Final SVD")
print(svd(ATA))
}
output(matrix(c(1,2,3,-1,0,4,3,4),nrow=2,ncol=4))
## [1] "Input Matrix(A):\n"
## [,1] [,2] [,3] [,4]
## [1,] 1 3 0 3
## [2,] 2 -1 4 4
## [1] "Transpose Matrix AA^T (X)"
## [,1] [,2]
## [1,] 19 11
## [2,] 11 37
## [1] "Eigen Value for X:"
## [1] 42.21267 13.78733
## [1] "Eigen Vector for X:"
## [,1] [,2]
## [1,] 0.4282302 -0.9036697
## [2,] 0.9036697 0.4282302
## [1] "SVD for X:"
## [1] "U matrix for X:"
## [,1] [,2]
## [1,] 0.4282302 0.9036697
## [2,] 0.9036697 -0.4282302
## [1] "Diagonal for X:"
## [1] 42.21267 13.78733
## [1] "V matrix for X:"
## [,1] [,2]
## [1,] 0.4282302 0.9036697
## [2,] 0.9036697 -0.4282302
## [1] "Final SVD"
## $d
## [1] 42.21267 13.78733
##
## $u
## [,1] [,2]
## [1,] 0.4282302 0.9036697
## [2,] 0.9036697 -0.4282302
##
## $v
## [,1] [,2]
## [1,] 0.4282302 0.9036697
## [2,] 0.9036697 -0.4282302
##
## [1] "Transpose Matrix AA^T (Y)"
## [,1] [,2] [,3] [,4]
## [1,] 5 1 8 11
## [2,] 1 10 -4 5
## [3,] 8 -4 16 16
## [4,] 11 5 16 25
## [1] "Eigen Value for Y:"
## [1] 4.221267e+01 1.378733e+01 1.058349e-15 -4.166087e-15
## [1] "Eigen Vector for Y:"
## [,1] [,2] [,3] [,4]
## [1,] -0.34408582 0.01271415 -0.5034725 0.79243847
## [2,] -0.05864452 0.84544301 -0.4295231 -0.31192442
## [3,] -0.55635025 -0.46131461 -0.4529918 -0.52197861
## [4,] -0.75408233 0.26879975 0.5973473 0.04777827
## [1] "SVD for Y:"
## [1] "U matrix for Y:"
## [,1] [,2] [,3] [,4]
## [1,] -0.34408582 -0.01271415 0.9167089 -0.2027020
## [2,] -0.05864452 -0.84544301 -0.1465540 -0.5102047
## [3,] -0.55635025 0.46131461 -0.3359773 -0.6039722
## [4,] -0.75408233 -0.26879975 -0.1590156 0.5777721
## [1] "Diagonal for Y:"
## [1] 4.221267e+01 1.378733e+01 1.980271e-15 1.807049e-16
## [1] "V matrix for Y:"
## [,1] [,2] [,3] [,4]
## [1,] -0.34408582 -0.01271415 0.2286776 0.91057667
## [2,] -0.05864452 -0.84544301 -0.5218803 0.09709725
## [3,] -0.55635025 0.46131461 -0.6904633 -0.03039122
## [4,] -0.75408233 -0.26879975 0.4456545 -0.40062280
## [1] "Final SVD"
## $d
## [1] 4.221267e+01 1.378733e+01 1.980271e-15 1.807049e-16
##
## $u
## [,1] [,2] [,3] [,4]
## [1,] -0.34408582 -0.01271415 0.9167089 -0.2027020
## [2,] -0.05864452 -0.84544301 -0.1465540 -0.5102047
## [3,] -0.55635025 0.46131461 -0.3359773 -0.6039722
## [4,] -0.75408233 -0.26879975 -0.1590156 0.5777721
##
## $v
## [,1] [,2] [,3] [,4]
## [1,] -0.34408582 -0.01271415 0.2286776 0.91057667
## [2,] -0.05864452 -0.84544301 -0.5218803 0.09709725
## [3,] -0.55635025 0.46131461 -0.6904633 -0.03039122
## [4,] -0.75408233 -0.26879975 0.4456545 -0.40062280
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 AB = I. The o-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){
if(dim(A)[1]==dim(A)[2]) {
C = diag(dim(A)[1])
for(i in 1:dim(A)[1]) {
v1=c(1:dim(A)[1])
v2=c(i)
C[i,i] = det(A[setdiff(v1,v2),setdiff(v1,v2)])
}
} else {
print("Matrix is not square") }
CT = t(C)
A_inv = CT/det(A)
print("Normal inverse:")
print(inv(A))
print("Inverse using Co-factor menthod:")
print(A_inv)
}
myinverse(matrix(c(1,0,0,2,4,0,3,5,6),nrow=3,ncol=3))
## [1] "Normal inverse:"
## [,1] [,2] [,3]
## [1,] 1 -0.50 -0.08333333
## [2,] 0 0.25 -0.20833333
## [3,] 0 0.00 0.16666667
## [1] "Inverse using Co-factor menthod:"
## [,1] [,2] [,3]
## [1,] 1 0.00 0.0000000
## [2,] 0 0.25 0.0000000
## [3,] 0 0.00 0.1666667
myinverse(matrix(c(1,7,3,7,4,-5,3,-5,6,12,2,5,2,3,2,4),nrow=4,ncol=4))
## [1] "Normal inverse:"
## [,1] [,2] [,3] [,4]
## [1,] -0.36605317 0.18404908 0.47443763 -0.19222904
## [2,] -0.02658487 0.02453988 0.21881391 -0.11451943
## [3,] 0.07361963 0.08588957 -0.06748466 -0.06748466
## [4,] 0.51533742 -0.39877301 -0.47239264 0.52760736
## [1] "Inverse using Co-factor menthod:"
## [,1] [,2] [,3] [,4]
## [1,] -0.3660532 0.00000000 0.00000000 0.0000000
## [2,] 0.0000000 0.02453988 0.00000000 0.0000000
## [3,] 0.0000000 0.00000000 -0.06748466 0.0000000
## [4,] 0.0000000 0.00000000 0.00000000 0.5276074