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
/
REGGAUSS.LZB
/
REGGAUSS.ÌIB
Wrap
Text File
|
2000-06-30
|
3KB
|
103 lines
{ Solution of Simultaneous Linear Equations Ax=b Using Gaussian Elimination
Method with No Pivoting - Suitable for Symmetric matrices but less efficient
than Choelski decomp for positive semi definite matrices - outputs LU
factorization in A suitable for InverseDiagonal subroutine below - slicing
used for speed.
The types are assumed defined in the calling program:
Const
N=...; (maximum dimensions of coefficient matrix A)
Type
SUBS= 1..N;
RVEC = Array [SUBS] Of Real;
NBYN = Array [SUBS] Of RVEC;}
{ function used by both routines }
Function Dotprod(u, v: RVEC; k, n: SUBS): Real;
Var
i: SUBS;
sum: Real;
Begin
sum:=0; For i:=k To n Do sum:=sum+u[i]*v[i]; Dotprod:=sum;
End; { of Dotprod }
Procedure Gausslice(
n : SUBS; { Dimension of Square matrix A }
Var a : NBYN; { Coefficient Matrix A returns LU decomposition }
b : RVEC; { Initial b's }
Var x : RVEC; { Solved X Values }
Var error : Boolean);{ error - not solved }
label 999;
const
assumezero=1E-20;
Var
i, j: SUBS;
mult, pivot: Real;
state: (incrementingj, jisnminus1, zeropivot);
Procedure SubtractRow(Var u: RVEC; v: RVEC; m: Real; k, n:SUBS);
Var
i: SUBS;
Begin
For i:=k To n Do u[i]:=u[i]-m*v[i];
End; { of SubtractRow }
Begin { of GAUSSLICE main }
j:=1; state:=incrementingj;
Repeat
pivot:=a[j,j];
If Abs(pivot)<=assumezero Then state:=zeropivot Else
Begin
For i:=j+1 To n Do
Begin
mult:=a[i,j]/pivot;
If Abs(mult)>assumezero Then
Begin
a[i,j]:=mult; SubtractRow(a[i],a[j],mult,j+1,n);
b[i]:=b[i]-mult*b[j];
End;
End;
If j=n-1 Then state:=jisnminus1 Else j:=j+1;
End;
Until state<>incrementingj;
Case state Of
zeropivot: error:=true;
jisnminus1:
Begin
error:=Abs(a[n,n])<=assumezero;
If not error Then
Begin
x[n]:=b[n]/a[n,n];
For i:=n-1 Downto 1 Do x[i]:=(b[i]-Dotprod(a[i],x,i+1,n))/a[i,i];
End;
End;
End; { of case };
End; { of GAUSSLICE }
{ Companion program to GAUSSLICE uses repeated calls To LUSolving with Ax=b
coming from columns of AinvA=Identity to solve for inverse matrix columns in
this version only diagonals are computed for statistical tests on x's }
Procedure InverseDiagonals(
n : SUBS; { Dimension of Square matrix A }
a : NBYN; { LU decomposition }
Var x : RVEC); { Inverse diagonals }
Var
i, j: SUBS;
b: RVEC;
Begin
For i:=1 To n Do
Begin
b[i]:=1;
For j:=i+1 To n Do b[j]:=b[j]-Dotprod(a[j],b,i,j-1);
x[n]:=b[n]/a[n,n];
For j:=n-1 Downto i Do x[j]:=(b[j]-Dotprod(a[j],x,j+1,n))/a[j,j];
End;
End; { of InverseDiagonals }
ngj;
Case state Of
zeropivot: error:=true;
jisnminus1:
Begin
erro