casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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>
37 
38 //# Forward Declarations
39 #include <casacore/casa/iosfwd.h>
40 namespace casacore { //# NAMESPACE CASACORE - BEGIN
41 
42 class MUString;
43 template <class T> class Vector;
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.
94  FuncExpression(const FuncExpression &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)
144  Bool compStmt(MUString &prg);
145  // Compile an expression (in prg, which will be adjusted)
146  Bool compExpr(MUString &prg);
147  // Compile a term (in prg, which will be adjusted)
148  Bool compTerm(MUString &prg);
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
FuncExpression & operator=(const FuncExpression &other)
Make this object a (deep) copy of other.
A 1-D Specialization of the Array class.
std::vector< double > Vector
Definition: ds9context.h:24
uInt getNdim() const
Get the number of dimensions of executable program.
Bool compTerm(MUString &prg)
Compile a term (in prg, which will be adjusted)
const vector< FuncExprData::ExprOperator > & getCode() const
Get the executable program.
vector< Double > exec_p
Executing stack.
void initState()
Initialise the state.
Pointed String class to aid analysis of quantity strings.
Definition: MUString.h:229
const vector< FuncExprData::ExprOperator > & getCode()
Get reference to the compiled program.
String error_p
The latest error message.
ostream & operator<<(ostream &os, const std::pair< T, U > &p)
Bool exec(Double &res) const
Execute the program.
void print(ostream &os) const
Print the stack information (mainly for debugging)
FuncExprData::ExprCompState state_p
The current state of the compilation.
The operator description: code; priority; # of arguments; # of arguments used up (or produced for fun...
Definition: FuncExprData.h:185
vector< Double > const_p
The current constant stack.
Bool compStmt(MUString &prg)
Compile a statement (in prg, which will be adjusted)
double Double
Definition: aipstype.h:55
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Bool setCode(const FuncExprData::ExprOperator &oper)
Save an executable code.
Bool create(const String &prog)
Create an executable program.
const vector< Double > & getConst()
Get reference to compiled constants.
FuncExprData exd
The expression data /// later into a singleton.
An expression executable as function.
vector< FuncExprData::ExprOperator > rps_p
The reverse Polish work stack (a vector, since deque did not work on gcc)
uInt npar_p
The number of parameters in code.
uInt getNpar() const
Get the number of parameters in executable program.
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:223
const String & errorMessage()
Get the current error message.
FuncExpression()
Construct an empty executable expression.
vector< FuncExprData::ExprOperator > code_p
The executable code stack (a vector, since it is a re-usable stack)
Bool setOp(FuncExprData::ExprOperator &oper)
Save an operation on compilation RP stack.
~FuncExpression()
Destructor.
uInt ndim_p
The number of dimensions of expression.
unsigned int uInt
Definition: aipstype.h:51
The compilation state descriptor.
Definition: FuncExprData.h:173
Bool setVal(const Double &val)
Save a value on constant stack.
Data and enumerations for functional expressions.
Definition: FuncExprData.h:74
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42