Linear Algebra Powertool
Finding the Inverse of a Matrix
Worksheet by Russell Blyth
> with(linalg):
Warning, the protected names norm and trace have been redefined and unprotected
Enter matrix to be inverted
> A:=matrix([[1,4,-1],[-2,0,3],[3,6,1]]);
>
Augment the matrix by appending the identity
> K:=augment(A,[1,0,0],[0,1,0],[0,0,1]);
>
Reduce the augmented matrix to reduced row echelon form
> K:=rref(K);
>
If the n x n identity appears in the first n columns of the matrix, the last n x n columns is the inverse; extract it using the submatrix command
> AINV:=submatrix(K,1..3,4..6);
>
Check the result:
> evalm(A &* AINV);
> evalm(AINV &* A);
>
The linalg package has a built-in inverse command
> inverse(A);
>
If the matrix has no inverse, the procedure above will not give the nx n identity in the first n x n columns of K. Here is how the inverse function behaves:
> B:=matrix([[2,4],[4,8]]);
>
> inverse(B);
Error, (in inverse) singular matrix
The linalg package includes a routine to generate random matrices with integer entries, for testing purposes. Let's use it to generate matrices and inverses.
> C:=randmatrix(4,4);
>
> inverse(C);
>
By default, the entries of the randomly generated matrix fall between -99 and 99. An option can be added to specify the allowed range.
> E:=randmatrix(4,4,entries=rand(-5..5));
>
(The variable name D is protected, and is used for the differential operator)
> D(x^2);
> inverse(E);
>
>