/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ /* program: sddspoly * purpose: evaluation of polynomials for N-dimensional input * * Michael Borland, 2002 * $Log: sddspoly.c,v $ Revision 1.8 2006/12/14 22:22:00 soliday Updated a bunch of programs because SDDS_SaveLayout is now called by SDDS_WriteLayout and it is no longer required to be called directly. Also the AutoCheckMode is turned off by default now so I removed calls to SDDS_SetAutoCheckMode that would attempt to turn it off. It is now up to the programmer to turn it on in new programs until debugging is completed and then remove the call to SDDS_SetAutoCheckMode. Revision 1.7 2006/10/19 17:55:40 soliday Updated to work with linux-x86_64. Revision 1.6 2005/11/04 22:46:17 soliday Updated code to be compiled by a 64 bit processor. Revision 1.5 2003/09/02 19:16:06 soliday Cleaned up code for Linux. Revision 1.4 2002/08/14 17:12:51 soliday Added Open License Revision 1.3 2002/08/07 01:07:56 borland Fixed silly bug that prevented doign more than two polynomials. Revision 1.2 2002/08/06 21:02:29 shang made -pipe option work and freed memory leaks Revision 1.1 2002/07/30 00:04:07 borland First version. */ #include "mdb.h" #include "SDDS.h" #include "scan.h" #include "SDDSutils.h" #include #define CLO_PIPE 0 #define CLO_EVALUATE 1 #define CLO_OPTIONS 2 char *option[CLO_OPTIONS] = { "pipe", "evaluate", }; static char *USAGE = "sddspoly [] [] [-pipe=[input][,output]]\n\ -evaluate=filename=,output=,coefficients=,input0=,power0=[,input1=,power1=][,...]\n\ [-evaluate=...]\n\n\ -pipe Standard SDDS Toolkit pipe option.\n\ -evaluate Specifies evaluation of polynomial, specified in file\n\ . The results of the evaluation are in \n\ the under the name given with output=.\n\ The polynomial coefficients are in the column named with\n\ coefficients=. The input qualifiers give the\n\ names of columns in that are inputs to the \n\ polynomial. The power qualifiers give the names of\n\ columns in the file that give the powers\n\ of the input columns to use for each coefficient.\n\n\ Program by Michael Borland. (This is version 1.0, July 2002.)\n"; #define POLY_FILE_SEEN 0x0001U #define POLY_OUTPUT_SEEN 0x0002U #define POLY_COEF_SEEN 0x0004U #define POLY_IN0_SEEN 0x0008U #define POLY_OUT0_SEEN 0x0010U #define POLY_IN1_SEEN 0x0020U #define POLY_OUT1_SEEN 0x0040U #define POLY_IN2_SEEN 0x0080U #define POLY_OUT2_SEEN 0x0100U #define POLY_IN3_SEEN 0x0200U #define POLY_OUT3_SEEN 0x0400U #define POLY_IN4_SEEN 0x0800U #define POLY_OUT4_SEEN 0x1000U typedef struct { unsigned long flags; char *filename, *outputColumn, *coefColumn; char **inputColumn, **powerColumn; long nInputs, nTerms; int32_t **power; double *coef; double **inputData, *input; } POLYNOMIAL; void initializePolynomial(POLYNOMIAL *poly, SDDS_DATASET *SDDSin, SDDS_DATASET *SDDSout); double evaluatePoly(double *coef, int32_t **power, long nCoef, double *input, long nInputs); void FreePolynormialMemory(POLYNOMIAL *poly, long npoly); int main(int argc, char **argv) { POLYNOMIAL *poly; long nPoly, iPoly, row, rows, iInput; int iArg; char *input, *output; unsigned long pipeFlags; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout; double *outputData ; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<3) bomb(NULL, USAGE); outputData = NULL; input = output = NULL; pipeFlags = 0; poly = NULL; nPoly = 0; for (iArg=1; iArg0) { rows = SDDS_CountRowsOfInterest(&SDDSin); if (!SDDS_CopyPage(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(outputData = SDDS_Realloc(outputData, sizeof(*outputData)*rows))) SDDS_Bomb("memory allocation failure"); for (iPoly=0; iPolyflags&(POLY_IN0_SEEN<<(2*i))) && !(poly->flags&(POLY_OUT0_SEEN<<(2*i)))) SDDS_Bomb("input qualifier seen without matching output qualifier"); if (!(poly->flags&(POLY_IN0_SEEN<<(2*i))) && (poly->flags&(POLY_OUT0_SEEN<<(2*i)))) SDDS_Bomb("output qualifier seen without matching input qualifier"); if (!(poly->flags&(POLY_IN0_SEEN<<(2*i))) && !(poly->flags&(POLY_OUT0_SEEN<<(2*i)))) break; } poly->nInputs = i; for (i++; i<5; i++) { if ((poly->flags&(POLY_IN0_SEEN<<(2*i))) || (poly->flags&(POLY_OUT0_SEEN<<(2*i)))) SDDS_Bomb("input or output qualifiers skipped"); } if (!SDDS_InitializeInput(&SDDSpoly, poly->filename)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_GetColumnIndex(&SDDSpoly,poly->outputColumn)==-1) { if (!SDDS_DefineSimpleColumn(SDDSout, poly->outputColumn, NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (SDDS_CheckColumn(&SDDSpoly, poly->outputColumn, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (SDDS_CheckColumn(&SDDSpoly, poly->coefColumn, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; inInputs; i++) { if (SDDS_CheckColumn(SDDSin, poly->inputColumn[i], NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDSpoly, poly->powerColumn[i], NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (SDDS_ReadPage(&SDDSpoly)<=0) { sprintf(buffer, "problem with file %s\n", poly->filename); SDDS_SetError(buffer); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ((poly->nTerms=SDDS_RowCount(&SDDSpoly))<=0) { sprintf(buffer, "problem with file %s: no rows\n", poly->filename); SDDS_SetError(buffer); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!(poly->coef=SDDS_GetColumnInDoubles(&SDDSpoly, poly->coefColumn))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(poly->power=SDDS_Malloc(sizeof(*(poly->power))*poly->nInputs))) SDDS_Bomb("memory allocation failure"); for (i=0; inInputs; i++) { if (!(poly->power[i]= SDDS_GetColumnInLong(&SDDSpoly, poly->powerColumn[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDSpoly)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(poly->input = SDDS_Malloc(sizeof(*(poly->input))*poly->nInputs))) SDDS_Bomb("memory allocation failure"); if (!(poly->inputData = SDDS_Malloc(sizeof(*(poly->inputData))*poly->nInputs))) SDDS_Bomb("memory allocation failure"); } void FreePolynormialMemory(POLYNOMIAL *poly, long npoly) { long i,j; for (i=0;i