- Timestamp:
- Jul 17, 2014, 12:36:19 PM (12 years ago)
- Location:
- branches/eam_branches/ipp-ops-20130712
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
psLib/src/math/psMinimizePolyFit.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ipp-ops-20130712
- Property svn:mergeinfo changed
-
branches/eam_branches/ipp-ops-20130712/psLib/src/math/psMinimizePolyFit.c
r31152 r37068 42 42 #include "psLogMsg.h" 43 43 #include "psMathUtils.h" 44 #include "psBinomialCoeff.h" 44 45 /*****************************************************************************/ 45 46 /* DEFINE STATEMENTS */ 46 47 /*****************************************************************************/ 48 #define CZW 0 47 49 48 50 # define USE_GAUSS_JORDAN 1 … … 603 605 bool status = false; 604 606 if (USE_GAUSS_JORDAN) { 607 608 #if (CZW) 609 printf("CZW: about to do GJ: %d\n",status); 610 for (psS32 k = 0; k < nTerm; k++) { 611 printf("CZW: %d %f \t",k,B->data.F64[k]); 612 for (psS32 kk = 0; kk < nTerm; kk++) { 613 printf(" %f ",(A->data.F64[k][kk])); 614 } 615 printf("\n"); 616 } 617 #endif 605 618 status = psMatrixGJSolve(A, B); 619 #if (CZW) 620 printf("CZW: just did GJ: %d\n",status); 621 for (psS32 k = 0; k < nTerm; k++) { 622 printf("CZW: %d %f \t",k,B->data.F64[k]); 623 for (psS32 kk = 0; kk < nTerm; kk++) { 624 printf(" %f ",(A->data.F64[k][kk])); 625 } 626 printf("\n"); 627 } 628 #endif 606 629 } else { 607 630 status = psMatrixLUSolve(A, B); … … 676 699 bool result = true; 677 700 701 // Define values that may be used by PS_POLYNOMIAL_ORD form. 702 bool scale = false; 703 double median = 0.0; 704 double sigma = 1.0; 705 psVector *sorted = NULL; 706 psVector *z64 = NULL; 707 678 708 switch (poly->type) { 679 709 case PS_POLYNOMIAL_ORD: 680 result = VectorFitPolynomial1DOrd(poly, mask, maskValue, f64, fErr64, x64); 681 if (!result) { 682 psError(PS_ERR_UNKNOWN, false, "Could not fit polynomial. Returning NULL.\n"); 683 } 710 if ((f64->n < 10000)&&(poly->nX > 1)) { 711 scale = true; 712 sorted = psVectorSort(NULL,x64); 713 median = sorted->data.F64[sorted->n / 2]; 714 // CZW: I'm not bothering to scale this because it doesn't really matter. 715 sigma = (sorted->data.F64[3 * sorted->n / 4] - sorted->data.F64[sorted->n / 4]); 716 psFree(sorted); 717 718 if ((!isfinite(median))|| 719 (!isfinite(sigma))) { 720 median = 0.0; 721 sigma = 1.0; 722 } 723 if ((median == 0.0)&&(sigma == 1.0)) { 724 scale = false; 725 } 726 // I can't see a way to not clobber x if it's already F64, so make a copy.x 727 z64 = psVectorCopy(NULL,x64,PS_TYPE_F64); 728 psBinaryOp(z64,z64,"-",psScalarAlloc(median,PS_TYPE_F64)); 729 psBinaryOp(z64,z64,"/",psScalarAlloc(sigma,PS_TYPE_F64)); 730 731 #if (CZW) 732 printf("poly1d: Scale parameters: %f %f\n",median,sigma); 733 #endif 734 735 736 result = VectorFitPolynomial1DOrd(poly, mask, maskValue, f64, fErr64, z64); 737 psFree(z64); // Done with this. 738 } 739 else { 740 result = VectorFitPolynomial1DOrd(poly, mask, maskValue, f64, fErr64, x64); 741 } 742 743 if (!result) { 744 psError(PS_ERR_UNKNOWN, false, "Could not fit polynomial. Returning NULL.\n"); 745 } 746 747 if (scale) { 748 // Undo scaling in the polynomial values. 749 psF64 *Zcoeff = psAlloc((1 + poly->nX) * sizeof(psF64)); 750 psF64 *ZcoeffErr = psAlloc((1 + poly->nX) * sizeof(psF64)); 751 752 for (psS32 i = 0; i <= poly->nX; i++) { 753 Zcoeff[i] = poly->coeff[i]; 754 ZcoeffErr[i] = poly->coeffErr[i]; 755 #if (CZW) 756 printf("poly1d: fit parameters: %d %f %f\n", 757 i,poly->coeff[i],poly->coeffErr[i]); 758 #endif 759 } 760 for (psS32 i = 0; i <= poly->nX; i++) { 761 poly->coeff[i] = 0.0; 762 poly->coeffErr[i] = 0.0; 763 764 for (psS32 j = 0; j <= poly->nX; j++) { 765 #if (CZW) 766 printf(" %d %d %f %f %f %f => %f\n", 767 i,j,Zcoeff[j], 768 pow(1.0 / sigma,j) * pow(-1,j - i), 769 pow(median,j - i), 770 1.0 * psBinomialCoeff(j,i), 771 Zcoeff[j] * pow(1.0 / sigma,j) * pow(-1,j -i) * pow(median,j - i) * 1.0 * psBinomialCoeff(j,i) 772 ); 773 #endif 774 poly->coeff[i] += Zcoeff[j] * pow(1.0 / sigma,j) * pow(-1,j - i) * pow(median,j - i) * psBinomialCoeff(j,i); 775 poly->coeffErr[i] += pow(ZcoeffErr[j] * pow(1.0 / sigma,j) * pow(-1,j - i) * pow(median,j - 1) * psBinomialCoeff(j,i),2); 776 } 777 poly->coeffErr[i] = sqrt(poly->coeffErr[i]); 778 #if (CZW) 779 printf("poly1d: unscaled parameters: %d %f %f\n", 780 i,poly->coeff[i], poly->coeffErr[i]); 781 #endif 782 } 783 psFree(Zcoeff); 784 psFree(ZcoeffErr); 785 786 } 787 684 788 break; 685 789 case PS_POLYNOMIAL_CHEB:
Note:
See TracChangeset
for help on using the changeset viewer.
