Changeset 1945
- Timestamp:
- Oct 3, 2004, 1:35:47 PM (22 years ago)
- Location:
- trunk/psLib
- Files:
-
- 10 edited
-
src/dataManip/psConstants.h (modified) (4 diffs)
-
src/dataManip/psFunctions.c (modified) (6 diffs)
-
src/dataManip/psMinimize.c (modified) (30 diffs)
-
src/math/psConstants.h (modified) (4 diffs)
-
src/math/psMinimize.c (modified) (30 diffs)
-
src/math/psPolynomial.c (modified) (6 diffs)
-
src/math/psSpline.c (modified) (6 diffs)
-
test/dataManip/Makefile (modified) (2 diffs)
-
test/dataManip/tst_psMinimize04.c (modified) (6 diffs)
-
test/dataManip/tst_psMinimize04b.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/dataManip/psConstants.h
r1921 r1945 6 6 * @author GLG, MHPCC 7 7 * 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 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 30 30 } 31 31 32 /** Preprocessor macro to generate error on a NULL vector */ 33 #define PS_CHECK_NULL_VECTOR_ACTION(NAME, ACTION) \ 34 if (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 32 44 /** Preprocessor macro to generate error on a NULL poniter */ 33 45 #define PS_CHECK_NULL_PTR(NAME) \ … … 40 52 if (NAME->n < 1) { \ 41 53 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) \ 58 if (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 } \ 42 66 } 43 67 … … 67 91 } 68 92 93 /** Preprocessor macro to generate error on a NULL 1DPolynomial */ 94 #define PS_CHECK_NULL_1DPOLY_ACTION(NAME, ACTION) \ 95 if (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 69 105 #define PS_PRINT_VECTOR(NAME) \ 70 106 for (int my_i=0;my_i<NAME->n;my_i++) { \ -
trunk/psLib/src/dataManip/psFunctions.c
r1921 r1945 7 7 * polynomials. It also contains a Gaussian functions. 8 8 * 9 * @version $Revision: 1.4 3$ $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 $ 11 11 * 12 12 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 486 486 float p_psChebPolynomial1DEval(float x, const psPolynomial1D* myPoly) 487 487 { 488 /*489 488 psVector *d; 490 489 int n; … … 508 507 psFree(d); 509 508 return(tmp); 510 */509 /* 511 510 512 511 int n; … … 527 526 528 527 return(tmp); 528 */ 529 529 } 530 530 … … 1869 1869 p_psMemSetPersistent(p, true); 1870 1870 p_psMemSetPersistent(p->data.F32, true); 1871 /* 1872 psVector *p = psVectorAlloc(n, PS_TYPE_F32); 1873 float tmp; 1874 */ 1871 1875 1872 1876 psTrace(".psLib.dataManip.psFunctions.p_ps1DFullInterpolateF32", 4, … … 1901 1905 "---- p_ps1DFullInterpolateF32() end ----\n"); 1902 1906 1907 /* 1908 tmp = p->data.F32[0]; 1909 psFree(p); 1910 return(tmp); 1911 */ 1903 1912 return(p->data.F32[0]); 1904 1913 } -
trunk/psLib/src/dataManip/psMinimize.c
r1921 r1945 9 9 * @author George Gusciora, MHPCC 10 10 * 11 * @version $Revision: 1.5 0$ $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 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 682 682 psScalar *fScalar; 683 683 684 685 684 // XXX: These assignments appear too simple to warrant code and 686 685 // variable declarations. I retain them here to maintain coherence … … 714 713 fScalar = p_psVectorInterpolate((psVector *) x32, (psVector *) y32, 715 714 3, tmpScalar); 715 716 716 f->data.F64[i] = (double) fScalar->data.F32; 717 psFree(fScalar); 717 718 718 719 psTrace(".psLib.dataManip.p_psVectorFitPolynomial1DCheby", 6, … … 724 725 // coefficients of the Chebyshev polynomial: NR 5.8.7. 725 726 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++) { 727 730 sum = 0.0; 728 731 for (k=0;k<n;k++) { … … 730 733 cos(M_PI * ((float) j) * (0.5 + ((float) k)) / ((float) n)); 731 734 } 735 732 736 myPoly->coeff[j] = fac * sum; 733 737 } 734 738 735 739 // XXX: Must free memory. 740 psFree(f); 736 741 psFree(x32); 737 742 psFree(y32); 738 743 psFree(tmpScalar); 739 psFree(fScalar); 744 740 745 return(myPoly); 741 746 } … … 745 750 polynomial of degree myPoly to the data points (x, y) and return the 746 751 coefficients of that polynomial. 747 748 XXX: yErr is currently ignored.749 752 750 753 XXX: must add type F32 (currently F64 only). … … 795 798 796 799 // Initialize data structures. 797 for (i = 0; i < (polyOrder); i++) {800 for (i = 0; i < polyOrder; i++) { 798 801 B->data.F64[i] = 0.0; 799 802 coeffs->data.F64[i] = 0.0; 800 803 outPerm->data.F64[i] = 0.0; 801 for (j = 0; j < (polyOrder); j++) {804 for (j = 0; j < polyOrder; j++) { 802 805 A->data.F64[i][j] = 0.0; 803 806 ALUD->data.F64[i][j] = 0.0; … … 809 812 810 813 // 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 } 821 842 } 822 843 } … … 826 847 coeffs = psMatrixLUSolve(coeffs, ALUD, B, outPerm); 827 848 828 for (k = 0; k < (polyOrder); k++) {849 for (k = 0; k < polyOrder; k++) { 829 850 myPoly->coeff[k] = coeffs->data.F64[k]; 830 851 // printf("myPoly->coeff[%d] is %f\n", k, myPoly->coeff[k]); … … 853 874 myPoly to the data points (x, y) and return the coefficients of that 854 875 polynomial. 855 856 XXX: yErr is currently ignored.857 876 858 877 XXX: must add type F32 (currently F64 only). … … 870 889 psVector *myYErr = NULL; 871 890 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); 875 894 876 895 // If yErr==NULL, set all errors equal. … … 880 899 881 900 if (y->type.type == PS_TYPE_F32) { 882 for (i=0;i< yErr->n;i++) {901 for (i=0;i<myYErr->n;i++) { 883 902 myYErr->data.F32[i] = 1.0; 884 903 } 885 904 } else if (y->type.type == PS_TYPE_F64) { 886 for (i=0;i< yErr->n;i++) {905 for (i=0;i<myYErr->n;i++) { 887 906 myYErr->data.F64[i] = 1.0; 888 907 } … … 893 912 894 913 // If x==NULL, create an myX vector with x values set to (0:n), and if 895 // this is a C Hebyshev polynomial, we must scale to (-1:1).914 // this is a Chebyshev polynomial, we must scale to (-1:1). 896 915 897 916 // XXX: Verify that this is the correct action. … … 902 921 if (y->type.type == PS_TYPE_F32) { 903 922 if (myPoly->type == PS_POLYNOMIAL_ORD) { 904 for (i=0;i< yErr->n;i++) {923 for (i=0;i<myX->n;i++) { 905 924 myX->data.F32[i] = (float) i; 906 925 } … … 909 928 float max = (float) (y->n - 1); 910 929 911 for (i=0;i< yErr->n;i++) {930 for (i=0;i<myX->n;i++) { 912 931 myX->data.F32[i] = (((float) i) - 0.5 * (min + max)) / 913 932 (0.5 * (max - min)); … … 916 935 } else if (y->type.type == PS_TYPE_F64) { 917 936 if (myPoly->type == PS_POLYNOMIAL_ORD) { 918 for (i=0;i< yErr->n;i++) {937 for (i=0;i<myX->n;i++) { 919 938 myX->data.F64[i] = (float) i; 920 939 } … … 923 942 double max = (double) (y->n - 1); 924 943 925 for (i=0;i< yErr->n;i++) {944 for (i=0;i<myX->n;i++) { 926 945 myX->data.F64[i] = (((float) i) - 0.5 * (min + max)) / 927 946 (0.5 * (max - min)); … … 1161 1180 1162 1181 /****************************************************************************** 1163 This routine must minimize a possibly multi-dimensional function 1164 along a vector defined by line. 1165 1166 XXX: Use a p_psName(). 1182 This routine takes as input a possibly multi-dimensional function, along 1183 with an initial guess at the parameters of that function and vector "line" 1184 of the same size as the parameter vector. It will minimize the function 1185 along that vector anr returns the offset along that vector at which the 1186 minimum is determined. 1187 1188 XXX: This routine is not very efficient in terms of total evaluations of the 1189 function. 1167 1190 *****************************************************************************/ 1168 float p sLineMin(psMinimization *min,1169 psVector *params,1170 psVector *line,1171 const psVector *paramMask,1172 const psArray *coords,1173 psMinimizePowellFunc func)1191 float p_psLineMin(psMinimization *min, 1192 psVector *params, 1193 psVector *line, 1194 const psVector *paramMask, 1195 const psArray *coords, 1196 psMinimizePowellFunc func) 1174 1197 { 1175 1198 psVector *bracket; … … 1190 1213 int null = 0; 1191 1214 1192 psTrace(".psLib.dataManip.p sLineMin", 4,1193 "---- p sLineMin() begin ----\n");1194 psTrace(".psLib.dataManip.p sLineMin", 6,1215 psTrace(".psLib.dataManip.p_psLineMin", 4, 1216 "---- p_psLineMin() begin ----\n"); 1217 psTrace(".psLib.dataManip.p_psLineMin", 6, 1195 1218 "min->maxIter is %d\n", min->maxIter); 1196 psTrace(".psLib.dataManip.p sLineMin", 6,1219 psTrace(".psLib.dataManip.p_psLineMin", 6, 1197 1220 "min->tol is %f\n", min->tol); 1198 1221 … … 1202 1225 if (line->data.F32[i] >= FLT_EPSILON) { 1203 1226 null = 1; 1204 psTrace(".psLib.dataManip.p sLineMin", 4,1227 psTrace(".psLib.dataManip.p_psLineMin", 4, 1205 1228 "line->data.F32[%d] is %f\n", i, line->data.F32[i]); 1206 1229 } … … 1211 1234 if (line->data.F32[i] >= FLT_EPSILON) { 1212 1235 null = 1; 1213 psTrace(".psLib.dataManip.p sLineMin", 4,1236 psTrace(".psLib.dataManip.p_psLineMin", 4, 1214 1237 "line->data.F32[%d] is %f\n", i, line->data.F32[i]); 1215 1238 } … … 1218 1241 1219 1242 if (null == 0) { 1220 psTrace(".psLib.dataManip.p sLineMin", 2,1221 "p sLineMin() called with zero line vector.\n");1243 psTrace(".psLib.dataManip.p_psLineMin", 2, 1244 "p_psLineMin() called with zero line vector.\n"); 1222 1245 return(0.0); 1223 1246 } … … 1229 1252 1230 1253 for (i=0;i<params->n;i++) { 1231 psTrace(".psLib.dataManip.p sLineMin", 6,1254 psTrace(".psLib.dataManip.p_psLineMin", 6, 1232 1255 "params, paramMask, line [%d] is (%f %d %f)\n", i, 1233 1256 params->data.F32[i], … … 1248 1271 while (min->iter < min->maxIter) { 1249 1272 min->iter++; 1250 psTrace(".psLib.dataManip.p sLineMin", 6,1251 "p sLineMin(): iteration %d\n", min->iter);1273 psTrace(".psLib.dataManip.p_psLineMin", 6, 1274 "p_psLineMin(): iteration %d\n", min->iter); 1252 1275 1253 1276 a = bracket->data.F32[0]; … … 1269 1292 fb = func(tmpb, coords); 1270 1293 fc = func(tmpc, coords); 1271 psTrace(".psLib.dataManip.p sLineMin", 6,1294 psTrace(".psLib.dataManip.p_psLineMin", 6, 1272 1295 "Iteration %d: f(%f %f %f) is (%f %f %f)\n", min->iter, 1273 1296 a, b, c, fa, fb, fc); … … 1307 1330 } 1308 1331 } 1309 psTrace(".psLib.dataManip.p sLineMin", 6,1332 psTrace(".psLib.dataManip.p_psLineMin", 6, 1310 1333 "Iteration %d: new bracket is (%f %f %f)\n", min->iter, bracket->data.F32[0], bracket->data.F32[1], bracket->data.F32[2]); 1311 1334 1312 1335 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)) { 1315 1337 for (i=0;i<params->n;i++) { 1316 1338 if (paramMask->data.U8[i] == 0) { … … 1324 1346 psFree(tmpc); 1325 1347 psFree(tmpn); 1326 psTrace(".psLib.dataManip.p sLineMin", 4,1327 "---- p sLineMin() end.a (%f) ----\n", mul);1348 psTrace(".psLib.dataManip.p_psLineMin", 4, 1349 "---- p_psLineMin() end.a (%f) ----\n", mul); 1328 1350 return(mul); 1329 1351 } … … 1336 1358 psFree(tmpn); 1337 1359 1338 psTrace(".psLib.dataManip.p sLineMin", 4,1339 "---- p sLineMin() end.b (0.0) ----\n");1360 psTrace(".psLib.dataManip.p_psLineMin", 4, 1361 "---- p_psLineMin() end.b (0.0) ----\n"); 1340 1362 return(0.0); 1341 1363 } … … 1429 1451 dummyMin.maxIter = 100; 1430 1452 dummyMin.tol = 0.01; 1431 mul = p sLineMin(&dummyMin, Q, v[i], paramMask, coords, func);1453 mul = p_psLineMin(&dummyMin, Q, v[i], paramMask, coords, func); 1432 1454 if (fabs(dummyMin.value - currFuncVal) > biggestDiff) { 1433 1455 biggestDiff = fabs(dummyMin.value - currFuncVal); … … 1460 1482 } 1461 1483 1462 mul = p sLineMin(min, params, u, paramMask, coords, func);1484 mul = p_psLineMin(min, params, u, paramMask, coords, func); 1463 1485 1464 1486 // 6: -
trunk/psLib/src/math/psConstants.h
r1921 r1945 6 6 * @author GLG, MHPCC 7 7 * 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 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 30 30 } 31 31 32 /** Preprocessor macro to generate error on a NULL vector */ 33 #define PS_CHECK_NULL_VECTOR_ACTION(NAME, ACTION) \ 34 if (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 32 44 /** Preprocessor macro to generate error on a NULL poniter */ 33 45 #define PS_CHECK_NULL_PTR(NAME) \ … … 40 52 if (NAME->n < 1) { \ 41 53 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) \ 58 if (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 } \ 42 66 } 43 67 … … 67 91 } 68 92 93 /** Preprocessor macro to generate error on a NULL 1DPolynomial */ 94 #define PS_CHECK_NULL_1DPOLY_ACTION(NAME, ACTION) \ 95 if (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 69 105 #define PS_PRINT_VECTOR(NAME) \ 70 106 for (int my_i=0;my_i<NAME->n;my_i++) { \ -
trunk/psLib/src/math/psMinimize.c
r1921 r1945 9 9 * @author George Gusciora, MHPCC 10 10 * 11 * @version $Revision: 1.5 0$ $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 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 682 682 psScalar *fScalar; 683 683 684 685 684 // XXX: These assignments appear too simple to warrant code and 686 685 // variable declarations. I retain them here to maintain coherence … … 714 713 fScalar = p_psVectorInterpolate((psVector *) x32, (psVector *) y32, 715 714 3, tmpScalar); 715 716 716 f->data.F64[i] = (double) fScalar->data.F32; 717 psFree(fScalar); 717 718 718 719 psTrace(".psLib.dataManip.p_psVectorFitPolynomial1DCheby", 6, … … 724 725 // coefficients of the Chebyshev polynomial: NR 5.8.7. 725 726 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++) { 727 730 sum = 0.0; 728 731 for (k=0;k<n;k++) { … … 730 733 cos(M_PI * ((float) j) * (0.5 + ((float) k)) / ((float) n)); 731 734 } 735 732 736 myPoly->coeff[j] = fac * sum; 733 737 } 734 738 735 739 // XXX: Must free memory. 740 psFree(f); 736 741 psFree(x32); 737 742 psFree(y32); 738 743 psFree(tmpScalar); 739 psFree(fScalar); 744 740 745 return(myPoly); 741 746 } … … 745 750 polynomial of degree myPoly to the data points (x, y) and return the 746 751 coefficients of that polynomial. 747 748 XXX: yErr is currently ignored.749 752 750 753 XXX: must add type F32 (currently F64 only). … … 795 798 796 799 // Initialize data structures. 797 for (i = 0; i < (polyOrder); i++) {800 for (i = 0; i < polyOrder; i++) { 798 801 B->data.F64[i] = 0.0; 799 802 coeffs->data.F64[i] = 0.0; 800 803 outPerm->data.F64[i] = 0.0; 801 for (j = 0; j < (polyOrder); j++) {804 for (j = 0; j < polyOrder; j++) { 802 805 A->data.F64[i][j] = 0.0; 803 806 ALUD->data.F64[i][j] = 0.0; … … 809 812 810 813 // 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 } 821 842 } 822 843 } … … 826 847 coeffs = psMatrixLUSolve(coeffs, ALUD, B, outPerm); 827 848 828 for (k = 0; k < (polyOrder); k++) {849 for (k = 0; k < polyOrder; k++) { 829 850 myPoly->coeff[k] = coeffs->data.F64[k]; 830 851 // printf("myPoly->coeff[%d] is %f\n", k, myPoly->coeff[k]); … … 853 874 myPoly to the data points (x, y) and return the coefficients of that 854 875 polynomial. 855 856 XXX: yErr is currently ignored.857 876 858 877 XXX: must add type F32 (currently F64 only). … … 870 889 psVector *myYErr = NULL; 871 890 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); 875 894 876 895 // If yErr==NULL, set all errors equal. … … 880 899 881 900 if (y->type.type == PS_TYPE_F32) { 882 for (i=0;i< yErr->n;i++) {901 for (i=0;i<myYErr->n;i++) { 883 902 myYErr->data.F32[i] = 1.0; 884 903 } 885 904 } else if (y->type.type == PS_TYPE_F64) { 886 for (i=0;i< yErr->n;i++) {905 for (i=0;i<myYErr->n;i++) { 887 906 myYErr->data.F64[i] = 1.0; 888 907 } … … 893 912 894 913 // If x==NULL, create an myX vector with x values set to (0:n), and if 895 // this is a C Hebyshev polynomial, we must scale to (-1:1).914 // this is a Chebyshev polynomial, we must scale to (-1:1). 896 915 897 916 // XXX: Verify that this is the correct action. … … 902 921 if (y->type.type == PS_TYPE_F32) { 903 922 if (myPoly->type == PS_POLYNOMIAL_ORD) { 904 for (i=0;i< yErr->n;i++) {923 for (i=0;i<myX->n;i++) { 905 924 myX->data.F32[i] = (float) i; 906 925 } … … 909 928 float max = (float) (y->n - 1); 910 929 911 for (i=0;i< yErr->n;i++) {930 for (i=0;i<myX->n;i++) { 912 931 myX->data.F32[i] = (((float) i) - 0.5 * (min + max)) / 913 932 (0.5 * (max - min)); … … 916 935 } else if (y->type.type == PS_TYPE_F64) { 917 936 if (myPoly->type == PS_POLYNOMIAL_ORD) { 918 for (i=0;i< yErr->n;i++) {937 for (i=0;i<myX->n;i++) { 919 938 myX->data.F64[i] = (float) i; 920 939 } … … 923 942 double max = (double) (y->n - 1); 924 943 925 for (i=0;i< yErr->n;i++) {944 for (i=0;i<myX->n;i++) { 926 945 myX->data.F64[i] = (((float) i) - 0.5 * (min + max)) / 927 946 (0.5 * (max - min)); … … 1161 1180 1162 1181 /****************************************************************************** 1163 This routine must minimize a possibly multi-dimensional function 1164 along a vector defined by line. 1165 1166 XXX: Use a p_psName(). 1182 This routine takes as input a possibly multi-dimensional function, along 1183 with an initial guess at the parameters of that function and vector "line" 1184 of the same size as the parameter vector. It will minimize the function 1185 along that vector anr returns the offset along that vector at which the 1186 minimum is determined. 1187 1188 XXX: This routine is not very efficient in terms of total evaluations of the 1189 function. 1167 1190 *****************************************************************************/ 1168 float p sLineMin(psMinimization *min,1169 psVector *params,1170 psVector *line,1171 const psVector *paramMask,1172 const psArray *coords,1173 psMinimizePowellFunc func)1191 float p_psLineMin(psMinimization *min, 1192 psVector *params, 1193 psVector *line, 1194 const psVector *paramMask, 1195 const psArray *coords, 1196 psMinimizePowellFunc func) 1174 1197 { 1175 1198 psVector *bracket; … … 1190 1213 int null = 0; 1191 1214 1192 psTrace(".psLib.dataManip.p sLineMin", 4,1193 "---- p sLineMin() begin ----\n");1194 psTrace(".psLib.dataManip.p sLineMin", 6,1215 psTrace(".psLib.dataManip.p_psLineMin", 4, 1216 "---- p_psLineMin() begin ----\n"); 1217 psTrace(".psLib.dataManip.p_psLineMin", 6, 1195 1218 "min->maxIter is %d\n", min->maxIter); 1196 psTrace(".psLib.dataManip.p sLineMin", 6,1219 psTrace(".psLib.dataManip.p_psLineMin", 6, 1197 1220 "min->tol is %f\n", min->tol); 1198 1221 … … 1202 1225 if (line->data.F32[i] >= FLT_EPSILON) { 1203 1226 null = 1; 1204 psTrace(".psLib.dataManip.p sLineMin", 4,1227 psTrace(".psLib.dataManip.p_psLineMin", 4, 1205 1228 "line->data.F32[%d] is %f\n", i, line->data.F32[i]); 1206 1229 } … … 1211 1234 if (line->data.F32[i] >= FLT_EPSILON) { 1212 1235 null = 1; 1213 psTrace(".psLib.dataManip.p sLineMin", 4,1236 psTrace(".psLib.dataManip.p_psLineMin", 4, 1214 1237 "line->data.F32[%d] is %f\n", i, line->data.F32[i]); 1215 1238 } … … 1218 1241 1219 1242 if (null == 0) { 1220 psTrace(".psLib.dataManip.p sLineMin", 2,1221 "p sLineMin() called with zero line vector.\n");1243 psTrace(".psLib.dataManip.p_psLineMin", 2, 1244 "p_psLineMin() called with zero line vector.\n"); 1222 1245 return(0.0); 1223 1246 } … … 1229 1252 1230 1253 for (i=0;i<params->n;i++) { 1231 psTrace(".psLib.dataManip.p sLineMin", 6,1254 psTrace(".psLib.dataManip.p_psLineMin", 6, 1232 1255 "params, paramMask, line [%d] is (%f %d %f)\n", i, 1233 1256 params->data.F32[i], … … 1248 1271 while (min->iter < min->maxIter) { 1249 1272 min->iter++; 1250 psTrace(".psLib.dataManip.p sLineMin", 6,1251 "p sLineMin(): iteration %d\n", min->iter);1273 psTrace(".psLib.dataManip.p_psLineMin", 6, 1274 "p_psLineMin(): iteration %d\n", min->iter); 1252 1275 1253 1276 a = bracket->data.F32[0]; … … 1269 1292 fb = func(tmpb, coords); 1270 1293 fc = func(tmpc, coords); 1271 psTrace(".psLib.dataManip.p sLineMin", 6,1294 psTrace(".psLib.dataManip.p_psLineMin", 6, 1272 1295 "Iteration %d: f(%f %f %f) is (%f %f %f)\n", min->iter, 1273 1296 a, b, c, fa, fb, fc); … … 1307 1330 } 1308 1331 } 1309 psTrace(".psLib.dataManip.p sLineMin", 6,1332 psTrace(".psLib.dataManip.p_psLineMin", 6, 1310 1333 "Iteration %d: new bracket is (%f %f %f)\n", min->iter, bracket->data.F32[0], bracket->data.F32[1], bracket->data.F32[2]); 1311 1334 1312 1335 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)) { 1315 1337 for (i=0;i<params->n;i++) { 1316 1338 if (paramMask->data.U8[i] == 0) { … … 1324 1346 psFree(tmpc); 1325 1347 psFree(tmpn); 1326 psTrace(".psLib.dataManip.p sLineMin", 4,1327 "---- p sLineMin() end.a (%f) ----\n", mul);1348 psTrace(".psLib.dataManip.p_psLineMin", 4, 1349 "---- p_psLineMin() end.a (%f) ----\n", mul); 1328 1350 return(mul); 1329 1351 } … … 1336 1358 psFree(tmpn); 1337 1359 1338 psTrace(".psLib.dataManip.p sLineMin", 4,1339 "---- p sLineMin() end.b (0.0) ----\n");1360 psTrace(".psLib.dataManip.p_psLineMin", 4, 1361 "---- p_psLineMin() end.b (0.0) ----\n"); 1340 1362 return(0.0); 1341 1363 } … … 1429 1451 dummyMin.maxIter = 100; 1430 1452 dummyMin.tol = 0.01; 1431 mul = p sLineMin(&dummyMin, Q, v[i], paramMask, coords, func);1453 mul = p_psLineMin(&dummyMin, Q, v[i], paramMask, coords, func); 1432 1454 if (fabs(dummyMin.value - currFuncVal) > biggestDiff) { 1433 1455 biggestDiff = fabs(dummyMin.value - currFuncVal); … … 1460 1482 } 1461 1483 1462 mul = p sLineMin(min, params, u, paramMask, coords, func);1484 mul = p_psLineMin(min, params, u, paramMask, coords, func); 1463 1485 1464 1486 // 6: -
trunk/psLib/src/math/psPolynomial.c
r1921 r1945 7 7 * polynomials. It also contains a Gaussian functions. 8 8 * 9 * @version $Revision: 1.4 3$ $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 $ 11 11 * 12 12 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 486 486 float p_psChebPolynomial1DEval(float x, const psPolynomial1D* myPoly) 487 487 { 488 /*489 488 psVector *d; 490 489 int n; … … 508 507 psFree(d); 509 508 return(tmp); 510 */509 /* 511 510 512 511 int n; … … 527 526 528 527 return(tmp); 528 */ 529 529 } 530 530 … … 1869 1869 p_psMemSetPersistent(p, true); 1870 1870 p_psMemSetPersistent(p->data.F32, true); 1871 /* 1872 psVector *p = psVectorAlloc(n, PS_TYPE_F32); 1873 float tmp; 1874 */ 1871 1875 1872 1876 psTrace(".psLib.dataManip.psFunctions.p_ps1DFullInterpolateF32", 4, … … 1901 1905 "---- p_ps1DFullInterpolateF32() end ----\n"); 1902 1906 1907 /* 1908 tmp = p->data.F32[0]; 1909 psFree(p); 1910 return(tmp); 1911 */ 1903 1912 return(p->data.F32[0]); 1904 1913 } -
trunk/psLib/src/math/psSpline.c
r1921 r1945 7 7 * polynomials. It also contains a Gaussian functions. 8 8 * 9 * @version $Revision: 1.4 3$ $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 $ 11 11 * 12 12 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 486 486 float p_psChebPolynomial1DEval(float x, const psPolynomial1D* myPoly) 487 487 { 488 /*489 488 psVector *d; 490 489 int n; … … 508 507 psFree(d); 509 508 return(tmp); 510 */509 /* 511 510 512 511 int n; … … 527 526 528 527 return(tmp); 528 */ 529 529 } 530 530 … … 1869 1869 p_psMemSetPersistent(p, true); 1870 1870 p_psMemSetPersistent(p->data.F32, true); 1871 /* 1872 psVector *p = psVectorAlloc(n, PS_TYPE_F32); 1873 float tmp; 1874 */ 1871 1875 1872 1876 psTrace(".psLib.dataManip.psFunctions.p_ps1DFullInterpolateF32", 4, … … 1901 1905 "---- p_ps1DFullInterpolateF32() end ----\n"); 1902 1906 1907 /* 1908 tmp = p->data.F32[0]; 1909 psFree(p); 1910 return(tmp); 1911 */ 1903 1912 return(p->data.F32[0]); 1904 1913 } -
trunk/psLib/test/dataManip/Makefile
r1909 r1945 3 3 ## Makefile: test/sysUtils 4 4 ## 5 ## $Revision: 1.4 7$ $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 $ 7 7 ## 8 8 ## Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 41 41 tst_psMatrixVectorArithmetic03 \ 42 42 tst_psMinimize04 \ 43 tst_psMinimize04b \ 43 44 tst_psMinimize05 \ 44 45 tst_psMinimize06 \ -
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 } -
trunk/psLib/test/dataManip/tst_psMinimize04b.c
r1919 r1945 11 11 #include "psMinimize.h" 12 12 #include <math.h> 13 #define NUM_DATA 1513 #define NUM_DATA 20 14 14 #define POLY_ORDER 10 15 15 #define A 2.0 … … 18 18 #define D 4.0 19 19 #define E 5.0 20 #define ERROR_TOLERANCE 0.10 21 #define IGNORE (ERROR_TOLERANCE * NUM_DATA) 20 22 21 23 double setData(double x) … … 24 26 } 25 27 26 int main()28 int t00() 27 29 { 28 30 psPolynomial1D *myPoly = NULL; … … 31 33 psVector *yErr = NULL; 32 34 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; 37 38 38 39 myPoly = psPolynomial1DAlloc(POLY_ORDER+1, PS_POLYNOMIAL_CHEB); … … 47 48 } 48 49 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"); 67 54 68 55 psVectorFitPolynomial1D(myPoly, x, y, yErr); … … 72 59 } 73 60 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); 87 76 psFree(myPoly); 88 77 psFree(x); 89 78 psFree(y); 90 79 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 94 int 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 152 int 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 209 int 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 238 int main() 239 { 240 t00(); 241 t01(); 242 t02(); 243 t03(); 244 }
Note:
See TracChangeset
for help on using the changeset viewer.
