home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.barnyard.co.uk
/
2015.02.ftp.barnyard.co.uk.tar
/
ftp.barnyard.co.uk
/
cpm
/
walnut-creek-CDROM
/
CPM
/
TURBOPAS
/
MAPSTATF.LBR
/
MATINV.LZB
/
MATINV.ÌIB
Wrap
Text File
|
2000-06-30
|
3KB
|
101 lines
{ Gauss Jordan matrix inversion }
procedure matinv (
var b: NBYN; { square matrix of coefficients inverted}
n: SUBS; { order of matrix }
var det: real; { determinant of matrix }
var error: boolean); { true, i.e.=1, if matrix singular }
label 99;
var
index : array[SUBS,1..3] of integer;
i,j,k,l,irow,icol,l1 : integer;
pivot,hold,sum,t,ab,big : real;
procedure swap(var a,b: real);
var hold : real;
begin
hold:=a; a:=b; b:=hold
end;
procedure minv2;
label 98;
var i,j,k,l,l1 : integer;
begin { procedure minv2 actual start of matinv }
error:=false; det:=1.0;
for i:=1 to n do index[i,3]:=0;
for i:=1 to n do
begin
{ search for largest element }
big:=0.0;
for j:=1 to n do
begin
if index[j,3]<>1 then
begin
for k:=1 to n do
begin
if index[k,3]>1 then
begin
writeln('ERROR: matrix is singular');
error:=true; goto 98 { abort }
end;
if index[k,3]<1 then
if abs(b[j,k])>big then
begin
irow:=j; icol:=k; big:=abs(b[j,k])
end
end { k-loop }
end
end; { j-loop }
index[icol,3]:=index[icol,3]+1; index[i,1]:=irow; index[i,2]:=icol;
{ interchange rows to put pivot on diagonal }
if irow<>icol then
begin
det:=-det;
for l:=1 to n do
swap(b[irow,l],b[icol,l]);
end;
{ divide pivot row by pivot column }
pivot:=b[icol,icol]; det:=det*pivot; b[icol,icol]:=1.0;
for l:=1 to n do b[icol,l]:=b[icol,l]/pivot;
{ reduce nonpivot rows }
for l1:=1 to n do
begin
if l1<>icol then
begin
t:=b[l1,icol]; b[l1,icol]:=0.0;
for l:=1 to n do b[l1,l]:=b[l1,l]-b[icol,l]*t;
end { if l1<>icol }
end
end; { i-loop }
98:
end; { minv2 }
begin { gaus-jordan main program }
minv2; { first half of matinv }
if error then goto 99;
{ interchange columns }
for i:=1 to n do
begin
l:=n-i+1;
if index[l,1]<>index[l,2] then
begin
irow:=index[l,1]; icol:=index[l,2];
for k:=1 to n do swap(b[k,irow],b[k,icol]);
end { if index }
end; { i-loop }
for k:=1 to n do
if index[k,3]<>1 then
begin
writeln(chr(7),'ERROR: matrix is singular');
error:=true; goto 99 { abort }
end;
99:
end; { procedure matinv }
ot column }
pivot:=b[icol,icol]; det:=det*pivot; b[icol,icol]:=1.0;
for l:=1 to n do b[icol,l]:=b[icol,l]/pivot;
{ r