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 <- matrix(c(1,2,3, -1, 0, 4), nrow = 2, ncol = 3, byrow = TRUE)
\[\mathbf{A} = \left[\begin{array}
{rrr}
1 & 2 & 3\\
-1 & 0 & 4\\
\end{array}\right]
\]
Computing X & Y
#Show X
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
#Show Y
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
eigen(X)$values
## [1] 26.601802 4.398198
eigen(X)$vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
eigen(Y)$values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
eigen(Y)$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
As we can see that the first 2 eigen values of X and Y are the same. Lets compute the singular values of A
S <- diag(c(eigen(X)$values[1], eigen(X)$values[2]), nrow = 2, ncol = 3)
S
## [,1] [,2] [,3]
## [1,] 26.6018 0.000000 0
## [2,] 0.0000 4.398198 0
The singular Value decomposition function is R is denoted by svd function and we decompose the matrix A
d <- svd(A)$d
d
## [1] 5.157693 2.097188
Left & Right Singular Vectors are denoted by u & v respectively
u <- svd(A)$u
v <- svd(A)$v
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
A comparision of eigen vectors of X with the left singular matrix u will reveal that u is composed of eigen vectors of X.
x1 <- eigen(X)$vectors[,1]
x2 <- eigen(X)$vectors[,2]
u1 <- u[,1]
u2 <- u[,2]
round(x1 - (-1*u1),12)
## [1] 0 0
round(x2 - u2, 12)
## [1] 0 0
From the result of the operations, it is clear that first column vector in U (u1) is -1 * first eigenvector of X.
comparing Y with right singular vector
y1 <- eigen(Y)$vectors[,1]
y2 <- eigen(Y)$vectors[,2]
v1 <- v[,1]
v2 <- v[,2]
round(y1 - (-1*v1), 12)
## [1] 0 0 0
round(y2 - v2,12)
## [1] 0 0 0
From the result of the operations, we can be concluded that the first column vector in V (v1) is -1 * first eigenvector of Y and the 2nd column vector in V (v2) is equal to the 2nd eigenvector of Y.
C <- matrix(c(1,2,4,2,-1,3,4,0,1), nrow=3, ncol=3, byrow = TRUE)
solve(C)
## [,1] [,2] [,3]
## [1,] -0.02857143 -0.05714286 0.2857143
## [2,] 0.28571429 -0.42857143 0.1428571
## [3,] 0.11428571 0.22857143 -0.1428571
invfun <- function(A){
# Identify dimension of A
dima <- nrow(A)
# Create an empty matrix with dimension of A
C <- matrix(data=NA,nrow=dima,ncol=dima)
for (i in 1:dima){
for (j in 1:dima){
Aij <-A[-i,-j]
C[i,j] <- det(Aij)
}
}
# determinant of A and store it in d_A
detA <- det(A)
# Calculate inverse of A as 1/det(A)*t(C), denoted inv_A
if (detA != 0){
inva <- t(C)/detA
}
return(inva)
}
invfun(C)
## [,1] [,2] [,3]
## [1,] -0.02857143 0.05714286 0.2857143
## [2,] -0.28571429 -0.42857143 -0.1428571
## [3,] 0.11428571 -0.22857143 -0.1428571