IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 24, 2005, 5:38:37 AM (21 years ago)
Author:
eugene
Message:

adding model tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/psPolynomials.c

    r4129 r4375  
    11# include "psphot.h"
    22
    3 // XXX EAM : this version uses myPoly->nX as Norder, not Nterms
    4 psVector *Polynomial2DEvalVector(const psPolynomial2D *myPoly,
    5                                  const psVector *x,
    6                                  const psVector *y)
    7 
    8 {
    9     PS_POLY_CHECK_NULL(myPoly, NULL);
    10     PS_VECTOR_CHECK_NULL(x, NULL);
    11     PS_VECTOR_CHECK_TYPE(x, PS_TYPE_F32, NULL);
    12     PS_VECTOR_CHECK_NULL(y, NULL);
    13     PS_VECTOR_CHECK_TYPE(y, PS_TYPE_F32, NULL);
    14 
    15     psVector *tmp;
    16     psS32 vecLen=x->n;
    17 
    18     // Determine the length of the output vector to by the minimum of the x,y vectors
    19     if (y->n < vecLen) {
    20         vecLen = y->n;
    21     }
    22 
    23     // Create output vector to return
    24     tmp = psVectorAlloc(vecLen, PS_TYPE_F32);
    25 
    26     // Evaluate the polynomial at the specified points
    27     for (psS32 i=0; i<vecLen; i++) {
    28         tmp->data.F32[i] = Polynomial2DEval(myPoly,x->data.F32[i],y->data.F32[i]);
    29     }
    30 
    31     // Return output vector
    32     return(tmp);
    33 }
    34 
    35 // XXX EAM : this version uses the F64 vectors
    36 psVector *Polynomial2DEvalVectorD(const psPolynomial2D *myPoly,
    37                                   const psVector *x,
    38                                   const psVector *y)
    39 
     3// write out the terms of the given 1D polynomial
     4void psPolynomial1DDump (psPolynomial1D *poly) {
     5
     6    for (int i = 0; i < poly->n + 1; i++) {
     7        fprintf (stderr, "x^%d : %g +/- %g\n", i, poly->coeff[i], poly->coeffErr[i]);
     8    }
     9}   
     10
     11psF32 Polynomial1DEval_EAM(psF32 x, const psPolynomial1D* myPoly)
     12{
     13    psS32 loop_x = 0;
     14    psF32 polySum = 0.0;
     15    psF32 xSum = 1.0;
     16
     17    for (loop_x = 0; loop_x < myPoly->n + 1; loop_x++) {
     18        if (myPoly->mask[loop_x] == 0) {
     19            polySum += xSum * myPoly->coeff[loop_x];
     20        }
     21        xSum *= x;
     22    }
     23
     24    return(polySum);
     25}
     26
     27psVector *Polynomial1DEvalVector_EAM(const psPolynomial1D *myPoly,
     28                                       const psVector *x)
    4029{
    4130    PS_POLY_CHECK_NULL(myPoly, NULL);
    4231    PS_VECTOR_CHECK_NULL(x, NULL);
    4332    PS_VECTOR_CHECK_TYPE(x, PS_TYPE_F64, NULL);
    44     PS_VECTOR_CHECK_NULL(y, NULL);
    45     PS_VECTOR_CHECK_TYPE(y, PS_TYPE_F64, NULL);
    4633
    4734    psVector *tmp;
    48     psS32 vecLen=x->n;
    49 
    50     // Determine the length of the output vector to by the minimum of the x,y vectors
    51     if (y->n < vecLen) {
    52         vecLen = y->n;
    53     }
    54 
    55     // Create output vector to return
    56     tmp = psVectorAlloc(vecLen, PS_TYPE_F64);
    57 
    58     // Evaluate the polynomial at the specified points
    59     for (psS32 i=0; i<vecLen; i++) {
    60         tmp->data.F64[i] = Polynomial2DEval(myPoly,x->data.F64[i],y->data.F64[i]);
    61     }
    62 
    63     // Return output vector
     35
     36    tmp = psVectorAlloc(x->n, PS_TYPE_F64);
     37    for (psS32 i=0;i<x->n;i++) {
     38        tmp->data.F64[i] = Polynomial1DEval_EAM(x->data.F64[i], myPoly);
     39    }
     40
    6441    return(tmp);
    6542}
     
    9370}
    9471
    95 // XXX EAM : use Nterm = Norder + 1 definition 
    96 // the user requests a polynomial of order Norder
    97 psPolynomial2D* Polynomial2DAlloc(psS32 nXorder, psS32 nYorder,
    98                                     psPolynomialType type)
    99 {
    100     PS_INT_CHECK_NON_NEGATIVE(nXorder, NULL);
    101     PS_INT_CHECK_NON_NEGATIVE(nYorder, NULL);
    102 
    103     psS32 x = 0;
    104     psS32 y = 0;
    105     psS32 nXterm = nXorder + 1;
    106     psS32 nYterm = nYorder + 1;
    107     psPolynomial2D* newPoly = NULL;
    108 
    109     newPoly = (psPolynomial2D* ) psAlloc(sizeof(psPolynomial2D));
    110     // p_psMemSetDeallocator(newPoly, (psFreeFcn) polynomial2DFree);
    111     // XXX EAM : me, being lazy
    112 
    113     newPoly->type = type;
    114     newPoly->nX = nXorder;
    115     newPoly->nY = nYorder;
    116 
    117     newPoly->coeff = (psF32 **)psAlloc(nXterm * sizeof(psF32 *));
    118     newPoly->coeffErr = (psF32 **)psAlloc(nXterm * sizeof(psF32 *));
    119     newPoly->mask = (psU8 **)psAlloc(nXterm * sizeof(psU8 *));
    120     for (x = 0; x < nXterm; x++) {
    121         newPoly->coeff[x] = (psF32 *)psAlloc(nYterm * sizeof(psF32));
    122         newPoly->coeffErr[x] = (psF32 *)psAlloc(nYterm * sizeof(psF32));
    123         newPoly->mask[x] = (psU8 *)psAlloc(nYterm * sizeof(psU8));
    124     }
    125     for (x = 0; x < nXterm; x++) {
    126         for (y = 0; y < nYterm; y++) {
    127             newPoly->coeff[x][y] = 0.0;
    128             newPoly->coeffErr[x][y] = 0.0;
    129             newPoly->mask[x][y] = 0;
    130         }
    131     }
    132     return(newPoly);
    133 }
    134 
    13572// XXX EAM : my alternate BuildSums1D
    13673static psVector *BuildSums1D(psVector* sums,
     
    15390        sums->data.F64[i] = xSum;
    15491        xSum *= x;
    155     }
    156     return (sums);
    157 }
    158 
    159 // XXX EAM : BuildSums2D in analogy with BuildSums1D
    160 static psImage *BuildSums2D(psImage* sums,
    161                             psF64 x,      psF64 y,
    162                             psS32 nXterm, psS32 nYterm)
    163 {
    164     psS32 nXsum = 0;
    165     psS32 nYsum = 0;
    166     psF64 xSum = 1.0;
    167     psF64 ySum = 1.0;
    168 
    169     nXsum = 2*nXterm;
    170     nYsum = 2*nYterm;
    171     if (sums == NULL) {
    172         sums = psImageAlloc(nXsum, nYsum, PS_TYPE_F64);
    173     }
    174     if ((nXsum != sums->numCols) || (nYsum != sums->numRows)) {
    175         psFree (sums);
    176         sums = psImageAlloc(nXsum, nYsum, PS_TYPE_F64);
    177     }
    178 
    179     ySum = 1.0;
    180     for (int j = 0; j < nYsum; j++) {
    181         xSum = ySum;
    182         for (int i = 0; i < nXsum; i++) {
    183             sums->data.F64[i][j] = xSum;
    184             xSum *= x;
    185         }
    186         ySum *= y;
    18792    }
    18893    return (sums);
     
    291196}
    292197
     198// ********************** 2D polynomial functions ******************
     199
     200// XXX EAM : this version uses myPoly->nX as Norder, not Nterms
     201psVector *Polynomial2DEvalVector(const psPolynomial2D *myPoly,
     202                                 const psVector *x,
     203                                 const psVector *y)
     204
     205{
     206    PS_POLY_CHECK_NULL(myPoly, NULL);
     207    PS_VECTOR_CHECK_NULL(x, NULL);
     208    PS_VECTOR_CHECK_TYPE(x, PS_TYPE_F32, NULL);
     209    PS_VECTOR_CHECK_NULL(y, NULL);
     210    PS_VECTOR_CHECK_TYPE(y, PS_TYPE_F32, NULL);
     211
     212    psVector *tmp;
     213    psS32 vecLen=x->n;
     214
     215    // Determine the length of the output vector to by the minimum of the x,y vectors
     216    if (y->n < vecLen) {
     217        vecLen = y->n;
     218    }
     219
     220    // Create output vector to return
     221    tmp = psVectorAlloc(vecLen, PS_TYPE_F32);
     222
     223    // Evaluate the polynomial at the specified points
     224    for (psS32 i=0; i<vecLen; i++) {
     225        tmp->data.F32[i] = Polynomial2DEval(myPoly,x->data.F32[i],y->data.F32[i]);
     226    }
     227
     228    // Return output vector
     229    return(tmp);
     230}
     231
     232// XXX EAM : this version uses the F64 vectors
     233psVector *Polynomial2DEvalVectorD(const psPolynomial2D *myPoly,
     234                                  const psVector *x,
     235                                  const psVector *y)
     236
     237{
     238    PS_POLY_CHECK_NULL(myPoly, NULL);
     239    PS_VECTOR_CHECK_NULL(x, NULL);
     240    PS_VECTOR_CHECK_TYPE(x, PS_TYPE_F64, NULL);
     241    PS_VECTOR_CHECK_NULL(y, NULL);
     242    PS_VECTOR_CHECK_TYPE(y, PS_TYPE_F64, NULL);
     243
     244    psVector *tmp;
     245    psS32 vecLen=x->n;
     246
     247    // Determine the length of the output vector to by the minimum of the x,y vectors
     248    if (y->n < vecLen) {
     249        vecLen = y->n;
     250    }
     251
     252    // Create output vector to return
     253    tmp = psVectorAlloc(vecLen, PS_TYPE_F64);
     254
     255    // Evaluate the polynomial at the specified points
     256    for (psS32 i=0; i<vecLen; i++) {
     257        tmp->data.F64[i] = Polynomial2DEval(myPoly,x->data.F64[i],y->data.F64[i]);
     258    }
     259
     260    // Return output vector
     261    return(tmp);
     262}
     263
     264// XXX EAM : use Nterm = Norder + 1 definition 
     265// the user requests a polynomial of order Norder
     266psPolynomial2D* Polynomial2DAlloc(psS32 nXorder, psS32 nYorder,
     267                                    psPolynomialType type)
     268{
     269    PS_INT_CHECK_NON_NEGATIVE(nXorder, NULL);
     270    PS_INT_CHECK_NON_NEGATIVE(nYorder, NULL);
     271
     272    psS32 x = 0;
     273    psS32 y = 0;
     274    psS32 nXterm = nXorder + 1;
     275    psS32 nYterm = nYorder + 1;
     276    psPolynomial2D* newPoly = NULL;
     277
     278    newPoly = (psPolynomial2D* ) psAlloc(sizeof(psPolynomial2D));
     279    // p_psMemSetDeallocator(newPoly, (psFreeFcn) polynomial2DFree);
     280    // XXX EAM : me, being lazy
     281
     282    newPoly->type = type;
     283    newPoly->nX = nXorder;
     284    newPoly->nY = nYorder;
     285
     286    newPoly->coeff = (psF32 **)psAlloc(nXterm * sizeof(psF32 *));
     287    newPoly->coeffErr = (psF32 **)psAlloc(nXterm * sizeof(psF32 *));
     288    newPoly->mask = (psU8 **)psAlloc(nXterm * sizeof(psU8 *));
     289    for (x = 0; x < nXterm; x++) {
     290        newPoly->coeff[x] = (psF32 *)psAlloc(nYterm * sizeof(psF32));
     291        newPoly->coeffErr[x] = (psF32 *)psAlloc(nYterm * sizeof(psF32));
     292        newPoly->mask[x] = (psU8 *)psAlloc(nYterm * sizeof(psU8));
     293    }
     294    for (x = 0; x < nXterm; x++) {
     295        for (y = 0; y < nYterm; y++) {
     296            newPoly->coeff[x][y] = 0.0;
     297            newPoly->coeffErr[x][y] = 0.0;
     298            newPoly->mask[x][y] = 0;
     299        }
     300    }
     301    return(newPoly);
     302}
     303
     304// XXX EAM : BuildSums2D in analogy with BuildSums1D
     305static psImage *BuildSums2D(psImage* sums,
     306                            psF64 x,      psF64 y,
     307                            psS32 nXterm, psS32 nYterm)
     308{
     309    psS32 nXsum = 0;
     310    psS32 nYsum = 0;
     311    psF64 xSum = 1.0;
     312    psF64 ySum = 1.0;
     313
     314    nXsum = 2*nXterm;
     315    nYsum = 2*nYterm;
     316    if (sums == NULL) {
     317        sums = psImageAlloc(nXsum, nYsum, PS_TYPE_F64);
     318    }
     319    if ((nXsum != sums->numCols) || (nYsum != sums->numRows)) {
     320        psFree (sums);
     321        sums = psImageAlloc(nXsum, nYsum, PS_TYPE_F64);
     322    }
     323
     324    ySum = 1.0;
     325    for (int j = 0; j < nYsum; j++) {
     326        xSum = ySum;
     327        for (int i = 0; i < nXsum; i++) {
     328            sums->data.F64[i][j] = xSum;
     329            xSum *= x;
     330        }
     331        ySum *= y;
     332    }
     333    return (sums);
     334}
     335
    293336// XXX EAM : test version of 2d fitting
    294337psPolynomial2D* VectorFitPolynomial2DOrd_EAM(psPolynomial2D* myPoly,
     
    387430}   
    388431
    389 // write out the terms of the given 1D polynomial
    390 void psPolynomial1DDump (psPolynomial1D *poly) {
    391 
    392     for (int i = 0; i < poly->n + 1; i++) {
    393         fprintf (stderr, "x^%d : %g +/- %g\n", i, poly->coeff[i], poly->coeffErr[i]);
    394     }
    395 }   
    396 
    397432psPolynomial2D* RobustFit2D_nomask(psPolynomial2D* poly,
    398433                            const psVector* x,
Note: See TracChangeset for help on using the changeset viewer.