A <- matrix(c(1,2,3,-1,0,4), nrow = 2, ncol = 3, byrow = TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
A_t = t(A)
A_t
## [,1] [,2]
## [1,] 1 -1
## [2,] 2 0
## [3,] 3 4
Computing X and Y
X = A%*%A_t
Y = A_t%*%A
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
Now we get the Eigenvalues and Eigenvectors of X
X_eigen = eigen(X)
X_eigen
## eigen() decomposition
## $values
## [1] 26.601802 4.398198
##
## $vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
X_eigenvalue = X_eigen$values
X_eigevector = X_eigen$vectors
Y_eigen = eigen(Y)
Y_eigen
## 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
Y_eigenvalue = Y_eigen$values
Y_eigevector = Y_eigen$vectors
Using SVD function to calculate the left singular values of A
A_left_svd = svd(A, nu = 2, nv = 0)
A_left_svd$u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
By comparing the result, we found the column 1 differs by a negative, otherwise the same.
A_left_svd$u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
X_eigevector
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
Using SVD function to calculate the right singular values of A
A_right_svd = svd(A, nu = 0, nv = 3)
A_right_svd$v
## [,1] [,2] [,3]
## [1,] 0.01856629 -0.6727903 -0.7396003
## [2,] -0.25499937 -0.7184510 0.6471502
## [3,] -0.96676296 0.1765824 -0.1849001
By comparing the result, we found the column 1,3 differs by a negative, otherwise the same. This also means that the singular value vectors form an orthonormal set like the eigenvectors.
A_right_svd$v
## [,1] [,2] [,3]
## [1,] 0.01856629 -0.6727903 -0.7396003
## [2,] -0.25499937 -0.7184510 0.6471502
## [3,] -0.96676296 0.1765824 -0.1849001
Y_eigevector
## [,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 for eigenvalues, you will find the values are the same.
(A_right_svd$d)^2
## [1] 26.601802 4.398198
(A_left_svd$d)^2
## [1] 26.601802 4.398198
X_eigenvalue
## [1] 26.601802 4.398198
Y_eigenvalue
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
inverse_matrix <- function(A){
#Create identiy matrix with rows and columns equal to A
C <- diag(1,nrow(A),ncol(A))
for (i in 1:nrow(A)) {
for (j in 1:ncol(A)){
#Calucate the value for each element
Pij <- A[-i,-j]
C[i,j] <- ((-1)^(i+j))*det(Pij)
}
}
#Use the transpose of the created matrix C and the determinant of the input matrix A to get the inverse
return(t(C)/det(A))
}
Sample
A <- matrix(sample(0:9, 25, replace = TRUE), nrow=5, ncol=5)
inverse_matrix(A)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.5585106 0.02659574 -2.6329787 -0.9813830 3.4946809
## [2,] -1.1223404 -0.10106383 6.0053191 2.5292553 -8.0797872
## [3,] -0.3244681 0.07978723 1.1010638 0.5558511 -1.5159574
## [4,] 0.3404255 0.06382979 -1.3191489 -0.7553191 1.7872340
## [5,] -0.2287234 -0.05851064 0.7925532 0.3590426 -0.8882979