casacore
FuncExpression.h
Go to the documentation of this file.
1 //# FuncExpression.h: An expression executable as function
2 //# Copyright (C) 2001,2002
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //#
27 //# $Id$
28 
29 #ifndef SCIMATH_FUNCEXPRESSION_H
30 #define SCIMATH_FUNCEXPRESSION_H
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
34 #include <casacore/casa/BasicSL/String.h>
35 #include <casacore/scimath/Functionals/FuncExprData.h>
36 #include <casacore/casa/stdvector.h>
37 #include <casacore/casa/Arrays/ArrayFwd.h>
38 
39 //# Forward Declarations
40 #include <casacore/casa/iosfwd.h>
41 namespace casacore { //# NAMESPACE CASACORE - BEGIN
42 
43 class MUString;
44 
45 // <summary> An expression executable as function
46 // </summary>
47 
48 // <use visibility=export>
49 
50 // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
51 // </reviewed>
52 
53 // <prerequisite>
54 // <li> <linkto class=Function>Function</linkto> class
55 // </prerequisite>
56 //
57 // <synopsis>
58 // This class acts as an interface between a program given as a string (e.g.
59 // from a command line interface) and a
60 // <linkto class=Function>Function</linkto> class. The grammar of the language
61 // use to express the function is given below. The <src>FuncEXpression</src>
62 // can be used in all places where Functions can be used (like in the
63 // linear and non-linear <linkto module=Fitting>Fitting</linkto> classes.
64 //
65 // An expression is created by either supplying a <src>String</src> to a
66 // constructor, or be setting a <src>String</src>.
67 // </synopsis>
68 //
69 // <example>
70 // </example>
71 //
72 // <motivation>
73 // To tie the Glish language to non-linear fitting procedures
74 // </motivation>
75 //
76 // <thrown>
77 // <li> AipsError if an illegal program passed in constructor
78 // </thrown>
79 //
80 // <todo asof="2001/11/21">
81 // <li> nothing directly
82 // </todo>
83 
85  public:
86  //# Enumerations
87 
88  //# Constructors
89  // Construct an empty executable expression
91  // Construct an executable expression from the given string
92  explicit FuncExpression(const String &prog);
93  // Make this object a (deep) copy of other.
95  // Make this object a (deep) copy of other.
97 
98  // Destructor
100 
101  //# Member functions
102  // Create an executable program
103  Bool create(const String &prog);
104  // Get the current error message
105  const String &errorMessage() { return error_p; }
106  // Get the executable program
107  const vector<FuncExprData::ExprOperator> &getCode() const;
108  // Get the number of parameters in executable program
109  uInt getNpar() const { return npar_p; }
110  // Get the number of dimensions of executable program
111  uInt getNdim() const {return ndim_p; }
112  // Get reference to the compiled program
113  const vector<FuncExprData::ExprOperator> &getCode() { return code_p; }
114  // Get reference to compiled constants
115  const vector<Double> &getConst() { return const_p; }
116  // Execute the program
117  Bool exec(Double &res) const;
118  // Print the stack information (mainly for debugging)
119  void print(ostream &os) const;
120 
121  private:
122  //# Data
123  // The expression data /// later into a singleton
125  // The latest error message
126  mutable String error_p;
127  // The executable code stack (a vector, since it is a re-usable stack)
128  vector<FuncExprData::ExprOperator> code_p;
129  // The reverse Polish work stack (a vector, since deque did not work on gcc)
130  vector<FuncExprData::ExprOperator> rps_p;
131  // The current state of the compilation
133  // The current constant stack
134  vector<Double> const_p;
135  // The number of parameters in code
137  // The number of dimensions of expression
139  // Executing stack
140  mutable vector<Double> exec_p;
141 
142  //# Member functions
143  // Compile a statement (in prg, which will be adjusted)
145  // Compile an expression (in prg, which will be adjusted)
147  // Compile a term (in prg, which will be adjusted)
149  // Save an operation on compilation RP stack.
151  // Save a value on constant stack.
152  Bool setVal(const Double &val);
153  // Save an executable code
155  // Initialise the state
156  void initState();
157 };
158 
159 //# Global Functions
160 
161 // <summary> Output function </summary>
162 // <group name=output>
163 // Show the program
164 ostream &operator<<(ostream &os, const FuncExpression &ed);
165 // </group>
166 
167 // <summary> Execute function </summary>
168 // <group name=execute>
169 // Execute the program
170 template <class T>
171 T FuncExecute(const Vector<T> &x, const Vector<T> &par);
172 // </group>
173 
174 
175 } //# NAMESPACE CASACORE - END
176 
177 #endif
Bool setVal(const Double &val)
Save a value on constant stack.
FuncExprData::ExprCompState state_p
The current state of the compilation.
FuncExpression()
Construct an empty executable expression.
const String & errorMessage()
Get the current error message.
const vector< Double > & getConst()
Get reference to compiled constants.
Bool compTerm(MUString &prg)
Compile a term (in prg, which will be adjusted)
uInt ndim_p
The number of dimensions of expression.
FuncExpression(const FuncExpression &other)
Make this object a (deep) copy of other.
FuncExpression & operator=(const FuncExpression &other)
Make this object a (deep) copy of other.
FuncExprData exd
The expression data /// later into a singleton.
vector< FuncExprData::ExprOperator > rps_p
The reverse Polish work stack (a vector, since deque did not work on gcc)
void initState()
Initialise the state.
uInt npar_p
The number of parameters in code.
Bool exec(Double &res) const
Execute the program.
Bool setOp(FuncExprData::ExprOperator &oper)
Save an operation on compilation RP stack.
void print(ostream &os) const
Print the stack information (mainly for debugging)
const vector< FuncExprData::ExprOperator > & getCode() const
Get the executable program.
~FuncExpression()
Destructor.
Bool create(const String &prog)
Create an executable program.
FuncExpression(const String &prog)
Construct an executable expression from the given string.
uInt getNdim() const
Get the number of dimensions of executable program.
const vector< FuncExprData::ExprOperator > & getCode()
Get reference to the compiled program.
uInt getNpar() const
Get the number of parameters in executable program.
Bool compStmt(MUString &prg)
Compile a statement (in prg, which will be adjusted)
vector< Double > const_p
The current constant stack.
Bool setCode(const FuncExprData::ExprOperator &oper)
Save an executable code.
String error_p
The latest error message.
vector< Double > exec_p
Executing stack.
vector< FuncExprData::ExprOperator > code_p
The executable code stack (a vector, since it is a re-usable stack)
Bool compExpr(MUString &prg)
Compile an expression (in prg, which will be adjusted)
String: the storage and methods of handling collections of characters.
Definition: String.h:225
this file contains all the compiler specific defines
Definition: mainpage.dox:28
unsigned int uInt
Definition: aipstype.h:51
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
double Double
Definition: aipstype.h:55
The compilation state descriptor.
Definition: FuncExprData.h:173
The operator description: code; priority; # of arguments; # of arguments used up (or produced for fun...
Definition: FuncExprData.h:185