HTBasic Help
×
Menu
Index

Matrix Inversion

 
INV calculate the inverse of a matrix and is one of the more complex matrix functions. Several precautions are in order when using the INV function.
 
The inverse of a matrix A is defined to be that matrix B, such that AB = BA = I where I is the identity matrix. For any matrix A, there is no guarantee that a matrix B exists such that the above relationship can be satisfied. When this is the case, A is called a singular matrix and has a determinant value of zero and the values assigned to the result matrix are meaningless.. After using the INV() the DET function will be set and should be tested to determine whether the matrix was singular or non-singular. In the case of a non-singular matrix, it is faster to do the inversion first (which automatically does a DET) and then check the DET results. The example below shows both methods:
 
MAT B=INV(A)                             ! invert first is the fast way
IF NOT DET THEN PRINT "A is singular"
 
IF NOT DET(A) THEN PRINT "A is singular" ! DET first is slower
MAT B=INV(A)
 
Another caution is when the determinant is very near zero compared to the other elements of the matrix because the inexact arithmetic used by a computer causes errors in the calculation of the inverse. The closer to zero, the more error is introduced into the result. To test for this condition, multiply the original matrix and its inverse together and compare the result to the identity matrix. If the difference is greater than what is acceptable for your application, then you will not be able to use the results.
 
The following example illustrates a matrix whose determinant is small compared to the elements of the matrix.
 
REM SMALLDET.BAS
DATA 100,200,100.000000000001,200
DIM A(1,1),B(1,1),Ab(1,1)
READ A(*)
MAT B=INV(A)
MAT Ab=A*B
D2x2: IMAGE K,/,2(2(SD.15DE,2X),/)
PRINT USING D2x2;"A=",A(*)
PRINT USING D2x2;"B=",B(*)
PRINT USING D2x2;"Ab=",Ab(*)
PRINT "DET = ";DET
END
 
The output from this program is shown below. Although the product AB is not exactly the identity matrix, it is close enough for many applications.
 
A=
+1.000000000000000E+02  +2.000000000000000E+02
+1.000000000000010E+02  +2.000000000000000E+02
 
B=
-1.005267773966630E+12  +1.005267773966630E+12
+5.026338869833190E+11  -5.026338869833140E+11
 
AB=
+9.956054687500000E-01  +5.371093750000000E-03
-4.394531250000000E-03  +1.005371093750000E+00
 
DET = -1.98951966013E-10