IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 16060


Ignore:
Timestamp:
Jan 14, 2008, 1:50:46 PM (18 years ago)
Author:
eugene
Message:

convert to new gauss-jordan code (non-Press); fixing Wall,Werror warnings

Location:
trunk/Ohana/src
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/libohana/include/ohana.h

    r16040 r16060  
    184184char   *ohana_version          PROTO(());
    185185
    186 int     dgaussj                PROTO((double **a, int n, double **b, int m));
    187 int     fgaussj                PROTO((float **a, int n, float **b, int m));
     186int     dgaussjordan           PROTO((double **A, double **B, int N, int M));
     187int     fgaussjordan           PROTO((float **A, float **B, int n, int m));
    188188
    189189/* in time.c */
  • trunk/Ohana/src/libohana/src/IOBufferOps.c

    r15851 r16060  
    4545
    4646  Nread = read (fd, &buffer[0].buffer[buffer[0].Nbuffer], buffer[0].Nblock);
    47   if (DEBUG) fprintf (stderr, "read IO buffer: (%x) %d from %d\n", buffer, Nread, buffer[0].Nblock);
     47  if (DEBUG) fprintf (stderr, "read IO buffer: (%lx) %d from %d\n", (unsigned long) buffer, Nread, buffer[0].Nblock);
    4848
    4949  /* on success, increase the block size for the next read */
  • trunk/Ohana/src/libohana/src/gaussj.c

    r8301 r16060  
    11# include <ohana.h>
    22
    3 int dgaussj (double **a, int n, double **b, int m) {
     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
     7// William Kahan
     8int dgaussjordan (double **A, double **B, int N, int M) {
    49
    5   int *indexCol;
    6   int *indexRow;
     10  int *colIndex;
     11  int *rowIndex;
    712  int *pivot;
    8   int i, col, row, j, k, l, ll;
    9   double big, temp;
     13  int maxcol, maxrow;
     14  double maxval, tmpval;
    1015 
    11   ALLOCATE (indexCol, int, n);
    12   ALLOCATE (indexRow, int, n);
    13   ALLOCATE (pivot, int, n);
    14   for (j = 0; j < n; j++) pivot[j] = 0;
     16  int diag, col, row;
    1517
    16   row = col = 0;
    17   big = fabs(a[0][0]);
     18  ALLOCATE (colIndex, int, N);
     19  ALLOCATE (rowIndex, int, N);
     20  ALLOCATE (pivot, int, N);
     21  memset (pivot, 0, N*sizeof(int));
    1822
    19   for (i = 0; i < n; i++) {
    20     big = 0.0;
    21     for (j = 0; j < n; j++) {
    22       if (!finite(a[i][j])) goto escape;
    23       if (pivot[j] != 1) {
    24         for (k = 0; k < n; k++) {
    25           if (pivot[k] == 0) {
    26             if (fabs (a[j][k]) >= big) {
    27               big = fabs (a[j][k]);
    28               row = j;
    29               col = k;
    30             }
    31           } else {
    32             if (pivot[k] > 1) goto escape;
    33           }
    34         }
     23  // init these so gcc does not complain
     24  maxval = 0.0;
     25  maxrow = maxcol = 0;
     26
     27  // we loop along the matrix diagonal, but we do not operate on the diagonal elements in
     28  // order instead, we are looking for the current max element and operating on that
     29  // diagonal element.  this is effectively column pivoting.  row pivoting is perfomed
     30  // explicitly
     31
     32  for (diag = 0; diag < N; diag++) {
     33
     34    maxval = 0.0;
     35
     36    // search for the next pivot
     37    for (row = 0; row < N; row++) {
     38      if (!finite(A[row][diag])) goto escape;
     39
     40      // if we have already operated on this row (pivot[row] is true), skip it
     41      if (pivot[row]) continue;
     42
     43      // if we have not yet operated on this row (pivot[row] is false), look for pivot for this row
     44      for (col = 0; col < N; col++) {
     45        if (pivot[col]) continue;
     46        if (fabs (A[row][col]) < maxval) continue;
     47        maxval = fabs (A[row][col]);
     48        maxrow = row;
     49        maxcol = col;
    3550      }
    3651    }
    37     pivot[col]++;
    38     if (row != col) {
    39       for (l = 0; l < n; l++) SWAP (a[row][l], a[col][l]);
    40       for (l = 0; l < m; l++) SWAP (b[row][l], b[col][l]);
     52
     53    // if pivot[maxcol] is set, we have already done this row: this implies a singular matrix
     54    if (pivot[maxcol]) goto escape;
     55    pivot[maxcol] = TRUE;
     56
     57    // if the selected pivot is off the diagonal, do a row swap
     58    if (maxrow != maxcol) {
     59      for (col = 0; col < N; col++) SWAP (A[maxrow][col], A[maxcol][col]);
     60      for (col = 0; col < M; col++) SWAP (B[maxrow][col], B[maxcol][col]);
    4161    }
    42     indexRow[i] = row;
    43     indexCol[i] = col;
    44     if (a[col][col] == 0.0) goto escape;
     62    rowIndex[diag] = maxrow;
     63    colIndex[diag] = maxcol;
     64    if (A[maxcol][maxcol] == 0.0) goto escape;
    4565
    4666    /* rescale by pivot reciprocal */
    47     temp = 1.0 / a[col][col];
    48     a[col][col] = 1.0;
    49     for (l = 0; l < n; l++) a[col][l] *= temp;
    50     for (l = 0; l < m; l++) b[col][l] *= temp;
     67    tmpval = 1.0 / A[maxcol][maxcol];
     68    A[maxcol][maxcol] = 1.0;
     69    for (col = 0; col < N; col++) A[maxcol][col] *= tmpval;
     70    for (col = 0; col < M; col++) B[maxcol][col] *= tmpval;
    5171
    5272    /* adjust the elements above the pivot */
    53     for (ll = 0; ll < n; ll++) {
    54       if (ll != col) {
    55         temp = a[ll][col];
    56         a[ll][col] = 0.0;
    57         for (l = 0; l < n; l++) a[ll][l] -= a[col][l]*temp;
    58         for (l = 0; l < m; l++) b[ll][l] -= b[col][l]*temp;
    59       }
     73    for (row = 0; row < N; row++) {
     74      if (row == maxcol) continue;
     75      tmpval = A[row][maxcol];
     76      A[row][maxcol] = 0.0;
     77      for (col = 0; col < N; col++) A[row][col] -= A[maxcol][col]*tmpval;
     78      for (col = 0; col < M; col++) B[row][col] -= B[maxcol][col]*tmpval;
    6079    }
    6180  }
    6281
    63   for (l = n - 1; l >= 0; l--) {
    64     if (indexRow[l] != indexCol[l]) {
    65       for (k = 0; k < n; k++) SWAP (a[k][indexRow[l]], a[k][indexCol[l]]);
     82  // swap back the inverse matrix based on the row swaps above
     83  for (col = N - 1; col >= 0; col--) {
     84    if (rowIndex[col] != colIndex[col]) {
     85      for (row = 0; row < N; row++) SWAP (A[row][rowIndex[col]], A[row][colIndex[col]]);
    6686    }
    6787  }
     88
    6889  free (pivot);
    69   free (indexRow);
    70   free (indexCol);
     90  free (rowIndex);
     91  free (colIndex);
    7192  return (TRUE);
    7293
    7394escape:
    7495  free (pivot);
    75   free (indexRow);
    76   free (indexCol);
     96  free (rowIndex);
     97  free (colIndex);
    7798  return (FALSE);
    7899}
    79100
    80 int fgaussj (float **a, int n, float **b, int m) {
     101int fgaussjordan (float **A, float **B, int N, int M) {
    81102
    82   int *indexCol;
    83   int *indexRow;
     103  int *colIndex;
     104  int *rowIndex;
    84105  int *pivot;
    85   int i, col, row, j, k, l, ll;
    86   float big, temp;
     106  int maxcol, maxrow;
     107  float maxval, tmpval;
    87108 
    88   ALLOCATE (indexCol, int, n);
    89   ALLOCATE (indexRow, int, n);
    90   ALLOCATE (pivot, int, n);
    91   for (j = 0; j < n; j++) pivot[j] = 0;
     109  int diag, col, row;
    92110
    93   row = col = 0;
    94   big = fabs(a[0][0]);
     111  ALLOCATE (colIndex, int, N);
     112  ALLOCATE (rowIndex, int, N);
     113  ALLOCATE (pivot, int, N);
     114  memset (pivot, 0, N*sizeof(int));
    95115
    96   for (i = 0; i < n; i++) {
    97     big = 0.0;
    98     for (j = 0; j < n; j++) {
    99       if (!finite(a[i][j])) goto escape;
    100       if (pivot[j] != 1) {
    101         for (k = 0; k < n; k++) {
    102           if (pivot[k] == 0) {
    103             if (fabs (a[j][k]) >= big) {
    104               big  = fabs (a[j][k]);
    105               row = j;
    106               col = k;
    107             }
    108           } else {
    109             if (pivot[k] > 1) goto escape;
    110           }
    111         }
     116  // init these so gcc does not complain
     117  maxval = 0.0;
     118  maxrow = maxcol = 0;
     119
     120  // we loop along the matrix diagonal, but we do not operate on the diagonal elements in
     121  // order instead, we are looking for the current max element and operating on that
     122  // diagonal element.  this is effectively column pivoting.  row pivoting is perfomed
     123  // explicitly
     124
     125  for (diag = 0; diag < N; diag++) {
     126
     127    maxval = 0.0;
     128
     129    // search for the next pivot
     130    for (row = 0; row < N; row++) {
     131      if (!finite(A[row][diag])) goto escape;
     132
     133      // if we have already operated on this row (pivot[row] is true), skip it
     134      if (pivot[row]) continue;
     135
     136      // if we have not yet operated on this row (pivot[row] is false), look for pivot for this row
     137      for (col = 0; col < N; col++) {
     138        if (pivot[col]) continue;
     139        if (fabs (A[row][col]) < maxval) continue;
     140        maxval = fabs (A[row][col]);
     141        maxrow = row;
     142        maxcol = col;
    112143      }
    113144    }
    114     pivot[col]++;
    115     if (row != col) {
    116       for (l = 0; l < n; l++) SWAP (a[row][l], a[col][l]);
    117       for (l = 0; l < m; l++) SWAP (b[row][l], b[col][l]);
     145
     146    // if pivot[maxcol] is set, we have already done this row: this implies a singular matrix
     147    if (pivot[maxcol]) goto escape;
     148    pivot[maxcol] = TRUE;
     149
     150    // if the selected pivot is off the diagonal, do a row swap
     151    if (maxrow != maxcol) {
     152      for (col = 0; col < N; col++) SWAP (A[maxrow][col], A[maxcol][col]);
     153      for (col = 0; col < M; col++) SWAP (B[maxrow][col], B[maxcol][col]);
    118154    }
    119     indexRow[i] = row;
    120     indexCol[i] = col;
    121     if (a[col][col] == 0.0) goto escape;
     155    rowIndex[diag] = maxrow;
     156    colIndex[diag] = maxcol;
     157    if (A[maxcol][maxcol] == 0.0) goto escape;
    122158
    123159    /* rescale by pivot reciprocal */
    124     temp = 1.0 / a[col][col];
    125     a[col][col] = 1.0;
    126     for (l = 0; l < n; l++) a[col][l] *= temp;
    127     for (l = 0; l < m; l++) b[col][l] *= temp;
    128  
     160    tmpval = 1.0 / A[maxcol][maxcol];
     161    A[maxcol][maxcol] = 1.0;
     162    for (col = 0; col < N; col++) A[maxcol][col] *= tmpval;
     163    for (col = 0; col < M; col++) B[maxcol][col] *= tmpval;
     164
    129165    /* adjust the elements above the pivot */
    130     for (ll = 0; ll < n; ll++) {
    131       if (ll != col) {
    132         temp = a[ll][col];
    133         a[ll][col] = 0.0;
    134         for (l = 0; l < n; l++) a[ll][l] -= a[col][l]*temp;
    135         for (l = 0; l < m; l++) b[ll][l] -= b[col][l]*temp;
    136       }
     166    for (row = 0; row < N; row++) {
     167      if (row == maxcol) continue;
     168      tmpval = A[row][maxcol];
     169      A[row][maxcol] = 0.0;
     170      for (col = 0; col < N; col++) A[row][col] -= A[maxcol][col]*tmpval;
     171      for (col = 0; col < M; col++) B[row][col] -= B[maxcol][col]*tmpval;
    137172    }
    138173  }
    139174
    140   for (l = n - 1; l >= 0; l--) {
    141     if (indexRow[l] != indexCol[l]) {
    142       for (k = 0; k < n; k++) SWAP (a[k][indexRow[l]], a[k][indexCol[l]]);
     175  // swap back the inverse matrix based on the row swaps above
     176  for (col = N - 1; col >= 0; col--) {
     177    if (rowIndex[col] != colIndex[col]) {
     178      for (row = 0; row < N; row++) SWAP (A[row][rowIndex[col]], A[row][colIndex[col]]);
    143179    }
    144180  }
     181
    145182  free (pivot);
    146   free (indexRow);
    147   free (indexCol);
     183  free (rowIndex);
     184  free (colIndex);
    148185  return (TRUE);
    149186
    150187escape:
    151188  free (pivot);
    152   free (indexRow);
    153   free (indexCol);
     189  free (rowIndex);
     190  free (colIndex);
    154191  return (FALSE);
    155192}
  • trunk/Ohana/src/relastro/include/relastro.h

    r15600 r16060  
    207207void unlock_image_db (FITS_DB *db);
    208208void create_image_db (FITS_DB *db);
     209void save_catalogs (Catalog *catalog, int Ncatalog);
    209210
    210211int           main                PROTO((int argc, char **argv));
  • trunk/Ohana/src/relastro/src/ConfigInit.c

    r15579 r16060  
    33void ConfigInit (int *argc, char **argv) {
    44
    5   double ZERO_POINT;
    65  char  *config, *file;
    76  char CatdirPhotcodeFile[256];
  • trunk/Ohana/src/relastro/src/FitPM.c

    r12731 r16060  
    5353  B[3][0] = YT;
    5454
    55   dgaussj (A, 4, B, 1);
     55  dgaussjordan (A, B, 4, 1);
    5656
    5757  fit[0].Ro = B[0][0];
  • trunk/Ohana/src/relastro/src/FitPMandPar.c

    r12731 r16060  
    7676  B[4][0] = PRX + PDY;
    7777
    78   dgaussj (A, 5, B, 1);
     78  dgaussjordan (A, B, 5, 1);
    7979
    8080  fit[0].Ro = B[0][0];
  • trunk/Ohana/src/relastro/src/FitPar.c

    r15600 r16060  
    77
    88  double **A, **B;
    9   double wx, wy, Wx, Wy, Tx, Ty, Tx2, Ty2, Xs, Ys, XT, YT;
    10   double PR, PD, PRT, PDT, PRX, PDY, PR2, PD2;
     9  double wx, wy, Wx, Wy, Xs, Ys;
     10  double PR, PD, PRX, PDY, PR2, PD2;
    1111
    1212  A = array_init (3, 3);
     
    5050  B[2][0] = PRX + PDY;
    5151
    52   dgaussj (A, 3, B, 1);
     52  dgaussjordan (A, B, 3, 1);
    5353
    5454  fit[0].Ro = B[0][0];
  • trunk/Ohana/src/relastro/src/ImageOps.c

    r15600 r16060  
    289289      LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, &image[im].coords);
    290290      break;
     291      default:
     292        fprintf (stderr, "invalid case");
     293        abort();
    291294    }
    292295  }
  • trunk/Ohana/src/relastro/src/MosaicOps.c

    r15600 r16060  
    2222void initMosaics (Image *image, int Nimage) {
    2323
    24   int i, j, status, found, NMOSAIC, *NIMLIST;
     24  int i, j, found, NMOSAIC;
    2525  unsigned int start, stop;
    26   char *pname;
    2726
    2827  Nmosaic = 0;
  • trunk/Ohana/src/relastro/src/UpdateObjects.c

    r15694 r16060  
    179179
    180180        default:
    181           fprintf (stderr, "programming error at %s, %s", __FILE__, __LINE__);
     181          fprintf (stderr, "programming error at %s, %d", __FILE__, __LINE__);
    182182          exit (2);
    183183      }   
  • trunk/Ohana/src/relastro/src/fitpoly.c

    r15590 r16060  
    137137  }     
    138138
    139   dgaussj (matrix, fit[0].Nelems, vector, 2);
     139  dgaussjordan (matrix, vector, fit[0].Nelems, 2);
    140140
    141141  for (i = 0; i < fit[0].Nelems; i++) {
  • trunk/Ohana/src/relastro/src/mkpolyterm.c

    r15590 r16060  
    6161      beta[1][0] = poly2d_eval (yfit, Nx, Ny, Xo, Yo);
    6262
    63       dgaussj (alpha, 2, beta, 1);
     63      dgaussjordan (alpha, beta, 2, 1);
    6464
    6565      Xo -= beta[0][0];
  • trunk/Ohana/src/relastro/src/plotstuff.c

    r13479 r16060  
    7979
    8080  float *values;
    81   int i, Nbytes;
     81  int i;
    8282
    8383  if (Npts < 1) return;
  • trunk/Ohana/src/relastro/src/relastro.c

    r15600 r16060  
    5555
    5656    default:
    57       fprintf (stderr, "programming error at %s:%s", __FILE__, __LINE__);
     57      fprintf (stderr, "programming error at %s:%d", __FILE__, __LINE__);
    5858      exit (2);
    5959  }
Note: See TracChangeset for help on using the changeset viewer.