Changeset 1945 for trunk/psLib/src/math/psMinimize.c
- Timestamp:
- Oct 3, 2004, 1:35:47 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psMinimize.c (modified) (30 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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:
Note:
See TracChangeset
for help on using the changeset viewer.
