IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 37515


Ignore:
Timestamp:
Oct 21, 2014, 2:51:52 PM (12 years ago)
Author:
watersc1
Message:

Commit with cleaner small-median handling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/math/psMinimizePolyFit.c

    r37510 r37515  
    702702    // these scaling values put the dynamic range of the data into some vaguely sensible location
    703703    bool scale = false;
     704    bool median_zero = false;
    704705    double median = 0.0;
    705706    double sigma = 1.0;
     
    728729          sigma = 1.0;
    729730        }
    730         if ((median == 0.0)&&(sigma == 1.0)) {
    731           scale = false;
    732         }
    733 
    734         if (median == 0.0) {
    735           if (sigma <= 0.0) {
    736             scale = false; // Try with what we started with.
    737           }
    738           else {
    739             median = 0.1 * sigma;
     731        if (fabs(median) < 1e-10) {
     732          // CZW 2014-10-21: This median is small and close to zero.  This can cause issues with the
     733          // scaling,
     734          median_zero = true;
     735          if ((sigma == 1.0)||(sigma <= 0.0)) {
     736            // Don't bother scaling if sigma is unity (it's already scaled) or if the sigma calculation has gone wrong.
     737            scale = false;
    740738          }
    741739        }
     
    774772#endif
    775773        }
     774
    776775        for (psS32 i = 0; i <= poly->nX; i++) {
    777776          poly->coeff[i] = 0.0;
    778777          poly->coeffErr[i] = 0.0;
    779          
    780           for (psS32 j = 0; j <= poly->nX; j++) {
     778          if (median_zero) {  // If the median is zero, the obtained solution just needs to be scaled by the sigma values.
     779            poly->coeff[i]    = Zcoeff[i] * pow(1.0 / sigma,i);
     780            poly->coeffErr[i] = ZcoeffErr[i] * pow(1.0 / sigma,i);
     781          }
     782          else { // Otherwise, do the correct transformations by expanding the (x-m)/s terms.
     783            for (psS32 j = 0; j <= poly->nX; j++) {
    781784#if (CZW)
    782             printf("        %d %d %f %f %f %f => %f\n",
    783                    i,j,Zcoeff[j],
    784                    pow(1.0 / sigma,j) * pow(-1,j - i),
    785                    pow(median,j - i),
    786                    1.0 * psBinomialCoeff(j,i),
    787                    Zcoeff[j] * pow(1.0 / sigma,j) * pow(-1,j  -i) * pow(median,j - i) * 1.0 * psBinomialCoeff(j,i)
    788                    );
     785              printf("        %d %d %f %f %f %f => %f\n",
     786                     i,j,Zcoeff[j],
     787                     pow(1.0 / sigma,j) * pow(-1,j - i),
     788                     pow(median,j - i),
     789                     1.0 * psBinomialCoeff(j,i),
     790                     Zcoeff[j] * pow(1.0 / sigma,j) * pow(-1,j  -i) * pow(median,j - i) * 1.0 * psBinomialCoeff(j,i)
     791                     );
    789792#endif
    790             poly->coeff[i] += Zcoeff[j] * pow(1.0 / sigma,j) * pow(-1,j - i) * pow(median,j - i) * psBinomialCoeff(j,i);
    791             poly->coeffErr[i] += pow(ZcoeffErr[j] * pow(1.0 / sigma,j) * pow(-1,j - i) * pow(median,j - 1) * psBinomialCoeff(j,i),2);
     793              poly->coeff[i] += Zcoeff[j] * pow(1.0 / sigma,j) * pow(-1,j - i) * pow(median,j - i) * psBinomialCoeff(j,i);
     794              poly->coeffErr[i] += pow(ZcoeffErr[j] * pow(1.0 / sigma,j) * pow(-1,j - i) * pow(median,j - 1) * psBinomialCoeff(j,i),2);
     795            }
     796            poly->coeffErr[i] = sqrt(poly->coeffErr[i]);
    792797          }
    793           poly->coeffErr[i] = sqrt(poly->coeffErr[i]);
    794798#if (CZW)
    795799          printf("poly1d: unscaled parameters: %d %f %f\n",
     
    800804        psFree(ZcoeffErr);
    801805
    802       }
     806      } // End scaling block.
    803807       
    804808        break;
Note: See TracChangeset for help on using the changeset viewer.