IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7101


Ignore:
Timestamp:
May 9, 2006, 2:48:50 PM (20 years ago)
Author:
Paul Price
Message:

Moving psGaussJordan into psMatrix.c, renaming to psMatrixGJSolve.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psMatrix.c

    r4589 r7101  
    2121 *  @author Robert DeSonia, MHPCC
    2222 *
    23  *  @version $Revision: 1.36 $ $Name: not supported by cvs2svn $
    24  *  @date $Date: 2005-07-21 01:40:10 $
     23 *  @version $Revision: 1.37 $ $Name: not supported by cvs2svn $
     24 *  @date $Date: 2006-05-10 00:48:50 $
    2525 *
    2626 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    4444#include "psConstants.h"
    4545#include "psErrorText.h"
    46 
     46#include "psTrace.h"
    4747
    4848/*****************************************************************************/
     
    304304
    305305    return out;
     306}
     307
     308// This is a temporary gauss-jordan solver based on gene's
     309// version based on the Numerical Recipes version
     310bool psMatrixGJSolve(
     311    psImage *a,
     312    psVector *b)
     313{
     314    int *indxc,*indxr,*ipiv;
     315    int icol, irow;
     316    int i, j, k, l, ll;
     317    float big, dum, pivinv;
     318    psF64 *vector = b->data.F64;
     319    psF64 **matrix = a->data.F64;
     320
     321    int Nx = a->numCols;
     322
     323    indxc = psAlloc(Nx*sizeof(int));
     324    indxr = psAlloc(Nx*sizeof(int));
     325    ipiv  = psAlloc(Nx*sizeof(int));
     326    for (j = 0; j < Nx; j++) {
     327        ipiv[j] = 0;
     328    }
     329
     330    irow = icol = 0;
     331    big = fabs(matrix[0][0]);
     332
     333    for (i = 0; i < Nx; i++) {
     334        big = 0.0;
     335        for (j = 0; j < Nx; j++) {
     336            if (!isfinite(matrix[i][j])) {
     337                psTrace (__func__, 3, "Input matrix contains NaNs: matrix[%d][%d] is %.2f\n", i, j, matrix[i][j]);
     338                goto fescape;
     339            }
     340            if (ipiv[j] != 1) {
     341                for (k = 0; k < Nx; k++) {
     342                    if (ipiv[k] == 0) {
     343                        if (fabs (matrix[j][k]) >= big) {
     344                            big  = fabs(matrix[j][k]);
     345                            irow = j;
     346                            icol = k;
     347                        }
     348                    } else {
     349                        if (ipiv[k] > 1) {
     350                            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Singular Matrix (1).\n");
     351                            goto fescape;
     352                        }
     353                    }
     354                }
     355            }
     356        }
     357        ipiv[icol]++;
     358        if (irow != icol) {
     359            for (l = 0; l < Nx; l++) {
     360                PS_SWAP(matrix[irow][l], matrix[icol][l]);
     361            }
     362            PS_SWAP(vector[irow], vector[icol]);
     363        }
     364        indxr[i] = irow;
     365        indxc[i] = icol;
     366        if (matrix[icol][icol] == 0.0) {
     367            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Singular Matrix (2).\n");
     368            goto fescape;
     369        }
     370        pivinv = 1.0 / matrix[icol][icol];
     371        matrix[icol][icol] = 1.0;
     372        for (l = 0; l < Nx; l++) {
     373            matrix[icol][l] *= pivinv;
     374        }
     375        vector[icol] *= pivinv;
     376
     377        for (ll = 0; ll < Nx; ll++) {
     378            if (ll != icol) {
     379                dum = matrix[ll][icol];
     380                matrix[ll][icol] = 0.0;
     381                for (l = 0; l < Nx; l++) {
     382                    matrix[ll][l] -= matrix[icol][l]*dum;
     383                }
     384                vector[ll] -= vector[icol]*dum;
     385            }
     386        }
     387    }
     388
     389    for (l = Nx - 1; l >= 0; l--) {
     390        if (indxr[l] != indxc[l]) {
     391            for (k = 0; k < Nx; k++) {
     392                PS_SWAP(matrix[k][indxr[l]], matrix[k][indxc[l]]);
     393            }
     394        }
     395    }
     396    psFree(ipiv);
     397    psFree(indxr);
     398    psFree(indxc);
     399    return true;
     400
     401fescape:
     402    psFree(ipiv);
     403    psFree(indxr);
     404    psFree(indxc);
     405    return false;
    306406}
    307407
Note: See TracChangeset for help on using the changeset viewer.