Reputation: 57
I want to find the value of inversed matrix without using a function solve(x)
.
inversed matrix = 1/det(x) * "cofactor"^(transpose)
I know that a determinant of matrix = det(x)
and a transpose of matrix = t(x)
. But I cannot find out how to get cofactor of matrix x and get inversed matrix.
Upvotes: 1
Views: 1811
Reputation: 121137
The (i,j)th minor of a matrix is that matrix with the ith row and the jth column removed.
minor <- function(A, i, j)
{
A[-i, -j]
}
The (i,j)th cofactor is the (i,j)th minor times -1 to the power i + j.
cofactor <- function(A, i, j)
{
-1 ^ (i + j) * minor(A, i, j)
}
A word of warning: you shouldn't use this method of calculating inverses of matrices, except for homework purposes, because it is computationally intensive, and (if I recall correctly) not very numerically stable. For real-world purposes use solve
or qr.solve
or chol2inv
. Compare these:
A <- matrix(c(5,1,1,3),2,2)
solve(A)
qr.solve(A)
chol2inv(chol(A))
Upvotes: 4
Reputation: 34677
If you have a precomputed cholesky decomposition, chol2inv
will sort you.
Upvotes: 1