Changeset 24080
- Timestamp:
- May 6, 2009, 2:14:25 PM (17 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
r16120 r24080 1 1 # include <ohana.h> 2 # define GROWTHTEST 0 3 # define MAX_RANGE 1.0e7 2 4 3 5 // Gauss-Jordan elimination using full pivots based on Press et al's description. Substantially 4 6 // reworked for Ohana: major modifications to conform to C indexing, use a boolean to track the 5 7 // 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 // William Kahan 8 // than their implementation. (largely based on version by William Kahan) 9 10 // MAX_RANGE is used to test for ill-conditioned input matrices. For an ill-conditioned 11 // matrix, one or more of the pivots trends towards zero. Rather than allow this to go to the 12 // numerical precision, I am raising an error if |growth| > 1e8 8 13 int dgaussjordan (double **A, double **B, int N, int M) { 9 14 … … 19 24 memset (pivot, 0, N*sizeof(int)); 20 25 26 double growth = 1.0; 27 21 28 // determine underflow conditions 22 29 // double underFlow = DBL_MIN; 23 # if ( 0)30 # if (GROWTHTEST) 24 31 double roundTest = 4.0; 25 32 roundTest /= 3.0; 26 33 roundTest -= 1.0; 27 34 double epsilon = fabs(((roundTest+roundTest) - 1.0) + roundTest); 28 double growth = 1.0;29 35 # endif 30 36 … … 57 63 } 58 64 65 # if (GROWTHTEST) 66 fprintf (stderr, "maxcol: %d\n", maxcol); 67 fprintf (stderr, "full A matrix:\n"); 68 for (row = 0; row < N; row++) { 69 for (col = 0; col < N; col++) { 70 fprintf (stderr, "%10.3e ", A[row][col]); 71 } 72 fprintf (stderr, "\n"); 73 } 74 fprintf (stderr, "\n"); 75 # endif 76 59 77 // if pivot[maxcol] is set, we have already done this row: this implies a singular matrix 60 78 if (pivot[maxcol]) goto escape; … … 76 94 for (col = 0; col < N; col++) A[maxcol][col] *= tmpval; 77 95 for (col = 0; col < M; col++) B[maxcol][col] *= tmpval; 78 // XXX measure the pivot growth and trigger on over/under flow 79 // growth *= tmpval; 80 // fprintf (stderr, "column: %d, growth: %e, epsilon: %e\n", maxcol, growth, epsilon); 96 97 // check for ill-conditioned matrix 98 growth *= tmpval; 99 100 // report the pivot growth 101 # if (GROWTHTEST) 102 fprintf (stderr, "column: %d, maxval : %f, growth: %e, epsilon: %e\n", maxcol, tmpval, growth, epsilon); 103 fprintf (stderr, "A diagonal: "); 104 for (col = 0; col < N; col++) fprintf (stderr, "%f ", A[col][col]); 105 fprintf (stderr, "\n"); 106 # endif 107 108 if (fabs(growth) > MAX_RANGE) goto escape; 81 109 82 110 /* adjust the elements above the pivot */ … … 122 150 memset (pivot, 0, N*sizeof(int)); 123 151 152 float growth = 1.0; 153 124 154 // determine underflow conditions 125 155 // float underFlow = FLT_MIN; 126 # if ( 0)156 # if (GROWTHTEST) 127 157 float roundTest = 4.0; 128 158 roundTest /= 3.0; 129 159 roundTest -= 1.0; 130 160 float epsilon = fabs(((roundTest+roundTest) - 1.0) + roundTest); 131 float growth = 1.0;132 161 # endif 133 162 … … 179 208 for (col = 0; col < N; col++) A[maxcol][col] *= tmpval; 180 209 for (col = 0; col < M; col++) B[maxcol][col] *= tmpval; 181 // growth *= tmpval; 182 // fprintf (stderr, "column: %d, growth: %e, epsilon: %e\n", maxcol, growth, epsilon); 210 211 // check for ill-conditioned matrix 212 growth *= tmpval; 213 214 // report the pivot growth 215 # if (GROWTHTEST) 216 fprintf (stderr, "column: %d, maxval : %f, growth: %e, epsilon: %e\n", maxcol, tmpval, growth, epsilon); 217 fprintf (stderr, "A diagonal: "); 218 for (col = 0; col < N; col++) fprintf (stderr, "%f ", A[col][col]); 219 fprintf (stderr, "\n"); 220 # endif 221 222 if (fabs(growth) > MAX_RANGE) goto escape; 183 223 184 224 /* adjust the elements above the pivot */
Note:
See TracChangeset
for help on using the changeset viewer.
