casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef SCIMATH_FUNCEXPRESSION_H
27#define SCIMATH_FUNCEXPRESSION_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/BasicSL/String.h>
32#include <casacore/scimath/Functionals/FuncExprData.h>
33#include <casacore/casa/stdvector.h>
34#include <casacore/casa/Arrays/ArrayFwd.h>
35
36//# Forward Declarations
37#include <casacore/casa/iosfwd.h>
38namespace casacore { //# NAMESPACE CASACORE - BEGIN
39
40class MUString;
41
42// <summary> An expression executable as function
43// </summary>
44
45// <use visibility=export>
46
47// <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos="">
48// </reviewed>
49
50// <prerequisite>
51// <li> <linkto class=Function>Function</linkto> class
52// </prerequisite>
53//
54// <synopsis>
55// This class acts as an interface between a program given as a string (e.g.
56// from a command line interface) and a
57// <linkto class=Function>Function</linkto> class. The grammar of the language
58// use to express the function is given below. The <src>FuncEXpression</src>
59// can be used in all places where Functions can be used (like in the
60// linear and non-linear <linkto module=Fitting>Fitting</linkto> classes.
61//
62// An expression is created by either supplying a <src>String</src> to a
63// constructor, or be setting a <src>String</src>.
64// </synopsis>
65//
66// <example>
67// </example>
68//
69// <motivation>
70// To tie the Glish language to non-linear fitting procedures
71// </motivation>
72//
73// <thrown>
74// <li> AipsError if an illegal program passed in constructor
75// </thrown>
76//
77// <todo asof="2001/11/21">
78// <li> nothing directly
79// </todo>
80
82 public:
83 //# Enumerations
84
85 //# Constructors
86 // Construct an empty executable expression
88 // Construct an executable expression from the given string
89 explicit FuncExpression(const String &prog);
90 // Make this object a (deep) copy of other.
92 // Make this object a (deep) copy of other.
94
95 // Destructor
97
98 //# Member functions
99 // Create an executable program
100 Bool create(const String &prog);
101 // Get the current error message
102 const String &errorMessage() { return error_p; }
103 // Get the executable program
104 const vector<FuncExprData::ExprOperator> &getCode() const;
105 // Get the number of parameters in executable program
106 uInt getNpar() const { return npar_p; }
107 // Get the number of dimensions of executable program
108 uInt getNdim() const {return ndim_p; }
109 // Get reference to the compiled program
110 const vector<FuncExprData::ExprOperator> &getCode() { return code_p; }
111 // Get reference to compiled constants
112 const vector<Double> &getConst() { return const_p; }
113 // Execute the program
114 Bool exec(Double &res) const;
115 // Print the stack information (mainly for debugging)
116 void print(ostream &os) const;
117
118 private:
119 //# Data
120 // The expression data /// later into a singleton
122 // The latest error message
124 // The executable code stack (a vector, since it is a re-usable stack)
125 vector<FuncExprData::ExprOperator> code_p;
126 // The reverse Polish work stack (a vector, since deque did not work on gcc)
127 vector<FuncExprData::ExprOperator> rps_p;
128 // The current state of the compilation
130 // The current constant stack
131 vector<Double> const_p;
132 // The number of parameters in code
134 // The number of dimensions of expression
136 // Executing stack
137 mutable vector<Double> exec_p;
138
139 //# Member functions
140 // Compile a statement (in prg, which will be adjusted)
142 // Compile an expression (in prg, which will be adjusted)
144 // Compile a term (in prg, which will be adjusted)
146 // Save an operation on compilation RP stack.
148 // Save a value on constant stack.
149 Bool setVal(const Double &val);
150 // Save an executable code
152 // Initialise the state
153 void initState();
154};
155
156//# Global Functions
157
158// <summary> Output function </summary>
159// <group name=output>
160// Show the program
161ostream &operator<<(ostream &os, const FuncExpression &ed);
162// </group>
164// <summary> Execute function </summary>
165// <group name=execute>
166// Execute the program
167template <class T>
168T FuncExecute(const Vector<T> &x, const Vector<T> &par);
169// </group>
170
172} //# NAMESPACE CASACORE - END
173
174#endif
Bool setVal(const Double &val)
Save a value on constant stack.
FuncExprData::ExprCompState state_p
The current state of the compilation.
const vector< FuncExprData::ExprOperator > & getCode() const
Get the executable program.
FuncExpression()
Construct an empty executable expression.
Bool compTerm(MUString &prg)
Compile a term (in prg, which will be adjusted)
uInt ndim_p
The number of dimensions of expression.
const vector< Double > & getConst()
Get reference to compiled constants.
FuncExpression(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.
const vector< FuncExprData::ExprOperator > & getCode()
Get reference to the compiled program.
const String & errorMessage()
Get the current error message.
Bool setOp(FuncExprData::ExprOperator &oper)
Save an operation on compilation RP stack.
void print(ostream &os) const
Print the stack information (mainly for debugging)
FuncExpression & operator=(const FuncExpression &other)
Make this object a (deep) copy of other.
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.
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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
ostream & operator<<(ostream &os, const IComplex &)
Show on ostream.
unsigned int uInt
Definition aipstype.h:49
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53
The compilation state descriptor.
The operator description: code; priority; # of arguments; # of arguments used up (or produced for fun...
T FuncExecute(const Vector< T > &x, const Vector< T > &par)
Execute the program.
ostream & operator<<(ostream &os, const FuncExpression &ed)
Show the program.