\[ A=\begin{bmatrix} 1 & 2 & 3 \\ -1 & 0 & 4 \end{bmatrix} \]
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
## [,1] [,2]
## [1,] 0.6576 -0.7534
## [2,] 0.7534 0.6576
## [,1] [,2] [,3]
## [1,] -0.0186 -0.6728 0.7396
## [2,] 0.2550 -0.7185 -0.6472
## [3,] 0.9668 0.1766 0.1849
## $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
## [1] 26.6018 4.3982
## [1] 26.6018 4.3982
## [1] 26.6018 4.3982 0.0000
## [1] 26.6018 4.3982 0.0000
## [1] 26.601802 4.398198
A function to compute the inverse of a well-conditioned full-rank square matrix using co-factors.
myinverse <- function(A){
matrix_of_cofactors <- matrix(NA, nrow = nrow(A), ncol = ncol(A)) # Initialize cofactors
for (i in c(1:nrow(A))){ # outer loop
for (j in c(1:ncol(A))){ # inner loop
matrix_of_cofactors[i, j] <- det(A[-i, -j]) * (-1) ^ (i + j) # Calc determinant of the matrix
}
}
adjugate <- t(matrix_of_cofactors) # Transpose elements of the previous matrix of cofactors
inverse <- adjugate / det(A) # Divide ajugate by the determinant of the first matrix
return(inverse)
}The function works by computing the co-factor matrix using : \(C_{ij} = (-1)^{i+j} det(M_{ij})\). The inverse matrix is then calculated using \(A^{-1}=\frac{C^T}{det(A)}\).
Let \(A=\begin{bmatrix} 1 & 6 & 7 \\2 & 5 & 8 \\ 3 & 6 & 9\end{bmatrix}\).
A <- matrix(c(1,2,3,6,5,6,7,8,9), nrow=3)
# Calculate inverse using 'myinverse' function
B <- myinverse(A)
B## [,1] [,2] [,3]
## [1,] -0.25 -1 1.0833333
## [2,] 0.50 -1 0.5000000
## [3,] -0.25 1 -0.5833333
## [,1] [,2] [,3]
## [1,] -0.25 -1 1.0833333
## [2,] 0.50 -1 0.5000000
## [3,] -0.25 1 -0.5833333
## [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1