Changeset 6186 for trunk/psLib/src/math/psMinimizePolyFit.c
- Timestamp:
- Jan 23, 2006, 12:25:31 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/math/psMinimizePolyFit.c (modified) (96 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/math/psMinimizePolyFit.c
r6185 r6186 10 10 * @author EAM, IfA 11 11 * 12 * @version $Revision: 1. 2$ $Name: not supported by cvs2svn $13 * @date $Date: 2006-01-23 2 0:44:29$12 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2006-01-23 22:25:31 $ 14 14 * 15 15 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii 16 16 * 17 * XXX: must follow coding name standards on local functions.18 * XXX: put local functions in front.19 17 * XXX: For clip-fit functions, what should we do if the mask is NULL? 20 * XXX: Generate macros for21 * PS_ASSERT_VECTOR_TYPES_MATCH()22 * PS_ASSERT_VECTOR_SIZES_MATCH()23 * They should have better error messages.24 18 * 25 19 */ … … 42 36 /*****************************************************************************/ 43 37 38 #define PS_VECTOR_GEN_CHEBY_INDEX(VEC, SIZE, TYPE) \ 39 VEC = psVectorAlloc(SIZE, TYPE); \ 40 if (TYPE == PS_TYPE_F64) { \ 41 for (psS32 i = 0 ; i < SIZE ; i++) { \ 42 VEC->data.F64[i] = ((2.0 / ((psF64) (SIZE - 1))) * ((psF64) i)) - 1.0; \ 43 }\ 44 } else if (TYPE == PS_TYPE_F32){ \ 45 for (psS32 i = 0 ; i < SIZE ; i++) { \ 46 VEC->data.F32[i] = ((2.0 / ((psF32) (SIZE - 1))) * ((psF32) i)) - 1.0; \ 47 }\ 48 }\ 49 44 50 /*****************************************************************************/ 45 51 /* TYPE DEFINITIONS */ … … 58 64 /*****************************************************************************/ 59 65 60 /******************************************************************************61 ******************************************************************************62 Analytical 1-D fitting routines.63 ******************************************************************************64 *****************************************************************************/65 // XXX: Make this a general type conversion macro, or function66 #define PS_VECTOR_GEN_F64_FROM_F32(VECF64, VECF32) \67 VECF64 = psVectorAlloc(VECF32->n, PS_TYPE_F64); \68 for (psS32 i = 0 ; i < VECF32->n ; i++) { \69 VECF64->data.F64[i] = (psF64) VECF32->data.F32[i]; \70 } \71 72 // XXX: Consolidate these73 #define PS_VECTOR_GEN_CHEBY_INDEX(VECF64, SIZE) \74 VECF64 = psVectorAlloc(SIZE, PS_TYPE_F64); \75 for (psS32 i = 0 ; i < SIZE ; i++) { \76 VECF64->data.F64[i] = ((2.0 / ((psF64) (SIZE - 1))) * ((psF64) i)) - 1.0; \77 }\78 79 #define PS_VECTOR_GEN_CHEBY_INDEX_F64(VECF64, SIZE) \80 VECF64 = psVectorAlloc(SIZE, PS_TYPE_F64); \81 for (psS32 i = 0 ; i < SIZE ; i++) { \82 VECF64->data.F64[i] = ((2.0 / ((psF64) (SIZE - 1))) * ((psF64) i)) - 1.0; \83 }\84 85 #define PS_VECTOR_GEN_CHEBY_INDEX_F32(VECF32, SIZE) \86 VECF32 = psVectorAlloc(SIZE, PS_TYPE_F32); \87 for (psS32 i = 0 ; i < SIZE ; i++) { \88 VECF32->data.F32[i] = ((2.0 / ((psF32) (SIZE - 1))) * ((psF32) i)) - 1.0; \89 }\90 66 /****************************************************************************** 91 67 BuildSums1D(sums, x, polyOrder, sums): this routine calculates the powers of … … 113 89 114 90 xSum = 1.0; 115 for ( inti = 0; i < nSum; i++) {91 for (psS32 i = 0; i < nSum; i++) { 116 92 sums->data.F64[i] = xSum; 117 93 xSum *= x; … … 148 124 149 125 xSum = 1.0; 150 for ( inti = 0; i < nXsum; i++) {126 for (psS32 i = 0; i < nXsum; i++) { 151 127 ySum = xSum; 152 for ( intj = 0; j < nYsum; j++) {128 for (psS32 j = 0; j < nYsum; j++) { 153 129 sums->data.F64[i][j] = ySum; 154 130 ySum *= y; 155 131 } 156 132 xSum *= x; 157 }158 159 if (0) {160 printf("--------------------- BuildSums2D(%.2f %.2f) ---------------------\n", x, y);161 for (int i = 0; i < nXsum; i++) {162 for (int j = 0; j < nYsum; j++) {163 printf("(%.2f) ", sums->data.F64[i][j]);164 }165 printf("\n");166 }167 133 } 168 134 … … 196 162 if (sums == NULL) { 197 163 sums = (psF64 ***) psAlloc (nXsum*sizeof(psF64)); 198 for ( inti = 0; i < nXsum; i++) {164 for (psS32 i = 0; i < nXsum; i++) { 199 165 sums[i] = (psF64 **) psAlloc (nYsum*sizeof(psF64)); 200 for ( intj = 0; j < nYsum; j++) {166 for (psS32 j = 0; j < nYsum; j++) { 201 167 sums[i][j] = (psF64 *) psAlloc (nZsum*sizeof(psF64)); 202 168 } … … 207 173 if (1) { 208 174 zSum = 1.0; 209 for ( intk = 0; k < nZsum; k++) {175 for (psS32 k = 0; k < nZsum; k++) { 210 176 ySum = zSum; 211 for ( intj = 0; j < nYsum; j++) {177 for (psS32 j = 0; j < nYsum; j++) { 212 178 xSum = ySum; 213 for ( inti = 0; i < nXsum; i++) {179 for (psS32 i = 0; i < nXsum; i++) { 214 180 sums[i][j][k] = xSum; 215 181 xSum *= x; … … 221 187 } else { 222 188 xSum = 1.0; 223 for ( inti = 0; i < nXsum; i++) {189 for (psS32 i = 0; i < nXsum; i++) { 224 190 ySum = xSum; 225 for ( intj = 0; j < nYsum; j++) {191 for (psS32 j = 0; j < nYsum; j++) { 226 192 zSum = ySum; 227 for ( intk = 0; k < nZsum; k++) {193 for (psS32 k = 0; k < nZsum; k++) { 228 194 sums[i][j][k] = zSum; 229 195 zSum *= z; … … 240 206 /****************************************************************************** 241 207 BuildSums4D(sums, x, y, z, t, nXterm, nYterm, nZterm, nTterm). equiv to 242 BuildSums2D(). The result is returned as a double****208 BuildSums2D(). The result is returned as a psF64 **** 243 209 *****************************************************************************/ 244 210 static psF64 ****BuildSums4D( … … 268 234 if (sums == NULL) { 269 235 sums = (psF64 ****) psAlloc (nXsum*sizeof(psF64)); 270 for ( inti = 0; i < nXsum; i++) {236 for (psS32 i = 0; i < nXsum; i++) { 271 237 sums[i] = (psF64 ***) psAlloc (nYsum*sizeof(psF64)); 272 for ( intj = 0; j < nYsum; j++) {238 for (psS32 j = 0; j < nYsum; j++) { 273 239 sums[i][j] = (psF64 **) psAlloc (nZsum*sizeof(psF64)); 274 for ( intk = 0; k < nZsum; k++) {240 for (psS32 k = 0; k < nZsum; k++) { 275 241 sums[i][j][k] = (psF64 *) psAlloc (nTsum*sizeof(psF64)); 276 242 } … … 281 247 282 248 tSum = 1.0; 283 for ( intm = 0; m < nTsum; m++) {249 for (psS32 m = 0; m < nTsum; m++) { 284 250 zSum = tSum; 285 for ( intk = 0; k < nZsum; k++) {251 for (psS32 k = 0; k < nZsum; k++) { 286 252 ySum = zSum; 287 for ( intj = 0; j < nYsum; j++) {253 for (psS32 j = 0; j < nYsum; j++) { 288 254 xSum = ySum; 289 for ( inti = 0; i < nXsum; i++) {255 for (psS32 i = 0; i < nXsum; i++) { 290 256 sums[i][j][k][m] = xSum; 291 257 xSum *= x; … … 299 265 return (sums); 300 266 } 267 268 /****************************************************************************** 269 ****************************************************************************** 270 Analytical 1-D fitting routines. 271 ****************************************************************************** 272 *****************************************************************************/ 273 301 274 302 275 /****************************************************************************** … … 356 329 NUM_DATA = x->n; 357 330 } 358 // XX:psTrace printf("vectorFitPolynomial1DChebySlow(): NUM_DATA is %d\n", NUM_DATA);359 360 psPolynomial1D **chebPolys = createChebyshevPolys(NUM_POLY);331 // psTrace printf("vectorFitPolynomial1DChebySlow(): NUM_DATA is %d\n", NUM_DATA); 332 333 psPolynomial1D **chebPolys = p_psCreateChebyshevPolys(NUM_POLY); 361 334 if (0) { 362 335 for (psS32 j = 0; j < NUM_POLY; j++) { … … 416 389 } 417 390 418 // GaussJordan version419 391 if (0) { 420 // does the solution in place 421 // XXX: Check error codes! 422 psGaussJordan (A, B); 423 424 // the first nTerm entries in B correspond directly to the desired 425 // polynomial coefficients. this is only true for the 1D case 426 for (psS32 k = 0; k < NUM_POLY; k++) { 427 myPoly->coeff[k] = B->data.F64[k]; 392 // GaussJordan version 393 if (false == psGaussJordan(A, B)) { 394 psError(PS_ERR_UNKNOWN, false, "Could not solve linear equations. Returning NULL.\n"); 395 psFree(myPoly); 396 myPoly = NULL; 397 } else { 398 // the first nTerm entries in B correspond directly to the desired 399 // polynomial coefficients. this is only true for the 1D case 400 for (psS32 k = 0; k < NUM_POLY; k++) { 401 myPoly->coeff[k] = B->data.F64[k]; 402 } 428 403 } 429 404 } else { … … 433 408 psVector* coeffs = NULL; 434 409 410 // XXX: Check return codes. 435 411 ALUD = psImageAlloc(NUM_POLY, NUM_POLY, PS_TYPE_F64); 436 412 ALUD = psMatrixLUD(ALUD, &outPerm, A); … … 468 444 the [-1:1] interval. 469 445 470 XXX: mask, maskValue, yErr are currently ignored.446 NOTE: mask, maskValue, yErr are ignored with this function. 471 447 *****************************************************************************/ 472 448 static psPolynomial1D *vectorFitPolynomial1DChebyFast( … … 501 477 tmpScalar.type.type = PS_TYPE_F64; 502 478 503 // XXX:These assignments appear too simple to warrant code and479 // These assignments appear too simple to warrant code and 504 480 // variable declarations. I retain them here to maintain coherence 505 481 // with the NR code. … … 538 514 fScalar = p_psVectorInterpolate((psVector *) x, (psVector *) y, 539 515 3, &tmpScalar); 516 if (fScalar == NULL) { 517 psError(PS_ERR_UNKNOWN, false, "Could not perform vector interpolation. Returning NULL.\n"); 518 psFree(myPoly) 519 return(NULL); 520 } 521 540 522 f->data.F64[i] = fScalar->data.F64; 541 523 psFree(fScalar); 542 524 } 543 525 544 psTrace(".psLib.dataManip.vectorFitPolynomial1DCheby", 6, 545 "(x, X, y, f(X)) is (%f, %f, %f, %f)\n", 526 psTrace(__func__, 6, "(x, X, y, f(X)) is (%f, %f, %f, %f)\n", 546 527 x->data.F64[i], X, y->data.F64[i], f->data.F64[i]); 547 528 } … … 551 532 552 533 fac = 2.0/((psF32) n); 553 // XXX: is this loop bound correct?554 534 for (j=0;j<myPoly->nX+1;j++) { 555 535 sum = 0.0; … … 573 553 PS_TYPE_F64. 574 554 *****************************************************************************/ 575 psPolynomial1D* VectorFitPolynomial1DOrd(555 static psPolynomial1D* VectorFitPolynomial1DOrd( 576 556 psPolynomial1D* myPoly, 577 557 const psVector *mask, … … 581 561 const psVector *x) 582 562 { 583 psTrace(__func__, 4, "---- VectorFitPolynomial1DOrd() begin ----\n"); 584 // XXX: these ASSERTS are redundant. 563 psTrace(__func__, 4, "---- %s() begin ----\n", __func__); 585 564 PS_ASSERT_POLY_NON_NULL(myPoly, NULL); 586 565 PS_ASSERT_INT_NONNEGATIVE(myPoly->nX, NULL); … … 600 579 } 601 580 602 psImage* A = NULL;603 psVector* B = NULL;581 psImage* A = NULL; 582 psVector* B = NULL; 604 583 psVector* xSums = NULL; 605 584 psS32 nTerm; … … 607 586 psF64 wt; 608 587 609 if (psTraceGetLevel (__func__) >= 6) {588 if (psTraceGetLevel(__func__) >= 6) { 610 589 psTrace(__func__, 6, "VectorFitPolynomial1D()\n"); 611 590 for (psS32 i = 0; i < f->n; i++) { … … 627 606 nOrder = nTerm - 1; 628 607 629 A = psImageAlloc(nTerm, nTerm, PS_TYPE_F64); 630 B = psVectorAlloc(nTerm, PS_TYPE_F64); 631 632 // 608 A = psImageAlloc(nTerm, nTerm, PS_TYPE_F64); 609 B = psVectorAlloc(nTerm, PS_TYPE_F64); 633 610 // Initialize data structures. 634 // XXX: Use psLib function. 635 // 636 PS_VECTOR_SET_F64(B, 0.0); 637 PS_IMAGE_SET_F64(A, 0.0); 611 if (!psImageInit(A, 0.0) || !psVectorInit(B, 0.0)) { 612 psError(PS_ERR_UNKNOWN, false, "Could initialize data structures A, B. Returning NULL.\n"); 613 psFree(myPoly); 614 psFree(A); 615 psFree(B); 616 psTrace(__func__, 4, "---- %s() End ----\n", __func__); 617 return(NULL); 618 } 638 619 639 620 // xSums look like: 1, x, x^2, ... x^(2n+1) … … 641 622 // XXX EAM : use temp pointers eg vB = B->data.F64 to save redirects 642 623 // XXX EAM : this function is only valid for data vectors of F64 643 for ( intk = 0; k < f->n; k++) {624 for (psS32 k = 0; k < f->n; k++) { 644 625 if ((mask != NULL) && (mask->data.U8[k] && maskValue)) { 645 626 continue; … … 657 638 wt = (fErr->data.F64[k] == 0) ? 0.0 : 1.0 / PS_SQR(fErr->data.F64[k]); 658 639 } 659 for ( inti = 0; i < nTerm; i++) {640 for (psS32 i = 0; i < nTerm; i++) { 660 641 B->data.F64[i] += f->data.F64[k] * xSums->data.F64[i] * wt; 661 642 } … … 663 644 // we could skip half of the array and assign at the end 664 645 // we must handle masked orders 665 for ( inti = 0; i < nTerm; i++) {666 for ( intj = 0; j < nTerm; j++) {646 for (psS32 i = 0; i < nTerm; i++) { 647 for (psS32 j = 0; j < nTerm; j++) { 667 648 A->data.F64[i][j] += xSums->data.F64[i + j] * wt; 668 649 } … … 670 651 } 671 652 672 // GaussJordan version673 653 if (0) { 674 // does the solution in place 675 // XXX: Check error codes! 676 psGaussJordan (A, B); 677 678 // the first nTerm entries in B correspond directly to the desired 679 // polynomial coefficients. this is only true for the 1D case 680 for (int k = 0; k < nTerm; k++) { 681 myPoly->coeff[k] = B->data.F64[k]; 682 } 654 // GaussJordan version 655 if (false == psGaussJordan(A, B)) { 656 psError(PS_ERR_UNKNOWN, false, "Could not solve linear equations. Returning NULL.\n"); 657 psFree(myPoly); 658 myPoly = NULL; 659 } else { 660 // the first nTerm entries in B correspond directly to the desired 661 // polynomial coefficients. this is only true for the 1D case 662 for (psS32 k = 0; k < nTerm; k++) { 663 myPoly->coeff[k] = B->data.F64[k]; 664 } 665 } 666 683 667 } else { 684 668 // LUD version of the fit … … 687 671 psVector* coeffs = NULL; 688 672 673 // XXX: Check return codes. 689 674 ALUD = psImageAlloc(nTerm, nTerm, PS_TYPE_F64); 690 675 ALUD = psMatrixLUD(ALUD, &outPerm, A); 691 676 coeffs = psMatrixLUSolve(coeffs, ALUD, B, outPerm); 692 for ( intk = 0; k < nTerm; k++) {677 for (psS32 k = 0; k < nTerm; k++) { 693 678 myPoly->coeff[k] = coeffs->data.F64[k]; 694 679 } … … 703 688 psFree(xSums); 704 689 705 psTrace(__func__, 4, "---- VectorFitPolynomial1DOrd() End ----\n");690 psTrace(__func__, 4, "---- %s() End ----\n", __func__); 706 691 return (myPoly); 707 692 } … … 713 698 Types F32 and F64 are supported, however, type F32 is done via vector 714 699 conversion only. 715 716 XXX: Put the asserts at the beginning. Why is this not failing tests?717 700 *****************************************************************************/ 718 701 psPolynomial1D *psVectorFitPolynomial1D( … … 748 731 749 732 if (f->type.type != PS_TYPE_F64) { 750 PS_VECTOR_GEN_F64_FROM_F32(f64, f);733 f64 = psVectorCopy(NULL, f, PS_TYPE_F64); 751 734 } else { 752 735 f64 = (psVector *) f; … … 755 738 if (x != NULL) { 756 739 if (x->type.type != PS_TYPE_F64) { 757 PS_VECTOR_GEN_F64_FROM_F32(x64, x);740 x64 = psVectorCopy(NULL, x, PS_TYPE_F64); 758 741 } else { 759 742 x64 = (psVector *) x; … … 763 746 if (fErr != NULL) { 764 747 if (fErr->type.type != PS_TYPE_F64) { 765 PS_VECTOR_GEN_F64_FROM_F32(fErr64, fErr);748 fErr64 = psVectorCopy(NULL, fErr, PS_TYPE_F64); 766 749 } else { 767 750 fErr64 = (psVector *) fErr; … … 784 767 if (x == NULL) { 785 768 // If x==NULL, create an x64 vector with x values set to (-1:1). 786 PS_VECTOR_GEN_CHEBY_INDEX(x64, f64->n );769 PS_VECTOR_GEN_CHEBY_INDEX(x64, f64->n, PS_TYPE_F64); 787 770 } 788 771 … … 850 833 } else if (poly->type == PS_POLYNOMIAL_CHEB) { 851 834 if (f->type.type == PS_TYPE_F32) { 852 PS_VECTOR_GEN_CHEBY_INDEX _F32(x, f->n);835 PS_VECTOR_GEN_CHEBY_INDEX(x, f->n, PS_TYPE_F32); 853 836 } else if (f->type.type == PS_TYPE_F64) { 854 PS_VECTOR_GEN_CHEBY_INDEX _F64(x, f->n);837 PS_VECTOR_GEN_CHEBY_INDEX(x, f->n, PS_TYPE_F64); 855 838 } 856 839 } else { 857 printf("XXX: error, bad poly type.\n"); 840 psError(PS_ERR_UNKNOWN, false, "Error, bad poly type.\n"); 841 return(NULL); 858 842 } 859 843 } … … 885 869 886 870 // 887 for ( intN = 0; N < stats->clipIter; N++) {871 for (psS32 N = 0; N < stats->clipIter; N++) { 888 872 psTrace(__func__, 6, "Loop iteration %d. Calling psVectorFitPolynomial1D()\n"); 889 intNkeep = 0;873 psS32 Nkeep = 0; 890 874 if (psTraceGetLevel(__func__) >= 6) { 891 875 if (mask != NULL) { … … 897 881 poly = psVectorFitPolynomial1D(poly, mask, maskValue, f, fErr, x); 898 882 if (poly == NULL) { 899 printf("XXX: psVectorFitPolynomial1D() returned NULL: Generate error, free data.\n"); 883 psError(PS_ERR_UNKNOWN, true, "Could not fit polynomial. Returning NULL.\n"); 884 if (xIn == NULL) { 885 psFree(x); 886 } 900 887 return(NULL); 901 888 } 902 889 903 // XXX: Check error codes904 890 fit = psPolynomial1DEvalVector(poly, x); 891 if (fit == NULL) { 892 psError(PS_ERR_UNKNOWN, false, "Could not call psPolynomial3DEvalVector(). Returning NULL.\n"); 893 psFree(resid) 894 return(NULL); 895 } 905 896 for (psS32 i = 0 ; i < f->n ; i++) { 906 897 if (f->type.type == PS_TYPE_F64) { … … 930 921 // we are masking out any point which is out of range 931 922 // recovery is not allowed with this scheme 932 for ( inti = 0; i < resid->n; i++) {923 for (psS32 i = 0; i < resid->n; i++) { 933 924 if ((mask != NULL) && (mask->data.U8[i] & maskValue)) { 934 925 continue; … … 954 945 955 946 // 956 // XXX:We should probably exit this loop if no new elements were masked947 // We should probably exit this loop if no new elements were masked 957 948 // since the polynomial fit won't change. 958 949 // … … 984 975 pairs. All non-NULL vectors must be of type PS_TYPE_F64. 985 976 986 // XXX: What about the maskValue?987 977 *****************************************************************************/ 988 psPolynomial2D* VectorFitPolynomial2DOrd(978 static psPolynomial2D* VectorFitPolynomial2DOrd( 989 979 psPolynomial2D* myPoly, 990 980 const psVector* mask, … … 995 985 const psVector *y) 996 986 { 997 psTrace(__func__, 4, "---- VectorFitPolynomial2DOrd() begin ----\n"); 998 // These ASSERTS are redundant. 987 psTrace(__func__, 4, "---- %s() begin ----\n", __func__); 999 988 PS_ASSERT_POLY_NON_NULL(myPoly, NULL); 1000 989 PS_ASSERT_INT_NONNEGATIVE(myPoly->nX, NULL); … … 1025 1014 psS32 nTerm; 1026 1015 1027 // XXX:Watch for changes to the psPolys: nTerm != nOrder.1028 1016 psS32 nXterm = 1 + myPoly->nX; 1029 1017 psS32 nYterm = 1 + myPoly->nY; … … 1032 1020 A = psImageAlloc(nTerm, nTerm, PS_TYPE_F64); 1033 1021 B = psVectorAlloc(nTerm, PS_TYPE_F64); 1034 1035 //1036 1022 // Initialize data structures. 1037 // XXX: Use psLib function. 1038 // 1039 PS_VECTOR_SET_F64(B, 0.0); 1040 PS_IMAGE_SET_F64(A, 0.0); 1023 if (!psImageInit(A, 0.0) || !psVectorInit(B, 0.0)) { 1024 psError(PS_ERR_UNKNOWN, false, "Could initialize data structures A, B. Returning NULL.\n"); 1025 psFree(myPoly); 1026 psFree(A); 1027 psFree(B); 1028 psTrace(__func__, 4, "---- %s() End ----\n", __func__); 1029 return(NULL); 1030 } 1041 1031 1042 1032 // Sums look like: 1, x, x^2, ... x^(2n+1), y, xy, x^2y, ... x^(2n+1) 1043 1033 1044 1034 // Build the B and A data structs. 1045 for ( intk = 0; k < x->n; k++) {1035 for (psS32 k = 0; k < x->n; k++) { 1046 1036 if ((mask != NULL) && (mask->data.U8[k] & maskValue)) { 1047 1037 continue; … … 1059 1049 // we could skip half of the array and assign at the end 1060 1050 // we must handle masked orders 1061 for ( intn = 0; n < nXterm; n++) {1062 for ( intm = 0; m < nYterm; m++) {1051 for (psS32 n = 0; n < nXterm; n++) { 1052 for (psS32 m = 0; m < nYterm; m++) { 1063 1053 B->data.F64[n+m*nXterm] += f->data.F64[k] * Sums->data.F64[n][m] * wt; 1064 1054 } 1065 1055 } 1066 1056 1067 for ( inti = 0; i < nXterm; i++) {1068 for ( intj = 0; j < nYterm; j++) {1069 for ( intn = 0; n < nXterm; n++) {1070 for ( intm = 0; m < nYterm; m++) {1057 for (psS32 i = 0; i < nXterm; i++) { 1058 for (psS32 j = 0; j < nYterm; j++) { 1059 for (psS32 n = 0; n < nXterm; n++) { 1060 for (psS32 m = 0; m < nYterm; m++) { 1071 1061 A->data.F64[i+j*nXterm][n+m*nXterm] += Sums->data.F64[i+n][j+m] * wt; 1072 1062 } … … 1076 1066 } 1077 1067 1078 // does the solution in place 1079 // XXX: Check error codes! 1080 psGaussJordan (A, B); 1081 1082 // select the appropriate solution entries 1083 for (int n = 0; n < nXterm; n++) { 1084 for (int m = 0; m < nYterm; m++) { 1085 myPoly->coeff[n][m] = B->data.F64[n+m*nXterm]; 1068 if (false == psGaussJordan(A, B)) { 1069 psError(PS_ERR_UNKNOWN, false, "Could not solve linear equations. Returning NULL.\n"); 1070 psFree(myPoly); 1071 myPoly = NULL; 1072 } else { 1073 // select the appropriate solution entries 1074 for (psS32 n = 0; n < nXterm; n++) { 1075 for (psS32 m = 0; m < nYterm; m++) { 1076 myPoly->coeff[n][m] = B->data.F64[n+m*nXterm]; 1077 } 1086 1078 } 1087 1079 } … … 1091 1083 psFree(Sums); 1092 1084 1093 psTrace(__func__, 4, "---- VectorFitPolynomial2DOrd() end ----\n");1085 psTrace(__func__, 4, "---- %s() end ----\n", __func__); 1094 1086 return (myPoly); 1095 1087 } … … 1139 1131 // 1140 1132 if (f->type.type != PS_TYPE_F64) { 1141 PS_VECTOR_GEN_F64_FROM_F32(f64, f);1133 f64 = psVectorCopy(NULL, f, PS_TYPE_F64); 1142 1134 } else { 1143 1135 f64 = (psVector *) f; … … 1145 1137 1146 1138 if (x->type.type != PS_TYPE_F64) { 1147 PS_VECTOR_GEN_F64_FROM_F32(x64, x);1139 x64 = psVectorCopy(NULL, x, PS_TYPE_F64); 1148 1140 } else { 1149 1141 x64 = (psVector *) x; … … 1151 1143 1152 1144 if (y->type.type != PS_TYPE_F64) { 1153 PS_VECTOR_GEN_F64_FROM_F32(y64, y);1145 y64 = psVectorCopy(NULL, y, PS_TYPE_F64); 1154 1146 } else { 1155 1147 y64 = (psVector *) y; … … 1161 1153 if (fErr != NULL) { 1162 1154 if (fErr->type.type != PS_TYPE_F64) { 1163 PS_VECTOR_GEN_F64_FROM_F32(fErr64, fErr);1155 fErr64 = psVectorCopy(NULL, fErr, PS_TYPE_F64); 1164 1156 } else { 1165 1157 fErr64 = (psVector *) fErr; … … 1272 1264 1273 1265 // clipping range defined by min and max and/or clipSigma 1274 floatminClipSigma;1275 floatmaxClipSigma;1266 psF32 minClipSigma; 1267 psF32 maxClipSigma; 1276 1268 if (isfinite(stats->max)) { 1277 1269 maxClipSigma = fabs(stats->max); … … 1294 1286 psTrace(__func__, 4, "(minClipSigma, maxClipSigma) is (%.2f, %.2f)\n", minClipSigma, maxClipSigma); 1295 1287 1296 for ( intN = 0; N < stats->clipIter; N++) {1288 for (psS32 N = 0; N < stats->clipIter; N++) { 1297 1289 psTrace(__func__, 6, "Loop iteration %d. Calling psVectorFitPolynomial1D()\n"); 1298 intNkeep = 0;1290 psS32 Nkeep = 0; 1299 1291 if (psTraceGetLevel(__func__) >= 6) { 1300 1292 if (mask != NULL) { … … 1306 1298 1307 1299 poly = psVectorFitPolynomial2D(poly, mask, maskValue, f, fErr, x, y); 1308 // XXX: Check error codes 1300 if (poly == NULL) { 1301 psError(PS_ERR_UNKNOWN, false, "Could not fit a polynomial to the data. Returning NULL.\n"); 1302 psFree(resid) 1303 return(NULL); 1304 } 1305 1309 1306 psVector *fit = psPolynomial2DEvalVector(poly, x, y); 1307 if (fit == NULL) { 1308 psError(PS_ERR_UNKNOWN, false, "Could not call psPolynomial3DEvalVector(). Returning NULL.\n"); 1309 psFree(resid) 1310 return(NULL); 1311 } 1310 1312 1311 1313 for (psS32 i = 0 ; i < f->n ; i++) { … … 1328 1330 } 1329 1331 1330 // XXX: Check error codes1331 1332 stats = psVectorStats(stats, resid, NULL, mask, maskValue); 1333 if (stats == NULL) { 1334 psError(PS_ERR_UNKNOWN, false, "Could not compute statistics on the resid vector. Returning NULL.\n"); 1335 psFree(resid) 1336 psFree(fit) 1337 return(NULL); 1338 } 1332 1339 psTrace(__func__, 6, "Median is %f\n", stats->sampleMedian); 1333 1340 psTrace(__func__, 6, "Stdev is %f\n", stats->sampleStdev); 1334 floatminClipValue = -minClipSigma*stats->sampleStdev;1335 floatmaxClipValue = +maxClipSigma*stats->sampleStdev;1341 psF32 minClipValue = -minClipSigma*stats->sampleStdev; 1342 psF32 maxClipValue = +maxClipSigma*stats->sampleStdev; 1336 1343 1337 1344 // set mask if pts are not valid 1338 1345 // we are masking out any point which is out of range 1339 1346 // recovery is not allowed with this scheme 1340 for ( inti = 0; i < resid->n; i++) {1347 for (psS32 i = 0; i < resid->n; i++) { 1341 1348 if ((mask != NULL) && (mask->data.U8[i] & maskValue)) { 1342 1349 continue; … … 1388 1395 y, z)-(f) pairs. All non-NULL vectors must be of type PS_TYPE_F64. 1389 1396 1390 XXX: This routine needs to be written. Currently, this is simply a shell. We1391 can assume that all vectors have been converted to F64, that (f, x, y, z) are1392 non-null and F64. fErr may be NULL, but will be F64 is not.1393 1397 *****************************************************************************/ 1394 psPolynomial3D* VectorFitPolynomial3DOrd(1398 static psPolynomial3D* VectorFitPolynomial3DOrd( 1395 1399 psPolynomial3D* myPoly, 1396 1400 const psVector* mask, … … 1402 1406 const psVector *z) 1403 1407 { 1404 psTrace(__func__, 4, "---- VectorFitPolynomial3DOrd() begin ----\n"); 1405 // These ASSERTS are redundant. 1408 psTrace(__func__, 4, "---- %s() begin ----\n", __func__); 1406 1409 PS_ASSERT_POLY_NON_NULL(myPoly, NULL); 1407 1410 PS_ASSERT_INT_NONNEGATIVE(myPoly->nX, NULL); … … 1435 1438 psS32 nTerm; 1436 1439 1437 // XXX:Watch for changes to the psPolys: nTerm != nOrder.1438 1440 psS32 nXterm = 1 + myPoly->nX; 1439 1441 psS32 nYterm = 1 + myPoly->nY; … … 1443 1445 A = psImageAlloc(nTerm, nTerm, PS_TYPE_F64); 1444 1446 B = psVectorAlloc(nTerm, PS_TYPE_F64); 1445 1446 1447 // Initialize data structures. 1447 psVectorInit (B, 0.0); 1448 psImageInit (A, 0.0); 1448 if (!psImageInit(A, 0.0) || !psVectorInit(B, 0.0)) { 1449 psError(PS_ERR_UNKNOWN, false, "Could initialize data structures A, B. Returning NULL.\n"); 1450 psFree(myPoly); 1451 psFree(A); 1452 psFree(B); 1453 psTrace(__func__, 4, "---- %s() End ----\n", __func__); 1454 return(NULL); 1455 } 1449 1456 1450 1457 // Sums look like: 1, x, x^2, ... x^(2n+1), y, xy, x^2y, ... x^(2n+1)*y, ... 1451 1458 1452 1459 // Build the B and A data structs. 1453 for ( intk = 0; k < x->n; k++) {1460 for (psS32 k = 0; k < x->n; k++) { 1454 1461 if ((mask != NULL) && (mask->data.U8[k] & maskValue)) { 1455 1462 continue; … … 1467 1474 // we could skip half of the array and assign at the end 1468 1475 // we must handle masked orders 1469 for ( intix = 0; ix < nXterm; ix++) {1470 for ( intiy = 0; iy < nYterm; iy++) {1471 for ( intiz = 0; iz < nZterm; iz++) {1476 for (psS32 ix = 0; ix < nXterm; ix++) { 1477 for (psS32 iy = 0; iy < nYterm; iy++) { 1478 for (psS32 iz = 0; iz < nZterm; iz++) { 1472 1479 if (myPoly->mask[ix][iy][iz]) { 1473 1480 continue; 1474 1481 } else { 1475 intnx = ix + iy*nXterm + iz*nXterm*nYterm;1482 psS32 nx = ix + iy*nXterm + iz*nXterm*nYterm; 1476 1483 B->data.F64[nx] += f->data.F64[k] * Sums[ix][iy][iz] * wt; 1477 1484 } … … 1480 1487 } 1481 1488 1482 for ( intix = 0; ix < nXterm; ix++) {1483 for ( intiy = 0; iy < nYterm; iy++) {1484 for ( intiz = 0; iz < nZterm; iz++) {1489 for (psS32 ix = 0; ix < nXterm; ix++) { 1490 for (psS32 iy = 0; iy < nYterm; iy++) { 1491 for (psS32 iz = 0; iz < nZterm; iz++) { 1485 1492 if (myPoly->mask[ix][iy][iz]) 1486 1493 continue; 1487 intnx = ix+iy*nXterm+iz*nXterm*nYterm;1488 for ( intjx = 0; jx < nXterm; jx++) {1489 for ( intjy = 0; jy < nYterm; jy++) {1490 for ( intjz = 0; jz < nZterm; jz++) {1494 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm; 1495 for (psS32 jx = 0; jx < nXterm; jx++) { 1496 for (psS32 jy = 0; jy < nYterm; jy++) { 1497 for (psS32 jz = 0; jz < nZterm; jz++) { 1491 1498 if (myPoly->mask[jx][jy][jz]) 1492 1499 continue; 1493 intny = jx+jy*nXterm+jz*nXterm*nYterm;1500 psS32 ny = jx+jy*nXterm+jz*nXterm*nYterm; 1494 1501 A->data.F64[nx][ny] += Sums[ix+jx][iy+jy][iz+jz] * wt; 1495 1502 } … … 1501 1508 } 1502 1509 1503 for ( intix = 0; ix < nXterm; ix++) {1504 for ( intiy = 0; iy < nYterm; iy++) {1505 for ( intiz = 0; iz < nZterm; iz++) {1510 for (psS32 ix = 0; ix < nXterm; ix++) { 1511 for (psS32 iy = 0; iy < nYterm; iy++) { 1512 for (psS32 iz = 0; iz < nZterm; iz++) { 1506 1513 if (!myPoly->mask[ix][iy][iz]) 1507 1514 continue; 1508 intnx = ix+iy*nXterm+iz*nXterm*nYterm;1515 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm; 1509 1516 B->data.F64[nx] = 0; 1510 for ( intjx = 0; jx < nXterm; jx++) {1511 for ( intjy = 0; jy < nYterm; jy++) {1512 for ( intjz = 0; jz < nZterm; jz++) {1513 intny = jx+jy*nXterm+jz*nXterm*nYterm;1517 for (psS32 jx = 0; jx < nXterm; jx++) { 1518 for (psS32 jy = 0; jy < nYterm; jy++) { 1519 for (psS32 jz = 0; jz < nZterm; jz++) { 1520 psS32 ny = jx+jy*nXterm+jz*nXterm*nYterm; 1514 1521 A->data.F64[nx][ny] = (nx == ny) ? 1 : 0; 1515 1522 } … … 1523 1530 // does the solution in place 1524 1531 // The matrices were overflowing, so I switched to LUD. 1525 if (false == psGaussJordan (A, B)) {1532 if (false == psGaussJordan(A, B)) { 1526 1533 psFree(A); 1527 1534 psFree(B); 1528 1535 1529 for ( intix = 0; ix < 2*nXterm; ix++) {1530 for ( intiy = 0; iy < 2*nYterm; iy++) {1536 for (psS32 ix = 0; ix < 2*nXterm; ix++) { 1537 for (psS32 iy = 0; iy < 2*nYterm; iy++) { 1531 1538 psFree(Sums[ix][iy]); 1532 1539 } … … 1538 1545 } 1539 1546 // select the appropriate solution entries 1540 for ( intix = 0; ix < nXterm; ix++) {1541 for ( intiy = 0; iy < nYterm; iy++) {1542 for ( intiz = 0; iz < nZterm; iz++) {1543 intnx = ix+iy*nXterm+iz*nXterm*nYterm;1547 for (psS32 ix = 0; ix < nXterm; ix++) { 1548 for (psS32 iy = 0; iy < nYterm; iy++) { 1549 for (psS32 iz = 0; iz < nZterm; iz++) { 1550 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm; 1544 1551 myPoly->coeff[ix][iy][iz] = B->data.F64[nx]; 1545 1552 myPoly->coeffErr[ix][iy][iz] = sqrt(A->data.F64[nx][nx]); … … 1553 1560 psVector* coeffs = NULL; 1554 1561 1562 // XXX: Check return codes. 1555 1563 ALUD = psImageAlloc(nTerm, nTerm, PS_TYPE_F64); 1556 1564 ALUD = psMatrixLUD(ALUD, &outPerm, A); … … 1558 1566 1559 1567 // select the appropriate solution entries 1560 for ( intix = 0; ix < nXterm; ix++) {1561 for ( intiy = 0; iy < nYterm; iy++) {1562 for ( intiz = 0; iz < nZterm; iz++) {1563 intnx = ix+iy*nXterm+iz*nXterm*nYterm;1568 for (psS32 ix = 0; ix < nXterm; ix++) { 1569 for (psS32 iy = 0; iy < nYterm; iy++) { 1570 for (psS32 iz = 0; iz < nZterm; iz++) { 1571 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm; 1564 1572 myPoly->coeff[ix][iy][iz] = coeffs->data.F64[nx]; 1565 1573 myPoly->coeffErr[ix][iy][iz] = sqrt(A->data.F64[nx][nx]); … … 1575 1583 psFree(B); 1576 1584 1577 for ( intix = 0; ix < 2*nXterm; ix++) {1578 for ( intiy = 0; iy < 2*nYterm; iy++) {1585 for (psS32 ix = 0; ix < 2*nXterm; ix++) { 1586 for (psS32 iy = 0; iy < 2*nYterm; iy++) { 1579 1587 psFree(Sums[ix][iy]); 1580 1588 } … … 1583 1591 psFree(Sums); 1584 1592 1585 psTrace(__func__, 4, "---- VectorFitPolynomial3DOrd() end ----\n");1593 psTrace(__func__, 4, "---- %s() end ----\n", __func__); 1586 1594 return (myPoly); 1587 1595 } … … 1621 1629 PS_ASSERT_VECTOR_NON_NULL(x, NULL); 1622 1630 PS_ASSERT_VECTORS_SIZE_EQUAL(f, x, NULL); 1623 // PS_ASSERT_VECTOR_TYPE(x, f->type.type, NULL);1624 1631 PS_ASSERT_VECTOR_NON_NULL(y, NULL); 1625 1632 PS_ASSERT_VECTORS_SIZE_EQUAL(f, y, NULL); 1626 // PS_ASSERT_VECTOR_TYPE(y, f->type.type, NULL);1627 1633 PS_ASSERT_VECTOR_NON_NULL(z, NULL); 1628 1634 PS_ASSERT_VECTORS_SIZE_EQUAL(f, z, NULL); 1629 // PS_ASSERT_VECTOR_TYPE(z, f->type.type, NULL);1630 1635 if (fErr != NULL) { 1631 1636 PS_ASSERT_VECTORS_SIZE_EQUAL(f, fErr, NULL); … … 1637 1642 // 1638 1643 if (f->type.type != PS_TYPE_F64) { 1639 PS_VECTOR_GEN_F64_FROM_F32(f64, f);1644 f64 = psVectorCopy(NULL, f, PS_TYPE_F64); 1640 1645 } else { 1641 1646 f64 = (psVector *) f; 1642 1647 } 1643 1648 if (x->type.type != PS_TYPE_F64) { 1644 PS_VECTOR_GEN_F64_FROM_F32(x64, x);1649 x64 = psVectorCopy(NULL, x, PS_TYPE_F64); 1645 1650 } else { 1646 1651 x64 = (psVector *) x; 1647 1652 } 1648 1653 if (y->type.type != PS_TYPE_F64) { 1649 PS_VECTOR_GEN_F64_FROM_F32(y64, y);1654 y64 = psVectorCopy(NULL, y, PS_TYPE_F64); 1650 1655 } else { 1651 1656 y64 = (psVector *) y; … … 1653 1658 1654 1659 if (z->type.type != PS_TYPE_F64) { 1655 PS_VECTOR_GEN_F64_FROM_F32(z64, z);1660 z64 = psVectorCopy(NULL, z, PS_TYPE_F64); 1656 1661 } else { 1657 1662 z64 = (psVector *) z; … … 1660 1665 if (fErr != NULL) { 1661 1666 if (fErr->type.type != PS_TYPE_F64) { 1662 PS_VECTOR_GEN_F64_FROM_F32(fErr64, fErr);1667 fErr64 = psVectorCopy(NULL, fErr, PS_TYPE_F64); 1663 1668 } else { 1664 1669 fErr64 = (psVector *) fErr; … … 1788 1793 1789 1794 // clipping range defined by min and max and/or clipSigma 1790 floatminClipSigma;1791 floatmaxClipSigma;1795 psF32 minClipSigma; 1796 psF32 maxClipSigma; 1792 1797 if (isfinite(stats->max)) { 1793 1798 maxClipSigma = fabs(stats->max); … … 1811 1816 psTrace(__func__, 4, "(minClipSigma, maxClipSigma) is (%.2f, %.2f)\n", minClipSigma, maxClipSigma); 1812 1817 1813 for ( intN = 0; N < stats->clipIter; N++) {1818 for (psS32 N = 0; N < stats->clipIter; N++) { 1814 1819 psTrace(__func__, 6, "Loop iteration %d. Calling psVectorFitPolynomial1D()\n"); 1815 intNkeep = 0;1820 psS32 Nkeep = 0; 1816 1821 if (psTraceGetLevel(__func__) >= 6) { 1817 1822 if (mask != NULL) { … … 1823 1828 1824 1829 poly = psVectorFitPolynomial3D(poly, mask, maskValue, f, fErr, x, y, z); 1825 // XXX: Check error codes 1830 if (poly == NULL) { 1831 psError(PS_ERR_UNKNOWN, false, "Could not fit a polynomial to the data. Returning NULL.\n"); 1832 psFree(resid) 1833 psFree(fit) 1834 return(NULL); 1835 } 1826 1836 fit = psPolynomial3DEvalVector(poly, x, y, z); 1837 if (fit == NULL) { 1838 psError(PS_ERR_UNKNOWN, false, "Could not call psPolynomial3DEvalVector(). Returning NULL.\n"); 1839 psFree(resid) 1840 return(NULL); 1841 } 1827 1842 for (psS32 i = 0 ; i < f->n ; i++) { 1828 1843 if (f->type.type == PS_TYPE_F64) { … … 1844 1859 } 1845 1860 1846 // XXX: Check error codes1847 1861 stats = psVectorStats(stats, resid, NULL, mask, maskValue); 1862 if (stats == NULL) { 1863 psError(PS_ERR_UNKNOWN, false, "Could not compute statistics on the resid vector. Returning NULL.\n"); 1864 psFree(resid) 1865 psFree(fit) 1866 return(NULL); 1867 } 1868 1848 1869 psTrace(__func__, 6, "Median is %f\n", stats->sampleMedian); 1849 1870 psTrace(__func__, 6, "Stdev is %f\n", stats->sampleStdev); 1850 floatminClipValue = -minClipSigma*stats->sampleStdev;1851 floatmaxClipValue = +maxClipSigma*stats->sampleStdev;1871 psF32 minClipValue = -minClipSigma*stats->sampleStdev; 1872 psF32 maxClipValue = +maxClipSigma*stats->sampleStdev; 1852 1873 1853 1874 // set mask if pts are not valid 1854 1875 // we are masking out any point which is out of range 1855 1876 // recovery is not allowed with this scheme 1856 for ( inti = 0; i < resid->n; i++) {1877 for (psS32 i = 0; i < resid->n; i++) { 1857 1878 if ((mask != NULL) && (mask->data.U8[i] & maskValue)) { 1858 1879 continue; … … 1902 1923 y, z, t)-(f) pairs. All non-NULL vectors must be of type PS_TYPE_F64. 1903 1924 1904 XXX: This routine needs to be written. Currently, this is simply a shell. We1905 can assume that all vectors have been converted to F64, that (f, x, y, z) are1906 non-null and F64. fErr may be NULL, but will be F64 is not.1907 1925 *****************************************************************************/ 1908 psPolynomial4D* VectorFitPolynomial4DOrd(1926 static psPolynomial4D* VectorFitPolynomial4DOrd( 1909 1927 psPolynomial4D* myPoly, 1910 1928 const psVector* mask, … … 1918 1936 { 1919 1937 psTrace(__func__, 4, "---- %s() begin ----\n", __func__); 1920 // These ASSERTS are redundant.1921 1938 PS_ASSERT_POLY_NON_NULL(myPoly, NULL); 1922 1939 PS_ASSERT_INT_NONNEGATIVE(myPoly->nX, NULL); … … 1954 1971 psS32 nTerm; 1955 1972 1956 // XXX:Watch for changes to the psPolys: nTerm != nOrder.1957 1973 psS32 nXterm = 1 + myPoly->nX; 1958 1974 psS32 nYterm = 1 + myPoly->nY; … … 1963 1979 A = psImageAlloc(nTerm, nTerm, PS_TYPE_F64); 1964 1980 B = psVectorAlloc(nTerm, PS_TYPE_F64); 1965 1966 1981 // Initialize data structures. 1967 psVectorInit (B, 0.0); 1968 psImageInit (A, 0.0); 1982 if (!psImageInit(A, 0.0) || !psVectorInit(B, 0.0)) { 1983 psError(PS_ERR_UNKNOWN, false, "Could initialize data structures A, B. Returning NULL.\n"); 1984 psFree(myPoly); 1985 psFree(A); 1986 psFree(B); 1987 psTrace(__func__, 4, "---- %s() End ----\n", __func__); 1988 return(NULL); 1989 } 1969 1990 1970 1991 // Sums look like: 1, x, x^2, ... x^(2n+1), y, xy, x^2y, ... x^(2n+1)*y, ... 1971 1992 1972 1993 // Build the B and A data structs. 1973 for ( intk = 0; k < x->n; k++) {1994 for (psS32 k = 0; k < x->n; k++) { 1974 1995 if ((mask != NULL) && (mask->data.U8[k] & maskValue)) { 1975 1996 continue; … … 1987 2008 // we could skip half of the array and assign at the end 1988 2009 // we must handle masked orders 1989 for ( intix = 0; ix < nXterm; ix++) {1990 for ( intiy = 0; iy < nYterm; iy++) {1991 for ( intiz = 0; iz < nZterm; iz++) {1992 for ( intit = 0; it < nTterm; it++) {2010 for (psS32 ix = 0; ix < nXterm; ix++) { 2011 for (psS32 iy = 0; iy < nYterm; iy++) { 2012 for (psS32 iz = 0; iz < nZterm; iz++) { 2013 for (psS32 it = 0; it < nTterm; it++) { 1993 2014 if (myPoly->mask[ix][iy][iz][it]) 1994 2015 continue; 1995 intnx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm;2016 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm; 1996 2017 B->data.F64[nx] += f->data.F64[k] * Sums[ix][iy][iz][it] * wt; 1997 2018 } … … 2000 2021 } 2001 2022 2002 for ( intix = 0; ix < nXterm; ix++) {2003 for ( intiy = 0; iy < nYterm; iy++) {2004 for ( intiz = 0; iz < nZterm; iz++) {2005 for ( intit = 0; it < nTterm; it++) {2023 for (psS32 ix = 0; ix < nXterm; ix++) { 2024 for (psS32 iy = 0; iy < nYterm; iy++) { 2025 for (psS32 iz = 0; iz < nZterm; iz++) { 2026 for (psS32 it = 0; it < nTterm; it++) { 2006 2027 if (myPoly->mask[ix][iy][iz][it]) 2007 2028 continue; 2008 intnx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm;2009 for ( intjx = 0; jx < nXterm; jx++) {2010 for ( intjy = 0; jy < nYterm; jy++) {2011 for ( intjz = 0; jz < nZterm; jz++) {2012 for ( intjt = 0; jt < nTterm; jt++) {2029 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm; 2030 for (psS32 jx = 0; jx < nXterm; jx++) { 2031 for (psS32 jy = 0; jy < nYterm; jy++) { 2032 for (psS32 jz = 0; jz < nZterm; jz++) { 2033 for (psS32 jt = 0; jt < nTterm; jt++) { 2013 2034 if (myPoly->mask[jx][jy][jz][jt]) 2014 2035 continue; 2015 intny = jx+jy*nXterm+jz*nXterm*nYterm+jt*nXterm*nYterm*nZterm;2036 psS32 ny = jx+jy*nXterm+jz*nXterm*nYterm+jt*nXterm*nYterm*nZterm; 2016 2037 A->data.F64[nx][ny]+= Sums[ix+jx][iy+jy][iz+jz][it+jt] * wt; 2017 2038 } … … 2025 2046 } 2026 2047 2027 for ( intix = 0; ix < nXterm; ix++) {2028 for ( intiy = 0; iy < nYterm; iy++) {2029 for ( intiz = 0; iz < nZterm; iz++) {2030 for ( intit = 0; it < nTterm; it++) {2048 for (psS32 ix = 0; ix < nXterm; ix++) { 2049 for (psS32 iy = 0; iy < nYterm; iy++) { 2050 for (psS32 iz = 0; iz < nZterm; iz++) { 2051 for (psS32 it = 0; it < nTterm; it++) { 2031 2052 if (!myPoly->mask[ix][iy][iz][it]) 2032 2053 continue; 2033 intnx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm;2054 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm; 2034 2055 B->data.F64[nx] = 0; 2035 for ( intjx = 0; jx < nXterm; jx++) {2036 for ( intjy = 0; jy < nYterm; jy++) {2037 for ( intjz = 0; jz < nZterm; jz++) {2038 for ( intjt = 0; jt < nTterm; jt++) {2039 intny = jx+jy*nXterm+jz*nXterm*nYterm+jt*nXterm*nYterm*nZterm;2056 for (psS32 jx = 0; jx < nXterm; jx++) { 2057 for (psS32 jy = 0; jy < nYterm; jy++) { 2058 for (psS32 jz = 0; jz < nZterm; jz++) { 2059 for (psS32 jt = 0; jt < nTterm; jt++) { 2060 psS32 ny = jx+jy*nXterm+jz*nXterm*nYterm+jt*nXterm*nYterm*nZterm; 2040 2061 A->data.F64[nx][ny] = (nx == ny) ? 1 : 0; 2041 2062 } … … 2051 2072 if (0) { 2052 2073 // does the solution in place 2053 // XXX:The GaussJordan version was overflowing, so I'm using LUD.2074 // The GaussJordan version was overflowing, so I'm using LUD. 2054 2075 if (false == psGaussJordan(A, B)) { 2055 2076 psFree(A); 2056 2077 psFree(B); 2057 for ( intix = 0; ix < 2*nXterm; ix++) {2058 for ( intiy = 0; iy < 2*nYterm; iy++) {2059 for ( intiz = 0; iz < 2*nZterm; iz++) {2078 for (psS32 ix = 0; ix < 2*nXterm; ix++) { 2079 for (psS32 iy = 0; iy < 2*nYterm; iy++) { 2080 for (psS32 iz = 0; iz < 2*nZterm; iz++) { 2060 2081 psFree(Sums[ix][iy][iz]); 2061 2082 } … … 2070 2091 2071 2092 // select the appropriate solution entries 2072 for ( intix = 0; ix < nXterm; ix++) {2073 for ( intiy = 0; iy < nYterm; iy++) {2074 for ( intiz = 0; iz < nZterm; iz++) {2075 for ( intit = 0; it < nTterm; it++) {2076 intnx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm;2093 for (psS32 ix = 0; ix < nXterm; ix++) { 2094 for (psS32 iy = 0; iy < nYterm; iy++) { 2095 for (psS32 iz = 0; iz < nZterm; iz++) { 2096 for (psS32 it = 0; it < nTterm; it++) { 2097 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm; 2077 2098 myPoly->coeff[ix][iy][iz][it] = B->data.F64[nx]; 2078 2099 myPoly->coeffErr[ix][iy][iz][it] = sqrt(A->data.F64[nx][nx]); … … 2087 2108 psVector* coeffs = NULL; 2088 2109 2110 // XXX: Check return codes. 2089 2111 ALUD = psImageAlloc(nTerm, nTerm, PS_TYPE_F64); 2090 2112 ALUD = psMatrixLUD(ALUD, &outPerm, A); … … 2092 2114 2093 2115 // select the appropriate solution entries 2094 for ( intix = 0; ix < nXterm; ix++) {2095 for ( intiy = 0; iy < nYterm; iy++) {2096 for ( intiz = 0; iz < nZterm; iz++) {2097 for ( intit = 0; it < nTterm; it++) {2098 intnx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm;2116 for (psS32 ix = 0; ix < nXterm; ix++) { 2117 for (psS32 iy = 0; iy < nYterm; iy++) { 2118 for (psS32 iz = 0; iz < nZterm; iz++) { 2119 for (psS32 it = 0; it < nTterm; it++) { 2120 psS32 nx = ix+iy*nXterm+iz*nXterm*nYterm+it*nXterm*nYterm*nZterm; 2099 2121 myPoly->coeff[ix][iy][iz][it] = coeffs->data.F64[nx]; 2100 2122 myPoly->coeffErr[ix][iy][iz][it] = sqrt(A->data.F64[nx][nx]); … … 2113 2135 psFree(B); 2114 2136 2115 for ( intix = 0; ix < 2*nXterm; ix++) {2116 for ( intiy = 0; iy < 2*nYterm; iy++) {2117 for ( intiz = 0; iz < 2*nZterm; iz++) {2137 for (psS32 ix = 0; ix < 2*nXterm; ix++) { 2138 for (psS32 iy = 0; iy < 2*nYterm; iy++) { 2139 for (psS32 iz = 0; iz < 2*nZterm; iz++) { 2118 2140 psFree(Sums[ix][iy][iz]); 2119 2141 } … … 2180 2202 // 2181 2203 if (f->type.type != PS_TYPE_F64) { 2182 PS_VECTOR_GEN_F64_FROM_F32(f64, f);2204 f64 = psVectorCopy(NULL, f, PS_TYPE_F64); 2183 2205 } else { 2184 2206 f64 = (psVector *) f; 2185 2207 } 2186 2208 if (x->type.type != PS_TYPE_F64) { 2187 PS_VECTOR_GEN_F64_FROM_F32(x64, x);2209 x64 = psVectorCopy(NULL, x, PS_TYPE_F64); 2188 2210 } else { 2189 2211 x64 = (psVector *) x; 2190 2212 } 2191 2213 if (y->type.type != PS_TYPE_F64) { 2192 PS_VECTOR_GEN_F64_FROM_F32(y64, y);2214 y64 = psVectorCopy(NULL, y, PS_TYPE_F64); 2193 2215 } else { 2194 2216 y64 = (psVector *) y; 2195 2217 } 2196 2218 if (z->type.type != PS_TYPE_F64) { 2197 PS_VECTOR_GEN_F64_FROM_F32(z64, z);2219 z64 = psVectorCopy(NULL, z, PS_TYPE_F64); 2198 2220 } else { 2199 2221 z64 = (psVector *) z; 2200 2222 } 2201 2223 if (t->type.type != PS_TYPE_F64) { 2202 PS_VECTOR_GEN_F64_FROM_F32(t64, t);2224 t64 = psVectorCopy(NULL, t, PS_TYPE_F64); 2203 2225 } else { 2204 2226 t64 = (psVector *) t; … … 2209 2231 if (fErr != NULL) { 2210 2232 if (fErr->type.type != PS_TYPE_F64) { 2211 PS_VECTOR_GEN_F64_FROM_F32(fErr64, fErr);2233 fErr64 = psVectorCopy(NULL, fErr, PS_TYPE_F64); 2212 2234 } else { 2213 2235 fErr64 = (psVector *) fErr; … … 2310 2332 2311 2333 2312 // XXX: Add F64 support.2313 2334 psPolynomial4D *psVectorClipFitPolynomial4D( 2314 2335 psPolynomial4D *poly, … … 2356 2377 2357 2378 // clipping range defined by min and max and/or clipSigma 2358 floatminClipSigma;2359 floatmaxClipSigma;2379 psF32 minClipSigma; 2380 psF32 maxClipSigma; 2360 2381 if (isfinite(stats->max)) { 2361 2382 maxClipSigma = fabs(stats->max); … … 2379 2400 psTrace(__func__, 4, "(minClipSigma, maxClipSigma) is (%.2f, %.2f)\n", minClipSigma, maxClipSigma); 2380 2401 2381 for ( intN = 0; N < stats->clipIter; N++) {2402 for (psS32 N = 0; N < stats->clipIter; N++) { 2382 2403 psTrace(__func__, 6, "Loop iteration %d. Calling psVectorFitPolynomial1D()\n"); 2383 intNkeep = 0;2404 psS32 Nkeep = 0; 2384 2405 if (psTraceGetLevel(__func__) >= 6) { 2385 2406 if (mask != NULL) { … … 2391 2412 2392 2413 poly = psVectorFitPolynomial4D (poly, mask, maskValue, f, fErr, x, y, z, t); 2393 // XXX: Check error codes 2414 if (poly == NULL) { 2415 psError(PS_ERR_UNKNOWN, false, "Could not fit a polynomial to the data. Returning NULL.\n"); 2416 psFree(resid) 2417 psFree(fit) 2418 return(NULL); 2419 } 2420 2394 2421 fit = psPolynomial4DEvalVector (poly, x, y, z, t); 2395 // XXX: This is coded differently for 1D, 2D. Consolidate. 2422 if (fit == NULL) { 2423 psError(PS_ERR_UNKNOWN, false, "Could not call psPolynomial3DEvalVector(). Returning NULL.\n"); 2424 psFree(resid) 2425 return(NULL); 2426 } 2396 2427 for (psS32 i = 0 ; i < f->n ; i++) { 2397 2428 if (f->type.type == PS_TYPE_F64) { … … 2401 2432 } 2402 2433 } 2403 // XXX: Check error codes2404 2434 2405 2435 if (psTraceGetLevel(__func__) >= 6) { … … 2414 2444 } 2415 2445 2416 // XXX: Check error codes2417 2446 stats = psVectorStats (stats, resid, NULL, mask, maskValue); 2447 if (stats == NULL) { 2448 psError(PS_ERR_UNKNOWN, false, "Could not compute statistics on the resid vector. Returning NULL.\n"); 2449 psFree(resid) 2450 psFree(fit) 2451 return(NULL); 2452 } 2418 2453 psTrace(__func__, 6, "Median is %f\n", stats->sampleMedian); 2419 2454 psTrace(__func__, 6, "Stdev is %f\n", stats->sampleStdev); 2420 floatminClipValue = -minClipSigma*stats->sampleStdev;2421 floatmaxClipValue = +maxClipSigma*stats->sampleStdev;2455 psF32 minClipValue = -minClipSigma*stats->sampleStdev; 2456 psF32 maxClipValue = +maxClipSigma*stats->sampleStdev; 2422 2457 2423 2458 // set mask if pts are not valid 2424 2459 // we are masking out any point which is out of range 2425 2460 // recovery is not allowed with this scheme 2426 for ( inti = 0; i < resid->n; i++) {2461 for (psS32 i = 0; i < resid->n; i++) { 2427 2462 if ((mask != NULL) && (mask->data.U8[i] & maskValue)) { 2428 2463 continue;
Note:
See TracChangeset
for help on using the changeset viewer.
