Changeset 1963 for trunk/psLib/src/math/psMinimize.c
- Timestamp:
- Oct 5, 2004, 12:47:21 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psMinimize.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/math/psMinimize.c
r1953 r1963 9 9 * @author George Gusciora, MHPCC 10 10 * 11 * @version $Revision: 1.5 2$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-10-05 01:43:58$11 * @version $Revision: 1.53 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-10-05 22:47:21 $ 13 13 * 14 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 56 56 57 57 XXX: change name 58 XXX: Use a static vector. 58 59 *****************************************************************************/ 59 60 void p_psBuildSums1D(double x, … … 63 64 int i = 0; 64 65 double xSum = 0.0; 66 65 67 if (sums == NULL) { 66 68 sums = psVectorAlloc(polyOrder, PS_TYPE_F64); 69 } 70 71 if (polyOrder > sums->n) { 72 sums = psVectorRealloc(sums, polyOrder); 67 73 } 68 74 … … 77 83 VectorNormalizeGen(): this routine returns a psVector with "x" elements. The 78 84 values of the vector will be scaled uniformly between -1.0 and 1.0. 85 86 XXX: use a static vector. 79 87 *****************************************************************************/ 80 88 psVector* VectorNormalizeGen(int x) … … 100 108 101 109 XXX: This algorithm is derived from the Numerical Recipes. 110 XXX: use recyvled vectors for internal data. 102 111 *****************************************************************************/ 103 112 float *CalculateSecondDerivs(const psVector* restrict x, ///< Ordinates (or NULL to just use the indices) … … 119 128 float qn; 120 129 121 if (x == NULL) {122 X = (float *) psAlloc(n * sizeof(float));123 for(i=0;i<n;i++) {124 X[i] = (float) i;125 }126 mustFreeX = true;127 }128 129 130 // XXX: The second derivatives at the endpoints, undefined in the SDR, 130 131 // are set in psConstants.h: LEFT_SPLINE_DERIV, RIGHT_SPLINE_DERIV. … … 167 168 return(derivs2); 168 169 } 169 170 170 171 171 /****************************************************************************** … … 212 212 } 213 213 214 215 214 /*****************************************************************************/ 216 215 /* FUNCTION IMPLEMENTATION - PUBLIC */ … … 258 257 int i; 259 258 float slope; 260 261 if (mySpline == NULL) { 262 //XXX psErrorMsg() 263 } 264 if (y == NULL) { 265 //XXX psErrorMsg() 266 } 259 psVector *x32 = NULL; 260 psVector *y32 = NULL; 261 psVector *yErr32 = NULL; 262 static psVector *x32Static = NULL; 263 static psVector *y32Static = NULL; 264 static psVector *yErr32Static = NULL; 265 266 PS_CHECK_NULL_PTR_RETURN_NULL(mySpline); 267 PS_CHECK_NULL_PTR_RETURN_NULL(y); 268 VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(y); 269 CONVERT_VECTOR_F64_TO_F32(y, y32, y32Static); 270 271 // If yErr==NULL, set all errors equal. 272 if (yErr == NULL) { 273 GEN_PERSIST_YERR_VEC_F32(yErr32Static, y->n); 274 yErr32 = yErr32Static; 275 } else { 276 VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(yErr); 277 CONVERT_VECTOR_F64_TO_F32(yErr, yErr32, yErr32Static); 278 } 279 280 // If x==NULL, create an x32 vector with x values set to (0:n). 281 if (x == NULL) { 282 GEN_PERSIST_X_INDEX_VEC_F32(x32Static, y->n); 283 x32 = x32Static; 284 } else { 285 VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(x); 286 CONVERT_VECTOR_F64_TO_F32(x, x32, x32Static); 287 } 288 PS_CHECK_VECTOR_SIZE_EQUAL_RETURN_NULL(x, y); 289 PS_CHECK_VECTOR_SIZE_EQUAL_RETURN_NULL(yErr, y); 267 290 268 291 if (y->n != (1 + mySpline->n)) { 269 psLogMsg(__func__, PS_LOG_WARN, 270 "data size / spline size mismatch (%d %d)\n", 271 y->n, mySpline->n); 272 // XXX: psErrorMsg() 292 psError(__func__, "data size / spline size mismatch (%d %d)\n", 293 y->n, mySpline->n); 294 return(NULL); 273 295 } 274 296 … … 295 317 // Check if these are cubic splines (n==4). If not, psError. 296 318 if (4 != (mySpline->spline[0])->n) { 297 psLogMsg(__func__, PS_LOG_WARN, 298 "Don't know how to generate %d-order splines.", 299 (mySpline->spline[0])->n-1); 300 // XXX: psErrorMsg() 319 psError(__func__, "Don't know how to generate %d-order splines.", 320 (mySpline->spline[0])->n-1); 301 321 return(NULL); 302 322 } … … 315 335 H = x->data.F32[i+1] - x->data.F32[i]; 316 336 psTrace(".psLib.dataManip.psVectorFitSpline1D", 4, 317 " Xdata (%f - %f) (%f)\n", x->data.F32[i], x->data.F32[i+1], H);337 "x data (%f - %f) (%f)\n", x->data.F32[i], x->data.F32[i+1], H); 318 338 // 319 339 // ******** Calculate 0-order term ******** … … 630 650 } 631 651 632 #define GEN_STATIC_RECYCLED_VECTOR(NAME, SIZE, TYPE) \633 static psVector *NAME = NULL; \634 NAME = psVectorRecycle(NAME, SIZE, TYPE); \635 p_psMemSetPersistent(NAME, true); \636 p_psMemSetPersistent(NAME->data.V, true); \637 638 639 652 /****************************************************************************** 640 653 p_psVectorFitPolynomial1DCheb(): This routine will fit a Chebyshev … … 854 867 int i; 855 868 psPolynomial1D *tmpPoly; 856 PS_CHECK_NULL_1DPOLY_ ACTION(myPoly, 0);857 PS_CHECK_NULL_VECTOR_ ACTION(y, 0);858 PS_CHECK_EMPTY_VECTOR_ ACTION(y, 0);869 PS_CHECK_NULL_1DPOLY_RETURN_NULL(myPoly); 870 PS_CHECK_NULL_VECTOR_RETURN_NULL(y); 871 PS_CHECK_EMPTY_VECTOR_RETURN_NULL(y); 859 872 psVector *x64 = NULL; 860 873 psVector *y64 = NULL; … … 864 877 static psVector *yErr64Static = NULL; 865 878 866 if (y->type.type == PS_TYPE_F64) { 867 y64 = (psVector *) y; 868 } else if (y->type.type == PS_TYPE_F32) { 869 y64Static = psVectorRecycle(y64Static, y->n, PS_TYPE_F64); 870 p_psMemSetPersistent(y64Static, true); 871 p_psMemSetPersistent(y64Static->data.V, true); 872 y64 = y64Static; 873 } else { 874 // XXX: psError() bad type. 875 psAbort(__func__, "Bad type for y64 (%d)", y->type.type); 876 } 877 879 VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(y); 880 CONVERT_VECTOR_F32_TO_F64(y, y64, y64Static); 878 881 879 882 // If yErr==NULL, set all errors equal. 880 883 if (yErr == NULL) { 881 yErr64Static = psVectorRecycle(yErr64Static, y->n, PS_TYPE_F64); 882 p_psMemSetPersistent(yErr64Static, true); 883 p_psMemSetPersistent(yErr64Static->data.V, true); 884 885 for (i=0;i<yErr64Static->n;i++) { 886 yErr64Static->data.F64[i] = 1.0; 887 } 884 GEN_PERSIST_YERR_VEC_F64(yErr64Static, y->n); 888 885 yErr64 = yErr64Static; 889 886 } else { 890 if (yErr->type.type == PS_TYPE_F64) { 891 yErr64 = (psVector *) yErr; 892 } else if (yErr->type.type == PS_TYPE_F32) { 893 yErr64Static = psVectorRecycle(yErr64Static, yErr->n, PS_TYPE_F64); 894 p_psMemSetPersistent(yErr64Static, true); 895 p_psMemSetPersistent(yErr64Static->data.V, true); 896 } else { 897 // XXX: psError() bad type. 898 psAbort(__func__, "Bad type for yErr64"); 899 } 900 } 901 902 // If x==NULL, create an x64 vector with x values set to (0:n), and if 903 // this is a Chebyshev polynomial, we must scale to (-1:1). 904 // XXX: Verify that this is the correct action. 887 VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(yErr); 888 CONVERT_VECTOR_F32_TO_F64(yErr, yErr64, yErr64Static); 889 } 890 891 // If x==NULL, create an x64 vector with x values set to (0:n). 905 892 if (x == NULL) { 906 x64Static = psVectorRecycle(x64Static, y->n, PS_TYPE_F64); 907 p_psMemSetPersistent(x64Static, true); 908 p_psMemSetPersistent(x64Static->data.V, true); 893 GEN_PERSIST_X_INDEX_VEC_F64(x64Static, y->n); 894 if (myPoly->type == PS_POLYNOMIAL_CHEB) { 895 p_psNormalizeVector(x64Static); 896 } 909 897 x64 = x64Static; 910 911 if (myPoly->type == PS_POLYNOMIAL_ORD) {912 for (i=0;i<x64->n;i++) {913 x64->data.F64[i] = (float) i;914 }915 } else if (myPoly->type == PS_POLYNOMIAL_CHEB) {916 double min = 0.0;917 double max = (double) (y->n - 1);918 919 for (i=0;i<x64->n;i++) {920 x64->data.F64[i] = (((double) i) - 0.5 * (min + max)) /921 (0.5 * (max - min));922 }923 }924 898 } else { 925 if (x->type.type == PS_TYPE_F64) { 926 x64 = (psVector *) x; 927 } else if (x->type.type == PS_TYPE_F32) { 928 x64Static = psVectorRecycle(x64Static, x->n, PS_TYPE_F64); 929 p_psMemSetPersistent(x64Static, true); 930 p_psMemSetPersistent(x64Static->data.V, true); 931 x64 = x64Static; 932 } else { 933 // XXX: psError() bad type. 934 psAbort(__func__, "Bad type for x64"); 935 } 936 } 937 938 PS_CHECK_VECTOR_SIZE_EQUAL(y64, x64); 939 PS_CHECK_VECTOR_SIZE_EQUAL(y64, yErr64); 899 VECTOR_CHECK_TYPE_F32_OR_F64_RETURN_NULL(x); 900 CONVERT_VECTOR_F32_TO_F64(x, x64, x64Static); 901 } 902 PS_CHECK_VECTOR_SIZE_EQUAL_RETURN_NULL(x64, y64); 903 PS_CHECK_VECTOR_SIZE_EQUAL_RETURN_NULL(yErr64, y64); 940 904 941 905 // Call the appropriate vector fitting routine. 942 906 if (myPoly->type == PS_POLYNOMIAL_CHEB) { 943 tmpPoly =p_psVectorFitPolynomial1DCheby(myPoly, x64, y64, yErr64);907 p_psVectorFitPolynomial1DCheby(myPoly, x64, y64, yErr64); 944 908 } else if (myPoly->type == PS_POLYNOMIAL_ORD) { 945 909 tmpPoly = p_psVectorFitPolynomial1DOrd(myPoly, x64, y64, yErr64); … … 948 912 return(NULL); 949 913 } 950 951 // Free any allocated memory.952 /*953 if ((yErr == NULL) || (yErr->type.type != PS_TYPE_F64)) {954 psFree(yErr64);955 }956 if ((x == NULL) || (x->type.type != PS_TYPE_F64)) {957 psFree(x64);958 }959 if ((y == NULL) || (y->type.type != PS_TYPE_F64)) {960 psFree(y64);961 }962 */963 914 964 915 return(myPoly);
Note:
See TracChangeset
for help on using the changeset viewer.
