Home  Contact Us  Orders: US  Orders: other countries   Review Shopping Cart

Serious mathematics, written with the reader in mind.
[MsE] Matrix Editions

 

 

Determinant Program

This program requires a Pascal compiler. It computes the determinant of any square matrix of size at most 10 x 10, using development by the first column.


Program determinant;
 
Const  maxsize = 10;
 
Type   matrix  = record
 size:integer;
 coeffs: array [1..maxsize, 1..maxsize] of real;
  end;
 
 submatrix  =  record
  size:  integer;
  rows,cols: array[1..maxsize] of  integer;
 end;
 Var M: Matrix; 
 S: submatrix; 
 d: real; 

 Function det(S:submatrix):real; 
 Var tempdet: real; 
 i,sign: integer
 S1: submatrix; 

Procedure erase(S:submatrix; i,j: integer; var S1:submatrix); 
 Var k:integer;
 begin {erase}
 S1.size: = S.size-1;  
 for k := i-1   downto  1  do S1.cols[k]: = S.cols[k];
 for k := S.size-1   downto  j  do   S1.rows[k]: = S.rows[k+1];
 for k := -1   downto  1   do  S1.rows[k]:=S.rows[k];r
 end;

 
 
begin {function det}
 If S.size = 1  then det := M.coeffs[S.rows[1],S.col[1]]
 else begin
tempdet := 0; sign := 1;
for i := 1 to S.size  do
begin
erase(S,i,l,S1)
tempdet := tempdet + sign*M.coeffs[S.rows[1],S.cols[i]]*det(S1);
 sign := -sign;
 end;
 det = tempdet;
   end;
end;

begin{function det}
  If S.size = 1  then det := M.coeffs[S.rows[1],S.col[1]]
 else begin
  tempdet := 0; sign := 1;
  for i : 1 to S.size  do
  begin
  erase(S,i,1,S1); 
 tempdet := tempdet + sign*M.coeffs[S.rows[1],S.cols[i]]*det(S1);
  sign := -sign;
 end
 det := tempdet; 
  end;
  end;
  Procedure InitSubmatrix  ( Var} S:submatrix);
 Var k:integer;
 begin
S.size := M.size;
 for k := 1 to S.size  do  begin S.rows[k] :=k; S.cols[k] :=  k  end;
 end;
  Procedure InitMatrix;
 begin {define M.size and M.coeffs any way you like}  end;

  Begin {main program}
 InitMatrix; 
 InitSubmatrix(S);
 d := det(S);
 writeln('determinant = ',d);
  end.