home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / DETERM.ZIP / MATRIX.PAS
Encoding:
Pascal/Delphi Source File  |  1988-03-14  |  5.5 KB  |  157 lines

  1. Program MatrixMath;
  2.  
  3. { -- Program Name:   Matrix Math
  4.  
  5.   -- Filename:       Matrix.PAS
  6.  
  7.   -- Author:         Bryan J. Rice
  8.  
  9.   -- Date Written:   March 14, 1988     }
  10.  
  11. Uses Crt;
  12.  
  13. Const
  14.    MaxSize = 10;
  15.  
  16. Type
  17.    RowArray     = array [1..MaxSize] of real;
  18.    SquareMatrix = array [1..MaxSize] of RowArray;
  19.  
  20. Var
  21.    Error  : boolean;       { -- Holds error condition   }
  22.    Matrix : SquareMatrix;  { -- Holds user input matrix }
  23.    Order  : integer;       { -- Order of matrix         }
  24.  
  25.  
  26. Procedure Cofactor (     ColIndex       : integer;
  27.                          RowIndex       : integer;
  28.                          OldOrder       : integer;
  29.                      var OldMatrix      : SquareMatrix;
  30.                      var CofactorMatrix : SquareMatrix);
  31.  
  32. { -- Procedure Cofactor to create the cofactor matrix for the position 'row
  33.   -- RowIndex, column ColIndex' in the OldMatrix of order OldOrder.  The new
  34.   -- matrix is returned as CofactorMatrix.
  35.  
  36.   -- NOTE:  Although the value of OldMatrix is never changed, since the data
  37.   -- structure containing the OldMatrix can be quite large the var parameter
  38.   -- has been added to conserve space.  Even though the amount of memory
  39.   -- saved may seem negligible, it is always a good idea to program
  40.   -- conservatively wherever recursive procedures and functions are concerned.}
  41.  
  42. Var
  43.    Col, Row       : integer;  { -- Loop control variables                     }
  44.    NewCol, NewRow : integer;  { -- Number of column and row in CofactorMatrix }
  45.  
  46. begin
  47. NewRow := 0;
  48. for Row := 1 to OldOrder do
  49.  
  50.    { -- Exclude the row of the position that we want the cofactor matrix for  }
  51.    if Row <> RowIndex then
  52.       begin
  53.       inc(NewRow);
  54.       NewCol := 0;
  55.       for Col := 1 to OldOrder do
  56.  
  57.          { -- Exclude the column of the position we want the cofactor matrix
  58.            -- for                                                             }
  59.          if Col <> ColIndex then
  60.             begin
  61.             inc(NewCol);
  62.  
  63.             { -- Assign values to the cofactor matrix from the old matrix     }
  64.             CofactorMatrix[NewRow,NewCol] := OldMatrix[Row,Col]
  65.             end  { if Col <> ColIndex }
  66.       end  { if Row <> RowIndex }
  67. end;  { Procedure Cofactor }
  68.  
  69.  
  70. Function Determinant ( Order  : integer;
  71.                        Matrix : SquareMatrix) : real;
  72.  
  73. { -- Function Determinant to evaluate the determinant of an n by n matrix.
  74.   -- This recursive procedure follows the cofactor method for computing
  75.   -- determinants, hence it works for n order square matrices with n greater
  76.   -- than 3.  The procedure is recursive in nature, calling itself to compute
  77.   -- the determinants of its integral cofactors as needed.  It calls the
  78.   -- above procedure Cofactor to create these cofactor matrices.
  79.  
  80.   -- NOTE: The upper limit of system memory used by this recursive procedure
  81.   -- is the ammount required to store N matrices of size MaxSize by MaxSize,
  82.   -- as well as N copies of this function, where N is the order of the square
  83.   -- matrix initially passed to the function.  Accordingly, please use caution
  84.   -- when computing the determinant of any matrices of order greater than
  85.   -- 15 or 20.                                                                }
  86.  
  87. Var
  88.    CofactorMatrix  : SquareMatrix;  { -- Holds temporary cofactor matrices    }
  89.    Index           : integer;       { -- Loop control variable                }
  90.    PartialSum, Sum : real;          { -- Holds temporary values of det Matrix }
  91.  
  92. begin
  93. Sum := 0;
  94.  
  95. { -- Third to MaxSize order determinant - must calculate recursively. }
  96. if Order > 2 then
  97.    for Index := 1 to Order do
  98.       begin
  99.  
  100.       { -- Build cofactor matrix for column number Index. }
  101.       Cofactor(Index,1,Order,Matrix,CofactorMatrix);
  102.  
  103.       { -- Compute partial sum by recursing. }
  104.       PartialSum := Matrix[1,Index] * Determinant(Order-1,CofactorMatrix);
  105.  
  106.       { -- Assign sign of partial sums using checkerboard/cofactor sign rule. }
  107.       if not Odd(Index) then
  108.          PartialSum := -PartialSum;
  109.       Sum := PartialSum + Sum
  110.       end  { for Index := 1 to Order }
  111. else
  112.  
  113.    { -- Second order determinant - can calculate directly. }
  114.    Sum := (Matrix[1,1] * Matrix[2,2]) - (Matrix[2,1] * Matrix[1,2])
  115. end;  { Function Determinant }
  116.  
  117.  
  118. Procedure ReadMatrixData ( var Order  : integer;
  119.                            var Matrix : SquareMatrix);
  120.  
  121. { -- Procedure ReadMatrixData to read in the order and values of Matrix. }
  122.  
  123. Var
  124.    Ch       : char;     { -- Dummy variable to clear keyboard buffer }
  125.    Col, Row : integer;  { -- Loop control variables                  }
  126.  
  127. begin
  128. write('Please enter the order of the matrix: ');
  129. readln(Order);
  130. if (Order >= 2) and (Order <= MaxSize) then
  131.    for Row := 1 to Order do
  132.       for Col := 1 to Order do
  133.          begin
  134.          ClrScr;
  135.          write('Enter real value (xx.xx format) for Row ',Row,', Column ',Col,': ');
  136.          readln(Matrix[Row,Col]);
  137.          end  { for Col := 1 to Order }
  138. else
  139.  
  140.    { -- Notify user of error conditions. }
  141.    begin
  142.    Error := true;
  143.    if Order < 2 then
  144.       writeln('Error 1 -- Cannot take determinant of ',Order,' order matrix.')
  145.    else
  146.       writeln('Error 2 -- Order of matrix out of range.  Adjust MaxSize constant.')
  147.    end  { Range checking loop }
  148. end;  { Procedure ReadMatrixData }
  149.  
  150.  
  151. Begin  { Program MatrixMath }
  152. ClrScr;
  153. ReadMatrixData(Order,Matrix);
  154. ClrScr;
  155. writeln('The determinant of your matrix is ',Determinant(Order,Matrix));
  156. End.   { Program MatrixMath }
  157.