A <- matrix(c(1,2,3,-1,0,4),2,3,byrow = TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
X <- A%*%t(A)
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
Y <- t(A)%*%A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
EVX <- eigen(X, symmetric = TRUE)
EVX
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
eigenvaluesX <- EVX$values
eigenvectorsX <- EVX$vectors
EVY <- eigen(Y, symmetric = TRUE)
EVY
## 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
eigenvaluesY <- EVY$values
eigenvectorsY <- EVY$vectors[,c(1,2)]
eigenvectorsY
## [,1] [,2]
## [1,] -0.01856629 -0.6727903
## [2,] 0.25499937 -0.7184510
## [3,] 0.96676296 0.1765824
svdA <- svd(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
compXtoLS <- eigenvectorsX%/%svdA$u
compXtoLS
## [,1] [,2]
## [1,] -1 1
## [2,] -1 1
compYtoRS <- matrix(rep(0),3,2)
for (i in c(1,2,3)) {
compYtoRS[i,1] <- eigenvectorsY[i,1]/svdA$v[i,1]
compYtoRS[i,2] <- eigenvectorsY[i,2]/svdA$v[i,2]
}
compYtoRS
## [,1] [,2]
## [1,] -1 1
## [2,] -1 1
## [3,] -1 1
myinverse <- function(A){
n <- dim(A)[1]
CoMatrix <- matrix(rep(0),n,n)
for (i in c(1:n)) {
for (j in c(1:n)) {
CoMatrix[i,j] <-det(A[-i,-j])*(-1)**(i+j)
}
}
return((1/det(A)*t(CoMatrix)))
}
MA <- matrix(c(1,2,3,-3,5,-8,4,5,4,2,1,4,-6,8,3,1),4,4, byrow = TRUE)
MA
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 -3
## [2,] 5 -8 4 5
## [3,] 4 2 1 4
## [4,] -6 8 3 1
myinverse(MA)
## [,1] [,2] [,3] [,4]
## [1,] 0.09149723 -0.03419593 0.13401109 -0.09057301
## [2,] 0.03558226 -0.06885397 0.10767098 0.02033272
## [3,] 0.13585952 0.10073937 -0.04343808 0.07763401
## [4,] -0.14325323 0.04343808 0.07301294 0.06099815
Id <- myinverse(MA)%*%MA
Id
## [,1] [,2] [,3] [,4]
## [1,] 1.000000e+00 -1.110223e-16 -1.110223e-16 2.359224e-16
## [2,] -1.387779e-16 1.000000e+00 1.387779e-17 -1.526557e-16
## [3,] -1.110223e-16 -1.110223e-16 1.000000e+00 -1.387779e-17
## [4,] 0.000000e+00 -1.110223e-16 -1.665335e-16 1.000000e+00