library(matlib)
library(MASS)
X = AAT
Y = ATA
A <- matrix(c(1, 2, 3, -1,0,4), nrow = 2, ncol = 3, byrow=T)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
X <- A%*%t(A) #AAT
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
Y <- t(A)%*%A #ATA
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
Y.e <- eigen(Y) #eigen of ATA
values.mat <-Y.e$values
values.mat #this will show that the last eignevalue is virtually zero
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
vector.mat <- Y.e$vectors #eignen vectors of ATA
#vector.mat[,1:2] <- vector.mat[,1:2]*-1
vector.moat <- vector.mat[,1:2]
vector.moat
## [,1] [,2]
## [1,] -0.01856629 -0.6727903
## [2,] 0.25499937 -0.7184510
## [3,] 0.96676296 0.1765824
X.e <- eigen(X) #eigen of AAT
uvalues.mat <- X.e$values #eigen values of AAT
uvector.mat <- X.e$vectors #eigen vectors of AAT
uvector.mat
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
r <- sqrt(Y.e$values)
r <- r*diag(length(r))[,1:2]
r <-r[1:2,]
r
## [,1] [,2]
## [1,] 5.157693 0.000000
## [2,] 0.000000 2.097188
The manual step by step process aligns with the output of the svd() function below.
We can also show that the matrix A is indeed equal to the components resulting from singular value decomposition.
svd.matrix <- uvector.mat %*% r %*% t(vector.moat)
svd.matrix # this results the same as the original A matrix we began with
## [,1] [,2] [,3]
## [1,] 1 2.000000e+00 3
## [2,] -1 -2.220446e-15 4
A.svd <- svd(A)
A.svd
## $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
myinverse <- function(A1,n){
# Pre-populate nxn matrix with zeros
nrow <- n
ncol <- n
#create place-holder C matrix of zeros
C <- matrix(0, nrow = n, ncol = n)
i <-1
#j<-1
while (i < n+1){
for (j in 1:n) {
C[i,j]<- cofactor(A1, i,j)
}
i<-i+1
}
#Matrix of Co-factors of original Matrix A
C
#transpose of Cofactor Matrix
print("Cofactors of Matrix A :")
Ctranspose <- t(C)
print(Ctranspose)
#Determinant of original Matrix A
print("The Determinant of Matrix A :")
Det_A1 <- det(A1)
print(Det_A1)
#Inverse of original Matrix A
print("The Inverse of A is :")
A_Inv <- Ctranspose/Det_A1
print(A_Inv)
print("To verify that AxA(inverse) = Identity :")
I <- A1 %*% A_Inv
return (I)
}
n<-3
A1 <- matrix(c(1,2,3,0,1,4,5,6,0),nrow=3,ncol=3, byrow=T)
print("Original Matrix of A :")
## [1] "Original Matrix of A :"
A1
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 0 1 4
## [3,] 5 6 0
myinverse(A1,n)
## [1] "Cofactors of Matrix A :"
## [,1] [,2] [,3]
## [1,] -24 18 5
## [2,] 20 -15 -4
## [3,] -5 4 1
## [1] "The Determinant of Matrix A :"
## [1] 1
## [1] "The Inverse of A is :"
## [,1] [,2] [,3]
## [1,] -24 18 5
## [2,] 20 -15 -4
## [3,] -5 4 1
## [1] "To verify that AxA(inverse) = Identity :"
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1