The purpose of this section is to illustrate how cramer's Rule works, and how the adjoint matrix can be used to calculate a matrix's inverse.
2.3.1 Cramer's Rule:
Consider a 6 by 6 random matrix:
Warning, the protected names norm and trace have been redefined and unprotected
|
|
> |
B:=matrix(6,6,rand(-6..6)); |
|
(3.1.1) |
To make sure that B isn't singular ( i.e the rank = 6 ):
|
(3.1.2) |
Suppose that this matrix represents a linear system of 6 equations in 6 unknowns as where x is a column vector of the 6 unknowns, and b is a column vector of 6 constants. Also, make b out of random integers:
> |
b:=vector(6,rand(-6..6)); |
|
(3.1.3) |
Now, call the augmented matrix of (B,b), and apply Gauss-Jordan method:
|
(3.1.4) |
|
(3.1.5) |
One can observe that the values of x1 to x6 had been obtained.
What Cramer's Rule Says?:
Cramer's Rule says : xk = det(Ak)/det(B), where Ak is the matrix obtained from B by replacing the kth column vector with b( det: is the determinant function) . To see this:
|
(3.1.6) |
> |
A1:=augment(b,col(B,2),col(B,3),col(B,4),col(B,5),col(B,6)); |
|
(3.1.7) |
Now to compute the result ( to evaluate x1) using Cramer's Rule:
|
(3.1.8) |
This agrees with the previous answer that obtained. And , one can do the same to obtain the other variables.
Why it works?
Cramer's Rule works for any non-singular matrix, to illustrate that, let's work with 6 by 6 general matrix:
|
(3.1.9) |
|
(3.1.10) |
Let x be the unique solution of ( Ax = c), where x and c are given as follows ( general vectors ):
> |
c:=vector(6); x:=vector(6); |
|
(3.1.11) |
|
(3.1.11) |
|
(3.1.12) |
|
(3.1.12) |
" evalm " here for constructing a matrix or a vector, the vectors c and x considered as column vectors.
Now, construct the identity matrix I6:
> |
I6:=matrix(6,6,(m,n)->if (m=n) then 1 else 0 fi); |
|
(3.1.13) |
Now, define Xj to be the identity matrix with its jth column replaced by x ( for example X3 ):
> |
X3:=augment(col(I6,1),col(I6,2),x,col(I6,4),col(I6,5),col(I6,6)); |
|
(3.1.14) |
Now, think about AX3! For any column of X3 other than column 3, one can get the corresponding column of A, now see what A does to the 2nd column of X3 ( again consider the result as a column vector ):
|
(3.1.15) |
In general, AX3 will give us back A, except for the 3rd column:
|
(3.1.16) |
The 3rd column will be Ax = c, which is the same as the definition of matrix B3 used in Cramer's Rule. Then one can demonstrated that AXj = Bj. Taking the determinant of this equation, one obtains: det(A)det(Xj) =det(Bj). Finally one sees that: det(Xj) = det(Bj)/det(A). Forexample:
|
(3.1.17) |
|
(3.1.18) |
This is Cramer's rule.
One can generates a procedure to implement Cramer's rule( after defining the matrix B and the vector b ):
> |
Cramer:=proc(B::matrix,b::vector):
detB:=det(B):
if (detB<>0) then n:=rank(B):
for i from 1 to n do:
X||i:=det(augment(submatrix(B,1..n,1..i-1),b,submatrix(B,1..n,i+1..n)))/detB:
od:
RETURN(eval([seq(X||i,i=1..n)])):
else
ERROR(`System has no solution,detB=0`):
fi:
end: |
Warning, `detB` is implicitly declared local to procedure `Cramer`
|
|
Warning, `n` is implicitly declared local to procedure `Cramer`
|
|
Warning, `i` is implicitly declared local to procedure `Cramer`
|
|
|
(3.1.19) |
One can compare this result with the previous one for matrix B and vector b,can see the same result. Where the " local procedure", which includes' detB, n and i ' generates the steps of the solution of Cramer's rule. The sample <> means not equal, so in this case the Cramer's rule will work, otherwise the error message will appear " System has no solution " and the determinant of the matrix B is equal to zero.
2.3.2 The Adjoint Matrix and the Inverse:
The adjoint of a matrix is defined to be a matrix which all of its elements replaced by their corresponding cofactors, transposed, the "adjoint " command works to evaluate the adjoint of a matrix as follows:
> |
N:=matrix(6,6,rand(-6..6)); |
|
(3.1.1.1) |
|
(3.1.1.2) |
Here, for example the (1,6) element of the above matrix ( namely 570 ), should be the cofactor of the (6,1) element of N, and it is 5 steps away from the (1,1) element, which is an odd number of steps, so the sign will be negative. To check:
> |
cofN61:=-det(minor(N,6,1)); |
|
(3.1.1.3) |
The converse, for example the (2,4) element (-378 ) of the adjoint, which is a cofactor of (4,2) element of N, it is 4 steps away from (1,1) which is an even number of steps, so the sign will be positive:
> |
cofN24:=det(minor(N,4,2)); |
|
(3.1.1.4) |
Where the "minor (N,r,c)" function, where N is n by n matrix, returns the determinant of the (n-1) x (n-1) submatrix found by deleting the rth row and the cth column of N:
|
(3.1.1.5) |
How to use the adjoint matrix in computing the inverse of that matrix? That can be done in one step:
> |
evalm(adjoint(N)/det(N)); |
|
(3.1.1.6) |
Compare that with the direct " inverse " command:
|
(3.1.1.7) |
It agreed.