casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
FuncExpression.h
Go to the documentation of this file.
00001 //# FuncExpression.h: An expression executable as function
00002 //# Copyright (C) 2001,2002
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: FuncExpression.h 21024 2011-03-01 11:46:18Z gervandiepen $
00028 
00029 #ifndef SCIMATH_FUNCEXPRESSION_H
00030 #define SCIMATH_FUNCEXPRESSION_H
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <casa/BasicSL/String.h>
00035 #include <scimath/Functionals/FuncExprData.h>
00036 #include <casa/stdvector.h>
00037 
00038 //# Forward Declarations
00039 #include <casa/iosfwd.h>
00040 namespace casa { //# NAMESPACE CASA - BEGIN
00041 
00042 class MUString;
00043 template <class T> class Vector;
00044 
00045 // <summary> An expression executable as function
00046 // </summary>
00047 
00048 // <use visibility=export> 
00049 
00050 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
00051 // </reviewed>
00052 
00053 // <prerequisite>
00054 //   <li> <linkto class=Function>Function</linkto> class
00055 // </prerequisite>
00056 //
00057 // <synopsis>
00058 // This class acts as an interface between a program given as a string (e.g.
00059 // from a command line interface) and a
00060 // <linkto class=Function>Function</linkto> class. The grammar of the language
00061 // use to express the function is given below. The <src>FuncEXpression</src>
00062 // can be used in all places where Functions can be used (like in the
00063 // linear and non-linear <linkto module=Fitting>Fitting</linkto> classes.
00064 //
00065 // An expression is created by either supplying a <src>String</src> to a 
00066 // constructor, or be setting a <src>String</src>. 
00067 // </synopsis>
00068 //
00069 // <example>
00070 // </example>
00071 //
00072 // <motivation>
00073 // To tie the Glish language to non-linear fitting procedures
00074 // </motivation>
00075 //
00076 // <thrown>
00077 //    <li> AipsError if an illegal program passed in constructor
00078 // </thrown>
00079 //
00080 // <todo asof="2001/11/21">
00081 //   <li> nothing directly
00082 // </todo>
00083 
00084 class FuncExpression {
00085  public:
00086   //# Enumerations
00087 
00088   //# Constructors
00089   // Construct an empty executable expression
00090   FuncExpression();
00091   // Construct an executable expression from the given string
00092   explicit FuncExpression(const String &prog);
00093   // Make this object a (deep) copy of other.
00094   FuncExpression(const FuncExpression &other);
00095   // Make this object a (deep) copy of other.
00096   FuncExpression &operator=(const FuncExpression &other);
00097 
00098   // Destructor
00099   ~FuncExpression() {}
00100 
00101   //# Member functions
00102   // Create an executable program
00103   Bool create(const String &prog);
00104   // Get the current error message
00105   const String &errorMessage() { return error_p; }
00106   // Get the executable program
00107   const vector<FuncExprData::ExprOperator> &getCode() const;
00108   // Get the number of parameters in executable program
00109   uInt getNpar() const { return npar_p; }
00110   // Get the number of dimensions of executable program
00111   uInt getNdim() const {return ndim_p; }
00112   // Get reference to the compiled program
00113   const vector<FuncExprData::ExprOperator> &getCode() { return code_p; }
00114   // Get reference to compiled constants
00115   const vector<Double> &getConst() { return const_p; }
00116   // Execute the program
00117   Bool exec(Double &res) const;
00118   // Print the stack information (mainly for debugging)
00119   void print(ostream &os) const;
00120 
00121  private:
00122   //# Data
00123   // The expression data /// later into a singleton
00124   FuncExprData exd;
00125   // The latest error message
00126   mutable String error_p;
00127   // The executable code stack (a vector, since it is a re-usable stack)
00128   vector<FuncExprData::ExprOperator> code_p;
00129   // The reverse Polish work stack (a vector, since deque did not work on gcc)
00130   vector<FuncExprData::ExprOperator> rps_p;
00131   // The current state of the compilation
00132   FuncExprData::ExprCompState state_p;
00133   // The current constant stack
00134   vector<Double> const_p;
00135   // The number of parameters in code
00136   uInt npar_p;
00137   // The number of dimensions of expression
00138   uInt ndim_p;
00139   // Executing stack
00140   mutable vector<Double> exec_p;
00141 
00142   //# Member functions
00143   // Compile a statement (in prg, which will be adjusted)
00144   Bool compStmt(MUString &prg);
00145   // Compile an expression (in prg, which will be adjusted)
00146   Bool compExpr(MUString &prg);
00147   // Compile a term (in prg, which will be adjusted)
00148   Bool compTerm(MUString &prg);
00149   // Save an operation on compilation RP stack.
00150   Bool setOp(FuncExprData::ExprOperator &oper);
00151   // Save a value on constant stack.
00152   Bool setVal(const Double &val);
00153   // Save an executable code
00154   Bool setCode(const FuncExprData::ExprOperator &oper);
00155   // Initialise the state
00156   void initState();
00157 };
00158 
00159 //# Global Functions
00160 
00161 // <summary> Output function </summary>
00162 // <group name=output>
00163 // Show the program
00164 ostream &operator<<(ostream &os, const FuncExpression &ed);
00165 // </group>
00166 
00167 // <summary> Execute function </summary>
00168 // <group name=execute>
00169 // Execute the program
00170 template <class T>
00171 T FuncExecute(const Vector<T> &x, const Vector<T> &par);
00172 // </group>
00173 
00174 
00175 } //# NAMESPACE CASA - END
00176 
00177 #endif