casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
CompiledParam.h
Go to the documentation of this file.
00001 //# CompiledParam.h: Parameters for a compiled string function
00002 //# Copyright (C) 2002,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: CompiledParam.h 21024 2011-03-01 11:46:18Z gervandiepen $
00028 
00029 #ifndef SCIMATH_COMPILEDPARAM_H
00030 #define SCIMATH_COMPILEDPARAM_H
00031 
00032 //# Includes
00033 #include <casa/aips.h>
00034 #include <casa/BasicSL/String.h>
00035 #include <scimath/Functionals/Function.h>
00036 #include <scimath/Functionals/FuncExpression.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 // <summary>
00041 // Parameters for a compiled string function object.
00042 // </summary>
00043 //
00044 // <use visibility=local>
00045 //
00046 // <reviewed reviewer="" date="" tests="tFuncExpression" demos="">
00047 // </reviewed>
00048 //
00049 // <prerequisite>
00050 //   <li> <linkto class="FuncExpression">FuncExpression</linkto> class
00051 // </prerequisite>
00052 //
00053 // <synopsis>
00054 // Given a string describing an expression
00055 // (see <linkto class=FuncExpression>FuncExpression</linkto> class for
00056 // details of the expression), the <src>CompiledFunction</src>class wraps
00057 // this expression as a 
00058 // Function (see <linkto class=Function>Function</linkto> class) which can
00059 // be used in all places where functions can be used (e.g. see
00060 // <linkto module=Fitting>Fitting</linkto>).
00061 //
00062 // This class takes care of the
00063 // <linkto class=CompiledFunction>CompiledFunction</linkto> parameter interface
00064 // (see <linkto class=FunctionParam>FunctionParam</linkto> class for details).
00065 // </synopsis>
00066 //
00067 // <example>
00068 // In the following example a Gaussian profile with three parameters
00069 // (height, center and halfwidth) is specified and its value and
00070 // derivatives with respect to the parameters are calculated at <src>x=2</src>.
00071 // <srcblock>
00072 // // the Gaussian
00073 // CompiledFunction<Double> prof("p0*exp(-((x-p1)/p2)^2)");
00074 // prof[0] = 2;                         // the height
00075 // prof[1] = 1.5;                       // the center
00076 // prof[2] = 1;                         // the width
00077 // Vector<Double> x(3);
00078 // X[0] = 1.9; x[1] = 2.0; x[2] = 2.1;
00079 // cout << "Gaussian at x=" << x << ": " << prof(x) << endl;
00080 // // and an automatic derivative one:
00081 // CompiledFunction<AutoDiff<Double> > profad("p0*exp(-((x-p1)/p2)^2)");
00082 // cout << "Gaussian at x=" << x << ": " << profad(x) << endl;
00083 // </srcblock>
00084 // will produce the output:
00085 // <srcblock>
00086 // </srcblock>
00087 // </example>
00088 
00089 // <templating arg=T>
00090 //  <li> T should have standard numerical operators and functions.
00091 //  <li> To obtain derivatives, the derivatives should be defined.
00092 // </templating>
00093 
00094 // <thrown>
00095 // </thrown>
00096 
00097 // <motivation>
00098 // This class was created to allow specialization of the function evaluation in
00099 // a simple way.
00100 // </motivation>
00101 //
00102 // <todo asof="2002/04/29">
00103 // <li> Nothing I know of
00104 // </todo>
00105 
00106 template <class T> class CompiledParam : public Function<T> {
00107  public:
00108   //# Constructors
00109   // The default constructor -- no functions, no parameters, nothing, the
00110   // function operator returns a 0.
00111   CompiledParam();
00112   // Make this object a (deep) copy of other.
00113   // <group>
00114   CompiledParam(const CompiledParam<T> &other);
00115   template <class W>
00116     CompiledParam(const CompiledParam<W> &other) :
00117     Function<T>(other), ndim_p(other.ndim()), msg_p(other.errorMessage()),
00118     text_p(other.getText()),
00119     functionPtr_p(new FuncExpression(*other.getFunctionPtr())) {}
00120   // </group>
00121   // Make this object a (deep) copy of other.
00122   CompiledParam<T> &operator=(const CompiledParam<T> &other);
00123   // Destructor
00124   virtual ~CompiledParam();
00125 
00126   //# Operators
00127   
00128   //# Member functions
00129   // Give name of function
00130   virtual const String &name() const { static String x("compiled");
00131     return x; }
00132 
00133   // Set a function. The return will be False (and an error message will be
00134   // set) if a compilation error occurs 
00135   Bool setFunction(const String &newFunction);
00136 
00137   // Return the error message of the compilation
00138   const String &errorMessage() const { return msg_p; }
00139 
00140   // Return the expression
00141   const FuncExpression &function() const;
00142 
00143   // Returns the dimension of function
00144   virtual uInt ndim() const { return ndim_p; }
00145 
00146   // Returns the text of the function string
00147   const String &getText() const { return text_p; }
00148 
00149   // Returns the function pointer (for debugging)
00150   const FuncExpression* getFunctionPtr() const {
00151     return functionPtr_p; }
00152 
00153 protected:
00154   //# Data
00155   // Number of dimensions of underlying function
00156   uInt ndim_p;
00157   // Possible error message
00158   String msg_p;
00159   // Input text string
00160   String text_p;
00161   
00162   // Pointer to function
00163   FuncExpression *functionPtr_p;
00164 
00165 };
00166 
00167 
00168 } //# NAMESPACE CASA - END
00169 
00170 #ifndef CASACORE_NO_AUTO_TEMPLATES
00171 #include <scimath/Functionals/CompiledParam.tcc>
00172 #endif //# CASACORE_NO_AUTO_TEMPLATES
00173 #endif