IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 1945


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

Modified the psVectorfit() tests and code.

Location:
trunk/psLib
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/dataManip/psConstants.h

    r1921 r1945  
    66 *  @author GLG, MHPCC
    77 *
    8  *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2004-09-28 23:27:37 $
     8 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2004-10-03 23:35:47 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3030}
    3131
     32/** Preprocessor macro to generate error on a NULL vector */
     33#define PS_CHECK_NULL_VECTOR_ACTION(NAME, ACTION) \
     34if (NAME == NULL || NAME->data.V == NULL) { \
     35    if (ACTION == 0) { \
     36        psError(__func__,"Invalid operation: %s or its data is NULL.", #NAME); \
     37        return(NULL); \
     38    } \
     39    if (ACTION == 1) { \
     40        psAbort(__func__, "Invalid operation: %s or its data is NULL.", #NAME); \
     41    } \
     42}
     43
    3244/** Preprocessor macro to generate error on a NULL poniter */
    3345#define PS_CHECK_NULL_PTR(NAME) \
     
    4052if (NAME->n < 1) { \
    4153    psError(__func__,"Invalid operation: %s has zero n value.", #NAME); \
     54}
     55
     56/** Preprocessor macro to generate error for zero length vector */
     57#define PS_CHECK_EMPTY_VECTOR_ACTION(NAME, ACTION) \
     58if (NAME->n < 1) { \
     59    if (ACTION == 0) { \
     60        psError(__func__,"Invalid operation: %s has zero n value.", #NAME); \
     61        return(NULL); \
     62    } \
     63    if (ACTION == 1) { \
     64        psAbort(__func__,"Invalid operation: %s has zero n value.", #NAME); \
     65    } \
    4266}
    4367
     
    6791}
    6892
     93/** Preprocessor macro to generate error on a NULL 1DPolynomial */
     94#define PS_CHECK_NULL_1DPOLY_ACTION(NAME, ACTION)                                                          \
     95if (NAME == NULL || NAME->coeff == NULL) {                                                         \
     96    if (ACTION == 0) { \
     97        psError(__func__,"Invalid operation: %s or its coeffs is NULL.", #NAME);                          \
     98        return(NULL); \
     99    } \
     100    if (ACTION == 1) { \
     101        psAbort(__func__,"Invalid operation: %s or its coeffs is NULL.", #NAME);                          \
     102    } \
     103}
     104
    69105#define PS_PRINT_VECTOR(NAME) \
    70106for (int my_i=0;my_i<NAME->n;my_i++) { \
  • trunk/psLib/src/dataManip/psFunctions.c

    r1921 r1945  
    77 *  polynomials.  It also contains a Gaussian functions.
    88 *
    9  *  @version $Revision: 1.43 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-09-28 23:27:37 $
     9 *  @version $Revision: 1.44 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2004-10-03 23:35:47 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    486486float p_psChebPolynomial1DEval(float x, const psPolynomial1D* myPoly)
    487487{
    488     /*
    489488    psVector *d;
    490489    int n;
     
    508507    psFree(d);
    509508    return(tmp);
    510     */
     509    /*
    511510
    512511    int n;
     
    527526
    528527    return(tmp);
     528    */
    529529}
    530530
     
    18691869    p_psMemSetPersistent(p, true);
    18701870    p_psMemSetPersistent(p->data.F32, true);
     1871    /*
     1872        psVector *p = psVectorAlloc(n, PS_TYPE_F32);
     1873        float tmp;
     1874    */
    18711875
    18721876    psTrace(".psLib.dataManip.psFunctions.p_ps1DFullInterpolateF32", 4,
     
    19011905            "---- p_ps1DFullInterpolateF32() end ----\n");
    19021906
     1907    /*
     1908        tmp = p->data.F32[0];
     1909        psFree(p);
     1910        return(tmp);
     1911    */
    19031912    return(p->data.F32[0]);
    19041913}
  • 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:
  • trunk/psLib/src/math/psConstants.h

    r1921 r1945  
    66 *  @author GLG, MHPCC
    77 *
    8  *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2004-09-28 23:27:37 $
     8 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2004-10-03 23:35:47 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3030}
    3131
     32/** Preprocessor macro to generate error on a NULL vector */
     33#define PS_CHECK_NULL_VECTOR_ACTION(NAME, ACTION) \
     34if (NAME == NULL || NAME->data.V == NULL) { \
     35    if (ACTION == 0) { \
     36        psError(__func__,"Invalid operation: %s or its data is NULL.", #NAME); \
     37        return(NULL); \
     38    } \
     39    if (ACTION == 1) { \
     40        psAbort(__func__, "Invalid operation: %s or its data is NULL.", #NAME); \
     41    } \
     42}
     43
    3244/** Preprocessor macro to generate error on a NULL poniter */
    3345#define PS_CHECK_NULL_PTR(NAME) \
     
    4052if (NAME->n < 1) { \
    4153    psError(__func__,"Invalid operation: %s has zero n value.", #NAME); \
     54}
     55
     56/** Preprocessor macro to generate error for zero length vector */
     57#define PS_CHECK_EMPTY_VECTOR_ACTION(NAME, ACTION) \
     58if (NAME->n < 1) { \
     59    if (ACTION == 0) { \
     60        psError(__func__,"Invalid operation: %s has zero n value.", #NAME); \
     61        return(NULL); \
     62    } \
     63    if (ACTION == 1) { \
     64        psAbort(__func__,"Invalid operation: %s has zero n value.", #NAME); \
     65    } \
    4266}
    4367
     
    6791}
    6892
     93/** Preprocessor macro to generate error on a NULL 1DPolynomial */
     94#define PS_CHECK_NULL_1DPOLY_ACTION(NAME, ACTION)                                                          \
     95if (NAME == NULL || NAME->coeff == NULL) {                                                         \
     96    if (ACTION == 0) { \
     97        psError(__func__,"Invalid operation: %s or its coeffs is NULL.", #NAME);                          \
     98        return(NULL); \
     99    } \
     100    if (ACTION == 1) { \
     101        psAbort(__func__,"Invalid operation: %s or its coeffs is NULL.", #NAME);                          \
     102    } \
     103}
     104
    69105#define PS_PRINT_VECTOR(NAME) \
    70106for (int my_i=0;my_i<NAME->n;my_i++) { \
  • trunk/psLib/src/math/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:
  • trunk/psLib/src/math/psPolynomial.c

    r1921 r1945  
    77 *  polynomials.  It also contains a Gaussian functions.
    88 *
    9  *  @version $Revision: 1.43 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-09-28 23:27:37 $
     9 *  @version $Revision: 1.44 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2004-10-03 23:35:47 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    486486float p_psChebPolynomial1DEval(float x, const psPolynomial1D* myPoly)
    487487{
    488     /*
    489488    psVector *d;
    490489    int n;
     
    508507    psFree(d);
    509508    return(tmp);
    510     */
     509    /*
    511510
    512511    int n;
     
    527526
    528527    return(tmp);
     528    */
    529529}
    530530
     
    18691869    p_psMemSetPersistent(p, true);
    18701870    p_psMemSetPersistent(p->data.F32, true);
     1871    /*
     1872        psVector *p = psVectorAlloc(n, PS_TYPE_F32);
     1873        float tmp;
     1874    */
    18711875
    18721876    psTrace(".psLib.dataManip.psFunctions.p_ps1DFullInterpolateF32", 4,
     
    19011905            "---- p_ps1DFullInterpolateF32() end ----\n");
    19021906
     1907    /*
     1908        tmp = p->data.F32[0];
     1909        psFree(p);
     1910        return(tmp);
     1911    */
    19031912    return(p->data.F32[0]);
    19041913}
  • trunk/psLib/src/math/psSpline.c

    r1921 r1945  
    77 *  polynomials.  It also contains a Gaussian functions.
    88 *
    9  *  @version $Revision: 1.43 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-09-28 23:27:37 $
     9 *  @version $Revision: 1.44 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2004-10-03 23:35:47 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    486486float p_psChebPolynomial1DEval(float x, const psPolynomial1D* myPoly)
    487487{
    488     /*
    489488    psVector *d;
    490489    int n;
     
    508507    psFree(d);
    509508    return(tmp);
    510     */
     509    /*
    511510
    512511    int n;
     
    527526
    528527    return(tmp);
     528    */
    529529}
    530530
     
    18691869    p_psMemSetPersistent(p, true);
    18701870    p_psMemSetPersistent(p->data.F32, true);
     1871    /*
     1872        psVector *p = psVectorAlloc(n, PS_TYPE_F32);
     1873        float tmp;
     1874    */
    18711875
    18721876    psTrace(".psLib.dataManip.psFunctions.p_ps1DFullInterpolateF32", 4,
     
    19011905            "---- p_ps1DFullInterpolateF32() end ----\n");
    19021906
     1907    /*
     1908        tmp = p->data.F32[0];
     1909        psFree(p);
     1910        return(tmp);
     1911    */
    19031912    return(p->data.F32[0]);
    19041913}
  • trunk/psLib/test/dataManip/Makefile

    r1909 r1945  
    33##  Makefile:   test/sysUtils
    44##
    5 ##  $Revision: 1.47 $  $Name: not supported by cvs2svn $
    6 ##  $Date: 2004-09-28 00:30:32 $
     5##  $Revision: 1.48 $  $Name: not supported by cvs2svn $
     6##  $Date: 2004-10-03 23:35:47 $
    77##
    88##  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4141tst_psMatrixVectorArithmetic03 \
    4242tst_psMinimize04 \
     43tst_psMinimize04b \
    4344tst_psMinimize05 \
    4445tst_psMinimize06 \
  • trunk/psLib/test/dataManip/tst_psMinimize04.c

    r1907 r1945  
    11/*****************************************************************************
    2     This routine must ensure that the psVectorFitPolynomial1D works correctly.
     2This routine must ensure that the psVectorFitPolynomial1D works correctly.
     3We create a vectors of data points (x and y), and populate them with the
     4values from an arbitrary function setData().  We then call
     5psVectorFitPolynomial1D() with a regular polynomial data structure.  We then
     6evaluate the polynomial with the coefficients generated above and determine
     7if they are within an error tolerance of the expected values.
     8 
     9    t00(): all input vectors are non-NULL.
     10    t01(): yErr is NULL.
     11    t02(): x, yErr is NULL.
     12    t03(): x, y, yErr is NULL.
    313 *****************************************************************************/
    414#include <stdio.h>
     
    1323#define NUM_DATA 10
    1424#define POLY_ORDER 5
    15 
    16 double setData(double A,
    17                double B,
    18                double C,
    19                double x)
     25#define A 3.0
     26#define B 2.0
     27#define C 3.0
     28#define ERROR_TOLERANCE 0.10
     29#define YERR 10.0
     30double setData(double x)
    2031{
    2132    return(A + (B * x) + (C * x * x));
    2233}
    2334
    24 int main()
     35int t00()
    2536{
    2637    psPolynomial1D *myPoly = NULL;
     
    3243    int testStatus = true;
    3344    int memLeaks = 0;
     45    double expectData;
     46    double actualData;
    3447
    3548    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
     
    4053    for (i=0;i<NUM_DATA;i++) {
    4154        x->data.F64[i] = (double) i;
    42         y->data.F64[i] = setData(3.0, 2.0, 3.0, x->data.F64[i]);
    43         yErr->data.F64[i] = 0.1;
     55        y->data.F64[i] = setData(x->data.F64[i]);
     56        yErr->data.F64[i] = YERR;
    4457        printf("Original data %d: (%.1f %.1f)\n", i, x->data.F64[i], y->data.F64[i]);
    4558    }
     59
     60    printPositiveTestHeader(stdout,
     61                            "psMinimize functions",
     62                            "psVectorFitPolynomial1D(): equal errors in yErr");
    4663
    4764    psVectorFitPolynomial1D(myPoly, x, y, yErr);
     
    5269
    5370    for (i=0;i<NUM_DATA;i++) {
    54         printf("Fitted data %d: (%.1f %.1f)\n", i, x->data.F64[i],
    55                setData(myPoly->coeff[0], myPoly->coeff[1],
    56                        myPoly->coeff[2], x->data.F64[i]));
    57     }
    58 
    59 
    60     psMemCheckCorruption(1);
    61     printFooter(stdout,
    62                 "psMinimize functions",
    63                 "psMinimize(): no masks",
    64                 testStatus);
     71        expectData = setData(x->data.F64[i]);
     72        actualData = psPolynomial1DEval(x->data.F64[i], myPoly);
     73        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
     74            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     75                   i, x->data.F64[i], actualData, expectData);
     76            testStatus = false;
     77        } else {
     78            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     79                   i, x->data.F64[i], actualData, expectData);
     80        }
     81    }
    6582
    6683    psMemCheckCorruption(1);
     
    6986    psFree(y);
    7087    psFree(yErr);
    71 
    72     psMemCheckCorruption(1);
    73     memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
    74     if (0 != memLeaks) {
    75         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    76     }
    77 
    78     return (!testStatus);
    79 }
     88    psMemCheckCorruption(1);
     89    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
     90    if (0 != memLeaks) {
     91        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
     92    }
     93
     94    printFooter(stdout,
     95                "psMinimize functions",
     96                "psVectorFitPolynomial1D(): equal errors in yErr",
     97                testStatus);
     98
     99    return (!testStatus);
     100}
     101
     102int t01()
     103{
     104    psPolynomial1D *myPoly = NULL;
     105    psVector *x = NULL;
     106    psVector *y = NULL;
     107    int i = 0;
     108    int currentId = psMemGetId();
     109    int testStatus = true;
     110    int memLeaks = 0;
     111    double expectData;
     112    double actualData;
     113
     114    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
     115    x = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
     116    y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
     117
     118    for (i=0;i<NUM_DATA;i++) {
     119        x->data.F64[i] = (double) i;
     120        y->data.F64[i] = setData(x->data.F64[i]);
     121        printf("Original data %d: (%.1f %.1f)\n", i, x->data.F64[i], y->data.F64[i]);
     122    }
     123
     124    printPositiveTestHeader(stdout,
     125                            "psMinimize functions",
     126                            "psVectorFitPolynomial1D(): yErr is NULL");
     127
     128    psVectorFitPolynomial1D(myPoly, x, y, NULL);
     129
     130    for (i=0;i<POLY_ORDER+1;i++) {
     131        printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i]);
     132    }
     133
     134    for (i=0;i<NUM_DATA;i++) {
     135        expectData = setData(x->data.F64[i]);
     136        actualData = psPolynomial1DEval(x->data.F64[i], myPoly);
     137        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
     138            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     139                   i, x->data.F64[i], actualData, expectData);
     140            testStatus = false;
     141        } else {
     142            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     143                   i, x->data.F64[i], actualData, expectData);
     144        }
     145    }
     146
     147    psMemCheckCorruption(1);
     148    psFree(myPoly);
     149    psFree(x);
     150    psFree(y);
     151    psMemCheckCorruption(1);
     152    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
     153    if (0 != memLeaks) {
     154        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
     155    }
     156
     157    printFooter(stdout,
     158                "psMinimize functions",
     159                "psVectorFitPolynomial1D(): yErr is NULL",
     160                testStatus);
     161
     162    return (!testStatus);
     163}
     164
     165int t02()
     166{
     167    psPolynomial1D *myPoly = NULL;
     168    psVector *y = NULL;
     169    int i = 0;
     170    int currentId = psMemGetId();
     171    int testStatus = true;
     172    int memLeaks = 0;
     173    double expectData;
     174    double actualData;
     175
     176    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
     177    y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
     178
     179    for (i=0;i<NUM_DATA;i++) {
     180        y->data.F64[i] = setData((double) i);
     181        printf("Original data %d: (%.1f)\n", i, y->data.F64[i]);
     182    }
     183
     184    printPositiveTestHeader(stdout,
     185                            "psMinimize functions",
     186                            "psVectorFitPolynomial1D(): x, yErr is NULL");
     187
     188    psVectorFitPolynomial1D(myPoly, NULL, y, NULL);
     189
     190    for (i=0;i<POLY_ORDER+1;i++) {
     191        printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i]);
     192    }
     193
     194    for (i=0;i<NUM_DATA;i++) {
     195        expectData = setData((double) i);
     196        actualData = psPolynomial1DEval((double) i, myPoly);
     197        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
     198            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     199                   i, (double) i, actualData, expectData);
     200            testStatus = false;
     201        } else {
     202            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     203                   i, (double) i, actualData, expectData);
     204        }
     205    }
     206
     207    psMemCheckCorruption(1);
     208    psFree(myPoly);
     209    psFree(y);
     210    psMemCheckCorruption(1);
     211    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
     212    if (0 != memLeaks) {
     213        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
     214    }
     215
     216    printFooter(stdout,
     217                "psMinimize functions",
     218                "psVectorFitPolynomial1D(): x, yErr is NULL",
     219                testStatus);
     220
     221    return (!testStatus);
     222}
     223
     224int t03()
     225{
     226    int currentId = psMemGetId();
     227    int testStatus = true;
     228    int memLeaks = 0;
     229
     230    printPositiveTestHeader(stdout,
     231                            "psMinimize functions",
     232                            "psVectorFitPolynomial1D(): all inputs are NULL");
     233
     234    psVectorFitPolynomial1D(NULL, NULL, NULL, NULL);
     235
     236
     237    psMemCheckCorruption(1);
     238    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
     239    if (0 != memLeaks) {
     240        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
     241    }
     242
     243    printFooter(stdout,
     244                "psMinimize functions",
     245                "psVectorFitPolynomial1D(): all inputs are NULL",
     246                testStatus);
     247
     248    return (!testStatus);
     249}
     250
     251
     252int main()
     253{
     254    t00();
     255    t01();
     256    t02();
     257    t03();
     258}
  • trunk/psLib/test/dataManip/tst_psMinimize04b.c

    r1919 r1945  
    1111#include "psMinimize.h"
    1212#include <math.h>
    13 #define NUM_DATA 15
     13#define NUM_DATA 20
    1414#define POLY_ORDER 10
    1515#define A 2.0
     
    1818#define D 4.0
    1919#define E 5.0
     20#define ERROR_TOLERANCE 0.10
     21#define IGNORE (ERROR_TOLERANCE * NUM_DATA)
    2022
    2123double setData(double x)
     
    2426}
    2527
    26 int main()
     28int t00()
    2729{
    2830    psPolynomial1D *myPoly = NULL;
     
    3133    psVector *yErr = NULL;
    3234    int i = 0;
    33     //    int currentId = psMemGetId();
    34     int testStatus = true;
    35     int memLeaks = 0;
    36     float sum=0.0;
     35    int currentId = psMemGetId();
     36    int testStatus = true;
     37    int memLeaks = 0;
    3738
    3839    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_CHEB);
     
    4748    }
    4849    p_psNormalizeVectorF64(x);
    49     for (i=0;i<NUM_DATA;i++) {
    50         //        printf("Original data %d: (%.1f %.1f)\n", i, x->data.F64[i], y->data.F64[i]);
    51     }
    52 
    53     sum=0.0;
    54     for (i=0;i<NUM_DATA;i++)
    55         sum+=y->data.F64[i];
    56     printf("c0 is %f\n", 2.0 * sum/((float) NUM_DATA));
    57 
    58     sum=0.0;
    59     for (i=0;i<NUM_DATA;i++)
    60         sum+=y->data.F64[i] * x->data.F64[i];
    61     printf("c1 is %f\n", 2.0 * sum/((float) NUM_DATA));
    62     sum=0.0;
    63 
    64     for (i=0;i<NUM_DATA;i++)
    65         sum+=y->data.F64[i] * ((2.0 * x->data.F64[i] * x->data.F64[i]) - 1.0);
    66     printf("c2 is %f\n", 2.0 * sum/((float) NUM_DATA));
     50
     51    printPositiveTestHeader(stdout,
     52                            "psMinimize functions",
     53                            "psVectorFitPolynomial1D(): CHEB, equal errors in yErr");
    6754
    6855    psVectorFitPolynomial1D(myPoly, x, y, yErr);
     
    7259    }
    7360
    74     for (i=0;i<NUM_DATA;i++) {
    75         printf("Fitted data %d: (%.1f %.1f) should be %.1f\n", i, x->data.F64[i],
    76                psPolynomial1DEval((float) x->data.F64[i], myPoly), y->data.F64[i]);
    77     }
    78 
    79 
    80     //    psMemCheckCorruption(1);
    81     printFooter(stdout,
    82                 "psMinimize functions",
    83                 "psMinimize(): no masks",
    84                 testStatus);
    85 
    86     //    psMemCheckCorruption(1);
     61    // We don't test the first or last few data items.
     62    for (i=IGNORE;i<NUM_DATA-IGNORE;i++) {
     63        double expectData = y->data.F64[i];
     64        double actualData = psPolynomial1DEval(x->data.F64[i], myPoly);
     65        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
     66            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     67                   i, x->data.F64[i], actualData, expectData);
     68            testStatus = false;
     69        } else {
     70            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     71                   i, x->data.F64[i], actualData, expectData);
     72        }
     73    }
     74
     75    psMemCheckCorruption(1);
    8776    psFree(myPoly);
    8877    psFree(x);
    8978    psFree(y);
    9079    psFree(yErr);
    91 
    92     //    psMemCheckCorruption(1);
    93     //    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
    94     if (0 != memLeaks) {
    95         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    96     }
    97 
    98     return (!testStatus);
    99 }
     80    psMemCheckCorruption(1);
     81    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
     82    if (0 != memLeaks) {
     83        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
     84    }
     85
     86    printFooter(stdout,
     87                "psMinimize functions",
     88                "psVectorFitPolynomial1D(): CHEB, equal errors in yErr",
     89                testStatus);
     90    return (!testStatus);
     91}
     92
     93
     94int t01()
     95{
     96    psPolynomial1D *myPoly = NULL;
     97    psVector *x = NULL;
     98    psVector *y = NULL;
     99    int i = 0;
     100    int currentId = psMemGetId();
     101    int testStatus = true;
     102    int memLeaks = 0;
     103
     104    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_CHEB);
     105    x = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
     106    y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
     107
     108    for (i=0;i<NUM_DATA;i++) {
     109        x->data.F64[i] = (double) i;
     110        y->data.F64[i] = setData(x->data.F64[i]);
     111    }
     112    p_psNormalizeVectorF64(x);
     113
     114    printPositiveTestHeader(stdout,
     115                            "psMinimize functions",
     116                            "psVectorFitPolynomial1D(): CHEB, yErr is NULL");
     117
     118    psVectorFitPolynomial1D(myPoly, x, y, NULL);
     119
     120    // We don't test the first or last few data items.
     121    for (i=IGNORE;i<NUM_DATA-IGNORE;i++) {
     122        double expectData = y->data.F64[i];
     123        double actualData = psPolynomial1DEval(x->data.F64[i], myPoly);
     124        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
     125            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     126                   i, x->data.F64[i], actualData, expectData);
     127            testStatus = false;
     128        } else {
     129            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     130                   i, x->data.F64[i], actualData, expectData);
     131        }
     132    }
     133
     134    psMemCheckCorruption(1);
     135    psFree(myPoly);
     136    psFree(x);
     137    psFree(y);
     138    psMemCheckCorruption(1);
     139    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
     140    if (0 != memLeaks) {
     141        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
     142    }
     143
     144    printFooter(stdout,
     145                "psMinimize functions",
     146                "psVectorFitPolynomial1D(): CHEB, yErr is NULL",
     147                testStatus);
     148    return (!testStatus);
     149}
     150
     151
     152int t02()
     153{
     154    psPolynomial1D *myPoly = NULL;
     155    psVector *x = NULL;
     156    psVector *y = NULL;
     157    int i = 0;
     158    int currentId = psMemGetId();
     159    int testStatus = true;
     160    int memLeaks = 0;
     161
     162    myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_CHEB);
     163    x = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
     164    y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
     165
     166    for (i=0;i<NUM_DATA;i++) {
     167        x->data.F64[i] = (double) i;
     168        y->data.F64[i] = setData(x->data.F64[i]);
     169    }
     170    p_psNormalizeVectorF64(x);
     171
     172    printPositiveTestHeader(stdout,
     173                            "psMinimize functions",
     174                            "psVectorFitPolynomial1D(): CHEB, x, yErr is NULL");
     175
     176    psVectorFitPolynomial1D(myPoly, NULL, y, NULL);
     177
     178    // We don't test the first or last few data items.
     179    for (i=IGNORE;i<NUM_DATA-IGNORE;i++) {
     180        double expectData = y->data.F64[i];
     181        double actualData = psPolynomial1DEval(x->data.F64[i], myPoly);
     182        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
     183            printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     184                   i, x->data.F64[i], actualData, expectData);
     185            testStatus = false;
     186        } else {
     187            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
     188                   i, x->data.F64[i], actualData, expectData);
     189        }
     190    }
     191
     192    psMemCheckCorruption(1);
     193    psFree(myPoly);
     194    psFree(x);
     195    psFree(y);
     196    psMemCheckCorruption(1);
     197    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
     198    if (0 != memLeaks) {
     199        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
     200    }
     201
     202    printFooter(stdout,
     203                "psMinimize functions",
     204                "psVectorFitPolynomial1D(): CHEB, x, yErr is NULL",
     205                testStatus);
     206    return (!testStatus);
     207}
     208
     209int t03()
     210{
     211    psPolynomial1D *myPoly = NULL;
     212    int currentId = psMemGetId();
     213    int testStatus = true;
     214    int memLeaks = 0;
     215
     216    printPositiveTestHeader(stdout,
     217                            "psMinimize functions",
     218                            "psVectorFitPolynomial1D(): CHEB, yErr is NULL");
     219
     220    myPoly = psVectorFitPolynomial1D(NULL, NULL, NULL, NULL);
     221    if (myPoly != NULL) {
     222        printf("ERROR: psVectorFitPolynomial1D() returned a non-NULL polynomial.\n");
     223        testStatus = false;
     224    }
     225
     226    psMemCheckCorruption(1);
     227    memLeaks = psMemCheckLeaks(currentId,NULL,stderr);
     228    if (0 != memLeaks) {
     229        psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
     230    }
     231    printFooter(stdout,
     232                "psMinimize functions",
     233                "psVectorFitPolynomial1D(): CHEB, yErr is NULL",
     234                testStatus);
     235    return (!testStatus);
     236}
     237
     238int main()
     239{
     240    t00();
     241    t01();
     242    t02();
     243    t03();
     244}
Note: See TracChangeset for help on using the changeset viewer.