symfun.cpp

00001 //
00002 //##################################################################
00003 //                CAM SYMBOLIC FUNCTION
00004 //##################################################################
00005 //
00006 //                                     Chris Anderson 7/21/97 (C) UCLA
00007 /*! 
00008     \class CAMsymbolicFunction
00009     \brief An instance of class CAMsymbolicFunction implements a 
00010     double valued function (of an arbitrary number of double arguments) 
00011     as specified by an initialization character string (i.e. symbolic initialization). 
00012     
00013     The syntax for the initialization string is that of general C++ expressions. 
00014     The initialization string can contain references to C++ math functions 
00015     (those in math.h) as well as symbolic constants. The ^ character can be used
00016     to specify exponentiation, e.g. x^2 = x*x.
00017     
00018     The () operator is overloaded so the standard functional 
00019     evaluation syntax, e.g. f(x), is used to evaluate the CAMsymbolicFuntion instance.
00020     
00021     Sample Usage:
00022 \code
00023 int Vcount = 2;                   // number of independent variables
00024 char*V []  = {"x","y"};           // x,y  = independent variable names
00025 char*S     = "x^2 + 2*y";         // specify a function 
00026      
00027 CAMsymbolicFunction F(V,Vcount,S); // initialize instance
00028     
00029 cout << F(2.0,3.0) << endl;        // evaluate and output result at (x,y) = (2.0,3.0) 
00030 \endcode
00031 
00032     @author Chris Anderson
00033     @date 7/21/97
00034     @version 01/26/07
00035     
00036 */
00037 #include <string.h>
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include "symfun.h"
00041 #include "symexit.h"
00042 #include "exptrans.h"
00043 #include "rOpLib.h"
00044 //
00045 //##################################################################
00046 //                      CONSTRUCTORS
00047 //##################################################################
00048 //
00049 /**
00050   Creates a "null" CAMsymbolicFunction. The CAMsymbolicFunction is initialized 
00051   with the initialize member functions.
00052 */
00053 CAMsymbolicFunction::CAMsymbolicFunction()
00054 {
00055     constructorString = 0;
00056 
00057     variableNames     = 0;
00058     variableCount     = 0;
00059 
00060     constantNames     = 0;
00061     constantCount     = 0;
00062     constantValues    = 0;
00063 
00064     symbolCount       = 0;
00065     sNames            = 0;
00066 
00067     evaluationData     = 0;
00068     evaluationDataSize = 0;
00069 
00070     executionArray     = 0;
00071     executionArraySize = 0;
00072 
00073     LibFunctions       = 0;
00074 }
00075 /**
00076   Copy constructor.
00077 */
00078 CAMsymbolicFunction::CAMsymbolicFunction(const CAMsymbolicFunction& F)
00079 {
00080 //
00081 //  Null initializers
00082 //
00083     constructorString = 0;
00084 
00085      variableNames     = 0;
00086     variableCount     = 0;
00087 
00088     constantNames     = 0;
00089     constantCount     = 0;
00090     constantValues    = 0;
00091 
00092     symbolCount       = 0;
00093     sNames            = 0;
00094 
00095     evaluationData     = 0;
00096     evaluationDataSize = 0;
00097 
00098     executionArray     = 0;
00099     executionArraySize = 0;
00100 
00101 //
00102 //  Allocate Storage and Initalize
00103 //
00104     int i;
00105 
00106     if(F.constructorString != 0)
00107     {
00108     constructorString = new char[strlen(F.constructorString) + 1];
00109     strcpy(constructorString,F.constructorString);
00110     }
00111 
00112     if(F.variableCount != 0)
00113     {
00114     variableNames  = new char*[F.variableCount];
00115     variableCount  = F.variableCount;
00116 
00117     for(i = 0; i < F.variableCount; i++)
00118     {
00119     variableNames[i] = new char[strlen(F.variableNames[i]) + 1];
00120     strcpy(variableNames[i],F.variableNames[i]);
00121     }
00122     }
00123 
00124     if(F.constantCount != 0)
00125     {
00126     constantNames  = new char*[F.constantCount];
00127     constantCount  = F.constantCount;
00128     constantValues = new double[F.constantCount];
00129 
00130     for(i = 0; i < F.constantCount; i++)
00131     {
00132     constantNames[i] = new char[strlen(F.constantNames[i]) + 1];
00133     strcpy(constantNames[i],F.constantNames[i]);
00134     constantValues[i] = F.constantValues[i];
00135     }
00136     }
00137 
00138     evaluationDataSize = F.evaluationDataSize;
00139 
00140     if(F.evaluationData != 0)
00141     {
00142     evaluationData     = new double[evaluationDataSize];
00143     for(i=0; i< evaluationDataSize; i++)
00144     {
00145     evaluationData[i] = F.evaluationData[i];
00146     }}
00147 
00148     executionArraySize = F.executionArraySize;
00149     
00150     if(F.executionArray != 0)
00151     {
00152     executionArray     = new long[executionArraySize];
00153     for(i=0; i< executionArraySize; i++)
00154     {
00155     executionArray[i] = F.executionArray[i];
00156     }}
00157 
00158     symbolCount = F.symbolCount;
00159     sNames      = new char*[symbolCount];
00160     for(i=0; i< symbolCount; i++)
00161     {
00162     sNames[i] = new char[strlen(F.sNames[i])+1];
00163     strcpy(sNames[i],F.sNames[i]);
00164     }
00165 
00166     LibFunctions       = CAMrealOperatorLib::getFunctionArrayPtr();
00167 }
00168 /**
00169 Creates a CAMsymbolicFunction in one variable, x, where the function is
00170 specified by the null terminated string S. If the construction process 
00171 fails, program execution stops and an error message is output.
00172 
00173 @arg S: Null terminated character string in the variable x that 
00174 specifies the function.
00175 */
00176 CAMsymbolicFunction::CAMsymbolicFunction(char const* S)
00177 {
00178     const char* V []      = {"x"};
00179     int Vcount            = 1;
00180     const char** C        = 0;
00181     int Ccount            = 0;
00182     double* Cvalues       = 0;
00183 
00184     create(V,Vcount,C,Ccount, Cvalues, S);
00185 }
00186 /**
00187 Creates a CAMsymbolicFunction of Vcount variables from the initialization 
00188 string S. S is a null terminated character string. If the construction process 
00189 fails, program execution stops and an error message is output.
00190 
00191 @arg V      : Array of null terminated strings specifying independent variable names
00192 @arg Vcount : The number of independent variables
00193 @arg S      : Null terminated character string specifying the function
00194 
00195 
00196 
00197 Sample specification and use of a function of two variables x and y: 
00198 
00199 \code
00200 int Vcount = 2;                   // number of independent variables
00201 char*V []  = {"x","y"};           // x,y  = independent variable names
00202 char*S     = "x^2 + 2*y";         // specify a function 
00203      
00204 CAMsymbolicFunction F(V,Vcount,S); // initialize instance
00205     
00206 cout << F(2.0,3.0) << endl;        // evaluate and output result at (x,y) = (2.0,3.0) 
00207 \endcode
00208 */
00209 CAMsymbolicFunction::CAMsymbolicFunction(const char** V, int Vcount, const char* S)
00210 {
00211 
00212     const char** C        = 0;
00213     int Ccount            = 0;
00214     double* Cvalues       = 0;
00215     create(V,Vcount,C,Ccount, Cvalues, S);
00216 }
00217 
00218 /**
00219 Creates a CAMsymbolicFunction of Vcount variables and 
00220 Ccount symbolic constants from the initialization string S. 
00221 If the construction process fails, program execution stops and an 
00222 error message is output.
00223   
00224 @arg V      : Array of null terminated strings specifying independent variable names
00225 @arg Vcount : The number of independent variables
00226 @arg S      : Null terminated character string specifying the function
00227 @arg V      : Array of null terminated strings specifying symbolic constant names
00228 @arg Ccount : The number of symbolic constants 
00229 @arg Cvalues: The values of the symbolic constants
00230 @arg S      : Null terminated character string specifying the function
00231 
00232 Sample:
00233 \code
00234 //
00235 //  Create a CAMsymbolicFunction that implements a*x^2 + b*x + c;
00236 //  a, b, c being symbolic constants.
00237 //
00238     int Vcount       = 1;                        // number of independent variables
00239     char*V []        = {"x"};                    // specify variable name
00240     int Ccount       = 3;                        // number of symbolic constants
00241     char*C []        = {"a","b","c"};            // specify constant names
00242     double Cvalues[] = {1.0, 2.0, 1.0};          // initial values of a,b,c
00243     char* S = "a*x^2 + b*x + c";                 // initialization string
00244 
00245     CAMsymbolicFunction f(V,Vcount,C,Ccount,Cvalues, S);
00246 
00247     cout << f << endl << endl;                    // print out function
00248 
00249     cout << "The value of the function at x = 1.0 is " 
00250          << f(1.0) << endl << endl;
00251 
00252     f.setConstantValue("a",2.0);                  // reset the symbolic constant
00253                                                   // a to have the value 2.0
00254     cout << f << endl << endl;                    // print out function
00255 
00256     cout << "The value of the function at x = 1.0 is  " 
00257          << f(1.0) << endl;
00258 \endcode
00259 */
00260 
00261 CAMsymbolicFunction::CAMsymbolicFunction(const char** V, int Vcount, const char** C,
00262 int Ccount, double const* Cvalues, char const* S)
00263 {
00264     create(V,Vcount,C,Ccount, Cvalues, S);
00265 }
00266 
00267 int CAMsymbolicFunction::create(const char** V, int Vcount, const char** C,
00268 int Ccount, double const* Cvalues, char const* S)
00269 {
00270 //
00271 //  Allocate Storage and Initalize
00272 //
00273     constructorString = new char[strlen(S) + 1];
00274     strcpy(constructorString,S);
00275 
00276     variableNames  = new char*[Vcount];
00277     variableCount  = Vcount;
00278 
00279     int i;
00280     for(i = 0; i < Vcount; i++)
00281     {
00282     variableNames[i] = new char[strlen(V[i]) + 1];
00283     strcpy(variableNames[i],V[i]);
00284     }
00285 
00286     constantNames  = new char*[Ccount];
00287     constantCount  = Ccount;
00288     constantValues = new double[Ccount];
00289 
00290     for(i = 0; i < Ccount; i++)
00291     {
00292     constantNames[i] = new char[strlen(C[i]) + 1];
00293     strcpy(constantNames[i],C[i]);
00294     constantValues[i] = Cvalues[i];
00295     }
00296 
00297     CAMrealOperatorLib L;
00298       expressionTransform T;
00299     int expReturn;
00300 
00301     expReturn =  T.initialize(variableNames, variableCount, constantNames,
00302                  constantCount,constructorString,&L);
00303     if(expReturn != 0) {destroy(); return 1;}
00304 
00305     evaluationDataSize = T.getEvaluationDataSize();
00306     evaluationData     = new double[evaluationDataSize];
00307 
00308     executionArraySize = T.getExecutionArraySize();
00309     executionArray     = new long[executionArraySize];
00310 
00311     initializeEvaluationData(T);
00312     initializeExecutionArray(T);
00313 //
00314 //  Save symbols
00315 //
00316     symbolCount    = T.getSymbolCount();
00317     char** TsNames = T.getSymbolNamesPtr();
00318     sNames      = new char*[symbolCount];
00319     for(i=0; i< symbolCount; i++)
00320     {
00321     sNames[i] = new char[strlen(TsNames[i])+1];
00322     strcpy(sNames[i],TsNames[i]);
00323     }
00324 
00325     LibFunctions       = CAMrealOperatorLib::getFunctionArrayPtr();
00326     return 0;
00327 }
00328 //
00329 //##################################################################
00330 //                      DESTRUCTORS
00331 //##################################################################
00332 //
00333 
00334 CAMsymbolicFunction::~CAMsymbolicFunction()
00335 {
00336     destroy();
00337 }
00338 
00339 void CAMsymbolicFunction::destroy()
00340 {
00341     int i;
00342     if(constructorString != 0) delete [] constructorString;
00343 
00344     if(variableNames != 0)
00345     {
00346     for(i =0; i < variableCount; i++) delete [] variableNames[i];
00347     delete [] variableNames;
00348     }
00349 
00350     if(constantNames != 0)
00351     {
00352     for(i =0; i < constantCount; i++) delete [] constantNames[i];
00353     delete [] constantNames;
00354     delete [] constantValues;
00355     }
00356 
00357     if(evaluationData != 0) delete [] evaluationData;
00358     if(executionArray != 0) delete [] executionArray;
00359 
00360     if(sNames         != 0)
00361     {
00362     for(i=0; i< symbolCount; i++) if(sNames[i] != 0) delete [] sNames[i];
00363     delete [] sNames;
00364     }
00365 
00366     constructorString = 0;
00367 
00368     variableNames     = 0;
00369     variableCount     = 0;
00370 
00371     constantNames     = 0;
00372     constantCount     = 0;
00373     constantValues    = 0;
00374 
00375     symbolCount       = 0;
00376     sNames            = 0;
00377 
00378     evaluationData     = 0;
00379     evaluationDataSize = 0;
00380 
00381     executionArray     = 0;
00382     executionArraySize = 0;
00383 
00384     LibFunctions       = 0;
00385 
00386 }
00387 //
00388 //##################################################################
00389 //                      INITIALIZATION
00390 //##################################################################
00391 //
00392 /**
00393   Initializes a CAMsymbolicFunction instance 
00394   to a null CAMsymbolicFunction.
00395 */
00396 int CAMsymbolicFunction::initialize()
00397 {
00398     destroy();
00399     return 0;
00400 }
00401 /**
00402 Initialize an CAMsymbolicFunction instance to one
00403 of a single variable, x, where the function is
00404 specified by the null terminated string S. If the initialization fails,
00405 error diagnostics are output to the standard error stream
00406 (cerr) and the program returns an error value. 
00407 
00408 @arg S: Null terminated character string in the variable x that 
00409 specifies the function.
00410 
00411 @returns 0 (= no error) 1 (= error). 
00412 */
00413 
00414 int CAMsymbolicFunction::initialize(char const* S)
00415 {
00416     destroy();
00417     const char*V []  = {"x"};
00418     int Vcount = 1;
00419 
00420     const char** C        = 0;
00421     int Ccount            = 0;
00422     const double* Cvalues = 0;
00423     
00424     int  cReturn;
00425     cReturn = create(V,Vcount,C,Ccount, Cvalues, S);
00426     if(cReturn != 0) return cReturn;
00427     return 0;
00428 }
00429 /**
00430 Initializes a CAMsymbolicFunction instance to one 
00431 of Vcount variables from the initialization string S. 
00432 S is a null terminated character string. If the initialization fails,
00433 error diagnostics are output to the standard error stream
00434 (cerr) and the program returns an error value. 
00435 
00436 @arg V      : Array of null terminated strings specifying independent variable names
00437 @arg Vcount : The number of independent variables
00438 @arg S      : Null terminated character string specifying the function
00439 
00440 @returns 0 (= no error) 1 (= error). 
00441 
00442 Sample specification and use of a function of two variables x and y: 
00443 
00444 \code
00445 CAMsymbolicFunction F;              // Create null instance
00446 int Vcount = 2;                     // number of independent variables
00447 char*V []  = {"x","y"};             // x,y  = independent variable names
00448 char*S     = "x^2 + 2*y";           // specify a function 
00449      
00450 int ierr = F.initialize(V,Vcount,S);// initialize 
00451 if(ierr != 0) 
00452 {
00453 cerr << "Initialization of CAMsymbolicFunction Failed" << endl;
00454 exit(1);
00455 }
00456 
00457     
00458 cout << F(2.0,3.0) << endl;        // evaluate and output result at (x,y) = (2.0,3.0) 
00459 \endcode
00460 */
00461 
00462 
00463 int CAMsymbolicFunction::initialize(const char** V, int Vcount, char const* S)
00464 {
00465     destroy();
00466     const char** C  = 0;
00467     int Ccount      = 0;
00468     double* Cvalues = 0;
00469     int  cReturn;
00470     cReturn = create(V,Vcount,C,Ccount, Cvalues, S);
00471     if(cReturn != 0) return cReturn;
00472     return 0;
00473 }
00474 /**
00475 Initializes a CAMsymbolicFunction instance to one of Vcount variables and 
00476 Ccount symbolic constants from the initialization string S. 
00477 If the initialization fails, error diagnostics are output to the 
00478 standard error stream (cerr) and the program returns an error value. 
00479   
00480 @arg V      : Array of null terminated strings specifying independent variable names
00481 @arg Vcount : The number of independent variables
00482 @arg S      : Null terminated character string specifying the function
00483 @arg V      : Array of null terminated strings specifying symbolic constant names
00484 @arg Ccount : The number of symbolic constants 
00485 @arg Cvalues: The values of the symbolic constants
00486 @arg S      : Null terminated character string specifying the function
00487 
00488 Sample:
00489 \code
00490 //
00491 //  Initializes a CAMsymbolic function instance to one that implements
00492 //  a*x^2 + b*x + c; a, b, c being symbolic constants.
00493 //
00494     CAMsymbolicFunction f;                       // create instance
00495      
00496     int Vcount       = 1;                        // number of independent variables
00497     char*V []        = {"x"};                    // specify variable name
00498     int Ccount       = 3;                        // number of symbolic constants
00499     char*C []        = {"a","b","c"};            // specify constant names
00500     double Cvalues[] = {1.0, 2.0, 1.0};          // initial values of a,b,c
00501     char* S = "a*x^2 + b*x + c";                 // initialization string
00502 
00503     int ierr = f.initialize(V,Vcount,C,Ccount,Cvalues, S);
00504     if(ierr != 0) 
00505     {
00506     cerr << "Initialization of CAMsymbolicFunction Failed" << endl;
00507     exit(1);
00508     }
00509 
00510     cout << f << endl << endl;                    // print out function
00511 
00512     cout << "The value of the function at x = 1.0 is " 
00513          << f(1.0) << endl << endl;
00514 
00515     f.setConstantValue("a",2.0);                  // reset the symbolic constant
00516                                                   // a to have the value 2.0
00517     cout << f << endl << endl;                    // print out function
00518 
00519     cout << "The value of the function at x = 1.0 is  " 
00520          << f(1.0) << endl;
00521 \endcode
00522 */
00523 int  CAMsymbolicFunction::initialize(const char** V, int Vcount, const char** C,
00524 int Ccount, double const* Cvalues, char const* S)
00525 {
00526     destroy();
00527     int  cReturn;
00528     cReturn = create(V,Vcount,C,Ccount, Cvalues, S);
00529     if(cReturn != 0) return cReturn;
00530     return 0;
00531 }
00532 
00533 /**
00534   Initializes the CAMsymbolicFunction instance with F 
00535 */
00536 
00537 int CAMsymbolicFunction::initialize(const CAMsymbolicFunction& F)
00538 {
00539     destroy();
00540 //
00541 //  Allocate Storage and Initalize
00542 //
00543     int i;
00544     if(F.constructorString != 0)
00545     {
00546     constructorString = new char[strlen(F.constructorString) + 1];
00547     strcpy(constructorString,F.constructorString);
00548     }
00549 
00550     if(F.variableCount != 0)
00551     {
00552     variableNames  = new char*[F.variableCount];
00553     variableCount  = F.variableCount;
00554 
00555     for(i = 0; i < F.variableCount; i++)
00556     {
00557     variableNames[i] = new char[strlen(F.variableNames[i]) + 1];
00558     strcpy(variableNames[i],F.variableNames[i]);
00559     }
00560     }
00561 
00562     if(F.constantCount != 0)
00563     {
00564     constantNames  = new char*[F.constantCount];
00565     constantCount  = F.constantCount;
00566     constantValues = new double[F.constantCount];
00567 
00568     for(i = 0; i < F.constantCount; i++)
00569     {
00570     constantNames[i] = new char[strlen(F.constantNames[i]) + 1];
00571     strcpy(constantNames[i],F.constantNames[i]);
00572     constantValues[i] = F.constantValues[i];
00573     }
00574     }
00575 
00576     evaluationDataSize = F.evaluationDataSize;
00577 
00578     if(F.evaluationData != 0)
00579     {
00580     evaluationData     = new double[evaluationDataSize];
00581     for(i=0; i< evaluationDataSize; i++)
00582     {
00583     evaluationData[i] = F.evaluationData[i];
00584     }}
00585 
00586     executionArraySize = F.executionArraySize;
00587     
00588     if(F.executionArray != 0)
00589     {
00590     executionArray     = new long[executionArraySize];
00591     for(i=0; i< executionArraySize; i++)
00592     {
00593     executionArray[i] = F.executionArray[i];
00594     }}
00595 
00596     symbolCount = F.symbolCount;
00597     sNames      = new char*[symbolCount];
00598     for(i=0; i< symbolCount; i++)
00599     {
00600     sNames[i] = new char[strlen(F.sNames[i])+1];
00601     strcpy(sNames[i],F.sNames[i]);
00602     }
00603 
00604     LibFunctions       = CAMrealOperatorLib::getFunctionArrayPtr();
00605     return 0;
00606 }
00607 /** Assignment operator. The instance is intialized using F. The
00608     data associated with the original instance is destroyed.  
00609 */
00610 void CAMsymbolicFunction::operator=(const CAMsymbolicFunction& F)
00611 {
00612     initialize(F);
00613 }
00614 
00615 long CAMsymbolicFunction::getSymbolCount() const
00616 {
00617     return symbolCount;
00618 }
00619 //
00620 //##################################################################
00621 //                      OUTPUT
00622 //##################################################################
00623 //
00624 /**
00625  Outputs the initialization string, the variable names, the symbolic 
00626  constant names and the symbolic constant values.
00627 */
00628 ostream&  operator <<(ostream& out_stream, const CAMsymbolicFunction& F)
00629 {
00630     int i;
00631      out_stream << F.getConstructorString() << endl;
00632     out_stream << " Variables : " << endl;
00633 
00634     for(i = 0; i < F.getVariableCount(); i++)
00635     {
00636     out_stream << F.getVariableName(i) << endl;
00637     }
00638 
00639     if(F.getConstantCount() > 0)
00640     {
00641     out_stream << " Constant Values : " << endl;
00642     for(i = 0; i < F.getConstantCount(); i++)
00643     {
00644     out_stream << F.getConstantName(i) << "  " << F.getConstantValue(i) << endl;
00645     }}
00646 
00647 return out_stream;
00648 
00649 }
00650 //
00651 //##################################################################
00652 //                      MEMBER FUNCTIONS
00653 //##################################################################
00654 //
00655 /**
00656  Returns the string used to initialize the CAMsymbolicFunction.
00657  */
00658  
00659 char* CAMsymbolicFunction::getConstructorString() const
00660 {
00661     return constructorString;
00662 }
00663 /**
00664  Returns the number of variables associated with the CAMsymbolicFunction.
00665 */
00666 int CAMsymbolicFunction::getVariableCount() const
00667 {
00668       return variableCount;
00669 }
00670 
00671 /**
00672 Returns the name of the ith variable associated with the 
00673 CAMsymbolicFunction.
00674 */
00675 char*  CAMsymbolicFunction::getVariableName(int i) const
00676 {
00677       return variableNames[i];
00678 }
00679 /**
00680 Returns the number of symbolic constants associated with the 
00681 CAMsymbolicFunction.
00682  */
00683 int CAMsymbolicFunction::getConstantCount() const
00684 {
00685       return constantCount;
00686 }
00687 /**
00688  Returns the name of the ith symbolic constant associated 
00689  with the CAMsymbolicFunction.
00690 */
00691 char*  CAMsymbolicFunction::getConstantName(int i) const
00692 {
00693       return constantNames[i];
00694 }
00695 /**
00696  Returns the value of the ith symbolic constant 
00697  associated with the CAMsymbolicFunction.
00698 */
00699 double  CAMsymbolicFunction::getConstantValue(int i) const
00700 {
00701       return constantValues[i];
00702 }
00703 /**
00704  Returns the value of the ith symbolic constant 
00705  associated with the CAMsymbolicFunction.
00706  
00707  @arg S: Character string with name of the symbolic constant.
00708  @arg x: Double value specifying the new value of the constant.
00709 */
00710 void  CAMsymbolicFunction::setConstantValue(char* S,double x)
00711 {
00712     int i;
00713     for(i =0; i < constantCount; i++)
00714     {
00715     if(strcmp(S,constantNames[i]) == 0) constantValues[i] = x;
00716     }
00717     setConstantEvaluationData();
00718 }
00719 
00720 char** CAMsymbolicFunction::getVariableNamePtr() const
00721 {
00722     return variableNames;
00723 }
00724 
00725 char** CAMsymbolicFunction::getConstantNamePtr() const
00726 {
00727     return constantNames;
00728 }
00729 
00730 double* CAMsymbolicFunction::getConstantValuePtr() const
00731 {
00732     return  constantValues;
00733 }
00734 void  CAMsymbolicFunction::initializeEvaluationData(const expressionTransform& T)
00735 {
00736     long symbolCount = T.getSymbolCount();
00737     char**sNames     = T.getSymbolNamesPtr();
00738     int i;  int j;
00739 
00740     if(variableCount != 0)
00741     for(i = 0; i < variableCount; i++)
00742     evaluationData[i] = 0.0;
00743 
00744     for(i = variableCount,j = 0; i < variableCount + constantCount; i++,j++)
00745     evaluationData[i] = constantValues[j];
00746 
00747     for(i = variableCount + constantCount; i < symbolCount; i++)
00748     {evaluationData[i] = atof(sNames[i]);}
00749 
00750 }
00751 
00752 void  CAMsymbolicFunction::initializeExecutionArray(const expressionTransform& T)
00753 {
00754     long* executionArrayPtr = T.getExecutionArrayPtr();
00755     int i;
00756     for(i=0; i < executionArraySize; i++)
00757     {executionArray[i] = executionArrayPtr[i];
00758     }
00759 }
00760 
00761 void  CAMsymbolicFunction::setConstantEvaluationData()
00762 {
00763     int i; int j;
00764 
00765     for(i = variableCount,j = 0; i < variableCount + constantCount; i++,j++)
00766     evaluationData[i] = constantValues[j];
00767 }
00768 
00769 /**
00770  Returns the value of the CAMsymbolicFunction 
00771  using the variable value x. 
00772 */
00773 double CAMsymbolicFunction::operator()(double x)
00774 {
00775     if(variableCount != 1) argError(1, variableCount);
00776     evaluationData[0] = x;
00777     return evaluate();
00778 }
00779 
00780 /**
00781  Returns the value of the CAMsymbolicFunction 
00782  using the variable value (x1,x2). 
00783 */
00784 double CAMsymbolicFunction::operator()(double x1, double x2)
00785 {
00786     if(variableCount != 2) argError(2, variableCount);
00787 
00788     evaluationData[0] = x1;
00789     evaluationData[1] = x2;
00790     return evaluate();
00791 
00792 }
00793 
00794 /**
00795  Returns the value of the CAMsymbolicFunction 
00796  using the variable value (x1,x2,x3). 
00797 */
00798 
00799 double CAMsymbolicFunction::operator()(double x1, double x2, double x3)
00800 {
00801      if(variableCount != 3) argError(3, variableCount);
00802 
00803     evaluationData[0] = x1;
00804     evaluationData[1] = x2;
00805     evaluationData[2] = x3;
00806     return evaluate();
00807 }
00808 
00809 /**
00810  Returns the value of the CAMsymbolicFunction 
00811  using the variable value (x1,x2,x3,x4). 
00812 */
00813 
00814 double CAMsymbolicFunction::operator()(double x1, double x2, double x3, double x4)
00815 {
00816      if(variableCount != 4) argError(4, variableCount);
00817 
00818     evaluationData[0] = x1;
00819     evaluationData[1] = x2;
00820     evaluationData[2] = x3;
00821     evaluationData[3] = x4;
00822     return evaluate();
00823 }
00824 /**
00825  Returns the value of the CAMsymbolicFunction using the 
00826  n variable values in the double array x.
00827  @arg x poiner to a double array
00828  @arg n the number of elements in x
00829 */
00830 double CAMsymbolicFunction::operator()(double*x, int n)
00831 {
00832      if(variableCount != n) argError(n, variableCount);
00833 
00834     int i;
00835     for(i = 0; i < n; i++) evaluationData[i] = x[i];
00836     return evaluate();
00837 }
00838 
00839 void CAMsymbolicFunction::argError(int argC, int vCount)
00840 {
00841     cerr << " Incorrect Number of Arguments in CAMsymbolicFunction " << endl;
00842     cerr << " Called with " << argC << " arguments, expecting " << vCount;
00843     CAMsymExit();
00844 }
00845 
00846 double CAMsymbolicFunction::evaluate()
00847 {
00848     int j;
00849     double* argData[10];   // limit of 10 args for now
00850 
00851     int functionIndex;
00852     int argCount;
00853 
00854     int executionIndex = 0;
00855     while(executionIndex < executionArraySize)
00856     {
00857     functionIndex = executionArray[executionIndex]; executionIndex++;
00858     argCount      = executionArray[executionIndex]; executionIndex++;
00859     for(j =0; j < argCount; j++)
00860     {
00861     argData[j] = &(evaluationData[executionArray[executionIndex]]);
00862     executionIndex++;
00863     }
00864     ((void(*)(double**))LibFunctions[functionIndex])(argData);
00865     }
00866 
00867     return evaluationData[evaluationDataSize - 1];
00868 }
00869 
00870 /*
00871  void CAMsymbolicFunction::createCcode()
00872  {
00873     CAMrealOperatorLib L;
00874     
00875     int binaryPlusIndex   = L.getBinaryOperatorIndex("+");
00876     int binaryMinusIndex  = L.getBinaryOperatorIndex("-");
00877     int binaryTimesIndex  = L.getOperatorIndex("*");
00878     int binaryDivideIndex = L.getOperatorIndex("/");
00879     int binaryExpoIndex   = L.getOperatorIndex("^");
00880     
00881         printf("//  \n");
00882         printf("// %s \n",getConstructorString());
00883         printf("// \n");
00884         
00885         printf("    double evalData[%d]; \n\n",evaluationDataSize);
00886         //
00887         // output variable initializations
00888         //
00889         int i; int j;
00890         if(variableCount != 0)
00891     for(i = 0; i < variableCount; i++)
00892     {
00893         printf("    evalData[%d] = %s; \n",i,getVariableName(i));
00894     }
00895     
00896     for(i = variableCount,j = 0; i < variableCount + constantCount; i++,j++)
00897     {
00898         printf("    evalData[%d] = %s; \n",i,getConstantName(j));
00899     }
00900     
00901     for(i = variableCount + constantCount; i < symbolCount; i++)
00902     {
00903         printf("    evalData[%d] = %s; \n",i,sNames[i]);
00904     }
00905         //
00906         // output evaluation code
00907         
00908     int argDataIndex[10];   // limit of 10 args for now
00909 
00910     int functionIndex;
00911     int argCount;
00912 
00913     int executionIndex = 0;
00914     while(executionIndex < executionArraySize)
00915     {
00916     functionIndex = executionArray[executionIndex]; executionIndex++;
00917     
00918     //printf("%s \n",L.getOperatorSymbol(functionIndex));
00919     
00920     argCount      = executionArray[executionIndex]; executionIndex++;
00921     
00922     for(j =0; j < argCount; j++)
00923     {
00924     argDataIndex[j] = executionArray[executionIndex];
00925     executionIndex++;
00926     }
00927     
00928     printf("    evalData[%d] = ",argDataIndex[argCount-1]);
00929     if(argCount == 2) // unary operator
00930     {
00931         printf("%s(evalData[%d]); \n",L.getOperatorSymbol(functionIndex),
00932         argDataIndex[0]);
00933     }
00934     else
00935     {
00936         if(     (functionIndex == binaryPlusIndex)||
00937                 (functionIndex == binaryMinusIndex)||
00938                 (functionIndex == binaryTimesIndex)||
00939                 (functionIndex == binaryDivideIndex))
00940         {
00941         printf("evalData[%d] %s evalData[%d];\n",
00942         argDataIndex[0],L.getOperatorSymbol(functionIndex),argDataIndex[1]);
00943         }
00944         else
00945         {
00946         if(functionIndex != binaryExpoIndex)
00947         {
00948                 printf("%s(",L.getOperatorSymbol(functionIndex));
00949             printf("evalData[%d],evalData[%d]);\n", argDataIndex[0],argDataIndex[1]);
00950         }
00951         else
00952         {
00953                 printf("pow(");
00954             printf("evalData[%d],evalData[%d]);\n", argDataIndex[0],argDataIndex[1]);
00955         }
00956         }
00957     }
00958     
00959     //((void(*)(double**))LibFunctions[functionIndex])(argData);
00960     }
00961     
00962         printf("    return evalData[%d]; \n",evaluationDataSize - 1);
00963     return;
00964  }
00965 */
00966 
00967 

Generated on Wed Jan 14 09:18:14 2009 for DoubleVector by  doxygen 1.5.1-p1