Load the packages
suppressWarnings(suppressMessages(library(Matrix))) # eigen and rankMatrix functions will be used
suppressWarnings(suppressMessages(library(pracma))) #charpoly function will be used
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{equation} A = \Bigg[ \begin{matrix} 1&2&3\\ 1&0&4 \end{matrix} \Bigg] \end{equation}\]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)
X <- A %*% t(A)
Y <- t(A) %*% A
# calculate the eigenvalues for X and Y
eigen_x <- eigen(X)
eigenvalue_x <- eigen_x$values
print(paste("eigenvalues for X include: ",list(round(eigenvalue_x,digit=2))))
## [1] "eigenvalues for X include: c(28.59, 2.41)"
eigen_y <- eigen(Y)
eigenvalue_y <- eigen_y$values
print(paste("eigenvalues for Y include: ",list(round(eigenvalue_y,digit=2))))
## [1] "eigenvalues for Y include: c(28.59, 2.41, 0)"
# calculate the eigenvectors for X and Y
I2 <- matrix(c(1,0,0,1),2, byrow=T)
I3 <- matrix(c(1,0,0,0,1,0,0,0,1),3, byrow=T)
(nullspace(X- eigenvalue_x[1]*I2)) # eigenvectors for first eigenvalue of X
## [,1]
## [1,] 0.6653480
## [2,] 0.7465334
(nullspace(X- eigenvalue_x[2]*I2)) # eigenvectors for second eigenvalue of X
## [,1]
## [1,] -0.7465334
## [2,] 0.6653480
(nullspace(Y- eigenvalue_y[1]*I3)) # eigenvectors for first eigenvalue of Y
## [,1]
## [1,] 0.2640703
## [2,] 0.2488859
## [3,] 0.9318383
(nullspace(Y- eigenvalue_y[2]*I3)) # eigenvectors for second eigenvalue of Y
## [,1]
## [1,] -0.05225548
## [2,] -0.96102189
## [3,] 0.27148903
svd_A <- svd(A)
(svd_A$u)
## [,1] [,2]
## [1,] -0.6653480 -0.7465334
## [2,] -0.7465334 0.6653480
(svd_A$d)
## [1] 5.346611 1.553624
(svd_A$v)
## [,1] [,2]
## [1,] -0.2640703 -0.05225548
## [2,] -0.2488859 -0.96102189
## [3,] -0.9318383 0.27148903
Conclusion: S vector is the eigenvectors of \(A^TA\) except the first column is (-1) multiply the first eigenvector of \(AA^T\). D vector is the eigenvectors of \(A^TA\) except the first column is (-1) multiply the first eigenvector of. eigenvalues of \(AA^T\) and \(A^TA\) are the same. The diagonal values of V is the squareroot of the eigenvalues.
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 x 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.
Please submit PS1 and PS2 in an R-markdown document with your first initial and last name.
The minors matrix, cofactors adn adjugate will be used to calculate inverse.
# Step 1: Matrix of Minors
minor_matrix <- function(x){
size <- dim(x)
minors <- matrix(rep(0,(size[1]*size[2])), size[1], byrow=T)
for(i in 1:nrow(x)){
temp_1 <- x[-i,]
for(j in 1:ncol(x)){
temp_2 <- temp_1[,-j]
minors[i,j] <- det(temp_2)
}
}
return(minors)
}
# Step 2: Matrix of Cofactors
# denote confactor as Cij. If i+j is even, then Cij = Mij. If i+j is odd, then Cij = - Mij
confactor_matrix <- function(x){
for(i in 1:nrow(x)){
for(j in 1:ncol(x)){
x[i,j] <-x[i,j]*((-1)^(i+j))
}
}
return(x)
}
fst_row_det <- function (x){
size <- dim(x)
minors <- minor_matrix(x)
detrm <- 0
for (j in 1:size[2]){
detrm <- detrm + (-1)^(1+j)*x[1,j]*minors[1,j]
}
return(detrm)
}
# Step 3: Adjugate (also called Adjoint):Transpose matrix of Minors
myinverse <- function(x){
# denote confactor as Cij. If i+j is even, then Cij = Mij. If i+j is odd, then Cij = - Mij
minors <- minor_matrix(x)
confactors <- confactor_matrix(minors)
adjugate <- t(confactors)
first_row_determinant <- fst_row_det(x)
#Transpose matrix of Minors
# Step 4: Multiply by 1/Determinant, then calculate inverse matrix.
inverse <- (1/det(x))*adjugate
}
(A <- matrix(c(3,0,2,2,0,-2,0,1,1),3,byrow=T))
## [,1] [,2] [,3]
## [1,] 3 0 2
## [2,] 2 0 -2
## [3,] 0 1 1
(B <- myinverse(A))
## [,1] [,2] [,3]
## [1,] 0.2 0.2 0
## [2,] -0.2 0.3 1
## [3,] 0.2 -0.3 0
round(A%*%B)
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1