A <- matrix(c(1, -1, 2, 0, 3, 4), nrow = 2, ncol = 3)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
t(A)
## [,1] [,2]
## [1,] 1 -1
## [2,] 2 0
## [3,] 3 4
Compute X = A*A^T
X <- A %*% t(A)
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
Compute Y = A^TA
Y <- t(A) %*% A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
(2)Then, compute the eigenvalues and eigenvectors of X and Y using the built-in commans in R. Calculate eigenvalues and eigenvectors of X
eig_X <- eigen(X)
eig_X
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
Calculate eigenvalues and eigenvectors of Y
eig_Y <- eigen(Y)
eig_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
(3)Then, compute the left-singular, singular values, and right-singular vectors of A using the svd command.
svd_A <- svd(A)
left_singular <- svd_A$u
left_singular
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
singular_values <- svd_A$d
singular_values
## [1] 5.157693 2.097188
right_singular <- svd_A$v
right_singular
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
(4)Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y. Compare the eigenvector of X with left_singular
eig_X
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
left_singular
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
so the left signular vector is the same of eigen vectors of x
Compare the eigenvector of Y with right_singular
eig_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
right_singular
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
so the right signular vector is the same of eigen vectors of Y
(4)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.
svd_A$d
## [1] 5.157693 2.097188
sqrt(eig_X$value)
## [1] 5.157693 2.097188
sqrt(eig_Y$values)
## [1] 5.157693e+00 2.097188e+00 1.029068e-08
myinverse <- function(A) {
C <- diag (nrow(A))
for (i in 1:nrow ( A )) {
for (j in 1:ncol( A )) {
C[i,j] <- ( det(A[(-1*i), (-1*j) ]) * ((-1)^(i+j) ) )
}
}
T <- diag(nrow(A))
for ( i in 1:nrow(A)) {
row <- C[i,]
for ( j in 1:length(row)) {
T [ j,i ] <- row[j]
}
}
inv <- 1/(det(A) ) * T
inv
}
A <- matrix(c(5, 2, 1, -1,1,1,1,2,3) , ncol = 3 , byrow = TRUE )
A
## [,1] [,2] [,3]
## [1,] 5 2 1
## [2,] -1 1 1
## [3,] 1 2 3
B <- myinverse(A)
B
## [,1] [,2] [,3]
## [1,] 0.1 -0.4 0.1
## [2,] 0.4 1.4 -0.6
## [3,] -0.3 -0.8 0.7
A %*% B
## [,1] [,2] [,3]
## [1,] 1.000000e+00 -5.551115e-16 0
## [2,] -5.551115e-17 1.000000e+00 0
## [3,] -2.220446e-16 0.000000e+00 1