IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 3, 2004, 1:35:47 PM (22 years ago)
Author:
gusciora
Message:

Modified the psVectorfit() tests and code.

File:
1 edited

Legend:

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

    r1921 r1945  
    99 *  @author George Gusciora, MHPCC
    1010 *
    11  *  @version $Revision: 1.50 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-09-28 23:27:37 $
     11 *  @version $Revision: 1.51 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-10-03 23:35:47 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    682682    psScalar *fScalar;
    683683
    684 
    685684    // XXX: These assignments appear too simple to warrant code and
    686685    // variable declarations.  I retain them here to maintain coherence
     
    714713        fScalar = p_psVectorInterpolate((psVector *) x32, (psVector *) y32,
    715714                                        3, tmpScalar);
     715
    716716        f->data.F64[i] = (double) fScalar->data.F32;
     717        psFree(fScalar);
    717718
    718719        psTrace(".psLib.dataManip.p_psVectorFitPolynomial1DCheby", 6,
     
    724725    // coefficients of the Chebyshev polynomial: NR 5.8.7.
    725726    fac = 2.0/((float) n);
    726     for (j=0;j<n;j++) {
     727
     728    // XXX: is this loop bound correct?
     729    for (j=0;j<myPoly->n;j++) {
    727730        sum = 0.0;
    728731        for (k=0;k<n;k++) {
     
    730733                  cos(M_PI * ((float) j) * (0.5 + ((float) k)) / ((float) n));
    731734        }
     735
    732736        myPoly->coeff[j] = fac * sum;
    733737    }
    734738
    735739    // XXX: Must free memory.
     740    psFree(f);
    736741    psFree(x32);
    737742    psFree(y32);
    738743    psFree(tmpScalar);
    739     psFree(fScalar);
     744
    740745    return(myPoly);
    741746}
     
    745750polynomial of degree myPoly to the data points (x, y) and return the
    746751coefficients of that polynomial.
    747  
    748 XXX: yErr is currently ignored.
    749752 
    750753XXX: must add type F32 (currently F64 only).
     
    795798
    796799    // Initialize data structures.
    797     for (i = 0; i < (polyOrder); i++) {
     800    for (i = 0; i < polyOrder; i++) {
    798801        B->data.F64[i] = 0.0;
    799802        coeffs->data.F64[i] = 0.0;
    800803        outPerm->data.F64[i] = 0.0;
    801         for (j = 0; j < (polyOrder); j++) {
     804        for (j = 0; j < polyOrder; j++) {
    802805            A->data.F64[i][j] = 0.0;
    803806            ALUD->data.F64[i][j] = 0.0;
     
    809812
    810813    // Build the B and A data structs.
    811     for (i = 0; i < X->n; i++) {
    812         p_psBuildSums1D(X->data.F64[i], 2 * polyOrder, xSums);
    813 
    814         for (k = 0; k < (polyOrder); k++) {
    815             B->data.F64[k] += y->data.F64[i] * xSums->data.F64[k];
    816         }
    817 
    818         for (k = 0; k < (polyOrder); k++) {
    819             for (j = 0; j < (polyOrder); j++) {
    820                 A->data.F64[k][j] += xSums->data.F64[k + j];
     814    if (yErr == NULL) {
     815        for (i = 0; i < X->n; i++) {
     816            p_psBuildSums1D(X->data.F64[i], 2 * polyOrder, xSums);
     817
     818            for (k = 0; k < polyOrder; k++) {
     819                B->data.F64[k] += y->data.F64[i] * xSums->data.F64[k];
     820            }
     821
     822            for (k = 0; k < polyOrder; k++) {
     823                for (j = 0; j < polyOrder; j++) {
     824                    A->data.F64[k][j] += xSums->data.F64[k + j];
     825                }
     826            }
     827        }
     828    } else {
     829        for (i = 0; i < X->n; i++) {
     830            p_psBuildSums1D(X->data.F64[i], 2 * polyOrder, xSums);
     831
     832            for (k = 0; k < polyOrder; k++) {
     833                B->data.F64[k] += y->data.F64[i] * xSums->data.F64[k] /
     834                                  yErr->data.F64[i];
     835            }
     836
     837            for (k = 0; k < polyOrder; k++) {
     838                for (j = 0; j < polyOrder; j++) {
     839                    A->data.F64[k][j] += xSums->data.F64[k + j] /
     840                                         yErr->data.F64[i];
     841                }
    821842            }
    822843        }
     
    826847    coeffs = psMatrixLUSolve(coeffs, ALUD, B, outPerm);
    827848
    828     for (k = 0; k < (polyOrder); k++) {
     849    for (k = 0; k < polyOrder; k++) {
    829850        myPoly->coeff[k] = coeffs->data.F64[k];
    830851        // printf("myPoly->coeff[%d] is %f\n", k, myPoly->coeff[k]);
     
    853874myPoly to the data points (x, y) and return the coefficients of that
    854875polynomial.
    855  
    856 XXX: yErr is currently ignored.
    857876 
    858877XXX: must add type F32 (currently F64 only).
     
    870889    psVector *myYErr = NULL;
    871890
    872     PS_CHECK_NULL_1DPOLY(myPoly);
    873     PS_CHECK_NULL_VECTOR(y);
    874     PS_CHECK_EMPTY_VECTOR(y);
     891    PS_CHECK_NULL_1DPOLY_ACTION(myPoly, 0);
     892    PS_CHECK_NULL_VECTOR_ACTION(y, 0);
     893    PS_CHECK_EMPTY_VECTOR_ACTION(y, 0);
    875894
    876895    // If yErr==NULL, set all errors equal.
     
    880899
    881900        if (y->type.type == PS_TYPE_F32) {
    882             for (i=0;i<yErr->n;i++) {
     901            for (i=0;i<myYErr->n;i++) {
    883902                myYErr->data.F32[i] = 1.0;
    884903            }
    885904        } else if (y->type.type == PS_TYPE_F64) {
    886             for (i=0;i<yErr->n;i++) {
     905            for (i=0;i<myYErr->n;i++) {
    887906                myYErr->data.F64[i] = 1.0;
    888907            }
     
    893912
    894913    // If x==NULL, create an myX vector with x values set to (0:n), and if
    895     // this is a CHebyshev polynomial, we must scale to (-1:1).
     914    // this is a Chebyshev polynomial, we must scale to (-1:1).
    896915
    897916    // XXX: Verify that this is the correct action.
     
    902921        if (y->type.type == PS_TYPE_F32) {
    903922            if (myPoly->type == PS_POLYNOMIAL_ORD) {
    904                 for (i=0;i<yErr->n;i++) {
     923                for (i=0;i<myX->n;i++) {
    905924                    myX->data.F32[i] = (float) i;
    906925                }
     
    909928                float max = (float) (y->n - 1);
    910929
    911                 for (i=0;i<yErr->n;i++) {
     930                for (i=0;i<myX->n;i++) {
    912931                    myX->data.F32[i] = (((float) i) - 0.5 * (min + max)) /
    913932                                       (0.5 * (max - min));
     
    916935        } else if (y->type.type == PS_TYPE_F64) {
    917936            if (myPoly->type == PS_POLYNOMIAL_ORD) {
    918                 for (i=0;i<yErr->n;i++) {
     937                for (i=0;i<myX->n;i++) {
    919938                    myX->data.F64[i] = (float) i;
    920939                }
     
    923942                double max = (double) (y->n - 1);
    924943
    925                 for (i=0;i<yErr->n;i++) {
     944                for (i=0;i<myX->n;i++) {
    926945                    myX->data.F64[i] = (((float) i) - 0.5 * (min + max)) /
    927946                                       (0.5 * (max - min));
     
    11611180
    11621181/******************************************************************************
    1163     This routine must minimize a possibly multi-dimensional function
    1164     along a vector defined by line.
    1165  
    1166 XXX: Use a p_psName().
     1182This routine takes as input a possibly multi-dimensional function, along
     1183with an initial guess at the parameters of that function and vector "line"
     1184of the same size as the parameter vector.  It will minimize the function
     1185along that vector anr returns the offset along that vector at which the
     1186minimum is determined.
     1187 
     1188XXX: This routine is not very efficient in terms of total evaluations of the
     1189function.
    11671190 *****************************************************************************/
    1168 float psLineMin(psMinimization *min,
    1169                 psVector *params,
    1170                 psVector *line,
    1171                 const psVector *paramMask,
    1172                 const psArray *coords,
    1173                 psMinimizePowellFunc func)
     1191float p_psLineMin(psMinimization *min,
     1192                  psVector *params,
     1193                  psVector *line,
     1194                  const psVector *paramMask,
     1195                  const psArray *coords,
     1196                  psMinimizePowellFunc func)
    11741197{
    11751198    psVector *bracket;
     
    11901213    int null = 0;
    11911214
    1192     psTrace(".psLib.dataManip.psLineMin", 4,
    1193             "---- psLineMin() begin ----\n");
    1194     psTrace(".psLib.dataManip.psLineMin", 6,
     1215    psTrace(".psLib.dataManip.p_psLineMin", 4,
     1216            "---- p_psLineMin() begin ----\n");
     1217    psTrace(".psLib.dataManip.p_psLineMin", 6,
    11951218            "min->maxIter is %d\n", min->maxIter);
    1196     psTrace(".psLib.dataManip.psLineMin", 6,
     1219    psTrace(".psLib.dataManip.p_psLineMin", 6,
    11971220            "min->tol is %f\n", min->tol);
    11981221
     
    12021225                if (line->data.F32[i] >= FLT_EPSILON) {
    12031226                    null = 1;
    1204                     psTrace(".psLib.dataManip.psLineMin", 4,
     1227                    psTrace(".psLib.dataManip.p_psLineMin", 4,
    12051228                            "line->data.F32[%d] is %f\n", i, line->data.F32[i]);
    12061229                }
     
    12111234            if (line->data.F32[i] >= FLT_EPSILON) {
    12121235                null = 1;
    1213                 psTrace(".psLib.dataManip.psLineMin", 4,
     1236                psTrace(".psLib.dataManip.p_psLineMin", 4,
    12141237                        "line->data.F32[%d] is %f\n", i, line->data.F32[i]);
    12151238            }
     
    12181241
    12191242    if (null == 0) {
    1220         psTrace(".psLib.dataManip.psLineMin", 2,
    1221                 "psLineMin() called with zero line vector.\n");
     1243        psTrace(".psLib.dataManip.p_psLineMin", 2,
     1244                "p_psLineMin() called with zero line vector.\n");
    12221245        return(0.0);
    12231246    }
     
    12291252
    12301253    for (i=0;i<params->n;i++) {
    1231         psTrace(".psLib.dataManip.psLineMin", 6,
     1254        psTrace(".psLib.dataManip.p_psLineMin", 6,
    12321255                "params, paramMask, line [%d] is (%f %d %f)\n", i,
    12331256                params->data.F32[i],
     
    12481271    while (min->iter < min->maxIter) {
    12491272        min->iter++;
    1250         psTrace(".psLib.dataManip.psLineMin", 6,
    1251                 "psLineMin(): iteration %d\n", min->iter);
     1273        psTrace(".psLib.dataManip.p_psLineMin", 6,
     1274                "p_psLineMin(): iteration %d\n", min->iter);
    12521275
    12531276        a = bracket->data.F32[0];
     
    12691292        fb = func(tmpb, coords);
    12701293        fc = func(tmpc, coords);
    1271         psTrace(".psLib.dataManip.psLineMin", 6,
     1294        psTrace(".psLib.dataManip.p_psLineMin", 6,
    12721295                "Iteration %d: f(%f %f %f) is (%f %f %f)\n", min->iter,
    12731296                a, b, c, fa, fb, fc);
     
    13071330            }
    13081331        }
    1309         psTrace(".psLib.dataManip.psLineMin", 6,
     1332        psTrace(".psLib.dataManip.p_psLineMin", 6,
    13101333                "Iteration %d: new bracket is (%f %f %f)\n", min->iter, bracket->data.F32[0], bracket->data.F32[1], bracket->data.F32[2]);
    13111334
    13121335        mul = bracket->data.F32[1];
    1313         if ((fabs(a-b) < min->tol) &&
    1314                 (fabs(b-c) < min->tol)) {
     1336        if ((fabs(a-b) < min->tol) && (fabs(b-c) < min->tol)) {
    13151337            for (i=0;i<params->n;i++) {
    13161338                if (paramMask->data.U8[i] == 0) {
     
    13241346            psFree(tmpc);
    13251347            psFree(tmpn);
    1326             psTrace(".psLib.dataManip.psLineMin", 4,
    1327                     "---- psLineMin() end.a (%f) ----\n", mul);
     1348            psTrace(".psLib.dataManip.p_psLineMin", 4,
     1349                    "---- p_psLineMin() end.a (%f) ----\n", mul);
    13281350            return(mul);
    13291351        }
     
    13361358    psFree(tmpn);
    13371359
    1338     psTrace(".psLib.dataManip.psLineMin", 4,
    1339             "---- psLineMin() end.b (0.0) ----\n");
     1360    psTrace(".psLib.dataManip.p_psLineMin", 4,
     1361            "---- p_psLineMin() end.b (0.0) ----\n");
    13401362    return(0.0);
    13411363}
     
    14291451                dummyMin.maxIter = 100;
    14301452                dummyMin.tol = 0.01;
    1431                 mul = psLineMin(&dummyMin, Q, v[i], paramMask, coords, func);
     1453                mul = p_psLineMin(&dummyMin, Q, v[i], paramMask, coords, func);
    14321454                if (fabs(dummyMin.value - currFuncVal) > biggestDiff) {
    14331455                    biggestDiff = fabs(dummyMin.value - currFuncVal);
     
    14601482        }
    14611483
    1462         mul = psLineMin(min, params, u, paramMask, coords, func);
     1484        mul = p_psLineMin(min, params, u, paramMask, coords, func);
    14631485
    14641486        // 6:
Note: See TracChangeset for help on using the changeset viewer.