Gaussian elimination: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|ALGOL 68}}: misc optimisations...)
Line 10: Line 10:
FIXABLE fixed exception = TRUE;
FIXABLE fixed exception = TRUE;


MODE SIMPLEOUTIII = [0]UNION(CHAR, STRING, INT, REAL, BOOL, BITS);
MODE SIMPLEOUTV = [0]UNION(CHAR, STRING, INT, REAL, BOOL, BITS);
MODE SIMPLEOUTII = [0]SIMPLEOUTIII;
MODE SIMPLEOUTM = [0]SIMPLEOUTV;
MODE SIMPLEOUTI = [0]SIMPLEOUTII;
MODE SIMPLEOUTT = [0]SIMPLEOUTM;
MODE SIMPLEOUT = [0]SIMPLEOUTI;
MODE SIMPLEOUT = [0]SIMPLEOUTT;


PROC raise = (SIMPLEOUT message)FIXABLE: (
PROC raise = (SIMPLEOUT message)FIXABLE: (
Line 67: Line 67:
# using Gaussian elimination, find x where A*x = b #
# using Gaussian elimination, find x where A*x = b #
####################################################
####################################################
# Note: a and b are modified "in situ" #

PROC in situ gaussian elimination = (REF MAT a, REF MAT b)REF MAT: (
PROC in situ gaussian elimination = (REF MAT a, REF MAT b)REF MAT: (
# Note: a and b are modified "in situ", and b is returned as x #


FOR row TO UPB a-1 DO
FOR diag TO UPB a-1 DO
INT pivot row := row; SCAL max mult := ABS a[row,row];
INT pivot row := diag; SCAL pivot factor := ABS a[diag,diag];
FOR sub row FROM row + 1 TO UPB a DO # Full pivoting #
FOR row FROM diag + 1 TO UPB a DO # Full pivoting #
SCAL abs a ij = ABS a[sub row,row];
SCAL abs a diag = ABS a[row,diag];
IF abs a ij>=max mult THEN pivot row := sub row; max mult := abs a ij FI
IF abs a diag>=pivot factor THEN
pivot row := row; pivot factor := abs a diag FI
OD;
OD;
# now we have the "best" row to full pivot, do the actual pivot #
# now we have the "best" diag to full pivot, do the actual pivot #
IF row NE pivot row THEN
IF diag NE pivot row THEN
# a[pivot row,] =:= a[row,]; XXX: unoptimised # #DB#
# a[pivot row,] =:= a[diag,]; XXX: unoptimised # #DB#
a[pivot row,row:] =:= a[row,row:]; # XXX: optimised #
a[pivot row,diag:] =:= a[diag,diag:]; # XXX: optimised #
b[pivot row,] =:= b[row,] # swap/pivot the rows of a & b #
b[pivot row,] =:= b[diag,] # swap/pivot the diags of a & b #
FI;
FI;


IF ABS a[row,row] < small real THEN raise value error("singular matrix") FI;
IF ABS a[diag,diag] <= near min scal THEN
raise value error("singular matrix") FI;
SCAL reciprocal a row row := 1 / a[row, row];
SCAL a diag reciprocal := 1 / a[diag, diag];


FOR sub row FROM row+1 TO UPB a DO
FOR row FROM diag+1 TO UPB a DO
SCAL mult = a[sub row,row] * reciprocal a row row;
SCAL factor = a[row,diag] * a diag reciprocal;
# a[sub row,] -:= mult*a[row,] XXX: "unoptimised" # #DB#
# a[row,] -:= factor * a[diag,] XXX: "unoptimised" # #DB#
a[sub row,row+1:] -:= mult*a[row,row+1:];# XXX: "optimised" #
a[row,diag+1:] -:= factor * a[diag,diag+1:];# XXX: "optimised" #
b[sub row,] -:= mult*b[row,]
b[row,] -:= factor * b[diag,]
OD
OD
OD;
OD;


# We have a triangular matrix, at this point we can traverse backwards
# We have a triangular matrix, at this point we can traverse backwards
up the diagonal calculating b\a Converting it initial to a diagonal
up the diagonal calculating b\A Converting it initial to a diagonal
matrix, then to the identity. #
matrix, then to the identity. #


FOR row FROM UPB a BY -1 TO 1+LWB a DO
FOR diag FROM UPB a BY -1 TO 1+LWB a DO

IF ABS a[row,row] < small real THEN raise value error("Zero pivot encountered?") FI;
IF ABS a[diag,diag] <= near min scal THEN
SCAL reciprocal a row row = 1 / a[row,row];
raise value error("Zero pivot encountered?") FI;
FOR sub row TO row-1 DO
SCAL mult = a[sub row,row] * reciprocal a row row;
SCAL a diag reciprocal = 1 / a[diag,diag];

# a[sub row,row] -:= mult*a[row,row]; XXX: "unoptimised" so remove # #DB#
b[sub row,] -:= mult*b[row,]
FOR row TO diag-1 DO
SCAL factor = a[row,diag] * a diag reciprocal;
# a[row,diag] -:= factor * a[diag,diag]; XXX: "unoptimised" so remove # #DB#
b[row,] -:= factor * b[diag,]
OD;
OD;
# Now we have a solo diagonal element we can simply divide b
# Now we have only diagonal elements we can simply divide b
by the values along the diagonal of A. #
by the values along the diagonal of A. #
b[row,] *:= reciprocal a row row
b[diag,] *:= a diag reciprocal
OD;
OD;


Line 128: Line 132:
stop</lang>'''File: test_Gaussian_elimination.a68'''<lang algol68>#!/usr/bin/algol68g-full --script #
stop</lang>'''File: test_Gaussian_elimination.a68'''<lang algol68>#!/usr/bin/algol68g-full --script #
# -*- coding: utf-8 -*- #
# -*- coding: utf-8 -*- #

PR READ "prelude_exception.a68" PR;


# define the attributes of the scalar field being used #
# define the attributes of the scalar field being used #
MODE SCAL = REAL;
MODE SCAL = REAL;
FORMAT scal repr = $g(-0,real width)$;
FORMAT scal repr = $g(-0,real width)$;
# create "near min scal" as is scales better then small real #
SCAL near min scal = min real ** 0.99;


PR READ "prelude_exception.a68" PR;
PR READ "prelude_mat_lib.a68" PR;
PR READ "prelude_mat_lib.a68" PR;
PR READ "prelude_gaussian_elimination.a68" PR;
PR READ "prelude_gaussian_elimination.a68" PR;

Revision as of 01:47, 1 October 2012

Gaussian elimination is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Problem: Solve Ax=b using Gaussian elimination then backwards substitution. A being an n by n matrix. Also, x and b are n by 1 vectors. To improve accuracy, please use partial pivoting and scaling.

ALGOL 68

Works with: ALGOL 68 version Revision 1 - extension to language used - "PRAGMA READ" (similar to C's #include directive.)
Works with: ALGOL 68G version Any - tested with release algol68g-2.4.1.

File: prelude_exception.a68<lang algol68># -*- coding: utf-8 -*- # MODE FIXABLE = BOOL; # if an exception is detected, can it be fixed "on-site"? # FIXABLE fixed exception = TRUE;

MODE SIMPLEOUTV = [0]UNION(CHAR, STRING, INT, REAL, BOOL, BITS); MODE SIMPLEOUTM = [0]SIMPLEOUTV; MODE SIMPLEOUTT = [0]SIMPLEOUTM; MODE SIMPLEOUT = [0]SIMPLEOUTT;

PROC raise = (SIMPLEOUT message)FIXABLE: (

 putf(stand error, ($"Exception:"$, $xg$, message, $l$));
 stop

);

PROC raise value error = (SIMPLEOUT message)FIXABLE:

 IF raise(message) NE fixed exception THEN exception value error; FALSE FI;

SKIP</lang>File: prelude_mat_lib.a68<lang algol68># -*- coding: utf-8 -*- # COMMENT REQUIRES

 MODE SCAL
 FORMAT scal repr

END COMMENT

INT lwb vec := 1, upb vec := 0; MODE VEC = [lwb vec:upb vec]SCAL,

    MAT = [lwb vec:upb vec,lwb vec:upb vec]SCAL;

FORMAT sub = $"("$, sep=$", "$, bus = $")"$; FORMAT vec repr = $f(sub)n(upb vec - lwb vec)(f(scal repr)f(sep))f(scal repr)f(bus)$;

  1. OPerators to swap the contents of two VECtors #

PRIO =:= = 1; OP =:= = (REF VEC u, v)VOID:

 FOR i TO UPB u DO SCAL scal=u[i]; u[i]:=v[i]; v[i]:=scal OD;

OP +:= = (REF VEC lhs, VEC rhs)REF VEC: (

 FOR i TO UPB lhs DO lhs[i] +:= rhs[i] OD;
 lhs

);

OP -:= = (REF VEC lhs, VEC rhs)REF VEC: (

 FOR i TO UPB lhs DO lhs[i] -:= rhs[i] OD;
 lhs

);

OP *:= = (REF VEC lhs, SCAL rhs)REF VEC: (

 FOR i TO UPB lhs DO lhs[i] *:= rhs OD;
 lhs

);

OP /:= = (REF VEC lhs, SCAL rhs)REF VEC: (

 SCAL inv = 1 / rhs; # multiplication is faster #
 FOR i TO UPB lhs DO lhs[i] *:= inv OD;
 lhs

);

SKIP</lang>File: prelude_gaussian_elimination.a68<lang algol68># -*- coding: utf-8 -*- #

  1. using Gaussian elimination, find x where A*x = b #

PROC in situ gaussian elimination = (REF MAT a, REF MAT b)REF MAT: (

  1. Note: a and b are modified "in situ", and b is returned as x #
 FOR diag TO UPB a-1 DO
   INT pivot row := diag; SCAL pivot factor := ABS a[diag,diag];
   FOR row FROM diag + 1 TO UPB a DO # Full pivoting #
     SCAL abs a diag = ABS a[row,diag];
     IF abs a diag>=pivot factor THEN 
       pivot row := row; pivot factor := abs a diag FI
   OD;
 # now we have the "best" diag to full pivot, do the actual pivot #
   IF diag NE pivot row THEN
   # a[pivot row,] =:= a[diag,]; XXX: unoptimised # #DB#
     a[pivot row,diag:] =:= a[diag,diag:]; # XXX: optimised #
     b[pivot row,] =:= b[diag,] # swap/pivot the diags of a & b #
   FI;
   IF ABS a[diag,diag] <= near min scal THEN 
     raise value error("singular matrix") FI;
   SCAL a diag reciprocal := 1 / a[diag, diag];
   FOR row FROM diag+1 TO UPB a DO
     SCAL factor = a[row,diag] * a diag reciprocal;
   # a[row,] -:= factor * a[diag,] XXX: "unoptimised" # #DB#
     a[row,diag+1:] -:= factor * a[diag,diag+1:];# XXX: "optimised" #
     b[row,] -:= factor * b[diag,]
   OD
 OD;
  1. We have a triangular matrix, at this point we can traverse backwards
 up the diagonal calculating b\A Converting it initial to a diagonal
 matrix, then to the identity.  #
 FOR diag FROM UPB a BY -1 TO 1+LWB a DO
   IF ABS a[diag,diag] <= near min scal THEN 
     raise value error("Zero pivot encountered?") FI;
   SCAL a diag reciprocal = 1 / a[diag,diag];
   FOR row TO diag-1 DO
     SCAL factor = a[row,diag] * a diag reciprocal;
   # a[row,diag] -:= factor * a[diag,diag]; XXX: "unoptimised" so remove # #DB#
     b[row,] -:= factor * b[diag,]
   OD;
  1. Now we have only diagonal elements we can simply divide b
 by the values along the diagonal of A. #
   b[diag,] *:= a diag reciprocal
 OD;
 b # EXIT #

);

PROC gaussian elimination = (MAT in a, MAT in b)MAT: (

  1. Note: a and b are cloned and not modified "in situ" #
 [UPB in a, 2 UPB in a]SCAL a := in a;
 [UPB in b, 2 UPB in b]SCAL b := in b;
 in situ gaussian elimination(a,b)

);

SKIP</lang>File: postlude_exception.a68<lang algol68># -*- coding: utf-8 -*- # SKIP EXIT exception too many iterations: exception value error:

 stop</lang>File: test_Gaussian_elimination.a68<lang algol68>#!/usr/bin/algol68g-full --script #
  1. -*- coding: utf-8 -*- #

PR READ "prelude_exception.a68" PR;

  1. define the attributes of the scalar field being used #

MODE SCAL = REAL; FORMAT scal repr = $g(-0,real width)$;

  1. create "near min scal" as is scales better then small real #

SCAL near min scal = min real ** 0.99;

PR READ "prelude_mat_lib.a68" PR; PR READ "prelude_gaussian_elimination.a68" PR;

MAT a =(( 1.00, 0.00, 0.00, 0.00, 0.00, 0.00),

       ( 1.00, 0.63, 0.39,  0.25,  0.16,   0.10),
       ( 1.00, 1.26, 1.58,  1.98,  2.49,   3.13),
       ( 1.00, 1.88, 3.55,  6.70, 12.62,  23.80),
       ( 1.00, 2.51, 6.32, 15.88, 39.90, 100.28),
       ( 1.00, 3.14, 9.87, 31.01, 97.41, 306.02));

VEC b = (-0.01, 0.61, 0.91, 0.99, 0.60, 0.02);

[UPB b,1]SCAL col b; col b[,1]:= b;

upb vec := 2 UPB a;

printf((vec repr, gaussian elimination(a,col b)));

PR READ "postlude_exception.a68" PR</lang>Output:

(-.010000000000002, 1.602790394502130, -1.613203059905640, 1.245494121371510, -.490989719584686, .065760696175236)

C

This modifies A and b in place, which might not be quite desirable. <lang c>#include <stdio.h>

  1. include <stdlib.h>
  2. include <math.h>
  1. define mat_elem(a, y, x, n) (a + ((y) * (n) + (x)))

void swap_row(double *a, double *b, int r1, int r2, int n) { double tmp, *p1, *p2; int i;

if (r1 == r2) return; for (i = 0; i < n; i++) { p1 = mat_elem(a, r1, i, n); p2 = mat_elem(a, r2, i, n); tmp = *p1, *p1 = *p2, *p2 = tmp; } tmp = b[r1], b[r1] = b[r2], b[r2] = tmp; }

void gauss_eliminate(double *a, double *b, double *x, int n) {

  1. define A(y, x) (*mat_elem(a, y, x, n))

int i, j, col; double max, tmp;

for (col = 0; col < n; col++) { j = col, max = A(j, j);

for (i = col + 1; i < n; i++) if ((tmp = fabs(A(i, col))) > max) j = i, max = tmp;

swap_row(a, b, col, j, n);

for (i = col + 1; i < n; i++) { tmp = A(i, col) / A(col, col); for (j = col + 1; j < n; j++) A(i, j) -= tmp * A(col, j); A(i, col) = 0; b[i] -= tmp * b[col]; } } for (col = n - 1; col >= 0; col--) { tmp = b[col]; for (j = n - 1; j > col; j--) tmp -= x[j] * A(col, j); x[col] = tmp / A(col, col); }

  1. undef A

}

int main(void) { double a[] = { 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.63, 0.39, 0.25, 0.16, 0.10, 1.00, 1.26, 1.58, 1.98, 2.49, 3.13, 1.00, 1.88, 3.55, 6.70, 12.62, 23.80, 1.00, 2.51, 6.32, 15.88, 39.90, 100.28, 1.00, 3.14, 9.87, 31.01, 97.41, 306.02 }; double b[] = { -0.01, 0.61, 0.91, 0.99, 0.60, 0.02 }; double x[6]; int i;

gauss_eliminate(a, b, x, 6);

for (i = 0; i < 6; i++) printf("%g\n", x[i]);

return 0;

}</lang>

Output:

-0.01 1.60279 -1.6132 1.24549 -0.49099 0.0657607

J

%. , J's matrix divide verb, directly solves systems of determined and of over-determined linear equations directly. This example J session builds a noisy sine curve on the half circle, fits quintic and quadratic equations, and displays the results of evaluating these polynomials.

<lang J>

  f=: 6j2&":   NB. formatting verb
  sin=: 1&o.   NB. verb to evaluate circle function 1, the sine
  add_noise=: ] + (* (_0.5 + 0 ?@:#~ #))   NB. AMPLITUDE add_noise SIGNAL
  f RADIANS=: o.@:(%~ i.@:>:)5  NB. monadic circle function is  pi times
 0.00  0.63  1.26  1.88  2.51  3.14
  f SINES=: sin RADIANS
 0.00  0.59  0.95  0.95  0.59  0.00
  f NOISY_SINES=: 0.1 add_noise SINES
_0.01  0.61  0.91  0.99  0.60  0.02
  A=: (^/ i.@:#) RADIANS  NB. A is the quintic coefficient matrix
  NB. display the equation to solve
  (f A) ; 'x' ; '=' ; f@:,. NOISY_SINES

┌────────────────────────────────────┬─┬─┬──────┐ │ 1.00 0.00 0.00 0.00 0.00 0.00│x│=│ _0.01│ │ 1.00 0.63 0.39 0.25 0.16 0.10│ │ │ 0.61│ │ 1.00 1.26 1.58 1.98 2.49 3.13│ │ │ 0.91│ │ 1.00 1.88 3.55 6.70 12.62 23.80│ │ │ 0.99│ │ 1.00 2.51 6.32 15.88 39.90100.28│ │ │ 0.60│ │ 1.00 3.14 9.87 31.01 97.41306.02│ │ │ 0.02│ └────────────────────────────────────┴─┴─┴──────┘

  f QUINTIC_COEFFICIENTS=: NOISY_SINES %. A   NB. %. solves the linear system
_0.01  1.71 _1.88  1.48 _0.58  0.08
  quintic=: QUINTIC_COEFFICIENTS&p.  NB. verb to evaluate the polynomial
  NB. %. also solves the least squares fit for overdetermined system
  quadratic=: (NOISY_SINES %. (^/ i.@:3:) RADIANS)&p.  NB. verb to evaluate quadratic.
  quadratic

_0.0200630695393961729 1.26066877804926536 _0.398275112136019516&p.

  NB. The quintic is agrees with the noisy data, as it should
  f@:(NOISY_SINES ,. sin ,. quadratic ,. quintic) RADIANS
_0.01  0.00 _0.02 _0.01
 0.61  0.59  0.61  0.61
 0.91  0.95  0.94  0.91
 0.99  0.95  0.94  0.99
 0.60  0.59  0.63  0.60
 0.02  0.00  0.01  0.02
  f MID_POINTS=: (+ -:@:(-/@:(2&{.)))RADIANS
_0.31  0.31  0.94  1.57  2.20  2.83
  f@:(sin ,. quadratic ,. quintic) MID_POINTS
_0.31 _0.46 _0.79
 0.31  0.34  0.38
 0.81  0.81  0.77
 1.00  0.98  1.00
 0.81  0.83  0.86
 0.31  0.36  0.27

</lang>

Mathematica

<lang Mathematica>GaussianElimination[A_?MatrixQ, b_?VectorQ] := Last /@ RowReduce[Flatten /@ Transpose[{A, b}]]</lang>

MATLAB

<lang MATLAB> function [ x ] = GaussElim( A, b)

% Ensures A is n by n sz = size(A); if sz(1)~=sz(2)

   fprintf('A is not n by n\n');
   clear x;
   return;

end

n = sz(1);

% Ensures b is n x 1. if n~=sz(1)

   fprintf('b is not 1 by n.\n');
   return

end

x = zeros(n,1); aug = [A b]; tempmatrix = aug;

for i=2:sz(1)


   % Find maximum of row and divide by the maximum
   tempmatrix(1,:) = tempmatrix(1,:)/max(tempmatrix(1,:));
   
   % Finds the maximum in column
   temp = find(abs(tempmatrix) - max(abs(tempmatrix(:,1))));
   if length(temp)>2
       for j=1:length(temp)-1
           if j~=temp(j)
               maxi = j; %maxi = column number of maximum
               break;
           end
       end
   else % length(temp)==2
       maxi=1;
   end
   
   % Row swap if maxi is not 1
   if maxi~=1
       temp = tempmatrix(maxi,:);
       tempmatrix(maxi,:) = tempmatrix(1,:);
       tempmatrix(1,:) = temp;
   end    
   
   % Row reducing
   for j=2:length(tempmatrix)-1
       tempmatrix(j,:) = tempmatrix(j,:)-tempmatrix(j,1)/tempmatrix(1,1)*tempmatrix(1,:);
       if tempmatrix(j,j)==0 || isnan(tempmatrix(j,j)) || abs(tempmatrix(j,j))==Inf
           fprintf('Error: Matrix is singular.\n');
           clear x;
           return
       end
   end
   aug(i-1:end,i-1:end) = tempmatrix;
   
   % Decrease matrix size
   tempmatrix = tempmatrix(2:end,2:end);

end

% Backwards Substitution x(end) = aug(end,end)/aug(end,end-1); for i=n-1:-1:1

   x(i) = (aug(i,end)-dot(aug(i,1:end-1),x))/aug(i,i);

end

end </lang>

PARI/GP

If A and B have floating-point numbers (t_REALs) then the following uses Gaussian elimination: <lang parigp>matsolve(A,B)</lang>

If the entries are integers, then p-adic lifting (Dixon 1982) is used instead.

Tcl

Library: Tcllib (Package: math::linearalgebra)

<lang tcl>package require math::linearalgebra

set A {

   {1.00  0.00  0.00  0.00  0.00   0.00}
   {1.00  0.63  0.39  0.25  0.16   0.10}
   {1.00  1.26  1.58  1.98  2.49   3.13}
   {1.00  1.88  3.55  6.70 12.62  23.80}
   {1.00  2.51  6.32 15.88 39.90 100.28}
   {1.00  3.14  9.87 31.01 97.41 306.02}

} set b {-0.01 0.61 0.91 0.99 0.60 0.02} puts -nonewline [math::linearalgebra::show [math::linearalgebra::solveGauss $A $b] "%.2f"]</lang>

Output:
-0.01
1.60
-1.61
1.25
-0.49
0.07