BEGIN
-- solve Ax=b by Gaussian Elimination
FOR i:
INTEGER
IN [0..n)
DO
bestk:INTEGER ← i;
FOR k:
INTEGER
IN [i..n)
DO
IF ABS[A[k][i]] > ABS[A[bestk][i]] THEN bestk ← k;
ENDLOOP;
{t:RowN ← A[i]; A[i] ← A[bestk]; A[bestk] ← t};
{t:REAL ← b[i]; b[i] ← b[bestk]; b[bestk] ← t};
FOR k:
INTEGER
IN (i..n)
DO
IF
A[k][i]#0
THEN
BEGIN
r:REAL = A[k][i]/A[i][i]; -- Singular A causes Real.RealException = divide by zero
FOR j:
INTEGER
IN [i..n)
DO
A[k][j] ← A[k][j] - A[i][j]*r
ENDLOOP;
b[k] ← b[k] - b[i]*r
END;
ENDLOOP;
ENDLOOP;
Now A is upper-triangular, so back substitute
x ← NEW[VecSeq[n]];
FOR i:INTEGER IN [0..n) DO x[i] ← 0; ENDLOOP;
FOR i:
INTEGER
DECREASING
IN [0..n)
DO
xi:REAL ← b[i];
FOR j:
INTEGER
IN (i..n)
DO
xi ← xi - A[i][j]*x[j];
ENDLOOP;
x[i] ← xi / A[i][i]
ENDLOOP