1. Matrix A =
A <- matrix(c(1,-1,2,0,3,4), nrow=2)
print(A)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
2. Matrix X =
X <- A %*% t(A)
print(X)
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
3. Matrix Y =
Y <- t(A) %*% A
print(Y)
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
4. Sungular Value Decomposition of A
eigenX <- eigen(X)
eigenY <- eigen(Y)
svdA <- svd(A)
print(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
5. Validate that U is same as Eigen Vector of X
eigenX$vectors[,1] <- eigenX$vectors[,1] * (-1)
round(eigenX$vectors, digits=10) == round(svdA$u, digits=10)
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
6. Validate that V is same as Eigen Vector of Y
eigenY$vectors[,1] <- eigenY$vectors[,1] * (-1)
round(eigenY$vectors[,1:2], digits=10) == round(svdA$v, digits=10)
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## [3,] TRUE TRUE
7. Validate that sigma is same as Square Root of Eigen Values of X
round(eigenX$values, digits=10) == round(svdA$d^2, digits=10)
## [1] TRUE TRUE
1. myinverse function definition
myinverse <- function(X) {
# Get Matrix Dimension
n <- dim(X)[1]
# Matrix Inversion Loop
Y <- matrix(0, n, n)
for(i in 1:n) {
for(j in 1:n) {
Y[i,j] <- (-1)^(i+j)*det(matrix(X[-i,-j], n-1))
}
}
# Calculate Inverse
Z <- t(Y)/det(X)
return(Z)
}
2. Test myinverse function
A <- matrix(c(5,3,7,2,8,9,3,5,7), nrow=3)
print(A)
## [,1] [,2] [,3]
## [1,] 5 2 3
## [2,] 3 8 5
## [3,] 7 9 7
B <- myinverse(A)
print(B)
## [,1] [,2] [,3]
## [1,] -2.75 -3.25 3.5
## [2,] -3.50 -3.50 4.0
## [3,] 7.25 7.75 -8.5
3. Validate that A.B = Identity Matrix
I = round(A %*% B, digits=10)
print(I)
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1