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.

Location:
trunk/psLib/test/dataManip
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • 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.