Changeset 1945 for trunk/psLib/test/dataManip/tst_psMinimize04.c
- Timestamp:
- Oct 3, 2004, 1:35:47 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/dataManip/tst_psMinimize04.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/dataManip/tst_psMinimize04.c
r1907 r1945 1 1 /***************************************************************************** 2 This routine must ensure that the psVectorFitPolynomial1D works correctly. 2 This routine must ensure that the psVectorFitPolynomial1D works correctly. 3 We create a vectors of data points (x and y), and populate them with the 4 values from an arbitrary function setData(). We then call 5 psVectorFitPolynomial1D() with a regular polynomial data structure. We then 6 evaluate the polynomial with the coefficients generated above and determine 7 if 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. 3 13 *****************************************************************************/ 4 14 #include <stdio.h> … … 13 23 #define NUM_DATA 10 14 24 #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 30 double setData(double x) 20 31 { 21 32 return(A + (B * x) + (C * x * x)); 22 33 } 23 34 24 int main()35 int t00() 25 36 { 26 37 psPolynomial1D *myPoly = NULL; … … 32 43 int testStatus = true; 33 44 int memLeaks = 0; 45 double expectData; 46 double actualData; 34 47 35 48 myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_ORD); … … 40 53 for (i=0;i<NUM_DATA;i++) { 41 54 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; 44 57 printf("Original data %d: (%.1f %.1f)\n", i, x->data.F64[i], y->data.F64[i]); 45 58 } 59 60 printPositiveTestHeader(stdout, 61 "psMinimize functions", 62 "psVectorFitPolynomial1D(): equal errors in yErr"); 46 63 47 64 psVectorFitPolynomial1D(myPoly, x, y, yErr); … … 52 69 53 70 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 } 65 82 66 83 psMemCheckCorruption(1); … … 69 86 psFree(y); 70 87 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 102 int 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 165 int 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 224 int 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 252 int main() 253 { 254 t00(); 255 t01(); 256 t02(); 257 t03(); 258 }
Note:
See TracChangeset
for help on using the changeset viewer.
