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