60char *option[N_OPTIONS] = {
66 "Usage: sddspoly [<inputfile>] [<outputfile>] "
67 "[-pipe=[input][,output]]\n"
68 " -evaluate=filename=<polyFilename>,output=<column>,coefficients=<column>,\n"
69 " input0=<inputColumn>,power0=<powerColumn>[,input1=<inputColumn>,power1=<polyColumn>][,...]\n"
70 " [-evaluate=...]\n\n"
72 " -pipe Standard SDDS Toolkit pipe option.\n"
73 " -evaluate Specifies evaluation of a polynomial defined in <polyFilename>.\n"
74 " The results are stored in <outputfile> under the column name\n"
75 " specified by output=<column>.\n"
76 " The coefficients are taken from the column named\n"
77 " coefficients=<column>.\n"
78 " The input<n> qualifiers specify the column names in <inputfile>\n"
79 " that serve as inputs to the polynomial.\n"
80 " The power<n> qualifiers specify the column names in\n"
81 " <polyFilename> that provide the exponents for each input.\n\n"
82 "Program by Michael Borland. (" __DATE__
" " __TIME__
", SVN revision: " SVN_VERSION
")\n";
84#define POLY_FILE_SEEN 0x0001U
85#define POLY_OUTPUT_SEEN 0x0002U
86#define POLY_COEF_SEEN 0x0004U
87#define POLY_IN0_SEEN 0x0008U
88#define POLY_OUT0_SEEN 0x0010U
89#define POLY_IN1_SEEN 0x0020U
90#define POLY_OUT1_SEEN 0x0040U
91#define POLY_IN2_SEEN 0x0080U
92#define POLY_OUT2_SEEN 0x0100U
93#define POLY_IN3_SEEN 0x0200U
94#define POLY_OUT3_SEEN 0x0400U
95#define POLY_IN4_SEEN 0x0800U
96#define POLY_OUT4_SEEN 0x1000U
114double evaluatePoly(
double *coef, int32_t **power,
long nCoef,
double *input,
long nInputs);
117int main(
int argc,
char **argv) {
119 long nPoly, iPoly, iInput;
122 char *input, *output;
123 unsigned long pipeFlags;
124 SCANNED_ARG *scanned;
129 argc =
scanargs(&scanned, argc, argv);
135 input = output = NULL;
140 for (iArg = 1; iArg < argc; iArg++) {
141 if (scanned[iArg].arg_type == OPTION) {
143 switch (
match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) {
152 scanned[iArg].n_items -= 1;
154 scanned[iArg].list + 1,
155 &scanned[iArg].n_items,
158 "output",
SDDS_STRING, &(
poly[nPoly].outputColumn), 1, POLY_OUTPUT_SEEN,
159 "coefficients",
SDDS_STRING, &(
poly[nPoly].coefColumn), 1, POLY_COEF_SEEN,
171 !(
poly[nPoly].flags & POLY_FILE_SEEN) ||
172 !(
poly[nPoly].flags & POLY_OUTPUT_SEEN) ||
173 !(
poly[nPoly].flags & POLY_COEF_SEEN) ||
174 !(
poly[nPoly].flags & POLY_IN0_SEEN) ||
175 !(
poly[nPoly].flags & POLY_OUT0_SEEN)) {
183 scanned[iArg].n_items - 1,
190 fprintf(stderr,
"error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]);
196 input = scanned[iArg].list[0];
197 }
else if (!output) {
198 output = scanned[iArg].list[0];
208 SDDS_Bomb(
"give at least one -evaluate option");
219 for (iPoly = 0; iPoly < nPoly; iPoly++) {
220 initializePolynomial(&
poly[iPoly], &SDDSin, &SDDSout);
233 outputData =
SDDS_Realloc(outputData,
sizeof(*outputData) * rows);
238 for (iPoly = 0; iPoly < nPoly; iPoly++) {
239 for (iInput = 0; iInput <
poly[iPoly].nInputs; iInput++) {
240 poly[iPoly].inputData[iInput] =
242 if (!
poly[iPoly].inputData[iInput]) {
247 for (row = 0; row < rows; row++) {
248 for (iInput = 0; iInput <
poly[iPoly].nInputs; iInput++) {
249 poly[iPoly].input[iInput] =
poly[iPoly].inputData[iInput][row];
251 outputData[row] = evaluatePoly(
poly[iPoly].coef,
255 poly[iPoly].nInputs);
258 if (!
SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, outputData, rows,
poly[iPoly].outputColumn)) {
281 FreePolynormialMemory(
poly, nPoly);
287double evaluatePoly(
double *coef, int32_t **power,
long nCoef,
double *input,
long nInputs) {
288 double sum = 0.0, term;
291 for (iCoef = 0; iCoef < nCoef; iCoef++) {
293 for (iInput = 0; iInput < nInputs; iInput++) {
294 term *=
ipow(input[iInput], power[iInput][iCoef]);
307 for (i = 1; i < 5; i++) {
308 if ((
poly->flags & (POLY_IN0_SEEN << (2 * i))) &&
309 !(
poly->flags & (POLY_OUT0_SEEN << (2 * i)))) {
310 SDDS_Bomb(
"input qualifier seen without matching output qualifier");
312 if (!(
poly->flags & (POLY_IN0_SEEN << (2 * i))) &&
313 (
poly->flags & (POLY_OUT0_SEEN << (2 * i)))) {
314 SDDS_Bomb(
"output qualifier seen without matching input qualifier");
316 if (!(
poly->flags & (POLY_IN0_SEEN << (2 * i))) &&
317 !(
poly->flags & (POLY_OUT0_SEEN << (2 * i)))) {
323 for (i++; i < 5; i++) {
324 if ((
poly->flags & (POLY_IN0_SEEN << (2 * i))) ||
325 (
poly->flags & (POLY_OUT0_SEEN << (2 * i)))) {
326 SDDS_Bomb(
"input<n> or output<n> qualifiers skipped");
348 for (i = 0; i <
poly->nInputs; i++) {
358 snprintf(buffer,
sizeof(buffer),
"problem with file %s\n",
poly->filename);
363 if ((
poly->nTerms = SDDS_RowCount(&SDDSpoly)) <= 0) {
364 snprintf(buffer,
sizeof(buffer),
"problem with file %s: no rows\n",
poly->filename);
379 for (i = 0; i <
poly->nInputs; i++) {
381 if (!
poly->power[i]) {
396 if (!
poly->inputData) {
404 for (i = 0; i < npoly; i++) {
405 for (j = 0; j <
poly[i].nInputs; j++) {
406 free(
poly[i].power[j]);
407 free(
poly[i].inputData[j]);
408 free(
poly[i].inputColumn[j]);
409 free(
poly[i].powerColumn[j]);
411 free(
poly[i].powerColumn);
412 free(
poly[i].inputColumn);
415 free(
poly[i].inputData);
417 free(
poly[i].filename);
418 free(
poly[i].coefColumn);
419 free(
poly[i].outputColumn);
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_InitializeCopy(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source, char *filename, char *filemode)
int32_t SDDS_CopyPage(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source)
int32_t SDDS_SetColumn(SDDS_DATASET *SDDS_dataset, int32_t mode, void *data, int64_t rows,...)
Sets the values for one data column in the current data table of an SDDS dataset.
int32_t SDDS_DefineSimpleColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data column 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.
void SDDS_SetError(char *error_text)
Records an error message in the SDDS error stack.
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.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
void * SDDS_Malloc(size_t size)
Allocates memory of a specified size.
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
void SDDS_Bomb(char *message)
Terminates the program after printing an error message and recorded errors.
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_ANY_NUMERIC_TYPE
Special identifier used by SDDS_Check*() routines to accept any numeric type.
#define SDDS_DOUBLE
Identifier for the double data type.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
double ipow(const double x, const int64_t p)
Compute x raised to the power p (x^p).
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.
double poly(double *a, long n, double x)
Evaluate a polynomial at a given point.
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)
void free_scanargs(SCANNED_ARG **scanned, int argc)
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.