casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
CompiledFunction.h
Go to the documentation of this file.
00001 //# CompiledFunction.h: Form a linear combination of Functions
00002 //# Copyright (C) 2002,2004,2005
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //#
00027 //# $Id: CompiledFunction.h 21024 2011-03-01 11:46:18Z gervandiepen $
00028 
00029 #ifndef SCIMATH_COMPILEDFUNCTION_H
00030 #define SCIMATH_COMPILEDFUNCTION_H
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <scimath/Functionals/CompiledParam.h>
00035 #include <casa/BasicSL/Complex.h>
00036 #include <casa/BasicMath/Math.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 //# Forward declarations
00041 
00042 // <summary>
00043 // Form a linear combination of function objects.
00044 // </summary>
00045 //
00046 // <use visibility=export>
00047 //
00048 // <reviewed reviewer="" date="" tests="tFuncExpression" demos="">
00049 // </reviewed>
00050 //
00051 // <prerequisite>
00052 //   <li> <linkto class="Function">Function</linkto> class
00053 // </prerequisite>
00054 //
00055 // <synopsis>
00056 // Given a string describing an expression
00057 // (see <linkto class=FuncExpression>FuncExpression</linkto> class for
00058 // details of the expression), the <src>CompiledFunction</src>class wraps
00059 // this expression as a 
00060 // Function (see <linkto class=Function>Function</linkto> class) which can
00061 // be used in all places where functions can be used (e.g. see
00062 // <linkto module=Fitting>Fitting</linkto>).
00063 //
00064 // The <linkto class=CompiledFunction>CompiledParam</linkto> class takes
00065 // care of the parameter interface.
00066 // </synopsis>
00067 //
00068 // <example>
00069 // In the following example a Gaussian profile with three parameters
00070 // (height, center and halfwidth) is specified and its value and
00071 // derivatives with respect to the parameters are calculated at
00072 // <src>x=[1.9,2,2.1]</src>.
00073 // <srcblock>
00074 // // the Gaussian
00075 // CompiledFunction<Double> prof;
00076 // prof.setFunction("p0*exp(-((x-p1)/p2)^2)");
00077 // prof[0] = 2;                         // the height
00078 // prof[1] = 1.5;                       // the center
00079 // prof[2] = 1;                         // the width
00080 // Vector<Double> x(3);
00081 // x[0] = 1.9; x[1] = 2.0; x[2] = 2.1;
00082 // for (uInt i=0; i<3; ++i) {
00083 //   cout << "Gaussian at x=" << x[i] << ": " << prof(x[i]) << endl;
00084 // }
00085 // // Calculate automatic derivatives of same function:
00086 // CompiledFunction<AutoDiff<Double> > profad;
00087 // profad.setFunction("p0*exp(-((x-p1)/p2)^2)");
00088 // // Set the parameters (note the specification of the number of
00089 // // derivatives and which derivative the parameter is)
00090 // profad[0] = AutoDiff<Double>(2,  3,0);       // the height
00091 // profad[1] = AutoDiff<Double>(1.5,3,1);       // the center
00092 // profad[2] = AutoDiff<Double>(1,  3,2);       // the width
00093 // for (uInt i=0; i<3; ++i) {
00094 //   cout << "Gaussian at x=" << x[i] << ": " << profad(x[i]) << endl;
00095 // }
00096 // cout << "Value (x=2): " << profad(x[1]).value() << endl;
00097 // cout << "Derivatives: " << profad(x[1]).derivatives() << endl;
00098 // cout << "Derivative1: " << profad(x[1]).derivatives()[1] << endl;
00099 // </srcblock>
00100 // will produce the output:
00101 // <srcblock>
00102 //      Gaussian at x=1.9: 1.70429
00103 //      Gaussian at x=2: 1.5576
00104 //      Gaussian at x=2.1: 1.39535
00105 //      Gaussian at x=1.9: (1.70429, [0.852144, 1.36343, 0.545372])
00106 //      Gaussian at x=2: (1.5576, [0.778801, 1.5576, 0.778801])
00107 //      Gaussian at x=2.1: (1.39535, [0.697676, 1.67442, 1.00465])
00108 //      Value (x=2): 1.5576
00109 //      Derivatives: [0.778801, 1.5576, 0.778801]
00110 //      Derivative1: 1.5576
00111 // </srcblock>
00112 // </example>
00113 
00114 // <templating arg=T>
00115 //  <li> T should have standard numerical operators and functions.
00116 //  <li> To obtain derivatives, the derivatives should be defined.
00117 // </templating>
00118 
00119 // <thrown>
00120 // </thrown>
00121 //
00122 // <motivation>
00123 // This class was created to allow specialization of the function evaluation in
00124 // a simple way.
00125 // </motivation>
00126 //
00127 // <todo asof="2002/04/29">
00128 // <li> Nothing I know of
00129 // </todo>
00130 
00131 template <class T> class CompiledFunction : public CompiledParam<T> {
00132  public:
00133   //# Constructors
00134   // The default constructor -- no functions, no parameters, nothing, the
00135   // function operator returns a 0.
00136   CompiledFunction() : CompiledParam<T>() {}
00137   // Make this object a (deep) copy of other.
00138   // <group>
00139   CompiledFunction(const CompiledFunction<T> &other) :
00140     CompiledParam<T>(other) {}
00141   template <class W>
00142     CompiledFunction(const CompiledFunction<W> &other) :
00143     CompiledParam<T>(other) {}
00144   // </group>
00145   // Make this object a (deep) copy of other.
00146   CompiledFunction<T> &operator=(const CompiledFunction<T> &other) {
00147     CompiledParam<T>::operator=(other); return *this; }
00148 
00149   // Destructor
00150   virtual ~CompiledFunction() {}
00151 
00152   //# Operators
00153   // Evaluate the function at <src>x</src>.
00154   virtual T eval(typename Function<T>::FunctionArg x) const;
00155   
00156   //# Member functions
00157   // Return a copy of this object from the heap. The caller is responsible for
00158   // deleting the pointer.
00159   // <group>
00160   virtual Function<T> *clone() const {
00161     return new CompiledFunction<T>(*this); }
00162   virtual Function<typename FunctionTraits<T>::DiffType> *cloneAD() const {
00163     return new CompiledFunction<typename FunctionTraits<T>::DiffType>(*this); }
00164   virtual Function<typename FunctionTraits<T>::BaseType> *cloneNonAD() const {
00165     return new CompiledFunction<typename FunctionTraits<T>::BaseType>(*this); }
00166   // </group>
00167   
00168 };
00169 
00170 
00171 } //# NAMESPACE CASA - END
00172 
00173 #ifndef CASACORE_NO_AUTO_TEMPLATES
00174 #include <scimath/Functionals/CompiledFunction.tcc>
00175 #endif //# CASACORE_NO_AUTO_TEMPLATES
00176 #endif