Lindo Systems

! Illustration of the @INVERSE() function.(InvertExmpl.lng)
If we need the solution to the matrix equation A*X = RHS
for several different RHS, then it is
efficient to first compute the inverse of A, AINV and then 
use the result that X = AINV*RHS;
!Keywords: Matix inverse, Inverse of matrix,
   Equation solving, Matrix multiply, Simultaneous equations;

SETS:
 DIM;
 DXD( DIM, DIM): A, AINV, RESULT1;
 CASE;
 DXC( DIM, CASE): RHS, RESULT2;
ENDSETS
DATA:
 DIM = 1..4;

!Example 1;
! A=
  5  7  3 -1
  1 -2  3  4
  1  2  4  5
  9  3 -4  7;

!Example 2;
! A permutation matrix.
 A(i,j) = 1 means move element in position i to position j;
 A=
  0  1  0  0
  0  0  1  0
  1  0  0  0
  0  0  0  1;
    
 CASE = 1..3; ! Different RHS cases;
 RHS =  14  1  2 
         6  2  4
        12  3  6
        15  4  8;
ENDDATA

CALC:
 @SET( 'TERSEO',2);    ! Output level (0:verb, 1:terse, 2:only errors, 3:none);

 AINV, err = @INVERSE( A); ! Compute inverse of matrix A;

 @WRITE(' The error code is: ', err, @NEWLINE(1));
 @WRITE(' The inverse is: ', @NEWLINE(1));
 @TABLE( AINV);

 @WRITE( @NEWLINE(1),' AINV * A =',@NEWLINE(1));

 RESULT1 = @MTXMUL( AINV, A);  !Matrix multiply;
 @TABLE( RESULT1);
 @WRITE( @NEWLINE(1)); 

 @WRITE( @NEWLINE(1),' AINV * RHS =',@NEWLINE(1));

 RESULT2 = @MTXMUL( AINV, RHS); !Matrix multiply;
 @TABLE( RESULT2);
 @WRITE( @NEWLINE(1));
ENDCALC