Changeset 16112
- Timestamp:
- Jan 17, 2008, 11:03:13 AM (18 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/libohana/src/gaussj.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/libohana/src/gaussj.c
r16060 r16112 1 1 # include <ohana.h> 2 2 3 // Gauss-Jordan elimination using full pivots based on Press et al's description. Major4 // modifications to conform to C indexing, use a boolean to track the completed pivot rows5 // and catch the singular matrix early on. Also, much cleaner control loops than their6 // implementation. XXX this really needs to check on round-off errors (see version by3 // 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 7 7 // William Kahan 8 8 int dgaussjordan (double **A, double **B, int N, int M) { … … 104 104 int *rowIndex; 105 105 int *pivot; 106 int maxcol, maxrow;107 float maxval, tmpval;108 106 109 107 int diag, col, row; … … 114 112 memset (pivot, 0, N*sizeof(int)); 115 113 116 // init these so gcc does not complain117 maxval = 0.0;118 maxrow = maxcol = 0;119 120 114 // we loop along the matrix diagonal, but we do not operate on the diagonal elements in 121 115 // order instead, we are looking for the current max element and operating on that … … 125 119 for (diag = 0; diag < N; diag++) { 126 120 127 maxval = 0.0; 121 float maxval = 0.0; 122 int maxrow = 0; 123 int maxcol = 0; 128 124 129 125 // search for the next pivot … … 158 154 159 155 /* rescale by pivot reciprocal */ 160 tmpval = 1.0 / A[maxcol][maxcol];156 float tmpval = 1.0 / A[maxcol][maxcol]; 161 157 A[maxcol][maxcol] = 1.0; 162 158 for (col = 0; col < N; col++) A[maxcol][col] *= tmpval; … … 191 187 return (FALSE); 192 188 } 189 190 191 /* Gauss-Jordan Inversion from William Kahan in Basic 192 500 ' Gauss-Jordan Matrix Inversion X = A^(-1) in IBM PC BASIC 193 510 ' including checks for excessive growth despite row-pivoting, 194 520 ' and adjustments for zero pivots to avoid .../0 . 195 530 ' DIM A(N,N), X(N,N), P(N) ... are assumed. 196 540 DEFINT I-N ' ... integer variables; the rest are REAL. 197 550 ' 198 560 ' First determine levels of roundoff and over/underflow. 199 570 UFL = 5.9E-39 ' ... = max{ under, 1/over}flow thresholds. 200 580 G=4 : G=G/3 : G=G-1 ' ... = 1/3 + roundoff in 4/3 201 590 EPS = ABS( ((G+G) - 1) + G ) ' ... = roundoff level. 202 600 G = 1 ' ... will record pivot-growth factor 203 610 ' 204 620 ' Copy A to X and record each column's biggest element. 205 630 FOR J=1 TO N : P(J)=0 206 640 FOR I=1 TO N : T = A(I,J) : X(I,J) = T : T = ABS(T) 207 650 IF T > P(J) THEN P(J) = T 208 660 NEXT I : NEXT J 209 670 ' 210 680 FOR K=1 TO N :' ... perform elimination upon column K . 211 690 Q=0 : J=K : ' ... search for Kth pivot ... 212 700 FOR I=K TO N 213 710 T=ABS(X(I,K)) : IF T>Q THEN Q=T : J=I 214 720 NEXT I 215 730 IF Q=0 THEN Q = EPS*P(K) + UFL : X(K,K)=Q 216 740 IF P(K)>0 THEN Q=Q/P(K) : IF Q>G THEN G=Q 217 750 IF G<=8*K THEN GOTO 790 218 760 PRINT "Growth factor g = ";G;" exceeds ";8*K;" ; try" 219 770 PRINT "moving A's column ";K;" to col. 1 to reduce g ." 220 780 STOP ' ... or go back to re-order A's columns. 221 790 P(K)=J ' ... record pivotal row exchange, if any. 222 800 IF J=K THEN GOTO 830 ' ... Don't bother to swap. 223 810 FOR L=1 TO N : Q=X(J,L) : X(J,L)=X(K,L) 224 820 X(K,L)=Q : NEXT L 225 830 Q = X(K,K) : X(K,K) = 1 226 840 FOR J=1 TO N : X(K,J) = X(K,J)/Q : NEXT J 227 850 FOR I=1 TO N : IF I=K THEN GOTO 890 228 860 Q = X(I,K) : X(I,K) = 0 229 870 FOR J=1 TO N 230 880 X(I,J) = X(I,J) - X(K,J)*Q : NEXT J 231 890 NEXT I : NEXT K 232 900 ' 233 910 FOR K=N-1 TO 1 STEP -1 ' ... unswap columns of X 234 920 J=P(K) : IF J=K THEN GOTO 950 235 930 FOR I=1 TO N : Q=X(I,K) : X(I,K)=X(I,J) 236 940 X(I,J)=Q : NEXT I 237 950 NEXT K 238 960 RETURN 239 */
Note:
See TracChangeset
for help on using the changeset viewer.
