IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 16112


Ignore:
Timestamp:
Jan 17, 2008, 11:03:13 AM (18 years ago)
Author:
eugene
Message:

adding notes from kahan

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libohana/src/gaussj.c

    r16060 r16112  
    11# include <ohana.h>
    22
    3 // Gauss-Jordan elimination using full pivots based on Press et al's description.  Major
    4 // modifications to conform to C indexing, use a boolean to track the completed pivot rows
    5 // and catch the singular matrix early on.  Also, much cleaner control loops than their
    6 // implementation.  XXX this really needs to check on round-off errors (see version by
     3// Gauss-Jordan elimination using full pivots based on Press et al's description.  Substantially
     4// reworked for Ohana: major modifications to conform to C indexing, use a boolean to track the
     5// completed pivot rows and catch the singular matrix early on.  Also, much cleaner control loops
     6// than their implementation.  XXX this really needs to check on round-off errors (see version by
    77// William Kahan
    88int dgaussjordan (double **A, double **B, int N, int M) {
     
    104104  int *rowIndex;
    105105  int *pivot;
    106   int maxcol, maxrow;
    107   float maxval, tmpval;
    108106 
    109107  int diag, col, row;
     
    114112  memset (pivot, 0, N*sizeof(int));
    115113
    116   // init these so gcc does not complain
    117   maxval = 0.0;
    118   maxrow = maxcol = 0;
    119 
    120114  // we loop along the matrix diagonal, but we do not operate on the diagonal elements in
    121115  // order instead, we are looking for the current max element and operating on that
     
    125119  for (diag = 0; diag < N; diag++) {
    126120
    127     maxval = 0.0;
     121    float maxval = 0.0;
     122    int maxrow = 0;
     123    int maxcol = 0;
    128124
    129125    // search for the next pivot
     
    158154
    159155    /* rescale by pivot reciprocal */
    160     tmpval = 1.0 / A[maxcol][maxcol];
     156    float tmpval = 1.0 / A[maxcol][maxcol];
    161157    A[maxcol][maxcol] = 1.0;
    162158    for (col = 0; col < N; col++) A[maxcol][col] *= tmpval;
     
    191187  return (FALSE);
    192188}
     189
     190
     191/* Gauss-Jordan Inversion from William Kahan in Basic
     192500 ' Gauss-Jordan Matrix Inversion     X = A^(-1) in IBM PC BASIC
     193510 ' including checks for excessive    growth despite row-pivoting,
     194520 '          and adjustments for zero pivots to avoid .../0 .
     195530 ' DIM A(N,N), X(N,N), P(N) ...      are assumed.
     196540     DEFINT I-N ' ... integer variables; the rest are REAL.
     197550   '
     198560   ' First determine levels of roundoff and over/underflow.
     199570     UFL = 5.9E-39 ' ... = max{ under, 1/over}flow thresholds.
     200580        G=4 : G=G/3 : G=G-1      ' ... = 1/3 + roundoff in 4/3
     201590     EPS = ABS( ((G+G) - 1) + G ) ' ... = roundoff level.
     202600     G = 1 ' ... will record pivot-growth factor
     203610   '
     204620   ' Copy A to X and record each column's biggest element.
     205630     FOR J=1 TO N : P(J)=0
     206640          FOR I=1 TO N : T = A(I,J) : X(I,J) = T : T = ABS(T)
     207650                IF T > P(J) THEN P(J) = T
     208660                NEXT I : NEXT J
     209670   '
     210680     FOR K=1 TO N :' ... perform elimination upon column K .
     211690          Q=0 : J=K : ' ... search for Kth pivot ...
     212700          FOR I=K TO N
     213710                T=ABS(X(I,K)) : IF T>Q THEN Q=T : J=I
     214720                NEXT I
     215730          IF Q=0 THEN Q = EPS*P(K) + UFL : X(K,K)=Q
     216740          IF P(K)>0 THEN Q=Q/P(K) : IF Q>G THEN G=Q
     217750          IF G<=8*K THEN GOTO 790
     218760      PRINT "Growth factor g = ";G;" exceeds ";8*K;" ; try"
     219770      PRINT "moving A's column ";K;" to col. 1 to reduce g ."
     220780            STOP ' ... or go back to re-order A's columns.
     221790         P(K)=J ' ... record pivotal row exchange, if any.
     222800         IF J=K THEN GOTO 830 ' ... Don't bother to swap.
     223810             FOR L=1 TO N : Q=X(J,L) : X(J,L)=X(K,L)
     224820                              X(K,L)=Q : NEXT L
     225830         Q = X(K,K) : X(K,K) = 1
     226840         FOR J=1 TO N : X(K,J) = X(K,J)/Q : NEXT J
     227850         FOR I=1 TO N : IF I=K THEN GOTO 890
     228860              Q = X(I,K) : X(I,K) = 0
     229870              FOR J=1 TO N
     230880                   X(I,J) = X(I,J) - X(K,J)*Q : NEXT J
     231890              NEXT I : NEXT K
     232900 '
     233910 FOR K=N-1 TO 1 STEP -1 ' ... unswap columns of X
     234920         J=P(K) : IF J=K THEN GOTO 950
     235930         FOR I=1 TO N : Q=X(I,K) : X(I,K)=X(I,J)
     236940                          X(I,J)=Q : NEXT I
     237950         NEXT K
     238960 RETURN
     239*/
Note: See TracChangeset for help on using the changeset viewer.