IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 6, 2004, 2:06:06 PM (22 years ago)
Author:
desonia
Message:

another attempt to get astyle to get it right.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/dataManip/psMinimize.c

    r1406 r1407  
     1
    12/** @file  psMinimize.c
    23 *  \brief basic minimization functions
     
    910 *  @author George Gusciora, MHPCC
    1011 *
    11  *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-08-06 22:34:05 $
     12 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-08-07 00:06:06 $
    1314 *
    1415 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1516 */
    16 /*****************************************************************************/
     17
     18/*****************************************************************************/
     19
    1720/* INCLUDE FILES                                                             */
     21
    1822/*****************************************************************************/
    1923#include <stdlib.h>
     
    4044#include "psMinimize.h"
    4145#include "psMatrix.h"
    42 /*****************************************************************************/
     46
     47/*****************************************************************************/
     48
    4349/* DEFINE STATEMENTS                                                         */
     50
    4451/*****************************************************************************/
    4552#define MAX_LMM_ITERATIONS 100
     
    8390}
    8491
    85 
    86 /*****************************************************************************/
     92/*****************************************************************************/
     93
    8794/* TYPE DEFINITIONS                                                          */
     95
    8896/*****************************************************************************/
    8997typedef struct
    9098{
    91     size_t n;   // Number of data points points in domain.
    92     int paramCount;   // Number of non-masked parameters.
    93     psVector *restrict       initialGuess;
    94     const psImage *restrict  domain;
     99    size_t n;                   // Number of data points points in domain.
     100    int paramCount;             // Number of non-masked parameters.
     101    psVector *restrict initialGuess;
     102    const psImage *restrict domain;
    95103    const psVector *restrict data;
    96     const psVector *restrict  errors;
     104    const psVector *restrict errors;
    97105    const psVector *restrict paramMask;
    98106    float (*evalModel) (const psVector *, const psVector *);
     
    103111typedef struct
    104112{
    105     int paramCount;   // Number of non-masked parameters.
    106     psVector *restrict       initialGuess;
    107     const psVector *restrict  coord;
     113    int paramCount;             // Number of non-masked parameters.
     114    psVector *restrict initialGuess;
     115    const psVector *restrict coord;
    108116    const psVector *restrict paramMask;
    109117    float (*evalModel) (const psVector *, const psVector *);
     
    113121
    114122/*****************************************************************************/
     123
    115124/* GLOBAL VARIABLES                                                          */
     125
    116126/*****************************************************************************/
    117127
     
    119129
    120130/*****************************************************************************/
     131
    121132/* FILE STATIC VARIABLES                                                     */
     133
    122134/*****************************************************************************/
    123135
     
    125137
    126138/*****************************************************************************/
     139
    127140/* FUNCTION IMPLEMENTATION - LOCAL                                           */
     141
    128142/*****************************************************************************/
    129143
     
    143157 guess at the parameters, an option parameter mask, etc.
    144158 *****************************************************************************/
    145 double p_psMinFunc(const gsl_vector *params,
    146                    void *funcData)
    147 {
    148     int i;    // Loop index variable.
    149     int j;    // Loop index variable.
    150     float tmpf;    // Temporary floating point variable.
    151     const psVector *restrict coord   = ((psMinimizeData *) funcData)->coord;
    152     const psVector *restrict mask     = ((psMinimizeData *) funcData)->paramMask;
    153     psVector *restrict initialGuess = ((psMinimizeData *)funcData)->initialGuess;
    154     float (*evalModel)(const psVector *, const psVector *) =
    155         ((psMinimizeData *) funcData)->evalModel;
     159double p_psMinFunc(const gsl_vector * params, void *funcData)
     160{
     161    int i;                      // Loop index variable.
     162    int j;                      // Loop index variable.
     163    float tmpf;                 // Temporary floating point variable.
     164    const psVector *restrict coord = ((psMinimizeData *) funcData)->coord;
     165    const psVector *restrict mask = ((psMinimizeData *) funcData)->paramMask;
     166    psVector *restrict initialGuess = ((psMinimizeData *) funcData)->initialGuess;
     167    float (*evalModel) (const psVector *, const psVector *) = ((psMinimizeData *) funcData)->evalModel;
    156168    psVector *inputParameterList = NULL;
    157169
     
    163175    if (mask != NULL) {
    164176        j = 0;
    165         for (i=0;i<mask->n;i++) {
     177        for (i = 0; i < mask->n; i++) {
    166178            if (mask->data.U8[i] != 0) {
    167179                inputParameterList->data.F32[i] = initialGuess->data.F32[i];
     
    171183        }
    172184    } else {
    173         for (i=0;i<initialGuess->n;i++) {
     185        for (i = 0; i < initialGuess->n; i++) {
    174186            inputParameterList->data.F32[i] = gsl_vector_get(params, i);
    175187        }
     
    181193    // Free allocated memory and return the value of the function.
    182194    psFree(inputParameterList);
    183     return(tmpf);
     195    return (tmpf);
    184196}
    185197
     
    199211 in "params" and return those derivatives in this psVector.
    200212 *****************************************************************************/
    201 void p_psMinFuncDeriv(const gsl_vector *params,
    202                       void *funcData,
    203                       gsl_vector *df)
    204 {
    205     int i;    // Loop index variable.
    206     int j;    // Loop index variable.
    207     float tmpf;    // Temporary floating point variable.
    208     const psVector *restrict coord   = ((psMinimizeData *) funcData)->coord;
    209     const psVector *restrict mask     = ((psMinimizeData *) funcData)->paramMask;
    210     psVector *restrict initialGuess = ((psMinimizeData *)funcData)->initialGuess;
    211     float (*d_evalModel)(const psVector *, const psVector *, int) =
     213void p_psMinFuncDeriv(const gsl_vector * params, void *funcData, gsl_vector * df)
     214{
     215    int i;                      // Loop index variable.
     216    int j;                      // Loop index variable.
     217    float tmpf;                 // Temporary floating point variable.
     218    const psVector *restrict coord = ((psMinimizeData *) funcData)->coord;
     219    const psVector *restrict mask = ((psMinimizeData *) funcData)->paramMask;
     220    psVector *restrict initialGuess = ((psMinimizeData *) funcData)->initialGuess;
     221    float (*d_evalModel) (const psVector *, const psVector *, int) =
    212222        ((psMinimizeData *) funcData)->d_evalModel;
    213223    psVector *inputParameterList = NULL;
     
    220230    if (mask != NULL) {
    221231        j = 0;
    222         for (i=0;i<mask->n;i++) {
     232        for (i = 0; i < mask->n; i++) {
    223233            if (mask->data.U8[i] != 0) {
    224234                inputParameterList->data.F32[i] = initialGuess->data.F32[i];
     
    228238        }
    229239    } else {
    230         for (i=0;i<initialGuess->n;i++) {
     240        for (i = 0; i < initialGuess->n; i++) {
    231241            inputParameterList->data.F32[i] = gsl_vector_get(params, i);
    232242        }
     
    235245    // Evaluate the derivative w.r.t. each parameter.
    236246    // NOTE: we can probably remove the calls for masked parameters.
    237     for (i=0;i<initialGuess->n;i++) {
     247    for (i = 0; i < initialGuess->n; i++) {
    238248        tmpf = d_evalModel(inputParameterList, coord, i);
    239249        gsl_vector_set(df, i, tmpf);
     
    247257    Compute both p_psMinFunc and p_psMinFuncDeriv together.
    248258 *****************************************************************************/
    249 void p_psMinFuncFuncDeriv(const gsl_vector *params,
    250                           void *funcData,
    251                           double *f,
    252                           gsl_vector *df)
     259void p_psMinFuncFuncDeriv(const gsl_vector * params, void *funcData, double *f, gsl_vector * df)
    253260{
    254261    *f = p_psMinFunc(params, funcData);
     
    281288  expected value and divide by the error.
    282289 *****************************************************************************/
    283 int p_psMinChi2Func(const gsl_vector *params,
    284                     void *funcData,
    285                     gsl_vector *outData)
    286 {
    287     int i;    // Loop index variable.
    288     int j;    // Loop index variable.
    289     float tmpf;    // Temporary floating point variable.
    290     const psImage *restrict  domain   = ((psMinChi2Data *)funcData)->domain;
    291     const psVector *restrict data     = ((psMinChi2Data *)funcData)->data;
    292     const psVector *restrict errors   = ((psMinChi2Data *) funcData)->errors;
    293     const psVector *restrict mask     = ((psMinChi2Data *) funcData)->paramMask;
    294     psVector *restrict initialGuess = ((psMinChi2Data *)funcData)->initialGuess;
    295     float (*evalModel)(const psVector *, const psVector *) = ((psMinChi2Data *) funcData)->evalModel;
     290int p_psMinChi2Func(const gsl_vector * params, void *funcData, gsl_vector * outData)
     291{
     292    int i;                      // Loop index variable.
     293    int j;                      // Loop index variable.
     294    float tmpf;                 // Temporary floating point variable.
     295    const psImage *restrict domain = ((psMinChi2Data *) funcData)->domain;
     296    const psVector *restrict data = ((psMinChi2Data *) funcData)->data;
     297    const psVector *restrict errors = ((psMinChi2Data *) funcData)->errors;
     298    const psVector *restrict mask = ((psMinChi2Data *) funcData)->paramMask;
     299    psVector *restrict initialGuess = ((psMinChi2Data *) funcData)->initialGuess;
     300    float (*evalModel) (const psVector *, const psVector *) = ((psMinChi2Data *) funcData)->evalModel;
    296301    psVector *inputParameterList = NULL;
    297302    psVector *tmpVecPtr = NULL;
     
    307312    if (mask != NULL) {
    308313        j = 0;
    309         for (i=0;i<mask->n;i++) {
     314        for (i = 0; i < mask->n; i++) {
    310315            if (mask->data.U8[i] != 0) {
    311316                inputParameterList->data.F32[i] = initialGuess->data.F32[i];
     
    315320        }
    316321    } else {
    317         for (i=0;i<initialGuess->n;i++) {
     322        for (i = 0; i < initialGuess->n; i++) {
    318323            inputParameterList->data.F32[i] = gsl_vector_get(params, i);
    319324        }
     
    321326
    322327    // Evaluate the function at each data point.
    323     for (i=0;i<domain->numRows;i++) {
    324         for (j=0;j<domain->numCols;j++) {
     328    for (i = 0; i < domain->numRows; i++) {
     329        for (j = 0; j < domain->numCols; j++) {
    325330            tmpVecPtr->data.F32[j] = domain->data.F32[i][j];
    326331        }
    327332        tmpf = evalModel(tmpVecPtr, inputParameterList);
    328333
    329         gsl_vector_set(outData, i, (tmpf - data->data.F32[i])/
    330                        errors->data.F32[i]);
     334        gsl_vector_set(outData, i, (tmpf - data->data.F32[i]) / errors->data.F32[i]);
    331335    }
    332336
     
    354358 and returned in this data structure.
    355359 *****************************************************************************/
    356 int p_psMinChi2FuncDeriv(const gsl_vector *params,
    357                          void *funcData,
    358                          gsl_matrix *J)
    359 {
    360     const psImage *restrict domain   = ((psMinChi2Data *)funcData)->domain;
    361     const psVector *restrict errors   = ((psMinChi2Data *) funcData)->errors;
    362     const psVector *restrict mask     = ((psMinChi2Data *) funcData)->paramMask;
    363     psVector *restrict initialGuess = ((psMinChi2Data *)funcData)->initialGuess;
     360int p_psMinChi2FuncDeriv(const gsl_vector * params, void *funcData, gsl_matrix * J)
     361{
     362    const psImage *restrict domain = ((psMinChi2Data *) funcData)->domain;
     363    const psVector *restrict errors = ((psMinChi2Data *) funcData)->errors;
     364    const psVector *restrict mask = ((psMinChi2Data *) funcData)->paramMask;
     365    psVector *restrict initialGuess = ((psMinChi2Data *) funcData)->initialGuess;
    364366    psVector *inputParameterList = NULL;
    365367    psVector *tmpVecPtr = NULL;
    366     float (*d_evalModel)(const psVector *, const psVector *, int) = ((psMinChi2Data *) funcData)->d_evalModel;
     368    float (*d_evalModel) (const psVector *, const psVector *, int) =
     369        ((psMinChi2Data *) funcData)->d_evalModel;
    367370
    368371    size_t i;
     
    380383    if (mask != NULL) {
    381384        j = 0;
    382         for (i=0;i<mask->n;i++) {
     385        for (i = 0; i < mask->n; i++) {
    383386            if (mask->data.U8[i] != 0) {
    384387                inputParameterList->data.F32[i] = initialGuess->data.F32[i];
     
    388391        }
    389392    } else {
    390         for (i=0;i<initialGuess->n;i++) {
     393        for (i = 0; i < initialGuess->n; i++) {
    391394            inputParameterList->data.F32[i] = gsl_vector_get(params, i);
    392395        }
     
    394397
    395398    // Evaluate the derivtaive at each data point, and w.r.t. each parameter.
    396     for (i=0;i<domain->numRows;i++) {
    397         for (j=0;j<tmpVecPtr->n;j++) {
     399    for (i = 0; i < domain->numRows; i++) {
     400        for (j = 0; j < tmpVecPtr->n; j++) {
    398401            tmpVecPtr->data.F32[j] = domain->data.F32[i][j];
    399402        }
    400403
    401         for (j=0;j<inputParameterList->n;j++) {
     404        for (j = 0; j < inputParameterList->n; j++) {
    402405            tmpf = d_evalModel(tmpVecPtr, inputParameterList, j);
    403             gsl_matrix_set(J, i, j, (tmpf/errors->data.F32[i]));
     406            gsl_matrix_set(J, i, j, (tmpf / errors->data.F32[i]));
    404407        }
    405408    }
     
    410413}
    411414
    412 
    413 int p_psMinChi2FuncFuncDeriv(const gsl_vector *params,
    414                              void *funcData,
    415                              gsl_vector *f,
    416                              gsl_matrix *J)
     415int p_psMinChi2FuncFuncDeriv(const gsl_vector * params, void *funcData, gsl_vector * f, gsl_matrix * J)
    417416{
    418417    p_psMinChi2Func(params, funcData, f);
     
    421420    return GSL_SUCCESS;
    422421}
    423 
    424422
    425423/******************************************************************************
     
    428426returned as a psVector sums.
    429427 *****************************************************************************/
    430 void p_psBuildSums1D(double x,
    431                      int polyOrder,
    432                      psVector *sums)
    433 {
    434     int       i = 0;
    435     double    xSum = 0.0;
     428void p_psBuildSums1D(double x, int polyOrder, psVector * sums)
     429{
     430    int i = 0;
     431    double xSum = 0.0;
    436432
    437433    xSum = 1.0;
    438     for(i=0;i<=polyOrder;i++) {
     434    for (i = 0; i <= polyOrder; i++) {
    439435        sums->data.F64[i] = xSum;
    440         xSum*= x;
    441     }
    442 }
    443 
     436        xSum *= x;
     437    }
     438}
    444439
    445440/******************************************************************************
     
    448443 *****************************************************************************/
    449444psVector *psBuildImageScalingFactors(int x)
    450 
    451445{
    452446    int i = 0;                  // loop index variable.
    453447    psVector *imageScalingFactors = NULL;
    454448
    455 
    456449    imageScalingFactors = psVectorAlloc(x, PS_TYPE_F32);
    457450
    458     for (i=0;i<x;i++) {
    459         imageScalingFactors->data.F32[i] = (((float) 2*i) / ((float) x)) - 1.0;
    460     }
    461 
    462     return(imageScalingFactors);
     451    for (i = 0; i < x; i++) {
     452        imageScalingFactors->data.F32[i] = (((float)2 * i) / ((float)x)) - 1.0;
     453    }
     454
     455    return (imageScalingFactors);
    463456}
    464457
     
    481474    *flag   Set this to 1 if we must recalculate the coefficients.
    482475 *****************************************************************************/
    483 void p_psPolyOrderCheck(float **A,
    484                         int N,
    485                         int *indx,
    486                         float *B,
    487                         int polyOrder,
    488                         int *flag)
    489 {
    490     float     **y = NULL;  // This 2-D matrix will hold A^-1
    491     float      *col = NULL;             // misc NumerRecipes data structure
    492     float      *error=NULL;             // will hold the sqrt() of the
     476void p_psPolyOrderCheck(float **A, int N, int *indx, float *B, int polyOrder, int *flag)
     477{
     478    float **y = NULL;           // This 2-D matrix will hold A^-1
     479    float *col = NULL;          // misc NumerRecipes data structure
     480    float *error = NULL;        // will hold the sqrt() of the
     481
    493482    // diagonal of y[][].
    494     int         i=0;                    // loop-index variable
    495     int         j=0;                    // loop-index variable
    496     int         numPolyTerms = 0;       // The number of terms in the
     483    int i = 0;                  // loop-index variable
     484    int j = 0;                  // loop-index variable
     485    int numPolyTerms = 0;       // The number of terms in the
     486
    497487    // polynomial.
    498     int         lastTerm = 0;           // The index location of the first
     488    int lastTerm = 0;           // The index location of the first
     489
    499490    // n-th order term in array B[].
    500     int         firstTerm = 0;          // Index location of last such term.
     491    int firstTerm = 0;          // Index location of last such term.
    501492
    502493    // Allocate the necessary data structures for this procedure...
    503     error = (float *) psAlloc((N + 1) * sizeof(float));
    504     col = (float *) psAlloc((N + 1) * sizeof(float));
    505     y = (float **) psAlloc((N + 1) * sizeof(float *));
    506     for(i=1;i<=N;i++) {
    507         y[i] = (float *) psAlloc((N + 1) * sizeof(float));
     494    error = (float *)psAlloc((N + 1) * sizeof(float));
     495    col = (float *)psAlloc((N + 1) * sizeof(float));
     496    y = (float **)psAlloc((N + 1) * sizeof(float *));
     497    for (i = 1; i <= N; i++) {
     498        y[i] = (float *)psAlloc((N + 1) * sizeof(float));
    508499    }
    509500
    510501    // Invert the matrix A and put the result in y[][].  This code is taken
    511502    // from Numerical Recipes in C page 48.
    512     for(j=1;j<=N;j++) {
    513         for(i=1;i<=N;i++) {
     503    for (j = 1; j <= N; j++) {
     504        for (i = 1; i <= N; i++) {
    514505            col[i] = 0.0;
    515506        }
    516507        col[j] = 1.0;
    517508        // NOTE: substitue the LUD rotine
    518         //        lubksb(A, N, indx, col);
    519         for(i=1;i<=N;i++) {
     509        // lubksb(A, N, indx, col);
     510        for (i = 1; i <= N; i++) {
    520511            y[i][j] = col[i];
    521512        }
     
    527518    // terms and check if they are consistent with zero.
    528519
    529     numPolyTerms = (((polyOrder+1) * (polyOrder + 2)) / 2);
     520    numPolyTerms = (((polyOrder + 1) * (polyOrder + 2)) / 2);
    530521    lastTerm = numPolyTerms + 1;
    531522    firstTerm = lastTerm - polyOrder;
    532523    *flag = 1;
    533     for (i=firstTerm; i<=lastTerm; i++) {
     524    for (i = firstTerm; i <= lastTerm; i++) {
    534525        #ifdef DARWIN
    535526        error[i] = (float)sqrt(y[i][i]);
     
    539530        #endif
    540531
    541         if (!((B[i]  <= (2.0f * error[i])) &&
    542                 ((-2.0f * error[i]) <= B[i]))) {
     532        if (!((B[i] <= (2.0f * error[i])) && ((-2.0f * error[i]) <= B[i]))) {
    543533            *flag = 0;
    544534        }
     
    548538    psFree(error);
    549539    psFree(col);
    550     for(j=1;j<=N;j++) {
     540    for (j = 1; j <= N; j++) {
    551541        psFree(y[j]);
    552542    }
     
    554544}
    555545
    556 
    557 
    558 
    559 /*****************************************************************************/
     546/*****************************************************************************/
     547
    560548/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    561 /*****************************************************************************/
    562 
    563 
     549
     550/*****************************************************************************/
    564551
    565552/******************************************************************************
     
    569556parameters of that function such that the ...
    570557 *****************************************************************************/
    571 psVector *
    572 psMinimize(psVector *restrict initialGuess,
    573            float (*myFunction)(const psVector *restrict, const psVector *restrict),
    574            float (*myFunctionDeriv)(const psVector *restrict, const psVector *restrict, int),
    575            const psVector *restrict coord,
    576            const psVector *restrict paramMask)
     558psVector *psMinimize(psVector * restrict initialGuess,
     559                     float (*myFunction) (const psVector * restrict, const psVector * restrict),
     560                     float (*myFunctionDeriv) (const psVector * restrict, const psVector * restrict, int),
     561                     const psVector * restrict coord, const psVector * restrict paramMask)
    577562{
    578563    int status;
     
    607592    // for the parameters.
    608593    if (paramMask != NULL) {
    609         for (i=0;i<paramMask->n;i++) {
     594        for (i = 0; i < paramMask->n; i++) {
    610595            if (paramMask->data.U8[i] != 0) {
    611596                inputData.paramCount++;
     
    613598        }
    614599    } else {
    615         inputData.paramCount= initialGuess->n;
     600        inputData.paramCount = initialGuess->n;
    616601    }
    617602
     
    622607    if (paramMask != NULL) {
    623608        j = 0;
    624         for (i=0;i<initialGuess->n;i++) {
     609        for (i = 0; i < initialGuess->n; i++) {
    625610            if (paramMask->data.U8[i] == 0) {
    626611                gsl_vector_set(x, j++, initialGuess->data.F32[i]);
     
    628613        }
    629614    } else {
    630         for (i=0;i<initialGuess->n;i++) {
     615        for (i = 0; i < initialGuess->n; i++) {
    631616            gsl_vector_set(x, i, initialGuess->data.F32[i]);
    632617        }
     
    651636
    652637        if (status == GSL_SUCCESS)
    653             printf ("Minimum found at:\n");
     638            printf("Minimum found at:\n");
    654639
    655640    } while (status == GSL_CONTINUE && iter < MAX_MINIMIZE_ITERATIONS);
     
    660645    if (paramMask != NULL) {
    661646        j = 0;
    662         for (i=0;i<initialGuess->n;i++) {
     647        for (i = 0; i < initialGuess->n; i++) {
    663648            if (paramMask->data.U8[i] == 0) {
    664649                initialGuess->data.F32[i] = gsl_vector_get(s->x, j++);
     
    668653        }
    669654    } else {
    670         for (i=0;i<initialGuess->n;i++) {
     655        for (i = 0; i < initialGuess->n; i++) {
    671656            initialGuess->data.F32[i] = gsl_vector_get(s->x, i);
    672657        }
    673658    }
    674     return(initialGuess);
    675 }
    676 
    677 
    678 
    679 
    680 
     659    return (initialGuess);
     660}
    681661
    682662/******************************************************************************
     
    684664    such that they best fit the supplied data points.
    685665 *****************************************************************************/
    686 psVector *
    687 psMinimizeChi2(float (*evalModel)(const psVector *restrict, const psVector *restrict),
    688                float (*DevalModel)(const psVector *restrict, const psVector *restrict, int),
    689                const psImage *restrict domain,
    690                const psVector *restrict data,
    691                const psVector *restrict errors,
    692                psVector *restrict initialGuess,
    693                const psVector *restrict paramMask,
    694                float *chiSq)
    695 {
    696     int numData = domain->numRows; // Number of data points
    697     int status;    // Return status for the GSL solver.
    698     int i = 0;    // Loop index variable.
    699     int j = 0;    // Loop index variable.
    700     int iter = 0;   // Iteration counter.
    701     gsl_multifit_function_fdf f; // GSL structure that contains the
     666psVector *psMinimizeChi2(float (*evalModel) (const psVector * restrict, const psVector * restrict),
     667                         float (*DevalModel) (const psVector * restrict, const psVector * restrict, int),
     668                         const psImage * restrict domain,
     669                         const psVector * restrict data,
     670                         const psVector * restrict errors,
     671                         psVector * restrict initialGuess, const psVector * restrict paramMask, float *chiSq)
     672{
     673    int numData = domain->numRows;      // Number of data points
     674    int status;                 // Return status for the GSL solver.
     675    int i = 0;                  // Loop index variable.
     676    int j = 0;                  // Loop index variable.
     677    int iter = 0;               // Iteration counter.
     678    gsl_multifit_function_fdf f;        // GSL structure that contains the
     679
    702680    // functions/derivative to be solved.
    703     double *xInit = NULL;        // The initial guess at the parameters
     681    double *xInit = NULL;       // The initial guess at the parameters
     682
    704683    // with masked parameters removed.
    705684    const gsl_multifit_fdfsolver_type *T;
     685
    706686    // This tells GSL to use the Levenberg-
    707687    // Marquardt algorithm for chi2
    708688    // minimization.
    709     gsl_multifit_fdfsolver *s; // GSL data structure.
     689    gsl_multifit_fdfsolver *s;  // GSL data structure.
    710690    psMinChi2Data inputData;
    711691    float chiSqOld = 0.0;
     
    721701    PS_CHECK_VECTOR_SIZE_EQUAL(data, errors);
    722702    if (domain->numRows != data->n) {
    723         psAbort(__func__,"Number of data points and data values not equal.");
     703        psAbort(__func__, "Number of data points and data values not equal.");
    724704    }
    725705    if (paramMask != NULL) {
     
    743723    // for the parameters.
    744724    if (paramMask != NULL) {
    745         for (i=0;i<paramMask->n;i++) {
     725        for (i = 0; i < paramMask->n; i++) {
    746726            if (paramMask->data.U8[i] != 0) {
    747727                inputData.paramCount++;
     
    749729        }
    750730    } else {
    751         inputData.paramCount= initialGuess->n;
     731        inputData.paramCount = initialGuess->n;
    752732    }
    753733
     
    755735    // the vector inputParameterList.  If the paramMask is not NULL, then those
    756736    // parameters are masked out.
    757     xInit = (double *) psAlloc(inputData.paramCount * sizeof(double));
     737    xInit = (double *)psAlloc(inputData.paramCount * sizeof(double));
    758738    if (paramMask != NULL) {
    759739        j = 0;
    760         for (i=0;i<initialGuess->n;i++) {
     740        for (i = 0; i < initialGuess->n; i++) {
    761741            if (paramMask->data.U8[i] == 0) {
    762742                xInit[j++] = initialGuess->data.F32[i];
     
    764744        }
    765745    } else {
    766         for (i=0;i<initialGuess->n;i++) {
     746        for (i = 0; i < initialGuess->n; i++) {
    767747            xInit[i] = initialGuess->data.F32[i];
    768748        }
     
    771751    const gsl_rng_type *type;
    772752    gsl_rng *r;
     753
    773754    gsl_rng_env_setup();
    774755
     
    789770
    790771    gsl_vector_view x = gsl_vector_view_array(xInit, inputData.paramCount);
     772
    791773    T = gsl_multifit_fdfsolver_lmsder;
    792774    s = gsl_multifit_fdfsolver_alloc(T, numData, inputData.paramCount);
     
    796778    do {
    797779        iter++;
    798         for (i=0;i<initialGuess->n;i++) {
     780        for (i = 0; i < initialGuess->n; i++) {
    799781            printf("Iteration %d: parameter %d is %.3f\n", iter, i, gsl_vector_get(s->x, i));
    800782        }
     
    802784        status = gsl_multifit_fdfsolver_iterate(s);
    803785        printf("gsl_multifit_fdfsolver_iterate() status is %s\n", gsl_strerror(status));
    804         for (i=0;i<initialGuess->n;i++) {
     786        for (i = 0; i < initialGuess->n; i++) {
    805787            printf("Iteration %d: parameter %d is %.3f\n", iter, i, gsl_vector_get(s->x, i));
    806788        }
     
    810792            psAbort(__func__, "gsl_multifit_fdfsolver_iterate(%s)\n", gsl_strerror(status));
    811793        }
    812 
    813794        // Test if the parameters changed by a small enough amount.
    814795        // NOTE: This wasn't working right when the parameters fit exactly.
    815796        // Figure out why.
    816         //        status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
     797        // status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);
    817798
    818799        // We test for convergence if chiSquared changes by less than 1.0
     
    829810    } while (status == GSL_CONTINUE && iter < MAX_LMM_ITERATIONS);
    830811
    831 
    832812    // In the above steps we had removed the masked elements from the
    833813    // the solver.  This next code blocks puts those masked elements
     
    835815    if (paramMask != NULL) {
    836816        j = 0;
    837         for (i=0;i<initialGuess->n;i++) {
     817        for (i = 0; i < initialGuess->n; i++) {
    838818            if (paramMask->data.U8[i] == 0) {
    839819                initialGuess->data.F32[i] = gsl_vector_get(s->x, j++);
     
    843823        }
    844824    } else {
    845         for (i=0;i<initialGuess->n;i++) {
     825        for (i = 0; i < initialGuess->n; i++) {
    846826            initialGuess->data.F32[i] = gsl_vector_get(s->x, i);
    847827        }
     
    857837
    858838    // Bye bye.
    859     return(initialGuess);
    860 }
    861 
     839    return (initialGuess);
     840}
    862841
    863842/******************************************************************************
     
    868847NOTE: yErr is currently ignored.
    869848 *****************************************************************************/
    870 psPolynomial1D *
    871 psVectorFitPolynomial1D(psPolynomial1D *myPoly,
    872                         const psVector *restrict x,
    873                         const psVector *restrict y,
    874                         const psVector *restrict yErr)
     849psPolynomial1D *psVectorFitPolynomial1D(psPolynomial1D * myPoly,
     850                                        const psVector * restrict x,
     851                                        const psVector * restrict y, const psVector * restrict yErr)
    875852{
    876853    int polyOrder = myPoly->n;
     
    879856    psVector *B = NULL;
    880857    psVector *outPerm = NULL;
    881     psVector *X = NULL;   // NOTE: do we need this?
     858    psVector *X = NULL;         // NOTE: do we need this?
    882859    psVector *coeffs = NULL;
    883860    int i = 0;
     
    886863    psVector *xSums = NULL;
    887864
    888     //    printf("psVectorFitPolynomial1D()\n");
    889     //    for (i=0;i<x->n;i++) {
    890     //        printf("(x, y, yErr) is (%f, %f, %f)\n", x->data.F64[i], y->data.F64[i], yErr->data.F64[i]);
    891     //    }
     865    // printf("psVectorFitPolynomial1D()\n");
     866    // for (i=0;i<x->n;i++) {
     867    // printf("(x, y, yErr) is (%f, %f, %f)\n", x->data.F64[i], y->data.F64[i], yErr->data.F64[i]);
     868    // }
    892869
    893870    PS_CHECK_NULL_1DPOLY(myPoly);
     
    901878    PS_CHECK_VECTOR_SIZE_EQUAL(y, yErr);
    902879
    903     A       = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64);
    904     ALUD    = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64);
    905     B       = psVectorAlloc(polyOrder, PS_TYPE_F64);
    906     coeffs  = psVectorAlloc(polyOrder, PS_TYPE_F64);
    907     X       = psVectorAlloc(x->n, PS_TYPE_F64);
     880    A = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64);
     881    ALUD = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64);
     882    B = psVectorAlloc(polyOrder, PS_TYPE_F64);
     883    coeffs = psVectorAlloc(polyOrder, PS_TYPE_F64);
     884    X = psVectorAlloc(x->n, PS_TYPE_F64);
    908885    outPerm = psVectorAlloc(polyOrder, PS_TYPE_F64);
    909     xSums   = psVectorAlloc(1+2*polyOrder, PS_TYPE_F64);
     886    xSums = psVectorAlloc(1 + 2 * polyOrder, PS_TYPE_F64);
    910887
    911888    // Initialize data structures.
    912     for(i=0;i<(polyOrder);i++) {
     889    for (i = 0; i < (polyOrder); i++) {
    913890        B->data.F64[i] = 0.0;
    914891        coeffs->data.F64[i] = 0.0;
    915892        outPerm->data.F64[i] = 0.0;
    916         for(j=0;j<(polyOrder);j++) {
     893        for (j = 0; j < (polyOrder); j++) {
    917894            A->data.F64[i][j] = 0.0;
    918895            ALUD->data.F64[i][j] = 0.0;
    919896        }
    920897    }
    921     for (i=0;i<X->n;i++) {
     898    for (i = 0; i < X->n; i++) {
    922899        X->data.F64[i] = x->data.F64[i];
    923900    }
    924901
    925902    // Build the B and A data structs.
    926     for (i=0;i<X->n;i++) {
    927         p_psBuildSums1D(X->data.F64[i], 2*polyOrder, xSums);
    928 
    929         for(k=0;k<(polyOrder);k++) {
    930             B->data.F64[k]+= y->data.F64[i] * xSums->data.F64[k];
    931         }
    932 
    933         for(k=0;k<(polyOrder);k++) {
    934             for(j=0;j<(polyOrder);j++) {
    935                 A->data.F64[k][j]+= xSums->data.F64[k+j];
     903    for (i = 0; i < X->n; i++) {
     904        p_psBuildSums1D(X->data.F64[i], 2 * polyOrder, xSums);
     905
     906        for (k = 0; k < (polyOrder); k++) {
     907            B->data.F64[k] += y->data.F64[i] * xSums->data.F64[k];
     908        }
     909
     910        for (k = 0; k < (polyOrder); k++) {
     911            for (j = 0; j < (polyOrder); j++) {
     912                A->data.F64[k][j] += xSums->data.F64[k + j];
    936913            }
    937914        }
     
    941918    coeffs = psMatrixLUSolve(coeffs, ALUD, B, outPerm);
    942919
    943     for(k=0;k<(polyOrder);k++) {
     920    for (k = 0; k < (polyOrder); k++) {
    944921        myPoly->coeff[k] = coeffs->data.F64[k];
    945         //        printf("myPoly->coeff[%d] is %f\n", k, myPoly->coeff[k]);
    946     }
    947 
    948 
    949     //    for (i=0;i<x->n;i++) {
    950     //        printf("HMMM: psEvalPolynomial1D(%f) is %f\n", x->data.F64[i], psEvalPolynomial1D(x->data.F64[i], myPoly));
    951     //    }
     922        // printf("myPoly->coeff[%d] is %f\n", k, myPoly->coeff[k]);
     923    }
     924
     925    // for (i=0;i<x->n;i++) {
     926    // printf("HMMM: psEvalPolynomial1D(%f) is %f\n", x->data.F64[i], psEvalPolynomial1D(x->data.F64[i],
     927    // myPoly));
     928    // }
    952929
    953930    psFree(A);
     
    959936    psFree(xSums);
    960937
    961     return(myPoly);
    962 }
     938    return (myPoly);
     939}
Note: See TracChangeset for help on using the changeset viewer.