home *** CD-ROM | disk | FTP | other *** search
- Program MatrixMath;
-
- { -- Program Name: Matrix Math
-
- -- Filename: Matrix.PAS
-
- -- Author: Bryan J. Rice
-
- -- Date Written: March 14, 1988 }
-
- Uses Crt;
-
- Const
- MaxSize = 10;
-
- Type
- RowArray = array [1..MaxSize] of real;
- SquareMatrix = array [1..MaxSize] of RowArray;
-
- Var
- Error : boolean; { -- Holds error condition }
- Matrix : SquareMatrix; { -- Holds user input matrix }
- Order : integer; { -- Order of matrix }
-
-
- Procedure Cofactor ( ColIndex : integer;
- RowIndex : integer;
- OldOrder : integer;
- var OldMatrix : SquareMatrix;
- var CofactorMatrix : SquareMatrix);
-
- { -- Procedure Cofactor to create the cofactor matrix for the position 'row
- -- RowIndex, column ColIndex' in the OldMatrix of order OldOrder. The new
- -- matrix is returned as CofactorMatrix.
-
- -- NOTE: Although the value of OldMatrix is never changed, since the data
- -- structure containing the OldMatrix can be quite large the var parameter
- -- has been added to conserve space. Even though the amount of memory
- -- saved may seem negligible, it is always a good idea to program
- -- conservatively wherever recursive procedures and functions are concerned.}
-
- Var
- Col, Row : integer; { -- Loop control variables }
- NewCol, NewRow : integer; { -- Number of column and row in CofactorMatrix }
-
- begin
- NewRow := 0;
- for Row := 1 to OldOrder do
-
- { -- Exclude the row of the position that we want the cofactor matrix for }
- if Row <> RowIndex then
- begin
- inc(NewRow);
- NewCol := 0;
- for Col := 1 to OldOrder do
-
- { -- Exclude the column of the position we want the cofactor matrix
- -- for }
- if Col <> ColIndex then
- begin
- inc(NewCol);
-
- { -- Assign values to the cofactor matrix from the old matrix }
- CofactorMatrix[NewRow,NewCol] := OldMatrix[Row,Col]
- end { if Col <> ColIndex }
- end { if Row <> RowIndex }
- end; { Procedure Cofactor }
-
-
- Function Determinant ( Order : integer;
- Matrix : SquareMatrix) : real;
-
- { -- Function Determinant to evaluate the determinant of an n by n matrix.
- -- This recursive procedure follows the cofactor method for computing
- -- determinants, hence it works for n order square matrices with n greater
- -- than 3. The procedure is recursive in nature, calling itself to compute
- -- the determinants of its integral cofactors as needed. It calls the
- -- above procedure Cofactor to create these cofactor matrices.
-
- -- NOTE: The upper limit of system memory used by this recursive procedure
- -- is the ammount required to store N matrices of size MaxSize by MaxSize,
- -- as well as N copies of this function, where N is the order of the square
- -- matrix initially passed to the function. Accordingly, please use caution
- -- when computing the determinant of any matrices of order greater than
- -- 15 or 20. }
-
- Var
- CofactorMatrix : SquareMatrix; { -- Holds temporary cofactor matrices }
- Index : integer; { -- Loop control variable }
- PartialSum, Sum : real; { -- Holds temporary values of det Matrix }
-
- begin
- Sum := 0;
-
- { -- Third to MaxSize order determinant - must calculate recursively. }
- if Order > 2 then
- for Index := 1 to Order do
- begin
-
- { -- Build cofactor matrix for column number Index. }
- Cofactor(Index,1,Order,Matrix,CofactorMatrix);
-
- { -- Compute partial sum by recursing. }
- PartialSum := Matrix[1,Index] * Determinant(Order-1,CofactorMatrix);
-
- { -- Assign sign of partial sums using checkerboard/cofactor sign rule. }
- if not Odd(Index) then
- PartialSum := -PartialSum;
- Sum := PartialSum + Sum
- end { for Index := 1 to Order }
- else
-
- { -- Second order determinant - can calculate directly. }
- Sum := (Matrix[1,1] * Matrix[2,2]) - (Matrix[2,1] * Matrix[1,2])
- end; { Function Determinant }
-
-
- Procedure ReadMatrixData ( var Order : integer;
- var Matrix : SquareMatrix);
-
- { -- Procedure ReadMatrixData to read in the order and values of Matrix. }
-
- Var
- Ch : char; { -- Dummy variable to clear keyboard buffer }
- Col, Row : integer; { -- Loop control variables }
-
- begin
- write('Please enter the order of the matrix: ');
- readln(Order);
- if (Order >= 2) and (Order <= MaxSize) then
- for Row := 1 to Order do
- for Col := 1 to Order do
- begin
- ClrScr;
- write('Enter real value (xx.xx format) for Row ',Row,', Column ',Col,': ');
- readln(Matrix[Row,Col]);
- end { for Col := 1 to Order }
- else
-
- { -- Notify user of error conditions. }
- begin
- Error := true;
- if Order < 2 then
- writeln('Error 1 -- Cannot take determinant of ',Order,' order matrix.')
- else
- writeln('Error 2 -- Order of matrix out of range. Adjust MaxSize constant.')
- end { Range checking loop }
- end; { Procedure ReadMatrixData }
-
-
- Begin { Program MatrixMath }
- ClrScr;
- ReadMatrixData(Order,Matrix);
- ClrScr;
- writeln('The determinant of your matrix is ',Determinant(Order,Matrix));
- End. { Program MatrixMath }