97void print_coefs(FILE *fprec,
double x_offset,
double x_scale,
long chebyshev,
double *coef,
double *s_coef,
98 int32_t *order,
long n_terms,
double chi,
long norm_term,
char *prepend);
99char **makeCoefficientUnits(
SDDS_DATASET *SDDSout,
char *xName,
char *yName, int32_t *order,
long terms);
100long setCoefficientData(
SDDS_DATASET *SDDSout,
double *coef,
double *coefSigma,
char **coefUnits,
long *order,
long terms);
102 SDDS_DATASET *SDDSin,
char *input,
char *xName,
char **yNames,
char *xSigmaName,
103 char **ySigmaNames,
long sigmasValid, int32_t *order,
long terms,
long isChebyshev,
104 long numCols,
long copyParameters);
105void checkInputFile(
SDDS_DATASET *SDDSin,
char *xName,
char **yNames,
char *xSigmaName,
char **ySigmaNames,
long numYNames);
106long coefficient_index(int32_t *order,
long terms,
long order_of_interest);
107void makeFitLabel(
char *buffer,
long bufsize,
char *fitLabelFormat,
double *coef, int32_t *order,
long terms,
long colIndex);
109char **ResolveColumnNames(
SDDS_DATASET *SDDSin,
char **wildcardList,
long length, int32_t *numYNames);
110char **GenerateYSigmaNames(
char *controlString,
char **yNames,
long numYNames);
111void RemoveElementFromStringArray(
char **array,
long index,
long length);
112char **RemoveNonNumericColumnsFromNameArray(
SDDS_DATASET *SDDSin,
char **columns, int32_t *numColumns);
113void compareOriginalToFit(
double *x,
double *y,
double **residual, int64_t points,
double *rmsResidual,
double *coef, int32_t *order,
long terms);
117double tcheby(
double x,
long n);
118double dtcheby(
double x,
long n);
119double ipower(
double x,
long n);
120double dipower(
double x,
long n);
143 CLO_SIGMAINDEPENDENT,
151char *option[N_OPTIONS] = {
179 "sddsmpfit [<inputfile>] [<outputfile>]\n"
180 " [-pipe=[input][,output]]\n"
181 " -independent=<xName>\n"
182 " -dependent=<yname1-wildcard>[,<yname2-wildcard>...]\n"
183 " [-sigmaIndependent=<xSigma>]\n"
184 " [-sigmaDependent=<ySigmaFormatString>]\n"
186 " -terms=<number> [-symmetry={none|odd|even}] | \n"
187 " -orders=<number>[,<number>...] \n"
189 " [-reviseOrders[=threshold=<value>][,verbose]]\n"
190 " [-chebyshev[=convert]]\n"
191 " [-xOffset=<value>] \n"
192 " [-xFactor=<value>]\n"
193 " [-sigmas=<value>,{absolute|fractional}] \n"
194 " [-minimumSigma=<value>]\n"
195 " [-modifySigmas] \n"
196 " [-generateSigmas={keepLargest|keepSmallest}]\n"
197 " [-sparse=<interval>] \n"
198 " [-range=<lower>,<upper>[,fitOnly]]\n"
199 " [-normalize[=<termNumber>]] \n"
201 " [-evaluate=<filename>[,begin=<value>][,end=<value>][,number=<integer>]]\n"
202 " [-fitLabelFormat=<sprintf-string>] \n"
203 " [-infoFile=<filename>]\n"
204 " [-copyParameters]\n"
205 "Program by Michael Borland, revised by Brad Dolin.\n"
206 "Compiled on " __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
"\n";
208static char *additional_help =
"\n\
209sddsmpfit does fits to the form y = SUM(i){ A[i] *P(x-x_offset, i)}, where P(x,i) is the ith basis\n\
210function evaluated at x. sddsmpfit returns the A[i] and estimates of the errors in the values.\n\
211By default P(x,i) = x^i. One can also select Chebyshev T polynomials as the basis functions.\n\n\
212-independent specify name of independent data column to use.\n\
213-dependent specify names of dependent data columns to use, using wildcards,\n\
214 separated by commas.\n\
215-sigmaIndependent specify name of independent sigma values to use\n\
216-sigmaDependent specify names of dependent sigma values to use by specifying a printf-style control\n\
217 string to generate the names from the independent variable names (e.g., %sSigma)\n\
218-terms number of terms desired in fit.\n\
219-symmetry symmetry of desired fit about x_offset.\n\
220-orders orders (P[i]) to use in fitting.\n\
221-reviseOrders the orders used in the fit are modified from the specified ones\n\
222 in order eliminate poorly-determined coefficients, based on fitting\n\
223 of the first data page.\n";
224static char *additional_help2 =
"-chebyshev use Chebyshev T polynomials (xOffset is set automatically).\n\
225 Giving the `convert' option causes the fit to be written out in\n\
226 terms of ordinary polynomials.\n\
227-xOffset desired value of x to fit about.\n\
228-xFactor desired factor to multiply x values by before fitting.\n\
229-sigmas specify absolute or fractional sigma for all points.\n\
230-minimumSigma specify minimum sigma value. If the value is less than this\n\
231 it is replaced by this value.\n\
232-modifySigmas modify the y sigmas using the x sigmas and an initial fit.\n\
233-generateSigmas generate y sigmas from the rms deviation from an initial fit.\n\
234 optionally keep the sigmas from the data if larger/smaller than rms\n\
236-sparse specify integer interval at which to sample data.\n\
237-range specify range of independent variable over which to perform fit and evaluation.\n\
238 If 'fitOnly' is given, then fit is compared to data over the original range.\n\
239-normalize normalize so that specified term is unity.\n\
240-evaluate specify evaluation of fit over a selected range of\n\
241 equispaced points.\n\
242-infoFile specify name of optional information file containing coefficients and fit statistics.\n\
243-copyParameters specify that parameters from input should be copied to output.\n\
244-verbose generates extra output that may be useful.\n\n";
247#define EVEN_SYMMETRY 1
248#define ODD_SYMMETRY 2
249#define N_SYMMETRY_OPTIONS 3
250char *symmetry_options[N_SYMMETRY_OPTIONS] = {
"none",
"even",
"odd"};
252#define ABSOLUTE_SIGMAS 0
253#define FRACTIONAL_SIGMAS 1
254#define N_SIGMAS_OPTIONS 2
255char *sigmas_options[N_SIGMAS_OPTIONS] = {
"absolute",
"fractional"};
257#define FLGS_GENERATESIGMAS 1
258#define FLGS_KEEPLARGEST 2
259#define FLGS_KEEPSMALLEST 4
261#define REVPOW_ACTIVE 0x0001
262#define REVPOW_VERBOSE 0x0002
264static long *iIntercept = NULL, *iInterceptO = NULL, *iInterceptSigma = NULL, *iInterceptSigmaO = NULL;
265static long *iSlope = NULL, *iSlopeO = NULL, *iSlopeSigma = NULL, *iSlopeSigmaO = NULL;
266static long *iCurvature = NULL, *iCurvatureO = NULL, *iCurvatureSigma = NULL, *iCurvatureSigmaO = NULL;
267static long iOffset = -1, iOffsetO = -1, iFactor = -1, iFactorO = -1;
268static long *iChiSq = NULL, *iChiSqO = NULL, *iRmsResidual = NULL, *iRmsResidualO = NULL, *iSigLevel = NULL, *iSigLevelO = NULL;
269static long *iFitIsValid = NULL, *iFitIsValidO = NULL, *iFitLabel = NULL, *iFitLabelO = NULL, iTerms = -1, iTermsO = -1;
271static long ix = -1, ixSigma = -1;
272static long *iy = NULL, *iySigma = NULL;
273static long *iFit = NULL, *iResidual = NULL;
275static long iOrder = -1, *iCoefficient = NULL, *iCoefficientSigma = NULL, *iCoefficientUnits = NULL;
277static char *xSymbol, **ySymbols;
279#define EVAL_BEGIN_GIVEN 0x0001U
280#define EVAL_END_GIVEN 0x0002U
281#define EVAL_NUMBER_GIVEN 0x0004U
283#define MAX_Y_SIGMA_NAME_SIZE 1024
295void makeEvaluationTable(
EVAL_PARAMETERS *evalParameters,
double *x, int64_t points,
double *coef, int32_t *order,
296 long terms,
char *xName,
char **yName,
long yNames,
long iYName);
298static double (*basis_fn)(
double xa,
long ordera);
299static double (*basis_dfn)(
double xa,
long ordera);
301int main(
int argc,
char **argv) {
302 double **y = NULL, **sy = NULL, **diff = NULL;
303 double *x = NULL, *sx = NULL;
304 double xOffset, xScaleFactor;
305 double *xOrig = NULL, **yOrig = NULL, *sxOrig = NULL, **syOrig = NULL, **sy0 = NULL;
306 long terms, normTerm, ip, ySigmasValid;
307 int64_t i, j, points, pointsOrig;
308 long symmetry, chebyshev, termsGiven;
309 double sigmas, minimumSigma;
310 long sigmasMode, sparseInterval;
311 double **coef = NULL, **coefSigma = NULL;
312 double *chi = NULL, xLow, xHigh, *rmsResidual = NULL;
313 char *xName = NULL, *yName = NULL, **yNames = NULL, *xSigmaName = NULL;
314 char **ySigmaNames = NULL, *ySigmaControlString = NULL;
315 char *input = NULL, *output = NULL;
317 long *isFit = NULL, iArg, modifySigmas, termIndex;
318 long generateSigmas, verbose, ignoreSigmas;
319 long outputInitialized, copyParameters = 0;
320 int32_t *order = NULL;
322 double xMin, xMax, revpowThreshold;
323 double rms_average(
double *d_x, int64_t d_n);
324 char *infoFile = NULL;
325 char *fitLabelFormat =
"%g";
326 static char fitLabelBuffer[SDDS_MAXLINE];
327 unsigned long pipeFlags, reviseOrders;
329 long rangeFitOnly = 0;
332 long cloDependentIndex = -1, numDependentItems;
336 argc =
scanargs(&s_arg, argc, argv);
337 if (argc < 2 || argc > (3 + N_OPTIONS)) {
338 fprintf(stderr,
"usage: %s\n", USAGE);
339 fprintf(stderr,
"%s%s", additional_help, additional_help2);
343 input = output = NULL;
344 xName = yName = xSigmaName = ySigmaControlString = NULL;
345 yNames = ySigmaNames = NULL;
346 numDependentItems = 0;
347 modifySigmas = reviseOrders = chebyshev = 0;
349 symmetry = NO_SYMMETRY;
357 verbose = ignoreSigmas = 0;
364 evalParameters.file = NULL;
368 for (iArg = 1; iArg < argc; iArg++) {
369 if (s_arg[iArg].arg_type == OPTION) {
370 switch (
match_string(s_arg[iArg].list[0], option, N_OPTIONS, 0)) {
371 case CLO_MODIFYSIGMAS:
376 SDDS_Bomb(
"give -order or -terms, not both");
377 if (s_arg[iArg].n_items < 2)
379 order =
tmalloc(
sizeof(*order) * (terms = s_arg[iArg].n_items - 1));
380 for (i = 1; i < s_arg[iArg].n_items; i++) {
381 if (sscanf(s_arg[iArg].list[i],
"%" SCNd32, order + i - 1) != 1)
382 SDDS_Bomb(
"unable to scan order from -orders list");
387 if ((s_arg[iArg].n_items != 3 && s_arg[iArg].n_items != 4) || 1 != sscanf(s_arg[iArg].list[1],
"%lf", &xMin) || 1 != sscanf(s_arg[iArg].list[2],
"%lf", &xMax) || xMin >= xMax)
389 if (s_arg[iArg].n_items == 4) {
390 if (strncmp(
str_tolower(s_arg[iArg].list[3]),
"fitonly", strlen(s_arg[iArg].list[3])) == 0) {
396 case CLO_GENERATESIGMAS:
397 generateSigmas = FLGS_GENERATESIGMAS;
398 if (s_arg[iArg].n_items > 1) {
399 if (s_arg[iArg].n_items != 2)
400 SDDS_Bomb(
"incorrect -generateSigmas synax");
401 if (strncmp(s_arg[iArg].list[1],
"keepsmallest", strlen(s_arg[iArg].list[1])) == 0)
402 generateSigmas |= FLGS_KEEPSMALLEST;
403 if (strncmp(s_arg[iArg].list[1],
"keeplargest", strlen(s_arg[iArg].list[1])) == 0)
404 generateSigmas |= FLGS_KEEPLARGEST;
405 if ((generateSigmas & FLGS_KEEPSMALLEST) && (generateSigmas & FLGS_KEEPLARGEST))
406 SDDS_Bomb(
"ambiguous -generateSigmas synax");
411 SDDS_Bomb(
"give -order or -terms, not both");
412 if (s_arg[iArg].n_items != 2 || sscanf(s_arg[iArg].list[1],
"%ld", &terms) != 1)
417 if (s_arg[iArg].n_items != 2 || sscanf(s_arg[iArg].list[1],
"%lf", &xOffset) != 1)
421 if (s_arg[iArg].n_items == 2) {
422 if ((symmetry =
match_string(s_arg[iArg].list[1], symmetry_options, N_SYMMETRY_OPTIONS, 0)) < 0)
423 SDDS_Bomb(
"unknown option used with -symmetry");
428 if (s_arg[iArg].n_items != 3)
430 if (sscanf(s_arg[iArg].list[1],
"%lf", &sigmas) != 1)
431 SDDS_Bomb(
"couldn't scan value for -sigmas");
432 if ((sigmasMode =
match_string(s_arg[iArg].list[2], sigmas_options, N_SIGMAS_OPTIONS, 0)) < 0)
436 if (s_arg[iArg].n_items != 2)
437 SDDS_Bomb(
"incorrect -minimumSigma syntax");
438 if (sscanf(s_arg[iArg].list[1],
"%lf", &minimumSigma) != 1)
439 SDDS_Bomb(
"couldn't scan value for -minimumSigma");
442 if (s_arg[iArg].n_items != 2)
444 if (sscanf(s_arg[iArg].list[1],
"%ld", &sparseInterval) != 1)
445 SDDS_Bomb(
"couldn't scan value for -sparse");
446 if (sparseInterval < 1)
454 if (s_arg[iArg].n_items > 2 ||
455 (s_arg[iArg].n_items == 2 && sscanf(s_arg[iArg].list[1],
"%ld", &normTerm) != 1) ||
459 case CLO_REVISEORDERS:
460 revpowThreshold = 0.1;
461 s_arg[iArg].n_items -= 1;
462 if (!
scanItemList(&reviseOrders, s_arg[iArg].list + 1, &s_arg[iArg].n_items, 0,
464 "verbose", -1, NULL, 1, REVPOW_VERBOSE, NULL))
465 SDDS_Bomb(
"invalid -reviseOrders syntax");
466 reviseOrders |= REVPOW_ACTIVE;
467 revpowThreshold = fabs(revpowThreshold);
470 if (s_arg[iArg].n_items > 2 ||
471 (s_arg[iArg].n_items == 2 && strncmp(s_arg[iArg].list[1],
"convert", strlen(s_arg[iArg].list[1])) != 0))
473 chebyshev = s_arg[iArg].n_items;
478 if (s_arg[iArg].n_items != 2 ||
479 sscanf(s_arg[iArg].list[1],
"%lf", &xScaleFactor) != 1 || xScaleFactor == 0)
482 case CLO_INDEPENDENT:
483 if (s_arg[iArg].n_items != 2)
484 SDDS_Bomb(
"invalid -independent syntax");
485 xName = s_arg[iArg].list[1];
488 numDependentItems = s_arg[iArg].n_items - 1;
489 cloDependentIndex = iArg;
490 if (numDependentItems < 1)
493 case CLO_SIGMAINDEPENDENT:
494 if (s_arg[iArg].n_items != 2)
495 SDDS_Bomb(
"invalid -sigmaIndependent syntax");
496 xSigmaName = s_arg[iArg].list[1];
498 case CLO_SIGMADEPENDENT:
499 if (s_arg[iArg].n_items != 2)
500 SDDS_Bomb(
"invalid -sigmaDependent syntax");
501 ySigmaControlString = s_arg[iArg].list[1];
503 case CLO_FITLABELFORMAT:
504 if (s_arg[iArg].n_items != 2)
505 SDDS_Bomb(
"invalid -fitLabelFormat syntax");
506 fitLabelFormat = s_arg[iArg].list[1];
509 if (!
processPipeOption(s_arg[iArg].list + 1, s_arg[iArg].n_items - 1, &pipeFlags))
513 if (s_arg[iArg].n_items != 2)
515 infoFile = s_arg[iArg].list[1];
518 if (s_arg[iArg].n_items < 2)
520 evalParameters.file = s_arg[iArg].list[1];
521 s_arg[iArg].n_items -= 2;
522 s_arg[iArg].list += 2;
523 if (!
scanItemList(&evalParameters.flags, s_arg[iArg].list, &s_arg[iArg].n_items, 0,
524 "begin",
SDDS_DOUBLE, &evalParameters.begin, 1, EVAL_BEGIN_GIVEN,
525 "end",
SDDS_DOUBLE, &evalParameters.end, 1, EVAL_END_GIVEN,
526 "number",
SDDS_LONG64, &evalParameters.number, 1, EVAL_NUMBER_GIVEN, NULL))
529 case CLO_COPYPARAMETERS:
533 bomb(
"unknown switch", USAGE);
538 input = s_arg[iArg].list[0];
539 else if (output == NULL)
540 output = s_arg[iArg].list[0];
548 if (symmetry && order)
549 SDDS_Bomb(
"can't specify both -symmetry and -orders");
550 if (!xName || !numDependentItems)
551 SDDS_Bomb(
"you must specify a column name for x and y");
552 if (modifySigmas && !xSigmaName)
553 SDDS_Bomb(
"you must specify x sigmas with -modifySigmas");
554 if (generateSigmas) {
556 SDDS_Bomb(
"you can't specify both -generateSigmas and -modifySigmas");
558 if (ySigmaControlString) {
559 if (sigmasMode != -1)
560 SDDS_Bomb(
"you can't specify both -sigmas and a y sigma name");
563 if (sigmasMode != -1 || generateSigmas || ySigmaControlString || modifySigmas)
566 if (normTerm >= 0 && normTerm >= terms)
567 SDDS_Bomb(
"can't normalize to that term--not that many terms");
568 if (reviseOrders && !(sigmasMode != -1 || generateSigmas || ySigmaNames))
569 SDDS_Bomb(
"can't use -reviseOrders unless a y sigma or -generateSigmas is given");
571 if (symmetry == EVEN_SYMMETRY) {
572 order =
tmalloc(
sizeof(*order) * terms);
573 for (i = 0; i < terms; i++)
575 }
else if (symmetry == ODD_SYMMETRY) {
576 order =
tmalloc(
sizeof(*order) * terms);
577 for (i = 0; i < terms; i++)
578 order[i] = 2 * i + 1;
580 order =
tmalloc(
sizeof(*order) * terms);
581 for (i = 0; i < terms; i++)
587 outputInitialized = 0;
588 yNames = ResolveColumnNames(&SDDSin, s_arg[cloDependentIndex].list + 1, numDependentItems, &numYNames);
589 if (ySigmaControlString != NULL)
590 ySigmaNames = GenerateYSigmaNames(ySigmaControlString, yNames, numYNames);
592 checkInputFile(&SDDSin, xName, yNames, xSigmaName, ySigmaNames, numYNames);
593 sy0 =
tmalloc(
sizeof(
double *) * numYNames);
594 y =
tmalloc(
sizeof(
double *) * numYNames);
595 sy =
tmalloc(
sizeof(
double *) * numYNames);
596 isFit =
tmalloc(
sizeof(
long) * numYNames);
597 chi =
tmalloc(
sizeof(
double) * numYNames);
598 coef =
tmalloc(
sizeof(
double *) * numYNames);
599 coefSigma =
tmalloc(
sizeof(
double *) * numYNames);
600 for (colIndex = 0; colIndex < numYNames; colIndex++) {
601 coef[colIndex] =
tmalloc(
sizeof(
double) * terms);
602 coefSigma[colIndex] =
tmalloc(
sizeof(
double) * terms);
604 iCoefficient =
tmalloc(
sizeof(
long) * numYNames);
605 iCoefficientSigma =
tmalloc(
sizeof(
long) * numYNames);
606 iCoefficientUnits =
tmalloc(
sizeof(
long) * numYNames);
614 fprintf(stderr,
"error: unable to read column %s\n", xName);
617 for (i = 0; i < numYNames; i++) {
619 fprintf(stderr,
"error: unable to read column %s\n", yNames[i]);
625 fprintf(stderr,
"error: unable to read column %s\n", xSigmaName);
628 for (colIndex = 0; colIndex < numYNames; colIndex++)
629 sy0[colIndex] =
tmalloc(
sizeof(
double) * points);
631 for (i = 0; i < numYNames; i++) {
633 fprintf(stderr,
"error: unable to read column %s\n", ySigmaNames[i]);
639 if (minimumSigma > 0) {
641 for (i = 0; i < numYNames; i++) {
642 for (j = 0; j < points; j++)
643 if (sy0[i][j] < minimumSigma)
644 sy0[i][j] = minimumSigma;
648 if (xMin != xMax || sparseInterval != 1) {
649 xOrig =
tmalloc(
sizeof(*xOrig) * points);
650 yOrig =
tmalloc(
sizeof(*yOrig) * numYNames);
651 for (colIndex = 0; colIndex < numYNames; colIndex++)
652 yOrig[colIndex] =
tmalloc(
sizeof(
double) * points);
654 sxOrig =
tmalloc(
sizeof(*sxOrig) * points);
656 syOrig =
tmalloc(
sizeof(*syOrig) * numYNames);
657 for (colIndex = 0; colIndex < numYNames; colIndex++)
658 syOrig[colIndex] =
tmalloc(
sizeof(
double) * points);
661 for (i = j = 0; i < points; i++) {
665 for (colIndex = 0; colIndex < numYNames; colIndex++) {
666 yOrig[colIndex][i] = y[colIndex][i];
668 syOrig[colIndex][i] = sy0[colIndex][i];
672 for (i = j = 0; i < points; i++) {
673 if (xOrig[i] <= xMax && xOrig[i] >= xMin) {
675 for (colIndex = 0; colIndex < numYNames; colIndex++) {
676 y[colIndex][j] = yOrig[colIndex][i];
678 sy0[colIndex][j] = syOrig[colIndex][i];
687 if (sparseInterval != 1) {
688 for (i = j = 0; i < points; i++) {
689 if (i % sparseInterval == 0) {
691 for (colIndex = 0; colIndex < numYNames; colIndex++) {
692 y[colIndex][j] = y[colIndex][i];
694 sy0[colIndex][j] = sy0[colIndex][i];
713 if (sigmasMode == ABSOLUTE_SIGMAS) {
714 for (colIndex = 0; colIndex < numYNames; colIndex++) {
715 for (i = 0; i < points; i++)
716 sy0[colIndex][i] = sigmas;
717 if (sy0[colIndex] != syOrig[colIndex])
718 for (i = 0; i < pointsOrig; i++)
719 syOrig[colIndex][i] = sigmas;
721 }
else if (sigmasMode == FRACTIONAL_SIGMAS) {
722 for (colIndex = 0; colIndex < numYNames; colIndex++) {
723 for (i = 0; i < points; i++)
724 sy0[colIndex][i] = sigmas * fabs(y[colIndex][i]);
725 if (sy0[colIndex] != syOrig[colIndex])
726 for (i = 0; i < pointsOrig; i++)
727 syOrig[colIndex][i] = fabs(yOrig[colIndex][i]) * sigmas;
731 for (i = 0; i < numYNames; i++) {
732 if (minimumSigma > 0) {
734 for (j = 0; j < points; j++)
735 if (sy0[i][j] < minimumSigma)
736 sy0[i][j] = minimumSigma;
740 if (!ySigmasValid || generateSigmas)
741 for (colIndex = 0; colIndex < numYNames; colIndex++) {
742 for (i = 0; i < points; i++)
743 sy0[colIndex][i] = 1;
746 for (i = 0; i < points; i++)
747 for (colIndex = 0; colIndex < numYNames; colIndex++) {
748 if (sy0[colIndex][i] == 0)
749 SDDS_Bomb(
"y sigma = 0 for one or more points.");
752 diff =
tmalloc(
sizeof(*diff) * numYNames);
753 sy =
tmalloc(
sizeof(*sy) * numYNames);
754 for (colIndex = 0; colIndex < numYNames; colIndex++) {
755 diff[colIndex] =
tmalloc(
sizeof(
double) * points);
756 sy[colIndex] =
tmalloc(
sizeof(
double) * points);
759 for (i = 0; i < points; i++) {
760 for (colIndex = 0; colIndex < numYNames; colIndex++)
761 sy[colIndex][i] = sy0[colIndex][i];
767 xOffset = (xHigh + xLow) / 2;
772 if (generateSigmas || modifySigmas) {
774 for (colIndex = 0; colIndex < numYNames; colIndex++) {
775 isFit[colIndex] =
lsfg(x, y[colIndex], sy[colIndex], points, terms, order, coef[colIndex], coefSigma[colIndex], &chi[colIndex], diff[colIndex], basis_fn);
776 if (!isFit[colIndex]) {
777 fprintf(stderr,
"Column %s: ", yNames[colIndex]);
781 fprintf(stderr,
"Column %s: ", yNames[colIndex]);
782 fputs(
"initial_fit:", stderr);
783 print_coefs(stderr, xOffset, xScaleFactor, chebyshev, coef[colIndex], NULL, order, terms, chi[colIndex], normTerm,
"");
784 fprintf(stderr,
"unweighted rms deviation from fit: %21.15le\n", rms_average(diff[colIndex], points));
788 for (i = 0; i < points; i++)
789 sy[colIndex][i] = fabs(
eval_sum(basis_dfn, coef[colIndex], order, terms, x[i]) * sx[i]);
791 for (i = 0; i < points; i++) {
792 sy[colIndex][i] = sqrt(sqr(sy0[colIndex][i]) + sqr(
eval_sum(basis_dfn, coef[colIndex], order, terms, x[i]) * sx[i]));
795 if (generateSigmas) {
797 for (i = sigma = 0; i < points; i++) {
798 sigma += sqr(diff[colIndex][i]);
800 sigma = sqrt(sigma / (points - terms));
801 for (i = 0; i < points; i++) {
802 if (generateSigmas & FLGS_KEEPSMALLEST) {
803 if (sigma < sy[colIndex][i])
804 sy[colIndex][i] = sigma;
805 }
else if (generateSigmas & FLGS_KEEPLARGEST) {
806 if (sigma > sy[colIndex][i])
807 sy[colIndex][i] = sigma;
809 sy[colIndex][i] = sigma;
812 for (i = 0; i < pointsOrig; i++) {
813 if (generateSigmas & FLGS_KEEPSMALLEST) {
814 if (sigma < sy0[colIndex][i])
815 sy0[colIndex][i] = sigma;
816 }
else if (generateSigmas & FLGS_KEEPLARGEST) {
817 if (sigma > sy0[colIndex][i])
818 sy0[colIndex][i] = sigma;
820 sy0[colIndex][i] = sigma;
827 if (reviseOrders & REVPOW_ACTIVE) {
829 long bestTerms, newBest;
833 bestOrder =
tmalloc(
sizeof(*bestOrder) * bestTerms);
834 for (ip = 0; ip < terms; ip++)
835 bestOrder[ip] = order[ip];
837 for (colIndex = 0; colIndex < numYNames; colIndex++) {
838 isFit[colIndex] =
lsfg(x, y[colIndex], sy[colIndex], points, bestTerms, bestOrder, coef[colIndex], coefSigma[colIndex], &bestChi, diff[colIndex], basis_fn);
839 if (!isFit[colIndex]) {
840 fprintf(stderr,
"Column %s: ", yNames[colIndex]);
842 if (reviseOrders & REVPOW_VERBOSE) {
843 fprintf(stderr,
"Column %s: ", yNames[colIndex]);
844 fputs(
"fit to revise orders:", stderr);
845 print_coefs(stderr, xOffset, xScaleFactor, chebyshev, coef[colIndex], (ySigmasValid ? coefSigma[colIndex] : NULL), bestOrder, bestTerms, bestChi, normTerm,
"");
846 fprintf(stderr,
"unweighted rms deviation from fit: %21.15le\n", rms_average(diff[colIndex], points));
852 terms = bestTerms - 1;
853 for (ip = bestTerms - 1; ip >= 0; ip--) {
854 for (i = j = 0; i < bestTerms; i++)
856 order[j++] = bestOrder[i];
857 isFit[colIndex] =
lsfg(x, y[colIndex], sy[colIndex], points, terms, order, coef[colIndex], coefSigma[colIndex], &chi[colIndex], diff[colIndex], basis_fn);
858 if (!isFit[colIndex]) {
859 fprintf(stderr,
"Column %s: ", yNames[colIndex]);
862 if (reviseOrders & REVPOW_VERBOSE) {
863 fprintf(stderr,
"Column %s: ", yNames[colIndex]);
864 fputs(
"new trial fit:", stderr);
865 print_coefs(stderr, xOffset, xScaleFactor, chebyshev, coef[colIndex], (ySigmasValid ? coefSigma[colIndex] : NULL), order, terms, chi[colIndex], normTerm,
"");
866 fprintf(stderr,
"unweighted rms deviation from fit: %21.15le\n", rms_average(diff[colIndex], points));
868 if (chi[colIndex] - bestChi < revpowThreshold) {
869 bestChi = chi[colIndex];
872 for (i = 0; i < terms; i++)
873 bestOrder[i] = order[i];
874 if (reviseOrders & REVPOW_VERBOSE) {
875 fputs(
"new best fit:", stderr);
876 print_coefs(stderr, xOffset, xScaleFactor, chebyshev, coef[colIndex], (ySigmasValid ? coefSigma[colIndex] : NULL), bestOrder, bestTerms, bestChi, normTerm,
"");
877 fprintf(stderr,
"unweighted rms deviation from fit: %21.15le\n", rms_average(diff[colIndex], points));
886 for (ip = 0; ip < terms; ip++)
887 order[ip] = bestOrder[ip];
893 if (!outputInitialized) {
894 initializeOutputFile(&SDDSout, &SDDSoutInfo, output, infoFile, &SDDSin, input, xName, yNames, xSigmaName, ySigmaNames, ySigmasValid, order, terms, chebyshev, numYNames, copyParameters);
896 outputInitialized = 1;
898 if (evalParameters.file)
899 setupEvaluationFile(&evalParameters, xName, yNames, numYNames, &SDDSin);
901 rmsResidual =
tmalloc(
sizeof(
double) * numYNames);
902 for (colIndex = 0; colIndex < numYNames; colIndex++) {
903 isFit[colIndex] =
lsfg(x, y[colIndex], sy[colIndex], points, terms, order, coef[colIndex], coefSigma[colIndex], &chi[colIndex], diff[colIndex], basis_fn);
904 if (isFit[colIndex]) {
905 rmsResidual[colIndex] = rms_average(diff[colIndex], points);
907 fprintf(stderr,
"Column: %s\n", yNames[colIndex]);
908 print_coefs(stderr, xOffset, xScaleFactor, chebyshev, coef[colIndex], (ySigmasValid ? coefSigma[colIndex] : NULL), order, terms, chi[colIndex], normTerm,
"");
909 fprintf(stderr,
"unweighted rms deviation from fit: %21.15le\n", rmsResidual[colIndex]);
912 fprintf(stderr,
"fit failed for %s.\n", yNames[colIndex]);
914 if (evalParameters.file)
915 makeEvaluationTable(&evalParameters, x, points, coef[colIndex], order, terms, xName, yNames, numYNames, colIndex);
918 if (outputInitialized) {
919 if (!
SDDS_StartPage(&SDDSout, rangeFitOnly ? pointsOrig : points) ||
922 if (copyParameters) {
928 if (!
SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_INDEX, rangeFitOnly ? xOrig : x, rangeFitOnly ? pointsOrig : points, ix) ||
931 for (colIndex = 0; colIndex < numYNames; colIndex++) {
933 double *residual, rmsResidual0;
934 compareOriginalToFit(xOrig, yOrig[colIndex], &residual, pointsOrig, &rmsResidual0, coef[colIndex], order, terms);
938 for (i = 0; i < pointsOrig; i++)
939 residual[i] = yOrig[colIndex][i] - residual[i];
944 for (i = 0; i < points; i++)
945 diff[colIndex][i] = -diff[colIndex][i];
949 for (i = 0; i < points; i++)
950 diff[colIndex][i] = y[colIndex][i] - diff[colIndex][i];
956 !
SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_INDEX, rangeFitOnly ? sxOrig : sx, rangeFitOnly ? pointsOrig : points, ixSigma))
958 for (colIndex = 0; colIndex < numYNames; colIndex++) {
959 if (ySigmasValid && iySigma[colIndex] != -1 &&
960 !
SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_INDEX, rangeFitOnly ? syOrig[colIndex] : sy[colIndex], rangeFitOnly ? pointsOrig : points, iySigma[colIndex]))
964 termIndex = coefficient_index(order, terms, 0);
965 if (iIntercept[colIndex] != -1 &&
966 !
SDDS_SetParameters(&SDDSoutInfo, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iIntercept[colIndex], coef[colIndex][termIndex], -1))
968 if (iInterceptSigma[colIndex] != -1 &&
969 !
SDDS_SetParameters(&SDDSoutInfo, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iInterceptSigma[colIndex], coefSigma[colIndex][termIndex], -1))
972 termIndex = coefficient_index(order, terms, 1);
973 if (iSlope[colIndex] != -1 &&
974 !
SDDS_SetParameters(&SDDSoutInfo, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iSlope[colIndex], coef[colIndex][termIndex], -1))
976 if (iSlopeSigma[colIndex] != -1 &&
977 !
SDDS_SetParameters(&SDDSoutInfo, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iSlopeSigma[colIndex], coefSigma[colIndex][termIndex], -1))
980 termIndex = coefficient_index(order, terms, 2);
981 if (iCurvature[colIndex] != -1 &&
982 !
SDDS_SetParameters(&SDDSoutInfo, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iCurvature[colIndex], coef[colIndex][termIndex], -1))
984 if (iCurvatureSigma[colIndex] != -1 &&
985 !
SDDS_SetParameters(&SDDSoutInfo, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iCurvatureSigma[colIndex], coefSigma[colIndex][termIndex], -1))
987 if (iFitLabel[colIndex] != -1) {
988 makeFitLabel(fitLabelBuffer, SDDS_MAXLINE, fitLabelFormat, coef[colIndex], order, terms, colIndex);
989 if (!
SDDS_SetParameters(&SDDSoutInfo, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iFitLabel[colIndex], fitLabelBuffer, -1))
997 iRmsResidual[colIndex], rmsResidual[colIndex], iChiSq[colIndex], chi[colIndex],
998 iTerms, terms, iSigLevel[colIndex],
ChiSqrSigLevel(chi[colIndex], points - terms),
999 iOffset, xOffset, iFactor, xScaleFactor, iFitIsValid[colIndex], isFit[colIndex] ?
'y' :
'n', -1))
1003 termIndex = coefficient_index(order, terms, 0);
1004 if (iInterceptO[colIndex] != -1 &&
1005 !
SDDS_SetParameters(&SDDSout, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iInterceptO[colIndex], coef[colIndex][termIndex], -1))
1007 if (iInterceptSigmaO[colIndex] != -1 &&
1008 !
SDDS_SetParameters(&SDDSout, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iInterceptSigmaO[colIndex], coefSigma[colIndex][termIndex], -1))
1011 termIndex = coefficient_index(order, terms, 1);
1012 if (iSlopeO[colIndex] != -1 &&
1013 !
SDDS_SetParameters(&SDDSout, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iSlopeO[colIndex], coef[colIndex][termIndex], -1))
1015 if (iSlopeSigmaO[colIndex] != -1 &&
1016 !
SDDS_SetParameters(&SDDSout, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iSlopeSigmaO[colIndex], coefSigma[colIndex][termIndex], -1))
1019 termIndex = coefficient_index(order, terms, 2);
1020 if (iCurvatureO[colIndex] != -1 &&
1021 !
SDDS_SetParameters(&SDDSout, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iCurvatureO[colIndex], coef[colIndex][termIndex], -1))
1023 if (iCurvatureSigmaO[colIndex] != -1 &&
1024 !
SDDS_SetParameters(&SDDSout, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iCurvatureSigmaO[colIndex], coefSigma[colIndex][termIndex], -1))
1026 if (iFitLabelO[colIndex] != -1) {
1027 makeFitLabel(fitLabelBuffer, SDDS_MAXLINE, fitLabelFormat, coef[colIndex], order, terms, colIndex);
1028 if (!
SDDS_SetParameters(&SDDSout, SDDS_SET_BY_INDEX | SDDS_PASS_BY_VALUE, iFitLabelO[colIndex], fitLabelBuffer, -1))
1032 iRmsResidualO[colIndex], rmsResidual[colIndex], iChiSqO[colIndex], chi[colIndex],
1033 iTermsO, terms, iSigLevelO[colIndex],
ChiSqrSigLevel(chi[colIndex], points - terms),
1034 iOffsetO, xOffset, iFactorO, xScaleFactor, iFitIsValidO[colIndex], isFit[colIndex] ?
'y' :
'n', -1))
1046 for (colIndex = 0; colIndex < numYNames; colIndex++) {
1047 free(diff[colIndex]);
1049 if (yOrig[colIndex] != y[colIndex])
1050 free(yOrig[colIndex]);
1051 if (syOrig && sy0 && syOrig[colIndex] != sy0[colIndex])
1052 free(syOrig[colIndex]);
1054 if (sy0 && sy0[colIndex])
1055 free(sy0[colIndex]);
1058 return (EXIT_SUCCESS);
1061void print_coefs(FILE * fpo,
double xOffset,
double xScaleFactor,
long chebyshev,
double *coef,
double *coefSigma,
1062 int32_t *order,
long terms,
double chi,
long normTerm,
char *prepend) {
1066 fprintf(fpo,
"%s%ld-term Chebyshev T polynomial least-squares fit about x=%21.15le, scaled by %21.15le:\n", prepend, terms, xOffset, xScaleFactor);
1068 fprintf(fpo,
"%s%ld-term polynomial least-squares fit about x=%21.15le:\n", prepend, terms, xOffset);
1069 if (normTerm >= 0 && terms > normTerm) {
1070 if (coef[normTerm] != 0)
1071 fprintf(fpo,
"%s coefficients are normalized with factor %21.15le to make a[%ld]==1\n", prepend, coef[normTerm], (order ? order[normTerm] : normTerm));
1073 fprintf(fpo,
"%s can't normalize coefficients as requested: a[%ld]==0\n", prepend, (order ? order[normTerm] : normTerm));
1079 for (i = 0; i < terms; i++) {
1080 fprintf(fpo,
"%sa[%ld] = %21.15le ", prepend, (order ? order[i] : i), (normTerm < 0 ? coef[i] : coef[i] / coef[normTerm]));
1082 fprintf(fpo,
"+/- %21.15le\n", (normTerm < 0 ? coefSigma[i] : coefSigma[i] / fabs(coef[normTerm])));
1087 fprintf(fpo,
"%sreduced chi-squared = %21.15le\n", prepend, chi);
1090void RemoveElementFromStringArray(
char **array,
long index,
long length) {
1093 for (lh = index; lh < length - 1; lh++)
1094 array[lh] = array[lh + 1];
1097char **RemoveNonNumericColumnsFromNameArray(
SDDS_DATASET * SDDSin,
char **columns, int32_t *numColumns) {
1098 long i, numNumericColumns = *numColumns;
1100 for (i = 0; i < *numColumns; i++) {
1102 printf(
"Removing %s because not a numeric type.\n", columns[i]);
1103 RemoveElementFromStringArray(columns, i, *numColumns);
1104 numNumericColumns--;
1108 *numColumns = numNumericColumns;
1112char **ResolveColumnNames(
SDDS_DATASET * SDDSin,
char **wildcardList,
long length, int32_t *numYNames) {
1118 for (i = 0; i < length; i++) {
1123 bomb(
"Error matching columns in ResolveColumnNames: No matches.", NULL);
1125 result = RemoveNonNumericColumnsFromNameArray(SDDSin, result, numYNames);
1129char **GenerateYSigmaNames(
char *controlString,
char **yNames,
long numYNames) {
1131 char **result, sigmaName[MAX_Y_SIGMA_NAME_SIZE];
1133 result =
tmalloc(
sizeof(
char *) * numYNames);
1134 for (i = 0; i < numYNames; i++) {
1135 sprintf(sigmaName, controlString, yNames[i]);
1136 nameLength = strlen(sigmaName);
1137 result[i] =
tmalloc(
sizeof(
char) * (nameLength + 1));
1138 strcpy(result[i], sigmaName);
1143void makeFitLabel(
char *buffer,
long bufsize,
char *fitLabelFormat,
double *coef, int32_t *order,
long terms,
long colIndex) {
1145 static char buffer1[SDDS_MAXLINE], buffer2[SDDS_MAXLINE];
1147 sprintf(buffer,
"%s = ", ySymbols[colIndex]);
1148 for (i = 0; i < terms; i++) {
1150 sprintf(buffer1, fitLabelFormat, coef[i]);
1153 strcpy(buffer1,
" +");
1154 sprintf(buffer1 + 2, fitLabelFormat, coef[i]);
1156 sprintf(buffer1, fitLabelFormat, coef[i]);
1157 strcat(buffer1,
"*");
1158 strcat(buffer1, xSymbol);
1160 sprintf(buffer2,
"$a%" PRId32
"$n", order[i]);
1161 strcat(buffer1, buffer2);
1164 if ((
long)(strlen(buffer) + strlen(buffer1)) > (
long)(0.95 * bufsize)) {
1165 fprintf(stderr,
"buffer overflow making fit label!\n");
1168 strcat(buffer, buffer1);
1172double rms_average(
double *x, int64_t n) {
1176 for (i = sum2 = 0; i < n; i++)
1179 return (sqrt(sum2 / n));
1182long coefficient_index(int32_t * order,
long terms,
long order_of_interest) {
1184 for (i = 0; i < terms; i++)
1185 if (order[i] == order_of_interest)
1190void checkInputFile(
SDDS_DATASET * SDDSin,
char *xName,
char **yNames,
char *xSigmaName,
char **ySigmaNames,
long numYNames) {
1195 SDDS_Bomb(
"x column doesn't exist or is nonnumeric");
1201 if (xSigmaName && !(ptr =
SDDS_FindColumn(SDDSin, FIND_NUMERIC_TYPE, xSigmaName, NULL)))
1202 SDDS_Bomb(
"x sigma column doesn't exist or is nonnumeric");
1207 for (i = 0; i < numYNames; i++) {
1209 if (!(ptr =
SDDS_FindColumn(SDDSin, FIND_NUMERIC_TYPE, ySigmaNames[i], NULL)))
1210 SDDS_Bomb(
"y sigma column doesn't exist or is nonnumeric");
1218 SDDS_DATASET *SDDSin,
char *input,
char *xName,
char **yNames,
char *xSigmaName,
1219 char **ySigmaNames,
long sigmasValid, int32_t *order,
long terms,
long isChebyshev,
1220 long numCols,
long copyParameters) {
1221 char buffer[SDDS_MAXLINE], buffer1[SDDS_MAXLINE], buffer2[SDDS_MAXLINE], buffer3[SDDS_MAXLINE];
1222 char *xUnits, *yUnits, ***coefUnits;
1227 coefUnits =
tmalloc(
sizeof(
char **) * numCols);
1228 ySymbols =
tmalloc(
sizeof(
char *) * numCols);
1229 iIntercept =
tmalloc(
sizeof(
long) * numCols);
1230 iInterceptO =
tmalloc(
sizeof(
long) * numCols);
1231 iInterceptSigma =
tmalloc(
sizeof(
long) * numCols);
1232 iInterceptSigmaO =
tmalloc(
sizeof(
long) * numCols);
1233 iSlope =
tmalloc(
sizeof(
long) * numCols);
1234 iSlopeO =
tmalloc(
sizeof(
long) * numCols);
1235 iSlopeSigma =
tmalloc(
sizeof(
long) * numCols);
1236 iSlopeSigmaO =
tmalloc(
sizeof(
long) * numCols);
1237 iCurvature =
tmalloc(
sizeof(
long) * numCols);
1238 iCurvatureO =
tmalloc(
sizeof(
long) * numCols);
1239 iCurvatureSigma =
tmalloc(
sizeof(
long) * numCols);
1240 iCurvatureSigmaO =
tmalloc(
sizeof(
long) * numCols);
1241 iChiSq =
tmalloc(
sizeof(
long) * numCols);
1242 iChiSqO =
tmalloc(
sizeof(
long) * numCols);
1243 iRmsResidual =
tmalloc(
sizeof(
long) * numCols);
1244 iRmsResidualO =
tmalloc(
sizeof(
long) * numCols);
1245 iSigLevel =
tmalloc(
sizeof(
long) * numCols);
1246 iSigLevelO =
tmalloc(
sizeof(
long) * numCols);
1247 iFitIsValid =
tmalloc(
sizeof(
long) * numCols);
1248 iFitIsValidO =
tmalloc(
sizeof(
long) * numCols);
1249 iFitLabel =
tmalloc(
sizeof(
long) * numCols);
1250 iFitLabelO =
tmalloc(
sizeof(
long) * numCols);
1251 iy =
tmalloc(
sizeof(
long) * numCols);
1252 iySigma =
tmalloc(
sizeof(
long) * numCols);
1253 iFit =
tmalloc(
sizeof(
long) * numCols);
1254 iResidual =
tmalloc(
sizeof(
long) * numCols);
1256 for (colIndex = 0; colIndex < numCols; colIndex++) {
1257 ySymbols[colIndex] = NULL;
1258 coefUnits[colIndex] =
tmalloc(
sizeof(
char *) * terms);
1259 iInterceptSigma[colIndex] = -1;
1260 iInterceptSigmaO[colIndex] = -1;
1261 iIntercept[colIndex] = -1;
1262 iInterceptO[colIndex] = -1;
1263 iInterceptSigma[colIndex] = -1;
1264 iInterceptSigmaO[colIndex] = -1;
1265 iSlope[colIndex] = -1;
1266 iSlopeO[colIndex] = -1;
1267 iSlopeSigma[colIndex] = -1;
1268 iSlopeSigmaO[colIndex] = -1;
1269 iCurvature[colIndex] = -1;
1270 iCurvatureO[colIndex] = -1;
1271 iCurvatureSigma[colIndex] = -1;
1272 iCurvatureSigmaO[colIndex] = -1;
1273 iChiSq[colIndex] = -1;
1274 iChiSqO[colIndex] = -1;
1275 iRmsResidual[colIndex] = -1;
1276 iRmsResidualO[colIndex] = -1;
1277 iSigLevel[colIndex] = -1;
1278 iSigLevelO[colIndex] = -1;
1279 iFitIsValid[colIndex] = -1;
1280 iFitIsValidO[colIndex] = -1;
1281 iFitLabel[colIndex] = -1;
1282 iFitLabelO[colIndex] = -1;
1284 iySigma[colIndex] = -1;
1285 iFit[colIndex] = -1;
1286 iResidual[colIndex] = -1;
1289 if (!
SDDS_InitializeOutput(SDDSout, SDDS_BINARY, 0, NULL,
"sddsmpfit output: fitted data", output) ||
1293 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1295 for (colIndex = 0; colIndex < numCols; colIndex++) {
1299 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1303 for (colIndex = 0; colIndex < numCols; colIndex++)
1305 ySymbols[colIndex] = yNames[colIndex];
1307 for (colIndex = 0; colIndex < numCols; colIndex++) {
1315 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1317 for (colIndex = 0; colIndex < numCols; colIndex++) {
1318 sprintf(buffer,
"%sFit", yNames[colIndex]);
1319 sprintf(buffer1,
"Fit[%s]", ySymbols[colIndex]);
1322 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1324 SDDS_Bomb(
"unable to get index of just-defined fit output column");
1326 sprintf(buffer,
"%sResidual", yNames[colIndex]);
1327 sprintf(buffer1,
"Residual[%s]", ySymbols[colIndex]);
1330 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1332 SDDS_Bomb(
"unable to get index of just-defined residual output column");
1334 if (sigmasValid && !ySigmaNames) {
1335 sprintf(buffer,
"%sSigma", yNames[colIndex]);
1337 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1340 sprintf(buffer1,
"Sigma[%s]", ySymbols[colIndex]);
1342 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1346 if (!(coefUnits[colIndex] = makeCoefficientUnits(SDDSout, xName, yNames[colIndex], order, terms)))
1347 SDDS_Bomb(
"unable to make coefficient units");
1350 if (outputInfo && !
SDDS_InitializeOutput(SDDSoutInfo, SDDS_BINARY, 0, NULL,
"sddsmpfit output: fit information", outputInfo))
1351 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1355 SDDS_DefineParameter(SDDSoutInfo,
"Basis", NULL, NULL,
"Function basis for fit", NULL,
SDDS_STRING, isChebyshev ?
"Chebyshev T polynomials" :
"ordinary polynomials") < 0)
1356 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1359 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1362 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1363 sprintf(buffer,
"%sOffset", xName);
1364 sprintf(buffer1,
"Offset of %s for fit", xName);
1366 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1367 sprintf(buffer,
"%sScale", xName);
1368 sprintf(buffer1,
"Scale factor of %s for fit", xName);
1370 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1372 for (colIndex = 0; colIndex < numCols; colIndex++) {
1374 sprintf(buffer1,
"%sCoefficient", yNames[colIndex]);
1375 sprintf(buffer2,
"%sCoefficientSigma", yNames[colIndex]);
1376 sprintf(buffer3,
"%sCoefficientUnits", yNames[colIndex]);
1379 (sigmasValid &&
SDDS_DefineColumn(SDDSoutInfo, buffer2,
"$gs$r$ba$n",
"[CoefficientUnits]",
"sigma of coefficient of term in fit", NULL,
SDDS_DOUBLE, 0) < 0))
1380 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1387 sprintf(buffer1,
"%sReducedChiSquared", yNames[colIndex]);
1388 sprintf(buffer2,
"%sRmsResidual", yNames[colIndex]);
1389 sprintf(buffer3,
"%sSignificanceLevel", yNames[colIndex]);
1392 "Reduced chi-squared of fit",
1395 (iRmsResidual[colIndex] =
1397 (iSigLevel[colIndex] =
SDDS_DefineParameter(SDDSoutInfo, buffer3, NULL, NULL,
"Probability that data is from fit function", NULL,
SDDS_DOUBLE, NULL)) < 0)
1398 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1402 sprintf(buffer,
"%sFitIsValid", yNames[colIndex]);
1404 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1408 sprintf(buffer,
"%sSddsmpfitlabel", yNames[colIndex]);
1410 if ((i = coefficient_index(order, terms, 0)) >= 0) {
1411 sprintf(buffer,
"%sIntercept", yNames[colIndex]);
1413 sprintf(buffer,
"%sInterceptSigma", yNames[colIndex]);
1415 iInterceptSigma[colIndex] =
SDDS_DefineParameter(SDDSoutInfo, buffer, buffer, coefUnits[colIndex][i],
"Sigma of intercept of fit", NULL,
SDDS_DOUBLE, NULL);
1417 sprintf(buffer,
"%sSlope", yNames[colIndex]);
1418 if ((i = coefficient_index(order, terms, 1)) >= 0) {
1421 sprintf(buffer,
"%sSlopeSigma", yNames[colIndex]);
1425 if ((i = coefficient_index(order, terms, 2)) >= 0) {
1426 sprintf(buffer,
"%sCurvature", yNames[colIndex]);
1429 sprintf(buffer,
"%sCurvatureSigma", yNames[colIndex]);
1430 iCurvatureSigma[colIndex] =
SDDS_DefineParameter(SDDSoutInfo, buffer, buffer, coefUnits[colIndex][i],
"Sigma of curvature of fit", NULL,
SDDS_DOUBLE, NULL);
1434 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1439 if (
SDDS_DefineParameter(SDDSout,
"Basis", NULL, NULL,
"Function basis for fit", NULL,
SDDS_STRING, isChebyshev ?
"Chebyshev T polynomials" :
"ordinary polynomials") < 0)
1440 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1443 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1446 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1447 sprintf(buffer,
"%sOffset", xName);
1448 sprintf(buffer1,
"Offset of %s for fit", xName);
1450 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1451 sprintf(buffer,
"%sScale", xName);
1452 sprintf(buffer1,
"Scale factor of %s for fit", xName);
1454 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1456 for (colIndex = 0; colIndex < numCols; colIndex++) {
1458 sprintf(buffer1,
"%sReducedChiSquared", yNames[colIndex]);
1459 sprintf(buffer2,
"%sRmsResidual", yNames[colIndex]);
1460 sprintf(buffer3,
"%sSignificanceLevel", yNames[colIndex]);
1463 "Reduced chi-squared of fit",
1466 (iRmsResidualO[colIndex] =
1468 (iSigLevelO[colIndex] =
SDDS_DefineParameter(SDDSout, buffer3, NULL, NULL,
"Probability that data is from fit function", NULL,
SDDS_DOUBLE, NULL)) < 0)
1469 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1473 sprintf(buffer,
"%sFitIsValid", yNames[colIndex]);
1475 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1478 sprintf(buffer,
"%sSddsmpfitlabel", yNames[colIndex]);
1480 if ((i = coefficient_index(order, terms, 0)) >= 0) {
1481 sprintf(buffer,
"%sIntercept", yNames[colIndex]);
1483 sprintf(buffer,
"%sInterceptSigma", yNames[colIndex]);
1485 iInterceptSigmaO[colIndex] =
SDDS_DefineParameter(SDDSout, buffer, buffer, coefUnits[colIndex][i],
"Sigma of intercept of fit", NULL,
SDDS_DOUBLE, NULL);
1487 sprintf(buffer,
"%sSlope", yNames[colIndex]);
1488 if ((i = coefficient_index(order, terms, 1)) >= 0) {
1491 sprintf(buffer,
"%sSlopeSigma", yNames[colIndex]);
1495 if ((i = coefficient_index(order, terms, 2)) >= 0) {
1496 sprintf(buffer,
"%sCurvature", yNames[colIndex]);
1499 sprintf(buffer,
"%sCurvatureSigma", yNames[colIndex]);
1500 iCurvatureSigmaO[colIndex] =
SDDS_DefineParameter(SDDSout, buffer, buffer, coefUnits[colIndex][i],
"Sigma of curvature of fit", NULL,
SDDS_DOUBLE, NULL);
1504 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1508 if (copyParameters) {
1510 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1512 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1516 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1521char **makeCoefficientUnits(
SDDS_DATASET * SDDSout,
char *xName,
char *yName, int32_t *order,
long terms) {
1522 char *xUnits, *yUnits, buffer[SDDS_MAXLINE];
1523 char **coefUnits = NULL;
1528 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1530 coefUnits =
tmalloc(
sizeof(*coefUnits) * terms);
1534 for (i = 0; i < terms; i++)
1535 coefUnits[i] = yUnits;
1539 for (i = 0; i < terms; i++) {
1540 if (order[i] == 0) {
1541 if (strcmp(yUnits,
"1") != 0)
1545 }
else if (strcmp(xUnits, yUnits) == 0) {
1547 sprintf(buffer,
"1/%s$a%" PRId32
"$n", xUnits, order[i] - 1);
1553 sprintf(buffer,
"%s/%s$a%" PRId32
"$n", yUnits, xUnits, order[i]);
1555 sprintf(buffer,
"%s/%s", yUnits, xUnits);
1566 SDDSout = &evalParameters->dataset;
1567 if (!
SDDS_InitializeOutput(SDDSout, SDDS_BINARY, 0, NULL,
"sddsmpfit output: evaluation of fits", evalParameters->file) ||
1569 SDDS_Bomb(
"Problem setting up evaluation file");
1570 for (i = 0; i < yNames; i++)
1572 SDDS_Bomb(
"Problem setting up evaluation file");
1574 SDDS_Bomb(
"Problem setting up evaluation file");
1577void makeEvaluationTable(
EVAL_PARAMETERS * evalParameters,
double *x, int64_t points,
double *coef, int32_t *order,
long terms,
1578 char *xName,
char **yName,
long yNames,
long iYName) {
1579 static double *xEval = NULL, *yEval = NULL;
1580 static int64_t maxEvals = 0;
1584 if (!(evalParameters->flags & EVAL_BEGIN_GIVEN) || !(evalParameters->flags & EVAL_END_GIVEN)) {
1587 if (!(evalParameters->flags & EVAL_BEGIN_GIVEN))
1588 evalParameters->begin = min;
1589 if (!(evalParameters->flags & EVAL_END_GIVEN))
1590 evalParameters->end = max;
1592 if (!(evalParameters->flags & EVAL_NUMBER_GIVEN))
1593 evalParameters->number = points;
1594 if (evalParameters->number > 1)
1595 delta = (evalParameters->end - evalParameters->begin) / (evalParameters->number - 1);
1599 if (!xEval || maxEvals < evalParameters->number) {
1600 if (!(xEval = (
double *)
SDDS_Realloc(xEval,
sizeof(*xEval) * evalParameters->number)) ||
1601 !(yEval = (
double *)
SDDS_Realloc(yEval,
sizeof(*yEval) * evalParameters->number)))
1603 maxEvals = evalParameters->number;
1606 for (i = 0; i < evalParameters->number; i++) {
1607 xEval[i] = evalParameters->begin + i * delta;
1608 yEval[i] =
eval_sum(basis_fn, coef, order, terms, xEval[i]);
1612 !
SDDS_StartPage(&evalParameters->dataset, evalParameters->number)) ||
1615 (iYName == yNames - 1 && !
SDDS_WritePage(&evalParameters->dataset)))
1616 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
1619void compareOriginalToFit(
double *x,
double *y,
double **residual, int64_t points,
double *rmsResidual,
double *coef, int32_t *order,
long terms) {
1621 double residualSum2, fit;
1623 *residual =
tmalloc(
sizeof(**residual) * points);
1626 for (i = 0; i < points; i++) {
1627 fit =
eval_sum(basis_fn, coef, order, terms, x[i]);
1628 (*residual)[i] = y[i] - fit;
1629 residualSum2 += sqr((*residual)[i]);
1631 *rmsResidual = sqrt(residualSum2 / points);
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_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_SetColumnFromLongs(SDDS_DATASET *SDDS_dataset, int32_t mode, int32_t *data, int64_t rows,...)
Sets the values for a single data column using long integer 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_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_DefineColumn(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)
Defines a data column within the SDDS dataset.
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.
int32_t SDDS_CheckColumn(SDDS_DATASET *SDDS_dataset, char *name, char *units, int32_t type, FILE *fp_message)
Checks if a column exists in the SDDS dataset with the specified name, units, and type.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns 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.
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
#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_ANY_NUMERIC_TYPE
Special identifier used by SDDS_Check*() routines to accept any numeric 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.
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 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.
double dtcheby(double x, long n)
Evaluate the derivative of the Chebyshev polynomial T_n(x).
void set_argument_offset(double offset)
Set the offset applied to the input argument of basis functions.
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.
char * str_tolower(char *s)
Convert a string to lower case.