Changeset 4375 for trunk/psphot/src/psPolynomials.c
- Timestamp:
- Jun 24, 2005, 5:38:37 AM (21 years ago)
- File:
-
- 1 edited
-
trunk/psphot/src/psPolynomials.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psphot/src/psPolynomials.c
r4129 r4375 1 1 # include "psphot.h" 2 2 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 4 void 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 11 psF32 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 27 psVector *Polynomial1DEvalVector_EAM(const psPolynomial1D *myPoly, 28 const psVector *x) 40 29 { 41 30 PS_POLY_CHECK_NULL(myPoly, NULL); 42 31 PS_VECTOR_CHECK_NULL(x, NULL); 43 32 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);46 33 47 34 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 64 41 return(tmp); 65 42 } … … 93 70 } 94 71 95 // XXX EAM : use Nterm = Norder + 1 definition96 // the user requests a polynomial of order Norder97 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 lazy112 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 135 72 // XXX EAM : my alternate BuildSums1D 136 73 static psVector *BuildSums1D(psVector* sums, … … 153 90 sums->data.F64[i] = xSum; 154 91 xSum *= x; 155 }156 return (sums);157 }158 159 // XXX EAM : BuildSums2D in analogy with BuildSums1D160 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;187 92 } 188 93 return (sums); … … 291 196 } 292 197 198 // ********************** 2D polynomial functions ****************** 199 200 // XXX EAM : this version uses myPoly->nX as Norder, not Nterms 201 psVector *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 233 psVector *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 266 psPolynomial2D* 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 305 static 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 293 336 // XXX EAM : test version of 2d fitting 294 337 psPolynomial2D* VectorFitPolynomial2DOrd_EAM(psPolynomial2D* myPoly, … … 387 430 } 388 431 389 // write out the terms of the given 1D polynomial390 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 397 432 psPolynomial2D* RobustFit2D_nomask(psPolynomial2D* poly, 398 433 const psVector* x,
Note:
See TracChangeset
for help on using the changeset viewer.
