# define matrix A
A <- matrix(cbind(1,2,3,-1,0,4), byrow = T, ncol = 3)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
# compute X using t() function for matrix transpose
X <- A %*% t(A)
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
# compute Y
Y <- t(A) %*% A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
eigen_X <- eigen(X)
# eigen values of X
eigen_X$values
## [1] 26.601802 4.398198
# eigen vectors of X
eigen_X$vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
eigen_Y <- eigen(Y)
# eigen values of Y
eigen_Y$values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
# eigen vectors of Y
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
# svd() function
# d -> vector containing the singular values of given matrix
# u -> matrix whose columns contain the left singular vectors of given marix
# v -> matrix whose columns contain the right singular vectors of given marix
svd_A <- svd(A)
svd_A
## $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
list(eigen_X$vectors, svd_A$u)
## [[1]]
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
##
## [[2]]
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
# comparing the left singular value svd_A$u with eigen_X
round(svd(A)$u,4) == round(eigen_X$vectors,4)
## [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE TRUE
u_rounded <- round(svd(A)$u,4)
# multiply first column of left singular by -1
u_rounded[,1] = -u_rounded[,1]
round(u_rounded,4) == round(eigen_X$vectors,4)
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
list(eigen_Y$vectors, svd_A$v)
## [[1]]
## [,1] [,2] [,3]
## [1,] -0.01856629 -0.6727903 0.7396003
## [2,] 0.25499937 -0.7184510 -0.6471502
## [3,] 0.96676296 0.1765824 0.1849001
##
## [[2]]
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
# comparing the left singular value svd_A$u with eigen_Y
round(svd(A)$v,4) == round(eigen_Y$vectors[,-3],4)
## [,1] [,2]
## [1,] FALSE TRUE
## [2,] FALSE TRUE
## [3,] FALSE TRUE
v_rounded <- round(svd(A)$v,4)
# multiply first column of right singular by -1
v_rounded[,1] = -v_rounded[,1]
# compare values
round(v_rounded,4) == round(eigen_Y$vectors[,-3],4)
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [3,] TRUE TRUE
round(svd_A$d^2,4)
## [1] 26.6018 4.3982
round(eigen_X$values,4)
## [1] 26.6018 4.3982
# using format function to not display eigen_Y$values in scientific notation
eigen_Y$values <- format(eigen_Y$values, scientific=F)
# convert the values from string to numeric values and then round off
round(as.numeric(eigen_Y$values), 4)
## [1] 26.6018 4.3982 0.0000
B = myinverse(A)
myinverse <- function(A) {
# get rows and columns
rows <- nrow(A)
cols <- ncol(A)
# check if given matrix is a square matrix and full rank
if(rows!=cols | qr(A)$rank != dim(A)[1]) {
return("Cant move forward. A should be square and full rank matrix.")
} else {
# create an empty co-factors matrix
cofactors <- matrix(rep(0, length(A)), nrow = rows, ncol = cols)
for(i in 1:rows) {
for(j in 1:cols) {
# M is a matrix with row i and col j excluded
M <- A[-i,-j]
# populate co-factors matrix
cofactors[i,j] <- (-1)^(i+j) * det(M)
}
}
# return inverse of matrix
B <- t(cofactors)/det(A)
return(B)
}
}
A <- matrix(cbind(1,2,3,4,5,6), nrow = 2, byrow = T)
myinverse(A)
## [1] "Cant move forward. A should be square and full rank matrix."
A = matrix(c(1,8,3,-1,9,4,6,7,9), nrow = 3, byrow = T)
B <- myinverse(A)
B
## [,1] [,2] [,3]
## [1,] 0.3955224 -0.38059701 0.03731343
## [2,] 0.2462687 -0.06716418 -0.05223881
## [3,] -0.4552239 0.30597015 0.12686567
round(A %*% B, 4)
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1