Changeset 1407 for trunk/psLib/src/dataManip/psMinimize.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/dataManip/psMinimize.c (modified) (63 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/dataManip/psMinimize.c
r1406 r1407 1 1 2 /** @file psMinimize.c 2 3 * \brief basic minimization functions … … 9 10 * @author George Gusciora, MHPCC 10 11 * 11 * @version $Revision: 1.2 7$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-08-0 6 22:34:05$12 * @version $Revision: 1.28 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-08-07 00:06:06 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 16 */ 16 /*****************************************************************************/ 17 18 /*****************************************************************************/ 19 17 20 /* INCLUDE FILES */ 21 18 22 /*****************************************************************************/ 19 23 #include <stdlib.h> … … 40 44 #include "psMinimize.h" 41 45 #include "psMatrix.h" 42 /*****************************************************************************/ 46 47 /*****************************************************************************/ 48 43 49 /* DEFINE STATEMENTS */ 50 44 51 /*****************************************************************************/ 45 52 #define MAX_LMM_ITERATIONS 100 … … 83 90 } 84 91 85 86 /*****************************************************************************/ 92 /*****************************************************************************/ 93 87 94 /* TYPE DEFINITIONS */ 95 88 96 /*****************************************************************************/ 89 97 typedef struct 90 98 { 91 size_t n; // Number of data points points in domain.92 int paramCount; // Number of non-masked parameters.93 psVector *restrict initialGuess;94 const psImage *restrict domain;99 size_t n; // Number of data points points in domain. 100 int paramCount; // Number of non-masked parameters. 101 psVector *restrict initialGuess; 102 const psImage *restrict domain; 95 103 const psVector *restrict data; 96 const psVector *restrict errors;104 const psVector *restrict errors; 97 105 const psVector *restrict paramMask; 98 106 float (*evalModel) (const psVector *, const psVector *); … … 103 111 typedef struct 104 112 { 105 int paramCount; // Number of non-masked parameters.106 psVector *restrict initialGuess;107 const psVector *restrict coord;113 int paramCount; // Number of non-masked parameters. 114 psVector *restrict initialGuess; 115 const psVector *restrict coord; 108 116 const psVector *restrict paramMask; 109 117 float (*evalModel) (const psVector *, const psVector *); … … 113 121 114 122 /*****************************************************************************/ 123 115 124 /* GLOBAL VARIABLES */ 125 116 126 /*****************************************************************************/ 117 127 … … 119 129 120 130 /*****************************************************************************/ 131 121 132 /* FILE STATIC VARIABLES */ 133 122 134 /*****************************************************************************/ 123 135 … … 125 137 126 138 /*****************************************************************************/ 139 127 140 /* FUNCTION IMPLEMENTATION - LOCAL */ 141 128 142 /*****************************************************************************/ 129 143 … … 143 157 guess at the parameters, an option parameter mask, etc. 144 158 *****************************************************************************/ 145 double p_psMinFunc(const gsl_vector *params, 146 void *funcData) 147 { 148 int i; // Loop index variable. 149 int j; // Loop index variable. 150 float tmpf; // Temporary floating point variable. 151 const psVector *restrict coord = ((psMinimizeData *) funcData)->coord; 152 const psVector *restrict mask = ((psMinimizeData *) funcData)->paramMask; 153 psVector *restrict initialGuess = ((psMinimizeData *)funcData)->initialGuess; 154 float (*evalModel)(const psVector *, const psVector *) = 155 ((psMinimizeData *) funcData)->evalModel; 159 double p_psMinFunc(const gsl_vector * params, void *funcData) 160 { 161 int i; // Loop index variable. 162 int j; // Loop index variable. 163 float tmpf; // Temporary floating point variable. 164 const psVector *restrict coord = ((psMinimizeData *) funcData)->coord; 165 const psVector *restrict mask = ((psMinimizeData *) funcData)->paramMask; 166 psVector *restrict initialGuess = ((psMinimizeData *) funcData)->initialGuess; 167 float (*evalModel) (const psVector *, const psVector *) = ((psMinimizeData *) funcData)->evalModel; 156 168 psVector *inputParameterList = NULL; 157 169 … … 163 175 if (mask != NULL) { 164 176 j = 0; 165 for (i =0;i<mask->n;i++) {177 for (i = 0; i < mask->n; i++) { 166 178 if (mask->data.U8[i] != 0) { 167 179 inputParameterList->data.F32[i] = initialGuess->data.F32[i]; … … 171 183 } 172 184 } else { 173 for (i =0;i<initialGuess->n;i++) {185 for (i = 0; i < initialGuess->n; i++) { 174 186 inputParameterList->data.F32[i] = gsl_vector_get(params, i); 175 187 } … … 181 193 // Free allocated memory and return the value of the function. 182 194 psFree(inputParameterList); 183 return (tmpf);195 return (tmpf); 184 196 } 185 197 … … 199 211 in "params" and return those derivatives in this psVector. 200 212 *****************************************************************************/ 201 void p_psMinFuncDeriv(const gsl_vector *params, 202 void *funcData, 203 gsl_vector *df) 204 { 205 int i; // Loop index variable. 206 int j; // Loop index variable. 207 float tmpf; // Temporary floating point variable. 208 const psVector *restrict coord = ((psMinimizeData *) funcData)->coord; 209 const psVector *restrict mask = ((psMinimizeData *) funcData)->paramMask; 210 psVector *restrict initialGuess = ((psMinimizeData *)funcData)->initialGuess; 211 float (*d_evalModel)(const psVector *, const psVector *, int) = 213 void p_psMinFuncDeriv(const gsl_vector * params, void *funcData, gsl_vector * df) 214 { 215 int i; // Loop index variable. 216 int j; // Loop index variable. 217 float tmpf; // Temporary floating point variable. 218 const psVector *restrict coord = ((psMinimizeData *) funcData)->coord; 219 const psVector *restrict mask = ((psMinimizeData *) funcData)->paramMask; 220 psVector *restrict initialGuess = ((psMinimizeData *) funcData)->initialGuess; 221 float (*d_evalModel) (const psVector *, const psVector *, int) = 212 222 ((psMinimizeData *) funcData)->d_evalModel; 213 223 psVector *inputParameterList = NULL; … … 220 230 if (mask != NULL) { 221 231 j = 0; 222 for (i =0;i<mask->n;i++) {232 for (i = 0; i < mask->n; i++) { 223 233 if (mask->data.U8[i] != 0) { 224 234 inputParameterList->data.F32[i] = initialGuess->data.F32[i]; … … 228 238 } 229 239 } else { 230 for (i =0;i<initialGuess->n;i++) {240 for (i = 0; i < initialGuess->n; i++) { 231 241 inputParameterList->data.F32[i] = gsl_vector_get(params, i); 232 242 } … … 235 245 // Evaluate the derivative w.r.t. each parameter. 236 246 // NOTE: we can probably remove the calls for masked parameters. 237 for (i =0;i<initialGuess->n;i++) {247 for (i = 0; i < initialGuess->n; i++) { 238 248 tmpf = d_evalModel(inputParameterList, coord, i); 239 249 gsl_vector_set(df, i, tmpf); … … 247 257 Compute both p_psMinFunc and p_psMinFuncDeriv together. 248 258 *****************************************************************************/ 249 void p_psMinFuncFuncDeriv(const gsl_vector *params, 250 void *funcData, 251 double *f, 252 gsl_vector *df) 259 void p_psMinFuncFuncDeriv(const gsl_vector * params, void *funcData, double *f, gsl_vector * df) 253 260 { 254 261 *f = p_psMinFunc(params, funcData); … … 281 288 expected value and divide by the error. 282 289 *****************************************************************************/ 283 int p_psMinChi2Func(const gsl_vector *params, 284 void *funcData, 285 gsl_vector *outData) 286 { 287 int i; // Loop index variable. 288 int j; // Loop index variable. 289 float tmpf; // Temporary floating point variable. 290 const psImage *restrict domain = ((psMinChi2Data *)funcData)->domain; 291 const psVector *restrict data = ((psMinChi2Data *)funcData)->data; 292 const psVector *restrict errors = ((psMinChi2Data *) funcData)->errors; 293 const psVector *restrict mask = ((psMinChi2Data *) funcData)->paramMask; 294 psVector *restrict initialGuess = ((psMinChi2Data *)funcData)->initialGuess; 295 float (*evalModel)(const psVector *, const psVector *) = ((psMinChi2Data *) funcData)->evalModel; 290 int p_psMinChi2Func(const gsl_vector * params, void *funcData, gsl_vector * outData) 291 { 292 int i; // Loop index variable. 293 int j; // Loop index variable. 294 float tmpf; // Temporary floating point variable. 295 const psImage *restrict domain = ((psMinChi2Data *) funcData)->domain; 296 const psVector *restrict data = ((psMinChi2Data *) funcData)->data; 297 const psVector *restrict errors = ((psMinChi2Data *) funcData)->errors; 298 const psVector *restrict mask = ((psMinChi2Data *) funcData)->paramMask; 299 psVector *restrict initialGuess = ((psMinChi2Data *) funcData)->initialGuess; 300 float (*evalModel) (const psVector *, const psVector *) = ((psMinChi2Data *) funcData)->evalModel; 296 301 psVector *inputParameterList = NULL; 297 302 psVector *tmpVecPtr = NULL; … … 307 312 if (mask != NULL) { 308 313 j = 0; 309 for (i =0;i<mask->n;i++) {314 for (i = 0; i < mask->n; i++) { 310 315 if (mask->data.U8[i] != 0) { 311 316 inputParameterList->data.F32[i] = initialGuess->data.F32[i]; … … 315 320 } 316 321 } else { 317 for (i =0;i<initialGuess->n;i++) {322 for (i = 0; i < initialGuess->n; i++) { 318 323 inputParameterList->data.F32[i] = gsl_vector_get(params, i); 319 324 } … … 321 326 322 327 // Evaluate the function at each data point. 323 for (i =0;i<domain->numRows;i++) {324 for (j =0;j<domain->numCols;j++) {328 for (i = 0; i < domain->numRows; i++) { 329 for (j = 0; j < domain->numCols; j++) { 325 330 tmpVecPtr->data.F32[j] = domain->data.F32[i][j]; 326 331 } 327 332 tmpf = evalModel(tmpVecPtr, inputParameterList); 328 333 329 gsl_vector_set(outData, i, (tmpf - data->data.F32[i])/ 330 errors->data.F32[i]); 334 gsl_vector_set(outData, i, (tmpf - data->data.F32[i]) / errors->data.F32[i]); 331 335 } 332 336 … … 354 358 and returned in this data structure. 355 359 *****************************************************************************/ 356 int p_psMinChi2FuncDeriv(const gsl_vector *params, 357 void *funcData, 358 gsl_matrix *J) 359 { 360 const psImage *restrict domain = ((psMinChi2Data *)funcData)->domain; 361 const psVector *restrict errors = ((psMinChi2Data *) funcData)->errors; 362 const psVector *restrict mask = ((psMinChi2Data *) funcData)->paramMask; 363 psVector *restrict initialGuess = ((psMinChi2Data *)funcData)->initialGuess; 360 int p_psMinChi2FuncDeriv(const gsl_vector * params, void *funcData, gsl_matrix * J) 361 { 362 const psImage *restrict domain = ((psMinChi2Data *) funcData)->domain; 363 const psVector *restrict errors = ((psMinChi2Data *) funcData)->errors; 364 const psVector *restrict mask = ((psMinChi2Data *) funcData)->paramMask; 365 psVector *restrict initialGuess = ((psMinChi2Data *) funcData)->initialGuess; 364 366 psVector *inputParameterList = NULL; 365 367 psVector *tmpVecPtr = NULL; 366 float (*d_evalModel)(const psVector *, const psVector *, int) = ((psMinChi2Data *) funcData)->d_evalModel; 368 float (*d_evalModel) (const psVector *, const psVector *, int) = 369 ((psMinChi2Data *) funcData)->d_evalModel; 367 370 368 371 size_t i; … … 380 383 if (mask != NULL) { 381 384 j = 0; 382 for (i =0;i<mask->n;i++) {385 for (i = 0; i < mask->n; i++) { 383 386 if (mask->data.U8[i] != 0) { 384 387 inputParameterList->data.F32[i] = initialGuess->data.F32[i]; … … 388 391 } 389 392 } else { 390 for (i =0;i<initialGuess->n;i++) {393 for (i = 0; i < initialGuess->n; i++) { 391 394 inputParameterList->data.F32[i] = gsl_vector_get(params, i); 392 395 } … … 394 397 395 398 // Evaluate the derivtaive at each data point, and w.r.t. each parameter. 396 for (i =0;i<domain->numRows;i++) {397 for (j =0;j<tmpVecPtr->n;j++) {399 for (i = 0; i < domain->numRows; i++) { 400 for (j = 0; j < tmpVecPtr->n; j++) { 398 401 tmpVecPtr->data.F32[j] = domain->data.F32[i][j]; 399 402 } 400 403 401 for (j =0;j<inputParameterList->n;j++) {404 for (j = 0; j < inputParameterList->n; j++) { 402 405 tmpf = d_evalModel(tmpVecPtr, inputParameterList, j); 403 gsl_matrix_set(J, i, j, (tmpf /errors->data.F32[i]));406 gsl_matrix_set(J, i, j, (tmpf / errors->data.F32[i])); 404 407 } 405 408 } … … 410 413 } 411 414 412 413 int p_psMinChi2FuncFuncDeriv(const gsl_vector *params, 414 void *funcData, 415 gsl_vector *f, 416 gsl_matrix *J) 415 int p_psMinChi2FuncFuncDeriv(const gsl_vector * params, void *funcData, gsl_vector * f, gsl_matrix * J) 417 416 { 418 417 p_psMinChi2Func(params, funcData, f); … … 421 420 return GSL_SUCCESS; 422 421 } 423 424 422 425 423 /****************************************************************************** … … 428 426 returned as a psVector sums. 429 427 *****************************************************************************/ 430 void p_psBuildSums1D(double x, 431 int polyOrder, 432 psVector *sums) 433 { 434 int i = 0; 435 double xSum = 0.0; 428 void p_psBuildSums1D(double x, int polyOrder, psVector * sums) 429 { 430 int i = 0; 431 double xSum = 0.0; 436 432 437 433 xSum = 1.0; 438 for (i=0;i<=polyOrder;i++) {434 for (i = 0; i <= polyOrder; i++) { 439 435 sums->data.F64[i] = xSum; 440 xSum*= x; 441 } 442 } 443 436 xSum *= x; 437 } 438 } 444 439 445 440 /****************************************************************************** … … 448 443 *****************************************************************************/ 449 444 psVector *psBuildImageScalingFactors(int x) 450 451 445 { 452 446 int i = 0; // loop index variable. 453 447 psVector *imageScalingFactors = NULL; 454 448 455 456 449 imageScalingFactors = psVectorAlloc(x, PS_TYPE_F32); 457 450 458 for (i =0;i<x;i++) {459 imageScalingFactors->data.F32[i] = (((float) 2*i) / ((float)x)) - 1.0;460 } 461 462 return (imageScalingFactors);451 for (i = 0; i < x; i++) { 452 imageScalingFactors->data.F32[i] = (((float)2 * i) / ((float)x)) - 1.0; 453 } 454 455 return (imageScalingFactors); 463 456 } 464 457 … … 481 474 *flag Set this to 1 if we must recalculate the coefficients. 482 475 *****************************************************************************/ 483 void p_psPolyOrderCheck(float **A, 484 int N, 485 int *indx, 486 float *B, 487 int polyOrder, 488 int *flag) 489 { 490 float **y = NULL; // This 2-D matrix will hold A^-1 491 float *col = NULL; // misc NumerRecipes data structure 492 float *error=NULL; // will hold the sqrt() of the 476 void p_psPolyOrderCheck(float **A, int N, int *indx, float *B, int polyOrder, int *flag) 477 { 478 float **y = NULL; // This 2-D matrix will hold A^-1 479 float *col = NULL; // misc NumerRecipes data structure 480 float *error = NULL; // will hold the sqrt() of the 481 493 482 // diagonal of y[][]. 494 int i=0; // loop-index variable 495 int j=0; // loop-index variable 496 int numPolyTerms = 0; // The number of terms in the 483 int i = 0; // loop-index variable 484 int j = 0; // loop-index variable 485 int numPolyTerms = 0; // The number of terms in the 486 497 487 // polynomial. 498 int lastTerm = 0; // The index location of the first 488 int lastTerm = 0; // The index location of the first 489 499 490 // n-th order term in array B[]. 500 int firstTerm = 0; // Index location of last such term.491 int firstTerm = 0; // Index location of last such term. 501 492 502 493 // Allocate the necessary data structures for this procedure... 503 error = (float *) psAlloc((N + 1) * sizeof(float));504 col = (float *) psAlloc((N + 1) * sizeof(float));505 y = (float **) psAlloc((N + 1) * sizeof(float *));506 for (i=1;i<=N;i++) {507 y[i] = (float *) psAlloc((N + 1) * sizeof(float));494 error = (float *)psAlloc((N + 1) * sizeof(float)); 495 col = (float *)psAlloc((N + 1) * sizeof(float)); 496 y = (float **)psAlloc((N + 1) * sizeof(float *)); 497 for (i = 1; i <= N; i++) { 498 y[i] = (float *)psAlloc((N + 1) * sizeof(float)); 508 499 } 509 500 510 501 // Invert the matrix A and put the result in y[][]. This code is taken 511 502 // from Numerical Recipes in C page 48. 512 for (j=1;j<=N;j++) {513 for (i=1;i<=N;i++) {503 for (j = 1; j <= N; j++) { 504 for (i = 1; i <= N; i++) { 514 505 col[i] = 0.0; 515 506 } 516 507 col[j] = 1.0; 517 508 // NOTE: substitue the LUD rotine 518 // lubksb(A, N, indx, col);519 for (i=1;i<=N;i++) {509 // lubksb(A, N, indx, col); 510 for (i = 1; i <= N; i++) { 520 511 y[i][j] = col[i]; 521 512 } … … 527 518 // terms and check if they are consistent with zero. 528 519 529 numPolyTerms = (((polyOrder +1) * (polyOrder + 2)) / 2);520 numPolyTerms = (((polyOrder + 1) * (polyOrder + 2)) / 2); 530 521 lastTerm = numPolyTerms + 1; 531 522 firstTerm = lastTerm - polyOrder; 532 523 *flag = 1; 533 for (i =firstTerm; i<=lastTerm; i++) {524 for (i = firstTerm; i <= lastTerm; i++) { 534 525 #ifdef DARWIN 535 526 error[i] = (float)sqrt(y[i][i]); … … 539 530 #endif 540 531 541 if (!((B[i] <= (2.0f * error[i])) && 542 ((-2.0f * error[i]) <= B[i]))) { 532 if (!((B[i] <= (2.0f * error[i])) && ((-2.0f * error[i]) <= B[i]))) { 543 533 *flag = 0; 544 534 } … … 548 538 psFree(error); 549 539 psFree(col); 550 for (j=1;j<=N;j++) {540 for (j = 1; j <= N; j++) { 551 541 psFree(y[j]); 552 542 } … … 554 544 } 555 545 556 557 558 559 /*****************************************************************************/ 546 /*****************************************************************************/ 547 560 548 /* FUNCTION IMPLEMENTATION - PUBLIC */ 561 /*****************************************************************************/ 562 563 549 550 /*****************************************************************************/ 564 551 565 552 /****************************************************************************** … … 569 556 parameters of that function such that the ... 570 557 *****************************************************************************/ 571 psVector * 572 psMinimize(psVector *restrict initialGuess, 573 float (*myFunction)(const psVector *restrict, const psVector *restrict), 574 float (*myFunctionDeriv)(const psVector *restrict, const psVector *restrict, int), 575 const psVector *restrict coord, 576 const psVector *restrict paramMask) 558 psVector *psMinimize(psVector * restrict initialGuess, 559 float (*myFunction) (const psVector * restrict, const psVector * restrict), 560 float (*myFunctionDeriv) (const psVector * restrict, const psVector * restrict, int), 561 const psVector * restrict coord, const psVector * restrict paramMask) 577 562 { 578 563 int status; … … 607 592 // for the parameters. 608 593 if (paramMask != NULL) { 609 for (i =0;i<paramMask->n;i++) {594 for (i = 0; i < paramMask->n; i++) { 610 595 if (paramMask->data.U8[i] != 0) { 611 596 inputData.paramCount++; … … 613 598 } 614 599 } else { 615 inputData.paramCount = initialGuess->n;600 inputData.paramCount = initialGuess->n; 616 601 } 617 602 … … 622 607 if (paramMask != NULL) { 623 608 j = 0; 624 for (i =0;i<initialGuess->n;i++) {609 for (i = 0; i < initialGuess->n; i++) { 625 610 if (paramMask->data.U8[i] == 0) { 626 611 gsl_vector_set(x, j++, initialGuess->data.F32[i]); … … 628 613 } 629 614 } else { 630 for (i =0;i<initialGuess->n;i++) {615 for (i = 0; i < initialGuess->n; i++) { 631 616 gsl_vector_set(x, i, initialGuess->data.F32[i]); 632 617 } … … 651 636 652 637 if (status == GSL_SUCCESS) 653 printf ("Minimum found at:\n");638 printf("Minimum found at:\n"); 654 639 655 640 } while (status == GSL_CONTINUE && iter < MAX_MINIMIZE_ITERATIONS); … … 660 645 if (paramMask != NULL) { 661 646 j = 0; 662 for (i =0;i<initialGuess->n;i++) {647 for (i = 0; i < initialGuess->n; i++) { 663 648 if (paramMask->data.U8[i] == 0) { 664 649 initialGuess->data.F32[i] = gsl_vector_get(s->x, j++); … … 668 653 } 669 654 } else { 670 for (i =0;i<initialGuess->n;i++) {655 for (i = 0; i < initialGuess->n; i++) { 671 656 initialGuess->data.F32[i] = gsl_vector_get(s->x, i); 672 657 } 673 658 } 674 return(initialGuess); 675 } 676 677 678 679 680 659 return (initialGuess); 660 } 681 661 682 662 /****************************************************************************** … … 684 664 such that they best fit the supplied data points. 685 665 *****************************************************************************/ 686 psVector * 687 psMinimizeChi2(float (*evalModel)(const psVector *restrict, const psVector *restrict), 688 float (*DevalModel)(const psVector *restrict, const psVector *restrict, int), 689 const psImage *restrict domain, 690 const psVector *restrict data, 691 const psVector *restrict errors, 692 psVector *restrict initialGuess, 693 const psVector *restrict paramMask, 694 float *chiSq) 695 { 696 int numData = domain->numRows; // Number of data points 697 int status; // Return status for the GSL solver. 698 int i = 0; // Loop index variable. 699 int j = 0; // Loop index variable. 700 int iter = 0; // Iteration counter. 701 gsl_multifit_function_fdf f; // GSL structure that contains the 666 psVector *psMinimizeChi2(float (*evalModel) (const psVector * restrict, const psVector * restrict), 667 float (*DevalModel) (const psVector * restrict, const psVector * restrict, int), 668 const psImage * restrict domain, 669 const psVector * restrict data, 670 const psVector * restrict errors, 671 psVector * restrict initialGuess, const psVector * restrict paramMask, float *chiSq) 672 { 673 int numData = domain->numRows; // Number of data points 674 int status; // Return status for the GSL solver. 675 int i = 0; // Loop index variable. 676 int j = 0; // Loop index variable. 677 int iter = 0; // Iteration counter. 678 gsl_multifit_function_fdf f; // GSL structure that contains the 679 702 680 // functions/derivative to be solved. 703 double *xInit = NULL; // The initial guess at the parameters 681 double *xInit = NULL; // The initial guess at the parameters 682 704 683 // with masked parameters removed. 705 684 const gsl_multifit_fdfsolver_type *T; 685 706 686 // This tells GSL to use the Levenberg- 707 687 // Marquardt algorithm for chi2 708 688 // minimization. 709 gsl_multifit_fdfsolver *s; // GSL data structure.689 gsl_multifit_fdfsolver *s; // GSL data structure. 710 690 psMinChi2Data inputData; 711 691 float chiSqOld = 0.0; … … 721 701 PS_CHECK_VECTOR_SIZE_EQUAL(data, errors); 722 702 if (domain->numRows != data->n) { 723 psAbort(__func__, "Number of data points and data values not equal.");703 psAbort(__func__, "Number of data points and data values not equal."); 724 704 } 725 705 if (paramMask != NULL) { … … 743 723 // for the parameters. 744 724 if (paramMask != NULL) { 745 for (i =0;i<paramMask->n;i++) {725 for (i = 0; i < paramMask->n; i++) { 746 726 if (paramMask->data.U8[i] != 0) { 747 727 inputData.paramCount++; … … 749 729 } 750 730 } else { 751 inputData.paramCount = initialGuess->n;731 inputData.paramCount = initialGuess->n; 752 732 } 753 733 … … 755 735 // the vector inputParameterList. If the paramMask is not NULL, then those 756 736 // parameters are masked out. 757 xInit = (double *) psAlloc(inputData.paramCount * sizeof(double));737 xInit = (double *)psAlloc(inputData.paramCount * sizeof(double)); 758 738 if (paramMask != NULL) { 759 739 j = 0; 760 for (i =0;i<initialGuess->n;i++) {740 for (i = 0; i < initialGuess->n; i++) { 761 741 if (paramMask->data.U8[i] == 0) { 762 742 xInit[j++] = initialGuess->data.F32[i]; … … 764 744 } 765 745 } else { 766 for (i =0;i<initialGuess->n;i++) {746 for (i = 0; i < initialGuess->n; i++) { 767 747 xInit[i] = initialGuess->data.F32[i]; 768 748 } … … 771 751 const gsl_rng_type *type; 772 752 gsl_rng *r; 753 773 754 gsl_rng_env_setup(); 774 755 … … 789 770 790 771 gsl_vector_view x = gsl_vector_view_array(xInit, inputData.paramCount); 772 791 773 T = gsl_multifit_fdfsolver_lmsder; 792 774 s = gsl_multifit_fdfsolver_alloc(T, numData, inputData.paramCount); … … 796 778 do { 797 779 iter++; 798 for (i =0;i<initialGuess->n;i++) {780 for (i = 0; i < initialGuess->n; i++) { 799 781 printf("Iteration %d: parameter %d is %.3f\n", iter, i, gsl_vector_get(s->x, i)); 800 782 } … … 802 784 status = gsl_multifit_fdfsolver_iterate(s); 803 785 printf("gsl_multifit_fdfsolver_iterate() status is %s\n", gsl_strerror(status)); 804 for (i =0;i<initialGuess->n;i++) {786 for (i = 0; i < initialGuess->n; i++) { 805 787 printf("Iteration %d: parameter %d is %.3f\n", iter, i, gsl_vector_get(s->x, i)); 806 788 } … … 810 792 psAbort(__func__, "gsl_multifit_fdfsolver_iterate(%s)\n", gsl_strerror(status)); 811 793 } 812 813 794 // Test if the parameters changed by a small enough amount. 814 795 // NOTE: This wasn't working right when the parameters fit exactly. 815 796 // Figure out why. 816 // status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4);797 // status = gsl_multifit_test_delta(s->dx, s->x, 1e-4, 1e-4); 817 798 818 799 // We test for convergence if chiSquared changes by less than 1.0 … … 829 810 } while (status == GSL_CONTINUE && iter < MAX_LMM_ITERATIONS); 830 811 831 832 812 // In the above steps we had removed the masked elements from the 833 813 // the solver. This next code blocks puts those masked elements … … 835 815 if (paramMask != NULL) { 836 816 j = 0; 837 for (i =0;i<initialGuess->n;i++) {817 for (i = 0; i < initialGuess->n; i++) { 838 818 if (paramMask->data.U8[i] == 0) { 839 819 initialGuess->data.F32[i] = gsl_vector_get(s->x, j++); … … 843 823 } 844 824 } else { 845 for (i =0;i<initialGuess->n;i++) {825 for (i = 0; i < initialGuess->n; i++) { 846 826 initialGuess->data.F32[i] = gsl_vector_get(s->x, i); 847 827 } … … 857 837 858 838 // Bye bye. 859 return(initialGuess); 860 } 861 839 return (initialGuess); 840 } 862 841 863 842 /****************************************************************************** … … 868 847 NOTE: yErr is currently ignored. 869 848 *****************************************************************************/ 870 psPolynomial1D * 871 psVectorFitPolynomial1D(psPolynomial1D *myPoly, 872 const psVector *restrict x, 873 const psVector *restrict y, 874 const psVector *restrict yErr) 849 psPolynomial1D *psVectorFitPolynomial1D(psPolynomial1D * myPoly, 850 const psVector * restrict x, 851 const psVector * restrict y, const psVector * restrict yErr) 875 852 { 876 853 int polyOrder = myPoly->n; … … 879 856 psVector *B = NULL; 880 857 psVector *outPerm = NULL; 881 psVector *X = NULL; // NOTE: do we need this?858 psVector *X = NULL; // NOTE: do we need this? 882 859 psVector *coeffs = NULL; 883 860 int i = 0; … … 886 863 psVector *xSums = NULL; 887 864 888 // printf("psVectorFitPolynomial1D()\n");889 // for (i=0;i<x->n;i++) {890 // printf("(x, y, yErr) is (%f, %f, %f)\n", x->data.F64[i], y->data.F64[i], yErr->data.F64[i]);891 // }865 // printf("psVectorFitPolynomial1D()\n"); 866 // for (i=0;i<x->n;i++) { 867 // printf("(x, y, yErr) is (%f, %f, %f)\n", x->data.F64[i], y->data.F64[i], yErr->data.F64[i]); 868 // } 892 869 893 870 PS_CHECK_NULL_1DPOLY(myPoly); … … 901 878 PS_CHECK_VECTOR_SIZE_EQUAL(y, yErr); 902 879 903 A = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64);904 ALUD = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64);905 B = psVectorAlloc(polyOrder, PS_TYPE_F64);906 coeffs = psVectorAlloc(polyOrder, PS_TYPE_F64);907 X = psVectorAlloc(x->n, PS_TYPE_F64);880 A = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64); 881 ALUD = psImageAlloc(polyOrder, polyOrder, PS_TYPE_F64); 882 B = psVectorAlloc(polyOrder, PS_TYPE_F64); 883 coeffs = psVectorAlloc(polyOrder, PS_TYPE_F64); 884 X = psVectorAlloc(x->n, PS_TYPE_F64); 908 885 outPerm = psVectorAlloc(polyOrder, PS_TYPE_F64); 909 xSums = psVectorAlloc(1+2*polyOrder, PS_TYPE_F64);886 xSums = psVectorAlloc(1 + 2 * polyOrder, PS_TYPE_F64); 910 887 911 888 // Initialize data structures. 912 for (i=0;i<(polyOrder);i++) {889 for (i = 0; i < (polyOrder); i++) { 913 890 B->data.F64[i] = 0.0; 914 891 coeffs->data.F64[i] = 0.0; 915 892 outPerm->data.F64[i] = 0.0; 916 for (j=0;j<(polyOrder);j++) {893 for (j = 0; j < (polyOrder); j++) { 917 894 A->data.F64[i][j] = 0.0; 918 895 ALUD->data.F64[i][j] = 0.0; 919 896 } 920 897 } 921 for (i =0;i<X->n;i++) {898 for (i = 0; i < X->n; i++) { 922 899 X->data.F64[i] = x->data.F64[i]; 923 900 } 924 901 925 902 // Build the B and A data structs. 926 for (i =0;i<X->n;i++) {927 p_psBuildSums1D(X->data.F64[i], 2 *polyOrder, xSums);928 929 for (k=0;k<(polyOrder);k++) {930 B->data.F64[k] += y->data.F64[i] * xSums->data.F64[k];931 } 932 933 for (k=0;k<(polyOrder);k++) {934 for (j=0;j<(polyOrder);j++) {935 A->data.F64[k][j] += xSums->data.F64[k+j];903 for (i = 0; i < X->n; i++) { 904 p_psBuildSums1D(X->data.F64[i], 2 * polyOrder, xSums); 905 906 for (k = 0; k < (polyOrder); k++) { 907 B->data.F64[k] += y->data.F64[i] * xSums->data.F64[k]; 908 } 909 910 for (k = 0; k < (polyOrder); k++) { 911 for (j = 0; j < (polyOrder); j++) { 912 A->data.F64[k][j] += xSums->data.F64[k + j]; 936 913 } 937 914 } … … 941 918 coeffs = psMatrixLUSolve(coeffs, ALUD, B, outPerm); 942 919 943 for (k=0;k<(polyOrder);k++) {920 for (k = 0; k < (polyOrder); k++) { 944 921 myPoly->coeff[k] = coeffs->data.F64[k]; 945 // printf("myPoly->coeff[%d] is %f\n", k, myPoly->coeff[k]);946 } 947 948 949 // for (i=0;i<x->n;i++) {950 // printf("HMMM: psEvalPolynomial1D(%f) is %f\n", x->data.F64[i], psEvalPolynomial1D(x->data.F64[i],myPoly));951 // }922 // printf("myPoly->coeff[%d] is %f\n", k, myPoly->coeff[k]); 923 } 924 925 // for (i=0;i<x->n;i++) { 926 // printf("HMMM: psEvalPolynomial1D(%f) is %f\n", x->data.F64[i], psEvalPolynomial1D(x->data.F64[i], 927 // myPoly)); 928 // } 952 929 953 930 psFree(A); … … 959 936 psFree(xSums); 960 937 961 return (myPoly);962 } 938 return (myPoly); 939 }
Note:
See TracChangeset
for help on using the changeset viewer.
