49#include "../../2d_interpolate/nn/nan.h"
51void print_coefs(FILE *fprec,
double x_offset,
double x_scale,
long chebyshev,
52 double *coef,
double *s_coef, int32_t *order,
long n_terms,
53 double chi,
long norm_term,
char *prepend);
54char **makeCoefficientUnits(
SDDS_DATASET *SDDSout,
char *xName,
char *yName,
55 int32_t *order,
long terms);
56long setCoefficientData(
SDDS_DATASET *SDDSout,
double *coef,
double *coefSigma,
57 char **coefUnits, int32_t *order,
long terms,
long chebyshev,
58 char *fitLabelFormat,
char *rpnSeqBuffer);
59char **initializeOutputFile(
SDDS_DATASET *SDDSout,
char *output,
61 char *yName,
char *xSigmaName,
char *ySigmaName,
62 long sigmasValid, int32_t *order,
long terms,
63 long chebyshev,
long copyParameters);
64void checkInputFile(
SDDS_DATASET *SDDSin,
char *xName,
char *yName,
65 char *xSigmaName,
char *ySigmaName);
66long coefficient_index(int32_t *order,
long terms,
long order_of_interest);
67void makeFitLabel(
char *buffer,
long bufsize,
char *fitLabelFormat,
68 double *coef, int32_t *order,
long terms,
long chebyshev);
69void createRpnSequence(
char *buffer,
long bufsize,
double *coef, int32_t *order,
72double tcheby(
double x,
long n);
73double dtcheby(
double x,
long n);
74double ipower(
double x,
long n);
75double dipower(
double x,
long n);
76long reviseFitOrders(
double *x,
double *y,
double *sy, int64_t points,
77 long terms, int32_t *order,
double *coef,
78 double *coefSigma,
double *diff,
79 double (*basis_fn)(
double xa,
long ordera),
80 unsigned long reviseOrders,
double xOffset,
81 double xScaleFactor,
long normTerm,
long ySigmasValid,
82 long chebyshev,
double revpowThreshold,
83 double revpowCompleteThres,
double goodEnoughChi);
84long reviseFitOrders1(
double *x,
double *y,
double *sy, int64_t points,
85 long terms, int32_t *order,
double *coef,
86 double *coefSigma,
double *diff,
87 double (*basis_fn)(
double xa,
long ordera),
88 unsigned long reviseOrders,
double xOffset,
89 double xScaleFactor,
long normTerm,
long ySigmasValid,
90 long chebyshev,
double revpowThreshold,
91 double goodEnoughChi);
92void compareOriginalToFit(
double *x,
double *y,
double **residual,
93 int64_t points,
double *rmsResidual,
double *coef,
94 int32_t *order,
long terms);
101CHEBYSHEV_COEF *makeChebyshevCoefficients(
long maxOrder,
long *nPoly);
102void convertFromChebyshev(
long termsT, int32_t *orderT,
double *coefT,
double *coefSigmaT,
103 long *termsOrdinaryRet, int32_t **orderOrdinaryRet,
double **coefOrdinaryRet,
double **coefSigmaOrdinaryRet);
131char *option[N_OPTIONS] = {
156 "sddspfit [<inputfile>] [<outputfile>] [-pipe=[input][,output]]\n\
157 -columns=<xname>,<yname>[,xSigma=<name>][,ySigma=<name>]\n\
158 [ {-terms=<number> [-symmetry={none|odd|even}] | -orders=<number>[,...]} ]\n\
159 [-reviseOrders [=threshold=<chiValue>] [,verbose] [,complete=<chiThreshold>] [,goodEnough=<chiValue>]]\n\
160 [-chebyshev [=convert]]\n\
161 [-xOffset=<value>] [-autoOffset] [-xFactor=<value>]\n\
162 [-sigmas=<value>,{absolute|fractional}] \n\
163 [-modifySigmas] [-generateSigmas[={keepLargest|keepSmallest}]]\n\
164 [-sparse=<interval>] [-range=<lower>,<upper>[,fitOnly]]\n\
165 [-normalize[=<termNumber>]] [-verbose]\n\
166 [-evaluate=<filename>[,begin=<value>] [,end=<value>] [,number=<integer>] \n\
167 [,valuesFile=<filename>,valuesColumn=<string>[,reusePage]]]\n\
168 [-fitLabelFormat=<sprintf-string>] [-copyParameters] [-majorOrder={row|column}]\n\n\
169Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
171static char *additional_help1 =
"\n\
172sddspfit fits data to the form y = SUM(i){ A[i] * P(x - x_offset, i) }, where P(x, i) is the ith basis\n\
173function evaluated at x. By default, P(x, i) = x^i. Chebyshev T polynomials can also be selected as the basis functions.\n\n\
174 -columns Specify names of data columns to use.\n\
175 -terms Number of terms desired in fit.\n\
176 -symmetry Symmetry of desired fit about x_offset.\n\
177 -orders Orders (P[i]) to use in fitting.\n\
178 -reviseOrders Modify the orders used in the fit to eliminate poorly-determined coefficients based on fitting\n\
179 of the first data page. The algorithm adds one order at a time, terminating when the reduced\n\
180 chi-squared is less than the \"goodEnough\" value (default: 1) or when the new term does not improve\n\
181 the reduced chi-squared by more than the threshold value (default: 0.1). It next tries removing terms one at a time.\n\
182 Finally, if the resulting best reduced chi-squared is greater than the threshold given with the \"complete\" option,\n\
183 it also tries all possible combinations of allowed terms.\n\
184 -chebyshev Use Chebyshev T polynomials (xOffset is set automatically).\n\
185 Giving the `convert` option causes the fit to be written out in terms of ordinary polynomials.\n\
186 -majorOrder Specify output file in row or column major order.\n\
187 -xOffset Desired value of x to fit about.\n";
189static char *additional_help2 =
190 " -autoOffset Automatically offset x values by the mean x value for fitting.\n\
191 Helpful if x values are very large in magnitude.\n\
192 -xFactor Desired factor to multiply x values by before fitting.\n\
193 -sigmas Specify absolute or fractional sigma for all points.\n\
194 -modifySigmas Modify the y sigmas using the x sigmas and an initial fit.\n\
195 -generateSigmas Generate y sigmas from the RMS deviation from an initial fit.\n\
196 Optionally keep the sigmas from the data if larger/smaller than RMS deviation.\n\
197 -sparse Specify integer interval at which to sample data.\n\
198 -range Specify range of independent variable over which to perform fit and evaluation.\n\
199 If 'fitOnly' is given, then fit is compared to data over the original range.\n\
200 -normalize Normalize so that the specified term is unity.\n\
201 -verbose Generates extra output that may be useful.\n\
202 -evaluate Specify evaluation of fit over a selected range of equispaced points,\n\
203 or at values listed in a file.\n\
204 -copyParameters If given, program copies all parameters from the input file into the main output file.\n\
205 By default, no parameters are copied.\n\n";
208#define EVEN_SYMMETRY 1
209#define ODD_SYMMETRY 2
210#define N_SYMMETRY_OPTIONS 3
211char *symmetry_options[N_SYMMETRY_OPTIONS] = {
"none",
"even",
"odd"};
213#define ABSOLUTE_SIGMAS 0
214#define FRACTIONAL_SIGMAS 1
215#define N_SIGMAS_OPTIONS 2
216char *sigmas_options[N_SIGMAS_OPTIONS] = {
"absolute",
"fractional"};
218#define FLGS_GENERATESIGMAS 1
219#define FLGS_KEEPLARGEST 2
220#define FLGS_KEEPSMALLEST 4
222#define REVPOW_ACTIVE 0x0001
223#define REVPOW_VERBOSE 0x0002
224#define REVPOW_COMPLETE 0x0004
227static long iIntercept = -1, iInterceptSigma = -1;
228static long iSlope = -1, iSlopeSigma = -1;
229static long iCurvature = -1, iCurvatureSigma = -1;
230static long *iTerm = NULL, *iTermSig = NULL;
231static long iOffset = -1, iFactor = -1;
232static long iChiSq = -1, iRmsResidual = -1, iSigLevel = -1;
233static long iFitIsValid = -1, iFitLabel = -1, iTerms = -1;
234static long iRpnSequence;
236static long ix = -1, iy = -1, ixSigma = -1, iySigma = -1;
237static long iFit = -1, iResidual = -1;
239static char *xSymbol, *ySymbol;
241#define EVAL_BEGIN_GIVEN 0x0001U
242#define EVAL_END_GIVEN 0x0002U
243#define EVAL_NUMBER_GIVEN 0x0004U
244#define EVAL_VALUESFILE_GIVEN 0x0008U
245#define EVAL_VALUESCOLUMN_GIVEN 0x0010U
246#define EVAL_REUSE_PAGE_GIVEN 0x0020U
255 short inputInitialized;
256 char *valuesFile, *valuesColumn;
259void makeEvaluationTable(
EVAL_PARAMETERS *evalParameters,
double *x, int64_t n,
260 double *coef, int32_t *order,
long terms,
263static double (*basis_fn)(
double xa,
long ordera);
264static double (*basis_dfn)(
double xa,
long ordera);
266int main(
int argc,
char **argv) {
267 double *x = NULL, *y = NULL, *sy = NULL, *sx = NULL, *diff = NULL, xOffset,
269 double *xOrig = NULL, *yOrig = NULL, *sxOrig, *syOrig, *sy0 = NULL;
270 long terms, normTerm, ySigmasValid;
271 int64_t i, j, points, pointsOrig;
272 long symmetry, chebyshev, autoOffset, copyParameters = 0;
274 long sigmasMode, sparseInterval;
276 double *coef, *coefSigma;
277 double chi, xLow, xHigh, rmsResidual;
278 char *xName, *yName, *xSigmaName, *ySigmaName;
279 char *input, *output, **coefUnits;
281 long isFit, iArg, modifySigmas;
282 long generateSigmas, verbose, ignoreSigmas;
283 long npages = 0, invalid = 0;
286 double xMin, xMax, revpowThreshold, revpowCompleteThres, goodEnoughChi;
287 long rangeFitOnly = 0;
288 double rms_average(
double *d_x, int64_t d_n);
289 char *fitLabelFormat =
"%g";
290 static char rpnSeqBuffer[SDDS_MAXLINE];
291 unsigned long pipeFlags, reviseOrders, majorOrderFlag;
293 short columnMajorOrder = -1;
295 sxOrig = syOrig = NULL;
299 argc =
scanargs(&s_arg, argc, argv);
300 if (argc < 2 || argc > (3 + N_OPTIONS)) {
301 fprintf(stderr,
"usage: %s%s%s\n", USAGE, additional_help1,
306 input = output = NULL;
307 xName = yName = xSigmaName = ySigmaName = NULL;
308 modifySigmas = reviseOrders = chebyshev = 0;
310 symmetry = NO_SYMMETRY;
318 verbose = ignoreSigmas = 0;
326 evalParameters.file = evalParameters.valuesFile = evalParameters.valuesColumn = NULL;
327 evalParameters.initialized = evalParameters.inputInitialized = 0;
329 for (iArg = 1; iArg < argc; iArg++) {
330 if (s_arg[iArg].arg_type == OPTION) {
331 switch (
match_string(s_arg[iArg].list[0], option, N_OPTIONS, 0)) {
332 case CLO_MAJOR_ORDER:
334 s_arg[iArg].n_items--;
335 if (s_arg[iArg].n_items > 0 &&
337 &s_arg[iArg].n_items, 0,
"row", -1, NULL, 0,
338 SDDS_ROW_MAJOR_ORDER,
"column", -1, NULL, 0,
339 SDDS_COLUMN_MAJOR_ORDER, NULL)))
340 SDDS_Bomb(
"invalid -majorOrder syntax/values");
341 if (majorOrderFlag & SDDS_COLUMN_MAJOR_ORDER)
342 columnMajorOrder = 1;
343 else if (majorOrderFlag & SDDS_ROW_MAJOR_ORDER)
344 columnMajorOrder = 0;
346 case CLO_MODIFYSIGMAS:
353 if (s_arg[iArg].n_items < 2)
355 order =
tmalloc(
sizeof(*order) * (terms = s_arg[iArg].n_items - 1));
356 for (i = 1; i < s_arg[iArg].n_items; i++) {
357 if (sscanf(s_arg[iArg].list[i],
"%" SCNd32, order + i - 1) != 1)
358 SDDS_Bomb(
"unable to scan order from -orders list");
363 if ((s_arg[iArg].n_items != 3 && s_arg[iArg].n_items != 4) ||
364 1 != sscanf(s_arg[iArg].list[1],
"%lf", &xMin) ||
365 1 != sscanf(s_arg[iArg].list[2],
"%lf", &xMax) || xMin >= xMax)
367 if (s_arg[iArg].n_items == 4) {
368 if (strncmp(
str_tolower(s_arg[iArg].list[3]),
"fitonly",
369 strlen(s_arg[iArg].list[3])) == 0) {
375 case CLO_GENERATESIGMAS:
376 generateSigmas = FLGS_GENERATESIGMAS;
377 if (s_arg[iArg].n_items > 1) {
378 if (s_arg[iArg].n_items != 2)
379 SDDS_Bomb(
"incorrect -generateSigmas syntax");
380 if (strncmp(s_arg[iArg].list[1],
"keepsmallest",
381 strlen(s_arg[iArg].list[1])) == 0)
382 generateSigmas |= FLGS_KEEPSMALLEST;
383 if (strncmp(s_arg[iArg].list[1],
"keeplargest",
384 strlen(s_arg[iArg].list[1])) == 0)
385 generateSigmas |= FLGS_KEEPLARGEST;
386 if ((generateSigmas & FLGS_KEEPSMALLEST) &&
387 (generateSigmas & FLGS_KEEPLARGEST))
388 SDDS_Bomb(
"ambiguous -generateSigmas syntax");
392 if (s_arg[iArg].n_items != 2 ||
393 sscanf(s_arg[iArg].list[1],
"%ld", &terms) != 1)
397 if (s_arg[iArg].n_items != 2 ||
398 sscanf(s_arg[iArg].list[1],
"%lf", &xOffset) != 1)
402 if (s_arg[iArg].n_items == 2) {
403 if ((symmetry =
match_string(s_arg[iArg].list[1], symmetry_options,
404 N_SYMMETRY_OPTIONS, 0)) < 0)
405 SDDS_Bomb(
"unknown option used with -symmetry");
410 if (s_arg[iArg].n_items != 3)
412 if (sscanf(s_arg[iArg].list[1],
"%lf", &sigmas) != 1)
413 SDDS_Bomb(
"couldn't scan value for -sigmas");
414 if ((sigmasMode =
match_string(s_arg[iArg].list[2], sigmas_options,
415 N_SIGMAS_OPTIONS, 0)) < 0)
419 if (s_arg[iArg].n_items != 2)
421 if (sscanf(s_arg[iArg].list[1],
"%ld", &sparseInterval) != 1)
422 SDDS_Bomb(
"couldn't scan value for -sparse");
423 if (sparseInterval < 1)
431 if (s_arg[iArg].n_items > 2 ||
432 (s_arg[iArg].n_items == 2 &&
433 sscanf(s_arg[iArg].list[1],
"%ld", &normTerm) != 1) ||
437 case CLO_REVISEORDERS:
438 revpowThreshold = 0.1;
439 revpowCompleteThres = 10;
441 s_arg[iArg].n_items -= 1;
443 &s_arg[iArg].n_items, 0,
445 "complete",
SDDS_DOUBLE, &revpowCompleteThres, 1, REVPOW_COMPLETE,
447 "verbose", -1, NULL, 1, REVPOW_VERBOSE, NULL) ||
448 revpowThreshold < 0 || revpowCompleteThres < 0 || goodEnoughChi < 0)
449 SDDS_Bomb(
"invalid -reviseOrders syntax");
450 reviseOrders |= REVPOW_ACTIVE;
453 if (s_arg[iArg].n_items > 2 ||
454 (s_arg[iArg].n_items == 2 &&
455 strncmp(s_arg[iArg].list[1],
"convert",
456 strlen(s_arg[iArg].list[1])) != 0))
458 chebyshev = s_arg[iArg].n_items;
463 if (s_arg[iArg].n_items != 2 ||
464 sscanf(s_arg[iArg].list[1],
"%lf", &xScaleFactor) != 1 ||
469 if (s_arg[iArg].n_items < 3 || s_arg[iArg].n_items > 5)
471 xName = s_arg[iArg].list[1];
472 yName = s_arg[iArg].list[2];
473 s_arg[iArg].n_items -= 3;
474 if (!
scanItemList(&flags, s_arg[iArg].list + 3, &s_arg[iArg].n_items, 0,
475 "xsigma",
SDDS_STRING, &xSigmaName, 1, 0,
"ysigma",
479 case CLO_FITLABELFORMAT:
480 if (s_arg[iArg].n_items != 2)
481 SDDS_Bomb(
"invalid -fitLabelFormat syntax");
482 fitLabelFormat = s_arg[iArg].list[1];
490 if (s_arg[iArg].n_items < 2)
492 evalParameters.file = s_arg[iArg].list[1];
493 s_arg[iArg].n_items -= 2;
494 s_arg[iArg].list += 2;
495 if (!
scanItemList(&evalParameters.flags, s_arg[iArg].list,
496 &s_arg[iArg].n_items, 0,
497 "begin",
SDDS_DOUBLE, &evalParameters.begin, 1, EVAL_BEGIN_GIVEN,
498 "end",
SDDS_DOUBLE, &evalParameters.end, 1, EVAL_END_GIVEN,
499 "number",
SDDS_LONG64, &evalParameters.number, 1, EVAL_NUMBER_GIVEN,
500 "valuesfile",
SDDS_STRING, &evalParameters.valuesFile, 1, EVAL_VALUESFILE_GIVEN,
501 "valuescolumn",
SDDS_STRING, &evalParameters.valuesColumn, 1, EVAL_VALUESCOLUMN_GIVEN,
502 "reusepage", 0, NULL, 0, EVAL_REUSE_PAGE_GIVEN,
505 if (evalParameters.flags & EVAL_VALUESFILE_GIVEN || evalParameters.flags & EVAL_VALUESCOLUMN_GIVEN) {
506 if (evalParameters.flags & (EVAL_BEGIN_GIVEN | EVAL_END_GIVEN | EVAL_NUMBER_GIVEN))
507 SDDS_Bomb(
"invalid -evaluate syntax: given begin/end/number or valuesFile/valuesColumn, not a mixture.");
508 if (!(evalParameters.flags & EVAL_VALUESFILE_GIVEN && evalParameters.flags & EVAL_VALUESCOLUMN_GIVEN))
509 SDDS_Bomb(
"invalid -evaluate syntax: give both valuesFile and valuesColumn, not just one");
511 evalParameters.initialized = 0;
513 case CLO_COPY_PARAMETERS:
517 bomb(
"unknown switch", USAGE);
522 input = s_arg[iArg].list[0];
523 else if (output == NULL)
524 output = s_arg[iArg].list[0];
532 if (symmetry && order)
533 SDDS_Bomb(
"can't specify both -symmetry and -orders");
534 if (chebyshev && order)
535 SDDS_Bomb(
"can't specify both -chebyshev and -orders");
536 if (chebyshev && symmetry)
537 SDDS_Bomb(
"can't specify both -chebyshev and -symmetry");
538 if (!xName || !yName)
539 SDDS_Bomb(
"you must specify a column name for x and y");
541 if (modifySigmas && !xSigmaName)
542 SDDS_Bomb(
"you must specify x sigmas with -modifySigmas");
543 if (generateSigmas) {
545 SDDS_Bomb(
"you can't specify both -generateSigmas and -modifySigmas");
548 if (sigmasMode != -1)
549 SDDS_Bomb(
"you can't specify both -sigmas and a y sigma name");
552 if (sigmasMode != -1 || generateSigmas || ySigmaName || modifySigmas)
555 if (normTerm >= 0 && normTerm >= terms)
556 SDDS_Bomb(
"can't normalize to that term--not that many terms");
557 if (reviseOrders && !(sigmasMode != -1 || generateSigmas || ySigmaName))
559 "can't use -reviseOrders unless a y sigma or -generateSigmas is given");
561 if (symmetry == EVEN_SYMMETRY) {
562 order =
tmalloc(
sizeof(*order) * terms);
563 for (i = 0; i < terms; i++)
565 }
else if (symmetry == ODD_SYMMETRY) {
566 order =
tmalloc(
sizeof(*order) * terms);
567 for (i = 0; i < terms; i++)
568 order[i] = 2 * i + 1;
570 order =
tmalloc(
sizeof(*order) * terms);
571 for (i = 0; i < terms; i++)
574 coef =
tmalloc(
sizeof(*coef) * terms);
575 coefSigma =
tmalloc(
sizeof(*coefSigma) * terms);
576 iTerm =
tmalloc(
sizeof(*iTerm) * terms);
577 iTermSig =
tmalloc(
sizeof(*iTermSig) * terms);
581 checkInputFile(&SDDSin, xName, yName, xSigmaName, ySigmaName);
582 coefUnits = initializeOutputFile(&SDDSout, output, &SDDSin, input, xName,
583 yName, xSigmaName, ySigmaName, ySigmasValid,
584 order, terms, chebyshev, copyParameters);
585 if (columnMajorOrder != -1)
586 SDDSout.layout.data_mode.column_major = columnMajorOrder;
588 SDDSout.layout.data_mode.column_major =
589 SDDSin.layout.data_mode.column_major;
599 fprintf(stderr,
"error: unable to read column %s\n", xName);
601 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
604 fprintf(stderr,
"error: unable to read column %s\n", yName);
606 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
610 fprintf(stderr,
"error: unable to read column %s\n", xSigmaName);
612 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
616 fprintf(stderr,
"error: unable to read column %s\n", ySigmaName);
618 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
621 sy0 =
tmalloc(
sizeof(*sy0) * points);
623 if (xMin != xMax || sparseInterval != 1) {
624 xOrig =
tmalloc(
sizeof(*xOrig) * points);
625 yOrig =
tmalloc(
sizeof(*yOrig) * points);
627 sxOrig =
tmalloc(
sizeof(*sxOrig) * points);
629 syOrig =
tmalloc(
sizeof(*syOrig) * points);
631 for (i = j = 0; i < points; i++) {
640 for (i = j = 0; i < points; i++) {
641 if (xOrig[i] <= xMax && xOrig[i] >= xMin) {
653 if (sparseInterval != 1) {
654 for (i = j = 0; i < points; i++) {
655 if (i % sparseInterval == 0) {
677 if (sigmasMode == ABSOLUTE_SIGMAS) {
678 for (i = 0; i < points; i++)
681 for (i = 0; i < pointsOrig; i++)
683 }
else if (sigmasMode == FRACTIONAL_SIGMAS) {
684 for (i = 0; i < points; i++)
685 sy0[i] = sigmas * fabs(y[i]);
687 for (i = 0; i < pointsOrig; i++)
688 syOrig[i] = fabs(yOrig[i]) * sigmas;
691 if (!ySigmasValid || generateSigmas)
692 for (i = 0; i < points; i++)
695 for (i = 0; i < points; i++)
697 SDDS_Bomb(
"y sigma = 0 for one or more points.");
699 diff =
tmalloc(
sizeof(*x) * points);
700 sy =
tmalloc(
sizeof(*sy) * points);
701 for (i = 0; i < points; i++)
712 xScaleFactor = MAX(fabs(xHigh - xOffset), fabs(xLow - xOffset));
715 xOffset = (xHigh + xLow) / 2;
716 xScaleFactor = (xHigh - xLow) / 2;
722 if (generateSigmas || modifySigmas) {
724 isFit =
lsfg(x, y, sy, points, terms, order, coef, coefSigma, &chi,
729 fputs(
"initial_fit:", stdout);
730 print_coefs(stdout, xOffset, xScaleFactor, chebyshev, coef, NULL,
731 order, terms, chi, normTerm,
"");
732 fprintf(stdout,
"unweighted rms deviation from fit: %21.15e\n",
733 rms_average(diff, points));
737 for (i = 0; i < points; i++)
739 fabs(
eval_sum(basis_dfn, coef, order, terms, x[i]) * sx[i]);
741 for (i = 0; i < points; i++) {
744 sqr(
eval_sum(basis_dfn, coef, order, terms, x[i]) * sx[i]));
747 if (generateSigmas) {
749 for (i = sigma = 0; i < points; i++) {
750 sigma += sqr(diff[i]);
752 sigma = sqrt(sigma / (points - terms));
753 for (i = 0; i < points; i++) {
754 if (generateSigmas & FLGS_KEEPSMALLEST) {
757 }
else if (generateSigmas & FLGS_KEEPLARGEST) {
764 for (i = 0; i < pointsOrig; i++) {
765 if (generateSigmas & FLGS_KEEPSMALLEST) {
768 }
else if (generateSigmas & FLGS_KEEPLARGEST) {
778 if (reviseOrders & REVPOW_ACTIVE) {
779 terms = reviseFitOrders(
780 x, y, sy, points, terms, order, coef, coefSigma, diff, basis_fn,
781 reviseOrders, xOffset, xScaleFactor, normTerm, ySigmasValid,
782 chebyshev, revpowThreshold, revpowCompleteThres, goodEnoughChi);
786 isFit =
lsfg(x, y, sy, points, terms, order, coef, coefSigma, &chi, diff,
789 rmsResidual = rms_average(diff, points);
791 print_coefs(stdout, xOffset, xScaleFactor, chebyshev, coef,
792 (ySigmasValid ? coefSigma : NULL), order, terms, chi,
794 fprintf(stdout,
"unweighted rms deviation from fit: %21.15e\n",
798 fprintf(stdout,
"fit failed.\n");
800 if (evalParameters.file)
801 makeEvaluationTable(&evalParameters, x, points, coef, order, terms,
802 &SDDSin, xName, yName);
805 if (!
SDDS_StartPage(&SDDSout, rangeFitOnly ? pointsOrig : points))
807 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
810 setCoefficientData(&SDDSout, coef, (ySigmasValid ? coefSigma : NULL),
811 coefUnits, order, terms, chebyshev, fitLabelFormat,
815 compareOriginalToFit(xOrig, yOrig, &residual, pointsOrig, &rmsResidual,
823 pointsOrig, iResidual))
825 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
826 for (i = 0; i < pointsOrig; i++)
827 residual[i] = yOrig[i] - residual[i];
832 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
836 pointsOrig, ixSigma))
838 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
839 if (ySigmasValid && iySigma != -1 &&
841 pointsOrig, iySigma))
843 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
846 for (i = 0; i < points; i++)
856 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
857 for (i = 0; i < points; i++)
863 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
869 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
870 if (ySigmasValid && iySigma != -1 &&
874 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
880 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
882 &SDDSout, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iRpnSequence,
883 invalid ?
"" : rpnSeqBuffer, iRmsResidual,
884 invalid ? NaN : rmsResidual, iChiSq, invalid ? NaN : chi, iTerms,
887 invalid ? NaN : xOffset, iFactor, invalid ? NaN : xScaleFactor,
888 iFitIsValid, isFit ?
'y' :
'n', -1) ||
891 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
913 if (evalParameters.initialized && !
SDDS_Terminate(&(evalParameters.dataset)))
923 return (EXIT_SUCCESS);
926void print_coefs(FILE *fpo,
double xOffset,
double xScaleFactor,
long chebyshev,
927 double *coef,
double *coefSigma, int32_t *order,
long terms,
928 double chi,
long normTerm,
char *prepend) {
932 fprintf(fpo,
"%s%ld-term Chebyshev T polynomial least-squares fit about "
933 "x=%21.15e, scaled by %21.15e:\n",
934 prepend, terms, xOffset, xScaleFactor);
936 fprintf(fpo,
"%s%ld-term polynomial least-squares fit about x=%21.15e:\n",
937 prepend, terms, xOffset);
938 if (normTerm >= 0 && terms > normTerm) {
939 if (coef[normTerm] != 0)
940 fprintf(fpo,
"%s coefficients are normalized with factor %21.15e to "
942 prepend, coef[normTerm], (order ? order[normTerm] : normTerm));
944 fprintf(fpo,
"%s can't normalize coefficients as requested: a[%ld]==0\n",
945 prepend, (order ? order[normTerm] : normTerm));
951 for (i = 0; i < terms; i++) {
952 fprintf(fpo,
"%sa[%ld] = %21.15e ", prepend, (order ? order[i] : i),
953 (normTerm < 0 ? coef[i] : coef[i] / coef[normTerm]));
956 fpo,
"+/- %21.15e\n",
957 (normTerm < 0 ? coefSigma[i] : coefSigma[i] / fabs(coef[normTerm])));
962 fprintf(fpo,
"%sreduced chi-squared = %21.15e\n", prepend, chi);
965void makeFitLabel(
char *buffer,
long bufsize,
char *fitLabelFormat,
966 double *coef, int32_t *order,
long terms,
long chebyshev) {
968 static char buffer1[SDDS_MAXLINE], buffer2[SDDS_MAXLINE];
970 sprintf(buffer,
"%s = ", ySymbol);
971 for (i = 0; i < terms; i++) {
977 strcat(buffer1,
"+");
978 sprintf(buffer1 + 1, fitLabelFormat, coef[i]);
980 sprintf(buffer1, fitLabelFormat, coef[i]);
983 strcat(buffer1,
"+");
984 sprintf(buffer1 + 1, fitLabelFormat, coef[i]);
986 sprintf(buffer1, fitLabelFormat, coef[i]);
988 strcat(buffer1,
"*");
989 if (chebyshev != 1) {
990 strcat(buffer1, xSymbol);
992 sprintf(buffer2,
"$a%d$n", order[i]);
993 strcat(buffer1, buffer2);
996 sprintf(buffer2,
"T$b%d$n(%s)", order[i], xSymbol);
997 strcat(buffer1, buffer2);
1001 if ((
long)(strlen(buffer) + strlen(buffer1)) > (
long)(0.95 * bufsize)) {
1002 fprintf(stderr,
"buffer overflow making fit label!\n");
1005 strcat(buffer, buffer1);
1009double rms_average(
double *x, int64_t n) {
1013 for (i = sum2 = 0; i < n; i++)
1016 return (sqrt(sum2 / n));
1019long coefficient_index(int32_t *order,
long terms,
long order_of_interest) {
1021 for (i = 0; i < terms; i++)
1022 if (order[i] == order_of_interest)
1027void checkInputFile(
SDDS_DATASET *SDDSin,
char *xName,
char *yName,
1028 char *xSigmaName,
char *ySigmaName) {
1031 SDDS_Bomb(
"x column doesn't exist or is nonnumeric");
1035 SDDS_Bomb(
"y column doesn't exist or is nonnumeric");
1039 !(ptr =
SDDS_FindColumn(SDDSin, FIND_NUMERIC_TYPE, xSigmaName, NULL)))
1040 SDDS_Bomb(
"x sigma column doesn't exist or is nonnumeric");
1045 !(ptr =
SDDS_FindColumn(SDDSin, FIND_NUMERIC_TYPE, ySigmaName, NULL)))
1046 SDDS_Bomb(
"y sigma column doesn't exist or is nonnumeric");
1052char **initializeOutputFile(
SDDS_DATASET *SDDSout,
char *output,
1054 char *yName,
char *xSigmaName,
char *ySigmaName,
1055 long sigmasValid, int32_t *order,
long terms,
1056 long chebyshev,
long copyParameters) {
1057 char buffer[SDDS_MAXLINE], buffer1[SDDS_MAXLINE], *xUnits, *yUnits,
1073 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1085 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1087 sprintf(buffer,
"%sFit", yName);
1088 sprintf(buffer1,
"Fit[%s]", ySymbol);
1091 SDDS_SET_BY_NAME, buffer))
1092 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1094 SDDS_Bomb(
"unable to get index of just-defined fit output column");
1096 sprintf(buffer,
"%sResidual", yName);
1097 sprintf(buffer1,
"Residual[%s]", ySymbol);
1100 SDDS_SET_BY_NAME, buffer))
1101 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1104 SDDS_Bomb(
"unable to get index of just-defined residual output column");
1106 if (sigmasValid && !ySigmaName) {
1107 sprintf(buffer,
"%sSigma", yName);
1110 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1113 sprintf(buffer1,
"Sigma[%s]", ySymbol);
1115 SDDS_SET_BY_NAME, buffer))
1117 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1121 if (!(coefUnits = makeCoefficientUnits(SDDSout, xName, yName, order, terms)))
1122 SDDS_Bomb(
"unable to make coefficient units");
1125 NULL,
SDDS_LONG, 0, 1,
"FitResults") < 0 ||
1127 "Coefficient of term in fit", NULL,
SDDS_DOUBLE, 0, 1,
1128 "FitResults") < 0 ||
1131 "[CoefficientUnits]",
1132 "Sigma of coefficient of term in fit", NULL,
1136 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1140 chebyshev ? (chebyshev == 1 ?
"Chebyshev T polynomials" :
"Converted Chebyshev T polynomials")
1141 :
"ordinary polynomials") < 0 ||
1143 SDDSout,
"ReducedChiSquared",
"$gh$r$a2$n/(N-M)", NULL,
1144 "Reduced chi-squared of fit", NULL,
SDDS_DOUBLE, NULL)) < 0 ||
1148 SDDSout,
"RmsResidual",
"$gs$r$bres$n", yUnits,
1149 "RMS residual of fit", NULL,
SDDS_DOUBLE, NULL)) < 0 ||
1152 "Probability that data is from fit function",
1155 "Rpn sequence to evaluate the fit",
1165 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1166 sprintf(buffer,
"%sOffset", xName);
1167 sprintf(buffer1,
"Offset of %s for fit", xName);
1170 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1171 sprintf(buffer,
"%sScale", xName);
1172 sprintf(buffer1,
"Scale factor of %s for fit", xName);
1175 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1180 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1183 "Number of terms in fit", NULL,
SDDS_LONG,
1185 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1190 if ((i = coefficient_index(order, terms, 0)) >= 0) {
1196 SDDSout,
"InterceptSigma",
"InterceptSigma", coefUnits[i],
1197 "Sigma of intercept of fit", NULL,
SDDS_DOUBLE, NULL);
1199 if ((i = coefficient_index(order, terms, 1)) >= 0) {
1204 coefUnits[i],
"Sigma of slope of fit",
1207 if ((i = coefficient_index(order, terms, 2)) >= 0) {
1213 SDDSout,
"CurvatureSigma",
"CurvatureSigma", coefUnits[i],
1214 "Sigma of curvature of fit", NULL,
SDDS_DOUBLE, NULL);
1218 for (i = 0; i < terms; i++) {
1220 sprintf(s,
"Coefficient%02ld", (
long)order[i]);
1224 for (i = 0; i < terms; i++) {
1227 sprintf(s,
"Coefficient%02ldSigma", (
long)order[i]);
1236 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1238 if (copyParameters &&
1240 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1243 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1248long setCoefficientData(
SDDS_DATASET *SDDSout,
double *coef,
double *coefSigma,
1249 char **coefUnits, int32_t *order,
long terms,
long chebyshev,
1250 char *fitLabelFormat,
char *rpnSeqBuffer) {
1253 static char fitLabelBuffer[SDDS_MAXLINE];
1255 if (chebyshev != 2) {
1256 createRpnSequence(rpnSeqBuffer, SDDS_MAXLINE, coef, order, terms);
1263 coefSigma, terms)) ||
1266 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1268 termIndex = coefficient_index(order, terms, 0);
1270 if (iIntercept != -1 &&
1272 iIntercept, invalid ? NaN : coef[termIndex], -1))
1274 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1275 if (iInterceptSigma != -1 &&
1278 invalid ? NaN : coefSigma[termIndex], -1))
1280 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1282 termIndex = coefficient_index(order, terms, 1);
1285 iSlope, invalid ? NaN : coef[termIndex], -1))
1287 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1288 if (iSlopeSigma != -1 &&
1290 iSlopeSigma, invalid ? NaN : coefSigma[termIndex],
1293 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1295 termIndex = coefficient_index(order, terms, 2);
1296 if (iCurvature != -1 &&
1298 iCurvature, invalid ? NaN : coef[termIndex], -1))
1300 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1301 if (iCurvatureSigma != -1 &&
1304 invalid ? NaN : coefSigma[termIndex], -1))
1306 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1307 if (iFitLabel != -1 && !invalid) {
1308 makeFitLabel(fitLabelBuffer, SDDS_MAXLINE, fitLabelFormat, coef, order,
1311 iFitLabel, fitLabelBuffer, -1))
1313 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1315 for (i = 0; i < terms; i++) {
1317 iTerm[i], coef[i], -1))
1319 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1320 if (iTermSig[i] != -1)
1322 SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE,
1323 iTermSig[i], coefSigma[i], -1))
1325 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1330 double *coefC, *coefSigmaC;
1331 convertFromChebyshev(terms, order, coef, coefSigma, &termsC, &orderC, &coefC, &coefSigmaC);
1332 setCoefficientData(SDDSout, coefC, coefSigmaC, coefUnits, orderC, termsC, 0, fitLabelFormat,
1339char **makeCoefficientUnits(
SDDS_DATASET *SDDSout,
char *xName,
char *yName,
1340 int32_t *order,
long terms) {
1341 char *xUnits, *yUnits, buffer[SDDS_MAXLINE];
1342 char **coefUnits = NULL;
1349 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1351 coefUnits =
tmalloc(
sizeof(*coefUnits) * terms);
1355 for (i = 0; i < terms; i++)
1356 coefUnits[i] = yUnits;
1360 for (i = 0; i < terms; i++) {
1361 if (order[i] == 0) {
1362 if (strcmp(yUnits,
"1") != 0)
1366 }
else if (strcmp(xUnits, yUnits) == 0) {
1368 sprintf(buffer,
"1/%s$a%d$n", xUnits, order[i] - 1);
1374 sprintf(buffer,
"%s/%s$a%d$n", yUnits, xUnits, order[i]);
1376 sprintf(buffer,
"%s/%s", yUnits, xUnits);
1384void compareOriginalToFit(
double *x,
double *y,
double **residual,
1385 int64_t points,
double *rmsResidual,
double *coef,
1386 int32_t *order,
long terms) {
1388 double residualSum2, fit;
1390 *residual =
tmalloc(
sizeof(**residual) * points);
1393 for (i = 0; i < points; i++) {
1394 fit =
eval_sum(basis_fn, coef, order, terms, x[i]);
1395 (*residual)[i] = y[i] - fit;
1396 residualSum2 += sqr((*residual)[i]);
1398 *rmsResidual = sqrt(residualSum2 / points);
1402 int64_t points,
double *coef, int32_t *order,
1405 double *xEval, *yEval, delta;
1408 if (!evalParameters->initialized) {
1410 "sddspfit evaluation table",
1411 evalParameters->file) ||
1418 SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1419 evalParameters->initialized = 1;
1422 if (evalParameters->flags & EVAL_VALUESFILE_GIVEN) {
1423 if (!evalParameters->inputInitialized) {
1425 fprintf(stderr,
"error: unable to initialize %s\n", evalParameters->valuesFile);
1426 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1429 fprintf(stderr,
"error: unable to read page from %s\n", evalParameters->valuesFile);
1430 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1432 evalParameters->inputInitialized = 1;
1434 if (!(evalParameters->flags & EVAL_REUSE_PAGE_GIVEN) &&
1436 fprintf(stderr,
"error: unable to read page from %s\n", evalParameters->valuesFile);
1437 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1441 fprintf(stderr,
"error: unable to read column %s from %s\n", evalParameters->valuesColumn, evalParameters->valuesFile);
1442 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1446 if (!(evalParameters->flags & EVAL_BEGIN_GIVEN) ||
1447 !(evalParameters->flags & EVAL_END_GIVEN)) {
1450 if (!(evalParameters->flags & EVAL_BEGIN_GIVEN))
1451 evalParameters->begin = min;
1452 if (!(evalParameters->flags & EVAL_END_GIVEN))
1453 evalParameters->end = max;
1455 if (!(evalParameters->flags & EVAL_NUMBER_GIVEN))
1456 evalParameters->number = points;
1457 if (evalParameters->number > 1)
1458 delta = (evalParameters->end - evalParameters->begin) /
1459 (evalParameters->number - 1);
1463 if (!(xEval = (
double *)malloc(
sizeof(*xEval) * evalParameters->number)))
1466 for (i = 0; i < evalParameters->number; i++)
1467 xEval[i] = evalParameters->begin + i * delta;
1470 if (!(yEval = (
double *)malloc(
sizeof(*yEval) * evalParameters->number)))
1472 for (i = 0; i < evalParameters->number; i++)
1473 yEval[i] =
eval_sum(basis_fn, coef, order, terms, xEval[i]);
1475 if (!
SDDS_StartPage(&evalParameters->dataset, evalParameters->number) ||
1477 xEval, evalParameters->number, xName) ||
1479 yEval, evalParameters->number, yName) ||
1481 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1486long reviseFitOrders(
double *x,
double *y,
double *sy, int64_t points,
1487 long terms, int32_t *order,
double *coef,
1488 double *coefSigma,
double *diff,
1489 double (*basis_fn)(
double xa,
long ordera),
1490 unsigned long reviseOrders,
double xOffset,
1491 double xScaleFactor,
long normTerm,
long ySigmasValid,
1492 long chebyshev,
double revpowThreshold,
1493 double revpowCompleteThreshold,
double goodEnoughChi) {
1494 double bestChi, chi;
1495 long bestTerms, newTerms, newBest, *termUsed;
1496 int32_t *newOrder, *bestOrder;
1498 long origTerms, *origOrder;
1500 bestOrder =
tmalloc(
sizeof(*bestOrder) * terms);
1501 newOrder =
tmalloc(
sizeof(*newOrder) * terms);
1502 termUsed =
tmalloc(
sizeof(*termUsed) * terms);
1503 origOrder =
tmalloc(
sizeof(*origOrder) * terms);
1505 for (i = 0; i < terms; i++)
1506 origOrder[i] = order[i];
1507 qsort((
void *)order, terms,
sizeof(*order),
long_cmpasc);
1508 bestOrder[0] = newOrder[0] = order[0];
1510 newTerms = bestTerms = 1;
1512 if (!
lsfg(x, y, sy, points, newTerms, newOrder, coef, coefSigma, &bestChi,
1515 if (reviseOrders & REVPOW_VERBOSE) {
1516 fputs(
"fit to revise orders:", stdout);
1517 print_coefs(stdout, xOffset, xScaleFactor, chebyshev, coef,
1518 (ySigmasValid ? coefSigma : NULL), bestOrder, bestTerms,
1519 bestChi, normTerm,
"");
1520 fprintf(stdout,
"unweighted rms deviation from fit: %21.15e\n",
1521 rms_average(diff, points));
1526 newTerms = newTerms + 1;
1527 for (ip = 1; ip < terms; ip++) {
1530 newOrder[newTerms - 1] = order[ip];
1531 if (!
lsfg(x, y, sy, points, newTerms, newOrder, coef, coefSigma, &chi,
1534 if (reviseOrders & REVPOW_VERBOSE) {
1535 fputs(
"trial fit:", stdout);
1536 print_coefs(stdout, xOffset, xScaleFactor, chebyshev, coef,
1537 (ySigmasValid ? coefSigma : NULL), newOrder, newTerms,
1539 fprintf(stdout,
"unweighted rms deviation from fit: %21.15e\n",
1540 rms_average(diff, points));
1542 if ((bestChi > goodEnoughChi && chi < bestChi) ||
1543 (chi + revpowThreshold < bestChi && newTerms < bestTerms)) {
1545 bestTerms = newTerms;
1548 for (i = 0; i < newTerms; i++)
1549 bestOrder[i] = newOrder[i];
1550 if (reviseOrders & REVPOW_VERBOSE) {
1551 fputs(
"new best fit:", stdout);
1552 print_coefs(stdout, xOffset, xScaleFactor, chebyshev, coef,
1553 (ySigmasValid ? coefSigma : NULL), bestOrder, bestTerms,
1554 bestChi, normTerm,
"");
1555 fprintf(stdout,
"unweighted rms deviation from fit: %21.15e\n",
1556 rms_average(diff, points));
1561 }
while (newBest && bestChi > goodEnoughChi);
1564 for (ip = 0; ip < terms; ip++)
1565 order[ip] = bestOrder[ip];
1570 for (ip = 0; ip < terms; ip++) {
1571 for (i = j = 0; i < terms; i++) {
1573 newOrder[j++] = order[i];
1575 newTerms = terms - 1;
1576 if (!
lsfg(x, y, sy, points, newTerms, newOrder, coef, coefSigma, &chi, diff,
1579 if ((bestChi > goodEnoughChi && chi < goodEnoughChi) ||
1580 (chi + revpowThreshold < bestChi && newTerms < terms)) {
1584 for (i = 0; i < newTerms; i++)
1585 order[i] = newOrder[i];
1586 if (reviseOrders & REVPOW_VERBOSE) {
1587 fputs(
"new best fit:", stdout);
1588 print_coefs(stdout, xOffset, xScaleFactor, chebyshev, coef,
1589 (ySigmasValid ? coefSigma : NULL), order, terms,
1590 bestChi, normTerm,
"");
1591 fprintf(stdout,
"unweighted rms deviation from fit: %21.15e\n",
1592 rms_average(diff, points));
1597 }
while (newBest && terms > 1 && bestChi > goodEnoughChi);
1604 if ((reviseOrders & REVPOW_COMPLETE) && bestChi > revpowCompleteThreshold) {
1606 for (i = 0; i < origTerms; i++)
1607 order[i] = origOrder[i];
1608 if (reviseOrders & REVPOW_VERBOSE)
1609 fprintf(stdout,
"Result unsatisfactory---attempting complete trials\n");
1610 return reviseFitOrders1(x, y, sy, points, terms, order, coef, coefSigma,
1611 diff, basis_fn, reviseOrders, xOffset, xScaleFactor,
1612 normTerm, ySigmasValid, chebyshev, revpowThreshold,
1620long reviseFitOrders1(
double *x,
double *y,
double *sy, int64_t points,
1621 long terms, int32_t *order,
double *coef,
1622 double *coefSigma,
double *diff,
1623 double (*basis_fn)(
double xa,
long ordera),
1624 unsigned long reviseOrders,
double xOffset,
1625 double xScaleFactor,
long normTerm,
long ySigmasValid,
1626 long chebyshev,
double revpowThreshold,
1627 double goodEnoughChi) {
1628 double bestChi, chi;
1629 long bestTerms, newTerms;
1630 int32_t *newOrder = NULL, *bestOrder;
1632 long *counter = NULL, *counterLim = NULL;
1634 if (!(bestOrder = malloc(
sizeof(*bestOrder) * terms)) ||
1635 !(newOrder = malloc(
sizeof(*newOrder) * terms)) ||
1636 !(counter = calloc(
sizeof(*counter), terms)) ||
1637 !(counterLim = calloc(
sizeof(*counterLim), terms))) {
1638 fprintf(stderr,
"Error: memory allocation failure (%ld terms)\n", terms);
1641 for (i = 0; i < terms; i++)
1643 qsort((
void *)order, terms,
sizeof(*order),
long_cmpasc);
1645 if (!
lsfg(x, y, sy, points, 2, order, coef, coefSigma, &bestChi, diff,
1648 for (i = 0; i < 2; i++)
1649 bestOrder[i] = order[i];
1651 if (reviseOrders & REVPOW_VERBOSE) {
1652 fputs(
"starting fit to revise orders:", stdout);
1653 print_coefs(stdout, xOffset, xScaleFactor, chebyshev, coef,
1654 (ySigmasValid ? coefSigma : NULL), order, 1, bestChi, normTerm,
1656 fprintf(stdout,
"unweighted rms deviation from fit: %21.15e\n",
1657 rms_average(diff, points));
1662 for (i = j = 0; i < terms; i++) {
1664 newOrder[j++] = order[i];
1667 if (!
lsfg(x, y, sy, points, newTerms, newOrder, coef, coefSigma, &chi, diff,
1670 if ((chi < goodEnoughChi && newTerms < bestTerms) ||
1671 (bestChi > goodEnoughChi && chi < bestChi)) {
1673 bestTerms = newTerms;
1674 for (i = 0; i < newTerms; i++)
1675 bestOrder[i] = newOrder[i];
1676 if (reviseOrders & REVPOW_VERBOSE) {
1677 fputs(
"new best fit:", stdout);
1678 print_coefs(stdout, xOffset, xScaleFactor, chebyshev, coef,
1679 (ySigmasValid ? coefSigma : NULL), bestOrder, bestTerms,
1680 bestChi, normTerm,
"");
1681 fprintf(stdout,
"unweighted rms deviation from fit: %21.15e\n",
1682 rms_average(diff, points));
1688 for (ip = 0; ip < terms; ip++)
1689 order[ip] = bestOrder[ip];
1698void createRpnSequence(
char *buffer,
long bufsize,
double *coef, int32_t *order,
1700 long i, j, maxOrder;
1701 static char buffer1[SDDS_MAXLINE];
1707 for (i = 0; i < terms; i++) {
1708 if (maxOrder < order[i])
1709 maxOrder = order[i];
1711 if (maxOrder == 0) {
1712 snprintf(buffer, SDDS_MAXLINE,
"%.15e", coef[0]);
1717 snprintf(buffer, SDDS_MAXLINE,
"%le - ", offset);
1718 for (i = 2; i <= maxOrder; i++) {
1719 strcat(buffer,
"= ");
1720 if ((strlen(buffer) + 2) > bufsize) {
1721 fprintf(stderr,
"buffer overflow making rpn expression!\n");
1725 for (i = maxOrder; i >= 0; i--) {
1726 for (j = 0; j < terms; j++) {
1735 snprintf(buffer1, SDDS_MAXLINE,
"%.15g * ", coef1);
1736 else if (i == 0 && order[j] == 0) {
1738 snprintf(buffer1, SDDS_MAXLINE,
"%.15g + ", coef1);
1743 strcpy(buffer1,
"* ");
1745 snprintf(buffer1, SDDS_MAXLINE,
"%.15g + * ", coef1);
1747 if ((strlen(buffer) + strlen(buffer1)) >= bufsize) {
1748 fprintf(stderr,
"buffer overflow making rpn expression!\n");
1751 strcat(buffer, buffer1);
1759CHEBYSHEV_COEF *makeChebyshevCoefficients(
long maxOrder,
long *nPoly) {
1766 *nPoly = maxOrder + 1;
1768 coef =
tmalloc(
sizeof(*coef) * (*nPoly));
1771 coef[0].coef =
tmalloc(
sizeof(*(coef[0].coef)) * coef[0].nTerms);
1772 coef[0].coef[0] = 1;
1775 coef[1].coef =
tmalloc(
sizeof(*(coef[1].coef)) * coef[1].nTerms);
1776 coef[1].coef[0] = 0;
1777 coef[1].coef[1] = 1;
1779 for (i = 2; i < *nPoly; i++) {
1780 coef[i].nTerms = coef[i - 1].nTerms + 1;
1781 coef[i].coef = calloc(coef[i].nTerms,
sizeof(*(coef[i].coef)));
1782 for (j = 0; j < coef[i - 2].nTerms; j++)
1783 coef[i].coef[j] = -coef[i - 2].coef[j];
1784 for (j = 0; j < coef[i - 1].nTerms; j++)
1785 coef[i].coef[j + 1] += 2 * coef[i - 1].coef[j];
1800void convertFromChebyshev(
long termsT, int32_t *orderT,
double *coefT,
double *coefSigmaT,
1801 long *termsOrdinaryRet, int32_t **orderOrdinaryRet,
double **coefOrdinaryRet,
double **coefSigmaOrdinaryRet) {
1804 int32_t *orderOrdinary;
1805 double *coefOrdinary, *coefSigmaOrdinary, scale;
1807 static long nChebyCoef = 0, chebyMaxOrder = 0;
1810 for (i = 0; i < termsT; i++)
1811 if (orderT[i] > maxOrder)
1812 maxOrder = orderT[i];
1814 termsOrdinary = maxOrder + 1;
1815 orderOrdinary =
tmalloc(
sizeof(*orderOrdinary) * termsOrdinary);
1816 coefOrdinary = calloc(termsOrdinary,
sizeof(*coefOrdinary));
1818 coefSigmaOrdinary = calloc(termsOrdinary,
sizeof(*coefSigmaOrdinary));
1820 coefSigmaOrdinary = NULL;
1822 if (chebyCoef == NULL || maxOrder > chebyMaxOrder) {
1824 for (i = 0; i < nChebyCoef; i++)
1825 free(chebyCoef[i].coef);
1828 chebyCoef = makeChebyshevCoefficients(chebyMaxOrder = maxOrder, &nChebyCoef);
1831 for (i = 0; i < termsT; i++) {
1833 for (j = 0; j < chebyCoef[orderT[i]].nTerms; j++) {
1834 coefOrdinary[j] += coefT[i] * chebyCoef[i].coef[j];
1836 coefSigmaOrdinary[j] += sqr(coefSigmaT[i] * chebyCoef[i].coef[j]);
1840 for (i = 0; i < termsOrdinary; i++) {
1842 coefSigmaOrdinary[i] = sqrt(coefSigmaOrdinary[i]) /
ipow(scale, i);
1843 orderOrdinary[i] = i;
1844 coefOrdinary[i] /=
ipow(scale, i);
1846 *termsOrdinaryRet = termsOrdinary;
1847 *orderOrdinaryRet = orderOrdinary;
1848 *coefOrdinaryRet = coefOrdinary;
1849 *coefSigmaOrdinaryRet = coefSigmaOrdinary;
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_CopyParameters(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetArrayVararg(SDDS_DATASET *SDDS_dataset, char *array_name, int32_t mode, void *data_pointer,...)
Sets the values of an array variable in the SDDS dataset using variable arguments for dimensions.
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
int32_t SDDS_SetColumnFromDoubles(SDDS_DATASET *SDDS_dataset, int32_t mode, double *data, int64_t rows,...)
Sets the values for a single data column using double-precision floating-point numbers.
int32_t SDDS_ChangeColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Modifies a specific field in a column definition within the SDDS dataset.
int32_t SDDS_GetColumnInformation(SDDS_DATASET *SDDS_dataset, char *field_name, void *memory, int32_t mode,...)
Retrieves information about a specified column in the SDDS dataset.
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_DefineArray(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, int32_t field_length, int32_t dimensions, const char *group_name)
Defines a data array within the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
int32_t SDDS_DefineParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *symbol, const char *units, const char *description, const char *format_string, int32_t type, char *fixed_value)
Defines a data parameter with a fixed string value.
int32_t SDDS_TransferColumnDefinition(SDDS_DATASET *target, SDDS_DATASET *source, char *name, char *newName)
Transfers a column definition from a source dataset to a target dataset.
int32_t SDDS_TransferAllParameterDefinitions(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, uint32_t mode)
Transfers all parameter definitions from a source dataset to a target dataset.
int32_t SDDS_GetColumnIndex(SDDS_DATASET *SDDS_dataset, char *name)
Retrieves the index of a named column in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
int32_t SDDS_NumberOfErrors()
Retrieves the number of errors recorded by SDDS library routines.
int32_t SDDS_StringIsBlank(char *s)
Checks if a string is blank (contains only whitespace characters).
char * SDDS_FindColumn(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
Finds the first column in the SDDS dataset that matches the specified criteria.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
int32_t SDDS_CopyString(char **target, const char *source)
Copies a source string to a target string with memory allocation.
#define SDDS_STRING
Identifier for the string data type.
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
#define SDDS_CHARACTER
Identifier for the character data type.
#define SDDS_DOUBLE
Identifier for the double data type.
#define SDDS_LONG64
Identifier for the signed 64-bit integer data type.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
long advance_counter(long *counter, long *max_count, long n_indices)
Advances the counter array based on maximum counts.
int find_min_max(double *min, double *max, double *list, int64_t n)
Finds the minimum and maximum values in a list of doubles.
double ipow(const double x, const int64_t p)
Compute x raised to the power p (x^p).
double get_argument_offset()
Get the current argument offset applied before function evaluations.
double get_argument_scale()
Get the current argument scale factor used before function evaluations.
double eval_sum(double(*fn)(double x, long ord), double *coef, int32_t *order, long n_coefs, double x0)
Evaluate a sum of basis functions.
long lsfg(double *xd, double *yd, double *sy, long n_pts, long n_terms, int32_t *order, double *coef, double *s_coef, double *chi, double *diff, double(*fn)(double x, long ord))
Computes generalized least squares fits using a function passed by the caller.
long match_string(char *string, char **option, long n_options, long mode)
Matches a given string against an array of option strings based on specified modes.
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
long processPipeOption(char **item, long items, unsigned long *flags)
void processFilenames(char *programName, char **input, char **output, unsigned long pipeFlags, long noWarnings, long *tmpOutputUsed)
long scanItemList(unsigned long *flags, char **item, long *items, unsigned long mode,...)
Scans a list of items and assigns values based on provided keywords and types.
void set_argument_scale(double scale)
Set the scale factor applied to the input argument of basis functions.
void set_argument_offset(double offset)
Set the offset applied to the input argument of basis functions.
double dtcheby(double x, long n)
Evaluate the derivative of the Chebyshev polynomial T_n(x).
double ipower(double x, long n)
Evaluate a power function x^n.
double dipower(double x, long n)
Evaluate the derivative of x^n.
double tcheby(double x, long n)
Evaluate the Chebyshev polynomial of the first kind T_n(x).
double ChiSqrSigLevel(double ChiSquared0, long nu)
Computes the probability that a chi-squared variable exceeds a given value.
int long_cmpasc(const void *a, const void *b)
Compare two long integers in ascending order.
char * str_tolower(char *s)
Convert a string to lower case.