casacore
Loading...
Searching...
No Matches
CompiledFunction.h
Go to the documentation of this file.
1//# CompiledFunction.h: Form a linear combination of Functions
2//# Copyright (C) 2002,2004,2005
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_COMPILEDFUNCTION_H
27#define SCIMATH_COMPILEDFUNCTION_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/scimath/Functionals/CompiledParam.h>
32#include <casacore/casa/BasicSL/Complex.h>
33#include <casacore/casa/BasicMath/Math.h>
34
35namespace casacore { //# NAMESPACE CASACORE - BEGIN
36
37//# Forward declarations
38
39// <summary>
40// Form a linear combination of function objects.
41// </summary>
42//
43// <use visibility=export>
44//
45// <reviewed reviewer="" date="" tests="tFuncExpression" demos="">
46// </reviewed>
47//
48// <prerequisite>
49// <li> <linkto class="Function">Function</linkto> class
50// </prerequisite>
51//
52// <synopsis>
53// Given a string describing an expression
54// (see <linkto class=FuncExpression>FuncExpression</linkto> class for
55// details of the expression), the <src>CompiledFunction</src>class wraps
56// this expression as a
57// Function (see <linkto class=Function>Function</linkto> class) which can
58// be used in all places where functions can be used (e.g. see
59// <linkto module=Fitting>Fitting</linkto>).
60//
61// The <linkto class=CompiledFunction>CompiledParam</linkto> class takes
62// care of the parameter interface.
63// </synopsis>
64//
65// <example>
66// In the following example a Gaussian profile with three parameters
67// (height, center and halfwidth) is specified and its value and
68// derivatives with respect to the parameters are calculated at
69// <src>x=[1.9,2,2.1]</src>.
70// <srcblock>
71// // the Gaussian
72// CompiledFunction<Double> prof;
73// prof.setFunction("p0*exp(-((x-p1)/p2)^2)");
74// prof[0] = 2; // the height
75// prof[1] = 1.5; // the center
76// prof[2] = 1; // the width
77// Vector<Double> x(3);
78// x[0] = 1.9; x[1] = 2.0; x[2] = 2.1;
79// for (uInt i=0; i<3; ++i) {
80// cout << "Gaussian at x=" << x[i] << ": " << prof(x[i]) << endl;
81// }
82// // Calculate automatic derivatives of same function:
83// CompiledFunction<AutoDiff<Double> > profad;
84// profad.setFunction("p0*exp(-((x-p1)/p2)^2)");
85// // Set the parameters (note the specification of the number of
86// // derivatives and which derivative the parameter is)
87// profad[0] = AutoDiff<Double>(2, 3,0); // the height
88// profad[1] = AutoDiff<Double>(1.5,3,1); // the center
89// profad[2] = AutoDiff<Double>(1, 3,2); // the width
90// for (uInt i=0; i<3; ++i) {
91// cout << "Gaussian at x=" << x[i] << ": " << profad(x[i]) << endl;
92// }
93// cout << "Value (x=2): " << profad(x[1]).value() << endl;
94// cout << "Derivatives: " << profad(x[1]).derivatives() << endl;
95// cout << "Derivative1: " << profad(x[1]).derivatives()[1] << endl;
96// </srcblock>
97// will produce the output:
98// <srcblock>
99// Gaussian at x=1.9: 1.70429
100// Gaussian at x=2: 1.5576
101// Gaussian at x=2.1: 1.39535
102// Gaussian at x=1.9: (1.70429, [0.852144, 1.36343, 0.545372])
103// Gaussian at x=2: (1.5576, [0.778801, 1.5576, 0.778801])
104// Gaussian at x=2.1: (1.39535, [0.697676, 1.67442, 1.00465])
105// Value (x=2): 1.5576
106// Derivatives: [0.778801, 1.5576, 0.778801]
107// Derivative1: 1.5576
108// </srcblock>
109// </example>
110
111// <templating arg=T>
112// <li> T should have standard numerical operators and functions.
113// <li> To obtain derivatives, the derivatives should be defined.
114// </templating>
115
116// <thrown>
117// </thrown>
118//
119// <motivation>
120// This class was created to allow specialization of the function evaluation in
121// a simple way.
122// </motivation>
123//
124// <todo asof="2002/04/29">
125// <li> Nothing I know of
126// </todo>
127
128template <class T> class CompiledFunction : public CompiledParam<T> {
129 public:
130 //# Constructors
131 // The default constructor -- no functions, no parameters, nothing, the
132 // function operator returns a 0.
134 // Make this object a (deep) copy of other.
135 // <group>
137 CompiledParam<T>(other) {}
138 template <class W>
140 CompiledParam<T>(other) {}
141 // </group>
142 // Make this object a (deep) copy of other.
145
146 // Destructor
147 virtual ~CompiledFunction() {}
148
149 //# Operators
150 // Evaluate the function at <src>x</src>.
151 virtual T eval(typename Function<T>::FunctionArg x) const;
152
153 //# Member functions
154 // Return a copy of this object from the heap. The caller is responsible for
155 // deleting the pointer.
156 // <group>
157 virtual Function<T> *clone() const {
158 return new CompiledFunction<T>(*this); }
163 // </group>
164
165};
166
167
168} //# NAMESPACE CASACORE - END
169
170#ifndef CASACORE_NO_AUTO_TEMPLATES
171#include <casacore/scimath/Functionals/CompiledFunction.tcc>
172#endif //# CASACORE_NO_AUTO_TEMPLATES
173#endif
CompiledFunction(const CompiledFunction< W > &other)
virtual ~CompiledFunction()
Destructor.
CompiledFunction< T > & operator=(const CompiledFunction< T > &other)
Make this object a (deep) copy of other.
CompiledFunction()
The default constructor – no functions, no parameters, nothing, the function operator returns a 0.
virtual Function< typename FunctionTraits< T >::BaseType > * cloneNonAD() const
virtual T eval(typename Function< T >::FunctionArg x) const
Evaluate the function at x.
CompiledFunction(const CompiledFunction< T > &other)
Make this object a (deep) copy of other.
virtual Function< typename FunctionTraits< T >::DiffType > * cloneAD() const
virtual Function< T > * clone() const
Return a copy of this object from the heap.
CompiledParam< T > & operator=(const CompiledParam< T > &other)
Make this object a (deep) copy of other.
this file contains all the compiler specific defines
Definition mainpage.dox:28