Changeset 4731 for trunk/psLib/test/math/tst_psMinimize06.c
- Timestamp:
- Aug 8, 2005, 11:44:09 AM (21 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/math/tst_psMinimize06.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/math/tst_psMinimize06.c
r4547 r4731 6 6 #include "psTest.h" 7 7 #include <math.h> 8 #define NUM_ITERATIONS 100 008 #define NUM_ITERATIONS 100 9 9 #define ERR_TOL 0.0 10 #define N 20 11 #define MIN_VALUE 5.0 12 #define NUM_PARAMS 3 10 #define NUM_DATA_POINTS 20 11 #define NUM_PARAMS 5 13 12 float expectedParm[NUM_PARAMS]; 14 13 psS32 testStatus = true; … … 16 15 /***************************************************************************** 17 16 myFunc(): 18 sum = (param[0] * x[0]) + 19 (param[1] * x[1]) + 20 ... 17 sum = param[0] + x[0] * x[1] + 18 param[1] * x[0] + 19 param[2] * x[0]^2 + 20 param[3] * x[1] + 21 param[4] * x[1]^2 22 21 23 *****************************************************************************/ 22 psF 64myFunc(psVector *deriv,24 psF32 myFunc(psVector *deriv, 23 25 psVector *params, 24 26 psVector *x) 25 27 { 26 if ((deriv == NULL) || 27 (params == NULL)) { 28 if ((deriv == NULL) || (params == NULL)) { 28 29 psError(PS_ERR_UNKNOWN, true, "deriv or params is NULL.\n"); 29 30 } 30 31 31 psF64 sum = 0.0; 32 for (psS32 i=0 ; i < params->n ; i++) { 33 sum += (params->data.F32[i] * x->data.F32[i]); 34 deriv->data.F32[i] = params->data.F32[i]; 35 } 32 psF32 sum = (params->data.F32[0] * x->data.F32[0] * x->data.F32[1]) + 33 (params->data.F32[1] * x->data.F32[0]) + 34 (params->data.F32[2] * PS_SQR(x->data.F32[0])) + 35 (params->data.F32[3] * x->data.F32[1]) + 36 (params->data.F32[4] * PS_SQR(x->data.F32[1])); 37 38 deriv->data.F32[0] = x->data.F32[0] * x->data.F32[1]; 39 deriv->data.F32[1] = x->data.F32[0]; 40 deriv->data.F32[2] = PS_SQR(x->data.F32[0]); 41 deriv->data.F32[3] = x->data.F32[1]; 42 deriv->data.F32[4] = PS_SQR(x->data.F32[1]); 36 43 37 44 return(sum); … … 42 49 { 43 50 psS32 currentId = psMemGetId(); 44 psS32 memLeaks = 0;45 psS32 i = 0;46 psArray *myCoords;47 psVector *myParams;48 psImage *myCovar;49 psMinimization *min;50 psVector *y;51 52 51 psTraceSetLevel(".psLib", 0); 53 52 /************************************************************************** 54 53 *************************************************************************/ 55 min = psMinimizationAlloc(NUM_ITERATIONS, ERR_TOL); 56 myCovar = psImageAlloc(NUM_PARAMS, NUM_PARAMS, PS_TYPE_F32); 57 myParams = psVectorAlloc(NUM_PARAMS, PS_TYPE_F32); 58 myCoords = psArrayAlloc(N); 59 y = psVectorAlloc(N, PS_TYPE_F32); 54 psMinimization *min = psMinimizationAlloc(NUM_ITERATIONS, ERR_TOL); 55 psImage *myCovar = psImageAlloc(NUM_PARAMS, NUM_PARAMS, PS_TYPE_F32); 56 psVector *myParams = psVectorAlloc(NUM_PARAMS, PS_TYPE_F32); 57 psVector *myDerivs = psVectorAlloc(NUM_PARAMS, PS_TYPE_F32); 58 psArray *myCoords = psArrayAlloc(NUM_DATA_POINTS); 59 psVector *y = psVectorAlloc(NUM_DATA_POINTS, PS_TYPE_F32); 60 60 61 for ( i=0;i<N;i++) {61 for (psS32 i=0;i<NUM_DATA_POINTS;i++) { 62 62 myCoords->data[i] = (psPtr *) psVectorAlloc(2, PS_TYPE_F32); 63 63 ((psVector *) (myCoords->data[i]))->data.F32[0] = (float) (i+10); 64 64 ((psVector *) (myCoords->data[i]))->data.F32[1] = (float) (i+3); 65 /* 66 printf("x[%d] is vector (%f, %f)\n", i, ((psVector *) (myCoords->data[i]))->data.F32[0], 67 ((psVector *) (myCoords->data[i]))->data.F32[1]); 68 */ 65 69 y->data.F32[i] = (float) i; 66 70 } 67 for ( i=0;i<NUM_PARAMS;i++) {71 for (psS32 i=0;i<NUM_PARAMS;i++) { 68 72 expectedParm[i] = 2.42 + (float) (2 * i); 69 myParams->data.F32[i] = (float) i; 70 myParams->data.F32[i] = expectedParm[i] * 1.3; 71 myParams->data.F32[i] = 0.0; 72 myParams->data.F32[i] = (float) (5 + i); 73 myParams->data.F32[i] = (float) (2 * i); 73 74 } 74 75 … … 82 83 (psMinimizeLMChi2Func) myFunc); 83 84 84 printf("\nThe chi-squared is %f\n", min->value); 85 for (i=0;i<NUM_PARAMS;i++) { 85 printf("\nThe value of the function at the minimum is %f\n", min->value); 86 for (psS32 i=0;i<NUM_DATA_POINTS;i++) { 87 printf("The minimum for data point %d: %f\n", i, myFunc(myDerivs, myParams, (psVector *) myCoords->data[i])); 88 } 89 90 for (psS32 i=0;i<NUM_PARAMS;i++) { 86 91 printf("Parameter %d at the minimum is %f (expected: %f)\n", i, 87 92 myParams->data.F32[i], expectedParm[i]); … … 91 96 psFree(myCovar); 92 97 psFree(myParams); 98 psFree(myDerivs); 93 99 psFree(myCoords); 94 100 psFree(y); 95 101 96 102 psMemCheckCorruption(1); 97 memLeaks = psMemCheckLeaks(currentId,NULL,NULL,false);103 psS32 memLeaks = psMemCheckLeaks(currentId,NULL,NULL,false); 98 104 if (0 != memLeaks) { 99 105 psAbort(__func__,"Memory Leaks! (%d leaks)", memLeaks);
Note:
See TracChangeset
for help on using the changeset viewer.
