IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 12, 2005, 10:31:24 AM (21 years ago)
Author:
gusciora
Message:

....

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/test/math/tst_psMinimize04.c

    r4973 r4995  
    11/*****************************************************************************
    2 This routine must ensure that the psVectorFitPolynomial1D works correctly.
     2This routine must ensure that the psVectorFitPolynomial2D works correctly.
    33We create a vectors of data points (x and y), and populate them with the
    44values from an arbitrary function setData().  We then call
    5 psVectorFitPolynomial1D() with a regular polynomial data structure.  We then
     5psVectorFitPolynomial2D() with a regular polynomial data structure.  We then
    66evaluate the polynomial with the coefficients generated above and determine
    77if they are within an error tolerance of the expected values.
     
    2525#define B 2.0
    2626#define C 3.0
     27#define D 3.0
     28#define E 3.0
     29#define F 3.0
    2730#define ERROR_TOLERANCE 0.10
    28 #define YERR 1.0
    29 double setData(double x)
     31#define FERR 1.0
     32#define PRINT_RESULTS 0
     33
     34double setData(double x, double y)
    3035{
    31     return(A + (B * x) + (C * x * x));
     36    return(A + (B * x) + (C * x * x) + (D * y) + (E * y * y) + (E * x * y));
    3237}
    3338
    3439psS32 t00()
    3540{
    36     psPolynomial1D *myPoly = NULL;
    37     psVector *x = NULL;
    38     psVector *y = NULL;
    39     psVector *yErr = NULL;
    40     psS32 i = 0;
    4141    psS32 currentId = psMemGetId();
    4242    psS32 testStatus = true;
     
    4545    double actualData;
    4646
    47     myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
    48     x = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    49     y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    50     yErr = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
     47    psPolynomial2D *myPoly = psPolynomial2DAlloc(POLY_ORDER+1, POLY_ORDER+1, PS_POLYNOMIAL_ORD);
     48    psVector *x = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
     49    psVector *y = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
     50    psVector *f = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
     51    psVector *fErr = psVectorAlloc(NUM_DATA, PS_TYPE_F32);
    5152
    52     for (i=0;i<NUM_DATA;i++) {
    53         x->data.F64[i] = (double) i;
    54         y->data.F64[i] = setData(x->data.F64[i]);
    55         yErr->data.F64[i] = YERR;
    56         //        printf("Original data %d: (%.1f %.1f)\n", i, x->data.F64[i], y->data.F64[i]);
     53    for (psS32 i=0;i<NUM_DATA;i++) {
     54        x->data.F32[i] = (double) i;
     55        y->data.F32[i] = (double) i;
     56        f->data.F32[i] = setData(x->data.F32[i], y->data.F32[i]);
     57        fErr->data.F32[i] = FERR;
    5758    }
    5859
    5960    printPositiveTestHeader(stdout,
    6061                            "psMinimize functions",
    61                             "psVectorFitPolynomial1D(): equal difference in variable yErr");
     62                            "psVectorFitPolynomial2D(): equal difference in variable fErr");
    6263
    63     psVectorFitPolynomial1D(myPoly, x, y, yErr);
     64    printf("TEST: calling psVectorFitPolynomial2D() with f, fErr, y, x as F32 vectors.\n");
     65    psVectorFitPolynomial2D(myPoly, NULL, 0, f, fErr, x, y);
    6466
    6567    //    for (i=0;i<POLY_ORDER+1;i++) {
     
    6769    //    }
    6870
    69     for (i=0;i<NUM_DATA;i++) {
    70         expectData = setData(x->data.F64[i]);
    71         actualData = psPolynomial1DEval(
    72                          myPoly,
    73                          x->data.F64[i]
    74                      );
     71    for (psS32 i=0;i<NUM_DATA;i++) {
     72        expectData = setData(x->data.F32[i],  y->data.F32[i]);
     73        actualData = psPolynomial2DEval(myPoly, x->data.F32[i], y->data.F32[i]);
    7574        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
    76             printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
    77                    i, x->data.F64[i], actualData, expectData);
     75            printf("ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
     76                   i, x->data.F32[i], y->data.F32[i], actualData, expectData);
    7877            testStatus = false;
    7978        }
    80         //        } else {
    81         //            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
    82         //                   i, x->data.F64[i], actualData, expectData);
    83         //        }
     79        if (PRINT_RESULTS) {
     80            printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
     81        }
     82    }
     83
     84
     85    printf("Running test with fErr set to NULL.\n");
     86    psVectorFitPolynomial2D(myPoly, NULL, 0, f, NULL, x, y);
     87    for (psS32 i=0;i<NUM_DATA;i++) {
     88        expectData = setData(x->data.F32[i],  y->data.F32[i]);
     89        actualData = psPolynomial2DEval(myPoly, x->data.F32[i], y->data.F32[i]);
     90        if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
     91            printf("ERROR: Fitted data %d: (%.1f %.1f %.1f), expected was (%.1f)\n",
     92                   i, x->data.F32[i], y->data.F32[i], actualData, expectData);
     93            testStatus = false;
     94        }
     95        if (PRINT_RESULTS) {
     96            printf("Data point [%d] is %f, should be %f.\n", i, actualData, expectData);
     97        }
     98    }
     99    psFree(myPoly);
     100
     101    printf("Running test with x, y set to NULL.  Should generate error.\n");
     102    myPoly = psVectorFitPolynomial2D(NULL, NULL, 0, f, fErr, NULL, NULL);
     103    if (myPoly != NULL) {
     104        printf("TEST ERROR: psVectorFitPolynomial2D() returned non-NULL.\n");
     105        testStatus = false;
    84106    }
    85107
    86108    psMemCheckCorruption(1);
    87     psFree(myPoly);
    88109    psFree(x);
    89110    psFree(y);
    90     psFree(yErr);
     111    psFree(f);
     112    psFree(fErr);
    91113    psMemCheckCorruption(1);
    92114    memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
     
    97119    printFooter(stdout,
    98120                "psMinimize functions",
    99                 "psVectorFitPolynomial1D(): equal differences in variable yErr",
     121                "psVectorFitPolynomial2D(): equal differences in variable fErr",
    100122                testStatus);
    101123
     
    103125}
    104126
    105 psS32 t01()
    106 {
    107     psPolynomial1D *myPoly = NULL;
    108     psVector *x = NULL;
    109     psVector *y = NULL;
    110     psS32 i = 0;
    111     psS32 currentId = psMemGetId();
    112     psS32 testStatus = true;
    113     psS32 memLeaks = 0;
    114     double expectData;
    115     double actualData;
    116 
    117     myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
    118     x = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    119     y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    120 
    121     for (i=0;i<NUM_DATA;i++) {
    122         x->data.F64[i] = (double) i;
    123         y->data.F64[i] = setData(x->data.F64[i]);
    124         //        printf("Original data %d: (%.1f %.1f)\n", i, x->data.F64[i], y->data.F64[i]);
    125     }
    126 
    127     printPositiveTestHeader(stdout,
    128                             "psMinimize functions",
    129                             "psVectorFitPolynomial1D(): yErr is NULL");
    130 
    131     psVectorFitPolynomial1D(myPoly, x, y, NULL);
    132 
    133     //    for (i=0;i<POLY_ORDER+1;i++) {
    134     //        printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i]);
    135     //    }
    136 
    137     for (i=0;i<NUM_DATA;i++) {
    138         expectData = setData(x->data.F64[i]);
    139         actualData = psPolynomial1DEval(
    140                          myPoly,
    141                          x->data.F64[i]
    142                      );
    143         if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
    144             printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
    145                    i, x->data.F64[i], actualData, expectData);
    146             testStatus = false;
    147             //        } else {
    148             //            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
    149             //                   i, x->data.F64[i], actualData, expectData);
    150         }
    151     }
    152 
    153     psMemCheckCorruption(1);
    154     psFree(myPoly);
    155     psFree(x);
    156     psFree(y);
    157     psMemCheckCorruption(1);
    158     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    159     if (0 != memLeaks) {
    160         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    161     }
    162 
    163     printFooter(stdout,
    164                 "psMinimize functions",
    165                 "psVectorFitPolynomial1D(): yErr is NULL",
    166                 testStatus);
    167 
    168     return (!testStatus);
    169 }
    170 
    171 psS32 t02()
    172 {
    173     psPolynomial1D *myPoly = NULL;
    174     psVector *y = NULL;
    175     psS32 i = 0;
    176     psS32 currentId = psMemGetId();
    177     psS32 testStatus = true;
    178     psS32 memLeaks = 0;
    179     double expectData;
    180     double actualData;
    181 
    182     myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD);
    183     y = psVectorAlloc(NUM_DATA, PS_TYPE_F64);
    184 
    185     for (i=0;i<NUM_DATA;i++) {
    186         y->data.F64[i] = setData((double) i);
    187         //        printf("Original data %d: (%.1f)\n", i, y->data.F64[i]);
    188     }
    189 
    190     printPositiveTestHeader(stdout,
    191                             "psMinimize functions",
    192                             "psVectorFitPolynomial1D(): x, yErr is NULL");
    193 
    194     psVectorFitPolynomial1D(myPoly, NULL, y, NULL);
    195 
    196     //    for (i=0;i<POLY_ORDER+1;i++) {
    197     //        printf("Polynomial coefficient %d is %0.1f\n", i, myPoly->coeff[i]);
    198     //    }
    199 
    200     for (i=0;i<NUM_DATA;i++) {
    201         expectData = setData((double) i);
    202         actualData = psPolynomial1DEval(
    203                          myPoly,
    204                          (double) i
    205                      );
    206         if (fabs(actualData-expectData) > fabs(ERROR_TOLERANCE * expectData)) {
    207             printf("ERROR: Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
    208                    i, (double) i, actualData, expectData);
    209             testStatus = false;
    210             //        } else {
    211             //            printf("Fitted data %d: (%.1f %.1f), expected was (%.1f)\n",
    212             //                   i, (double) i, actualData, expectData);
    213         }
    214     }
    215 
    216     psMemCheckCorruption(1);
    217     psFree(myPoly);
    218     psFree(y);
    219     psMemCheckCorruption(1);
    220     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    221     if (0 != memLeaks) {
    222         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    223     }
    224 
    225     printFooter(stdout,
    226                 "psMinimize functions",
    227                 "psVectorFitPolynomial1D(): x, yErr is NULL",
    228                 testStatus);
    229 
    230     return (!testStatus);
    231 }
    232 
    233 psS32 t03()
    234 {
    235     psS32 currentId = psMemGetId();
    236     psS32 testStatus = true;
    237     psS32 memLeaks = 0;
    238     psPolynomial1D *myPoly = NULL;
    239 
    240     printPositiveTestHeader(stdout,
    241                             "psMinimize functions",
    242                             "psVectorFitPolynomial1D(): all inputs are NULL");
    243 
    244     psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error for null input polynomial.");
    245     myPoly = psVectorFitPolynomial1D(NULL, NULL, NULL, NULL);
    246     if ( myPoly != NULL ) {
    247         psError(PS_ERR_UNKNOWN, true,"A null polynomial should have returned a null pointer.");
    248         testStatus = false;
    249     }
    250 
    251 
    252     psMemCheckCorruption(1);
    253     memLeaks = psMemCheckLeaks(currentId,NULL,stderr,false);
    254     if (0 != memLeaks) {
    255         psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
    256     }
    257 
    258     printFooter(stdout,
    259                 "psMinimize functions",
    260                 "psVectorFitPolynomial1D(): all inputs are NULL",
    261                 testStatus);
    262 
    263     return (!testStatus);
    264 }
    265 
    266 
    267127psS32 main()
    268128{
    269129    psLogSetFormat("HLNM");
    270130    t00();
    271     t01();
    272     t02();
    273     t03();
    274131}
Note: See TracChangeset for help on using the changeset viewer.