IS 605 FUNDAMENTALS OF COMPUTATIONAL MATHEMATICS - 2014
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}{rrr}1 & 2 & 3\\-1 & 0 & 4\end{array}\right] \] 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.
A <- matrix(c(1,2,3,-1,0,4), 2, byrow=T)
print("Matrix A:")
## [1] "Matrix A:"
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
\[X = AA^T\]
# multiply A by it's transpose
X <- A %*% t(A)
print("X:")
## [1] "X:"
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
\[Y = A^TA\]
Y <- t(A) %*% A
print("Y:")
## [1] "Y:"
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
eigen_X <- eigen(X)
eigen_Y <- eigen(Y)
print("eigen of X:")
## [1] "eigen of X:"
eigen_X
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
print("eigen of Y:")
## [1] "eigen of Y:"
eigen_Y
## eigen() decomposition
## $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
eigenValue_X <- eigen_X$values
print(paste("EigenValues for X include: ",list(round(eigenValue_X, digit=2))))
## [1] "EigenValues for X include: c(26.6, 4.4)"
eigenValue_Y <- eigen_Y$values
print(paste("EigenValues for Y include: ",list(round(eigenValue_Y, digit=2))))
## [1] "EigenValues for Y include: c(26.6, 4.4, 0)"
eigenVector_X <- eigen_X$vectors
print(paste("EigenVectors for X include: ",list(round(eigenVector_X, digit=2))))
## [1] "EigenVectors for X include: c(0.66, 0.75, -0.75, 0.66)"
eigenVector_Y <- eigen_Y$vectors
print(paste("EigenVectors for Y include: ",list(round(eigenVector_Y, digit=2))))
## [1] "EigenVectors for Y include: c(-0.02, 0.25, 0.97, -0.67, -0.72, 0.18, 0.74, -0.65, 0.18)"
Now, compute the left-singular, singular values, and right-singular vectors of A using the svd() command.
svdA <- svd(A)
print("SVD of matrix A:")
## [1] "SVD of matrix A:"
svdA
## $d
## [1] 5.157693 2.097188
##
## $u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
##
## $v
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
paste("Singular Values of A:", list(svdA$d))
## [1] "Singular Values of A: c(5.15769344335113, 2.09718819956931)"
paste("Left-singular Values of A:", list(svdA$u))
## [1] "Left-singular Values of A: c(-0.657604286507732, -0.753363526039492, -0.753363526039492, 0.657604286507732)"
paste("Right-singular Values of A:", list(svdA$v))
## [1] "Right-singular Values of A: c(0.0185662914214504, -0.254999368896366, -0.966762956823082, -0.672790268816595, -0.718451044302279, 0.176582420208403)"
As can be seen, the $u is close to the eigen vectors of X while $v is close to the eigen vectors of Y.
Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y.
comp1 <- cbind(svdA$u, eigen_X$vectors)
colnames(comp1) <- c('SVDu1', 'SVDu2', 'EVX1=u1', 'EVX2=u2')
knitr::kable(comp1)
| SVDu1 | SVDu2 | EVX1=u1 | EVX2=u2 |
|---|---|---|---|
| -0.6576043 | -0.7533635 | 0.6576043 | -0.7533635 |
| -0.7533635 | 0.6576043 | 0.7533635 | 0.6576043 |
comp2 <- cbind(svdA$v, eigen_Y$vectors)
comp2 <- comp2[,1:4]
colnames(comp2) <- c('SVDv1', 'SVDv2', 'EVY1=v1', 'EVY2=v2')
knitr::kable(comp2)
| SVDv1 | SVDv2 | EVY1=v1 | EVY2=v2 |
|---|---|---|---|
| 0.0185663 | -0.6727903 | -0.0185663 | -0.6727903 |
| -0.2549994 | -0.7184510 | 0.2549994 | -0.7184510 |
| -0.9667630 | 0.1765824 | 0.9667630 | 0.1765824 |
singular<-svd(A)$d
print(paste("EigenValues for X include: ",list(round(eigenValue_X, digit=2))))
## [1] "EigenValues for X include: c(26.6, 4.4)"
print(paste("EigenValues for Y include: ",list(round(eigenValue_Y, digit=2))))
## [1] "EigenValues for Y include: c(26.6, 4.4, 0)"
paste("Are Singular Vector equal to EigenVector of X:",(all.equal(eigenValue_X, singular^2)))
## [1] "Are Singular Vector equal to EigenVector of X: TRUE"
paste("Are Singular Vector equal to EigenVector of Y:",(all.equal(eigenValue_Y[1:2], singular^2)))
## [1] "Are Singular Vector equal to EigenVector of Y: TRUE"
We can see from the table that the singular vectors obtained using the SVD command are the same as the eigenvectors of X and Y. The only difference is whether they are positive or negative, which is due to them being declared in unit vector form. The ratios are otherwise consistent.
Thus, we can conclude that computed non-zero eigenvalues of X and Y are the same as the non-zero SVD values of matrix A.
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 AxB = 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. Please submit PS1 and PS2 in an R-markdown document with your first initial and last name.
myinverse <- function(A, debug=FALSE){
if (det(A)== 0) {
print("matrix is not invertible")
}
CoF <- diag(ncol<-nrow(A))
for (i in 1:nrow(A)) {
for (j in 1:ncol(A)){
CoF[i,j]=((-1)^(i+j))*det(A[-i,-j])
}
if (debug==TRUE){
print(sprintf('CoF_(%s,%s):', i, j))
print(CoF)
}
}
return(t(CoF)/det(A))
}
# T1
A <- matrix(c(1, 2, 3, 2, 1, 3, 1, 1, 3),nrow=3)
A
## [,1] [,2] [,3]
## [1,] 1 2 1
## [2,] 2 1 1
## [3,] 3 3 3
B <- myinverse(A, debug=TRUE)
## [1] "CoF_(1,3):"
## [,1] [,2] [,3]
## [1,] 0 -3 3
## [2,] 0 1 0
## [3,] 0 0 1
## [1] "CoF_(2,3):"
## [,1] [,2] [,3]
## [1,] 0 -3 3
## [2,] -3 0 3
## [3,] 0 0 1
## [1] "CoF_(3,3):"
## [,1] [,2] [,3]
## [1,] 0 -3 3
## [2,] -3 0 3
## [3,] 1 1 -3
B
## [,1] [,2] [,3]
## [1,] 0 1 -0.3333333
## [2,] 1 0 -0.3333333
## [3,] -1 -1 1.0000000
comp3 <- cbind(B, solve(A))
colnames(comp3) <- c('myI1', 'myI2', 'myI3', 'solve1', 'solve2', 'solve3')
knitr::kable(comp3)
| myI1 | myI2 | myI3 | solve1 | solve2 | solve3 |
|---|---|---|---|---|---|
| 0 | 1 | -0.3333333 | 0 | 1 | -0.3333333 |
| 1 | 0 | -0.3333333 | 1 | 0 | -0.3333333 |
| -1 | -1 | 1.0000000 | -1 | -1 | 1.0000000 |
# T2
A <- matrix(c(18, 8, 6, 8, 6, 2, 6, 2, 4), 3, byrow=TRUE)
A
## [,1] [,2] [,3]
## [1,] 18 8 6
## [2,] 8 6 2
## [3,] 6 2 4
B <- myinverse(B, debug=FALSE)
B
## [,1] [,2] [,3]
## [1,] 1 2 1
## [2,] 2 1 1
## [3,] 3 3 3
comp4 <- cbind(B, solve(A))
colnames(comp4) <- c('myI1', 'myI2', 'myI3', 'solve1', 'solve2', 'solve3')
knitr::kable(comp4)
| myI1 | myI2 | myI3 | solve1 | solve2 | solve3 |
|---|---|---|---|---|---|
| 1 | 2 | 1 | 0.25 | -0.25 | -0.25 |
| 2 | 1 | 1 | -0.25 | 0.45 | 0.15 |
| 3 | 3 | 3 | -0.25 | 0.15 | 0.55 |
A <- matrix(c(1,1,3,2,-1,5,-1,-2,4), nrow=3, ncol=3, byrow = TRUE)
print("Matrix A:")
## [1] "Matrix A:"
A
## [,1] [,2] [,3]
## [1,] 1 1 3
## [2,] 2 -1 5
## [3,] -1 -2 4
B <- myinverse(A)
print("myInverse of Matrix A:")
## [1] "myInverse of Matrix A:"
B
## [,1] [,2] [,3]
## [1,] -0.2727273 0.45454545 -0.36363636
## [2,] 0.5909091 -0.31818182 -0.04545455
## [3,] 0.2272727 -0.04545455 0.13636364
print("R-Inverse of Matrix A:")
## [1] "R-Inverse of Matrix A:"
solve(A)
## [,1] [,2] [,3]
## [1,] -0.2727273 0.45454545 -0.36363636
## [2,] 0.5909091 -0.31818182 -0.04545455
## [3,] 0.2272727 -0.04545455 0.13636364
paste("Are myInverse of Matrix A equal to R-Inverse of Matrix A:",(all.equal(B, solve(A))))
## [1] "Are myInverse of Matrix A equal to R-Inverse of Matrix A: TRUE"
As can be seen from above testing, our function is doing what it is supposed to.