00001 //# Function.h: Numerical functional interface class 00002 //# Copyright (C) 2001,2002,2003,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 //# $Id$ 00027 00028 #ifndef SCIMATH_FUNCTION_H 00029 #define SCIMATH_FUNCTION_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <casa/Arrays/Vector.h> 00034 #include <casa/BasicMath/Functional.h> 00035 #include <casa/Utilities/Assert.h> 00036 #include <scimath/Functionals/FunctionParam.h> 00037 #include <scimath/Functionals/FunctionTraits.h> 00038 00039 //# Forward declarations 00040 #include <casa/iosfwd.h> 00041 class casa::String; 00042 00043 namespace casa { //# NAMESPACE CASA - BEGIN 00044 00045 class RecordInterface; 00046 00047 // <summary> Numerical functional interface class 00048 // </summary> 00049 00050 // <use visibility=export> 00051 00052 // <reviewed reviewer="tcornwel" date="1996/02/22" tests="tGaussian2D" 00053 // demos=""> 00054 // </reviewed> 00055 00056 // <prerequisite> 00057 // <li> <linkto class="Functional">Functional</linkto> 00058 // <li> <linkto class="FunctionParam">FunctionParam</linkto> 00059 // </prerequisite> 00060 // 00061 // <synopsis> 00062 // A <src>Function</src> is used for classes which map a 00063 // scalar or n-dimensional Vector of type <src>T</src> into a <src>T</src>. 00064 // The object also has zero or more parameters which can be masked 00065 // if necessary, and be used in the <src>Fitting</src> module, and, implicitly, 00066 // in the <linkto class=AutoDiff>AutoDiff</linkto> differentiation module. 00067 // 00068 // The parameter interface is provided by the 00069 // <linkto class="FunctionParam"><src>FunctionParam</src></linkto> class. 00070 // 00071 // A Function can have a <src>name()</src> which can be used in generic 00072 // interfaces. 00073 // 00074 // The function calls implemented are: 00075 // <ul> 00076 // <li> <src>operator()()</src> 00077 // <li> <src>operator()(const T &x)</src> 00078 // <li> <src>operator()(const Vector<T> &x)</src> 00079 // <li> <src>operator()(Function::FunctionArg x)</src> 00080 // <li> <src>operator()(const T &x, const T &y)</src> (for 2D) 00081 // <li> <src>operator()(const T &x, const T &y, const T &z)</src> (for 3D) 00082 // </ul> 00083 // The <src>T</src> in the above is the <src>Function::ArgType</src> 00084 // as derived from the <linkto class="FunctionTraits">FunctionTraits</linkto> 00085 // class. 00086 // These calls are (in debug mode) tested for the correct number of arguments, 00087 // after which they call a <src>T eval(FunctionArg x) const = 0</src> to 00088 // be implemented in derived classes. The derived class should also implement 00089 // an <src>uInt ndim() const = 0</src>. The derived class can access the 00090 // nth parameter with the <src>[n]</src> operator, and the corresponding 00091 // mask with <src>mask(n)</src> method. 00092 // The variables are referenced with <src>x[i]</src>. 00093 // 00094 // </synopsis> 00095 00096 // <example> 00097 // A complete implementation of say an <src>A.sin(2pi.f.x)</src> with 00098 // parameters amplitude(<em>A</em>) and frequency(<em>f</em>) and variable 00099 // time(<em>x</em>) could be: 00100 // <srcblock> 00101 // //# Sinusoid.h 00102 // #include <casa/aips.h> 00103 // #include <scimath/Functionals/Function.h> 00104 // #include <casa/BasicSL/Constants.h> 00105 // #include <casa/BasicMath/Math.h> 00106 // // The sinusoid class 00107 // template<class T> class Sinusoid : public Function<T> { 00108 // public: 00109 // // For easy reference of the parameters 00110 // enum { AMPL=0, FREQ }; 00111 // // Constructors. Defaults are A=1, f=1 00112 // Sinusoid() : Function<T>(2) { 00113 // param_p[AMPL] = T(1.0); param_p[FREQ] = T(1.0); } 00114 // explicit Sinusoid(const T &l) : Function<T>(2) { 00115 // param_p[AMPL] = ampl; param_p[FREQ] = T(1.0); } 00116 // Sinusoid(const T &l, const T &freq) : Function<T>(2) { 00117 // param_p[AMPL] = ampl; param_p[FREQ] = freq; } 00118 // Sinusoid(const Sinusoid &other) : Function<T>(2) { 00119 // param_p[AMPL] = other.param_p[AMPL]; 00120 // param_p[FREQ] = other.parameter[FREQ]; } 00121 // Sinusoid<T> &operator=(const Sinusoid<T> &other) { 00122 // if (this != &other) param_p = other.param_p; 00123 // return *this; } 00124 // virtual ~Sinusoid() {}; 00125 // // Dimensionality 00126 // virtual uInt ndim() const { return 2; }; 00127 // // Evaluate 00128 // virtual T eval(Function<T>::FunctionArg x) const { 00129 // return param_p[AMPL]*sin(T(C::_2pi)*param_p[FREQ]*x[0]); }; 00130 // // Copy it 00131 // virtual Function<T> *clone() const { return new Sinusoid<T>(param_p); }; 00132 // }; 00133 // </srcblock> 00134 // The following will calculate the value and the derivative for 00135 // <src>A=2; f=3; x=0.1;</src> 00136 // <srcblock> 00137 // // The function objects for value, and for value + derivative 00138 // Sinusoid<Double> soid1(2.0, 3.0); 00139 // typedef AutoDiff<Double> Adif; 00140 // Sinusoid<Adif> soid2(Adif(2,2,0), Adif(3,2,1)); 00141 // cout << "Value: " << soid1(0.1) << endl; 00142 // cout << "(val, deriv): " << soid2(Adif(0.1)) << endl; 00143 // </srcblock> 00144 // 00145 // A shorter version, where all parameter handling is done at user level 00146 // could be: 00147 // <srcblock> 00148 // //# Sinusoid.h 00149 // #include <casa/aips.h> 00150 // #include <scimath/Functionals/Function.h> 00151 // #include <casa/BasicSL/Constants.h> 00152 // #include <casa/BasicMath/Math.h> 00153 // template<class T> class Sinusoid : public Function<T> { 00154 // public: 00155 // enum { AMPL=0, FREQ }; 00156 // Sinusoid() : Function<T>(2){param_p[AMPL] T(1);param_p[FREQ]=T(1);} 00157 // virtual ~Sinusoid() {}; 00158 // virtual uInt ndim() const { return 2; }; 00159 // virtual T eval(Function<T>::FunctionArg x) const { 00160 // return param_p[AMPL]*sin(T(C::_2pi)*param_p[FREQ]*x[0]); }; 00161 // virtual Function<T> *clone() const { return new Sinusoid<T>param_p; }; 00162 // }; 00163 // </srcblock> 00164 // The following will calculate the value and the derivative for 00165 // <src>A=2; f=3; x=0.1;</src> 00166 // <srcblock> 00167 // // The function objects for value, and for value + derivative 00168 // typedef AutoDiff<Double> Adif; 00169 // typedef Function<Double> FD; 00170 // typedef Function<AutoDiff<Double> > FAdif 00171 // Sinusoid<Double> soid1; 00172 // Sinusoid<Adif> soid2; 00173 // soid1[FD::AMPL] = 2; soid1[FD::FREQ] = 3; 00174 // soid2[FAdif::AMPL] = Adif(2,2,0); 00175 // soid2[FAdif::FREQ] = Adif(3,2,1); 00176 // cout << "Value: " << soid1(0.1) << endl; 00177 // cout << "(val, deriv): " << soid2(Adif(0.1)) << endl; 00178 // </srcblock> 00179 // </example> 00180 00181 // <motivation> 00182 // A function of more than one variable was required for a function which 00183 // represents the sky brightness. Adjustable parameters were required for 00184 // non-linear least squares fitting. 00185 // </motivation> 00186 // 00187 // <templating arg=T> 00188 // <li> Besides the requirements set by the 00189 // <linkto class="Functional">Functional</linkto> base class, it must be 00190 // possible to form a <src>Vector<T></src>. 00191 // </templating> 00192 // 00193 // <todo asof="2005/01/20"> 00194 // <li> At some point, we may want to implement a letter-envelope class, 00195 // implement function arithmetic, etc. 00196 // <li> use maybe Poolstack for static Vector 00197 // </todo> 00198 00199 template<class T, class U=T> class Function : 00200 public Functional<typename FunctionTraits<T>::ArgType, U>, 00201 public Functional<Vector<typename FunctionTraits<T>::ArgType>, U> { 00202 00203 public: 00204 //# Typedefs 00205 typedef typename FunctionTraits<T>::ArgType ArgType; 00206 typedef const ArgType* FunctionArg; 00207 00208 //# Constructors 00209 // Constructors 00210 // <group> 00211 Function() : param_p(), arg_p(0), parset_p(False), locked_p(False) {}; 00212 explicit Function(const uInt n) : param_p(n), arg_p(0), parset_p(False), 00213 locked_p(False) {}; 00214 explicit Function(const Vector<T> &in) : param_p(in), arg_p(0), 00215 parset_p(False), locked_p(False) {}; 00216 Function(const FunctionParam<T> &other) : param_p(other), arg_p(0), 00217 parset_p(False), locked_p(False) {}; 00218 template <class W, class X> 00219 Function(const Function<W,X> &other) : param_p(other.parameters()), 00220 arg_p(0), parset_p(other.parsetp()), locked_p(False) {} 00221 Function(const Function<T,U> &other) : param_p(other.param_p), 00222 arg_p(other.arg_p), parset_p(other.parset_p), locked_p(False) {} 00223 // </group> 00224 00225 // Destructor 00226 virtual ~Function() {}; 00227 00228 // Returns the number of dimensions of function 00229 virtual uInt ndim() const = 0; 00230 // Returns the number of parameters 00231 uInt nparameters() const { return param_p.nelements(); } 00232 00233 // Evaluate the function object 00234 virtual U eval(FunctionArg x) const = 0; 00235 00236 //# Operators 00237 // Manipulate the nth parameter (0-based) with no index check 00238 // <group> 00239 T &operator[](const uInt n) { parset_p |= !locked_p; 00240 return param_p[n]; }; 00241 const T &operator[](const uInt n) const { return param_p[n]; } 00242 // </group> 00243 // Evaluate this function object at <src>x</src>or at <src>x, y</src>. 00244 // The length of <src>x</src> must be greater than or equal to 00245 // <src>ndim()</src>. 00246 // <group> 00247 virtual U operator()() const { 00248 DebugAssert(ndim()==0, AipsError); return eval(FunctionArg(0)); }; 00249 virtual U operator()(const ArgType &x) const { 00250 DebugAssert(ndim()<=1, AipsError); return eval(&x); }; 00251 virtual U operator()(const Vector<ArgType> &x) const; 00252 virtual U operator()(FunctionArg x) const { return eval(x); }; 00253 virtual U operator()(const ArgType &x, const ArgType &y) const; 00254 virtual U operator()(const ArgType &x, const ArgType &y, 00255 const ArgType &z) const; 00256 // </group> 00257 00258 //# Member functions 00259 // Specify the name associated with the function (default will be 00260 // <src>unknown</src>) 00261 virtual const String &name() const; 00262 // Manipulate the mask associated with the nth parameter 00263 // (e.g. to indicate whether the parameter is adjustable or 00264 // nonadjustable). 00265 // Note: no index check. 00266 // <group> 00267 Bool &mask(const uInt n) { parset_p |= !locked_p; 00268 return param_p.mask(n); }; 00269 const Bool &mask(const uInt n) const { return param_p.mask(n); }; 00270 // </group> 00271 // Return the parameter interface 00272 // <group> 00273 const FunctionParam<T> ¶meters() const { return param_p; }; 00274 FunctionParam<T> ¶meters() { parset_p = True; return param_p; }; 00275 // </group> 00276 // Get <src>arg_p</src> and <src>parset_p</src>. Necessary for reasons 00277 // of protection in the copying of non-conforming Functions. 00278 // <group> 00279 const Vector<ArgType> &argp() const { return arg_p; }; 00280 const Bool parsetp() const { return parset_p; }; 00281 // </group> 00282 // Compiler cannot always find the correct 'const' version of parameter 00283 // access. In cases where this would lead to excessive overheads in 00284 // moving parameters around (like in <src>CompoundFunction</src>) the 00285 // parameter changing can be set to be locked, and no changes are 00286 // assumed. 00287 // <group> 00288 void lockParam() { locked_p = True; }; 00289 void unlockParam() { locked_p = False; }; 00290 // </group> 00291 00292 // get/set the function mode. These provide an interface to 00293 // function-specific configuration or state that controls how the 00294 // function calculates its values but otherwise does not qualify as 00295 // a parameter. Some part of the state, for example, might have a 00296 // type different from that of T. The state is passed as fields of a 00297 // record, mode--the names, types and values of which are specific to 00298 // the implementing function and should be documented in the implementing 00299 // class. It is recommended that all possible inputs passed to this 00300 // function via setMode() be considered optional such that if the 00301 // record omits a legal field, that part of the state is left unchanged. 00302 // Fields not recognized by the implementing class should be ignored. 00303 // An exception should be thrown if a recognized field contains illegal 00304 // data. The default implementations for both getMode() and setMode() 00305 // ignore the input record. 00306 // <group> 00307 virtual void setMode(const RecordInterface& mode); 00308 virtual void getMode(RecordInterface& mode) const; 00309 // </group> 00310 00311 // return True if the implementing function supports a mode. The default 00312 // implementation returns False. 00313 virtual Bool hasMode() const; 00314 00315 // Print the function (i.e. the parameters) 00316 ostream &print(ostream &os) const { return param_p.print(os); }; 00317 // Return a copy of this object from the heap. The caller is responsible 00318 // for deleting this pointer. The <src>cloneAD</src> will return a clone 00319 // with an <src>AutoDef<T></src>; the <src>cloneNonAD</src> a clone 00320 // with <src><T></src>. An <src>AipsError</src> will be thrown if the 00321 // <src>cloneAD()</src> or <src>cloneNonAD()</src> is not implemented 00322 // for a specific function. 00323 // <group> 00324 virtual Function<T,U> *clone() const = 0; 00325 virtual Function<typename FunctionTraits<T>::DiffType> *cloneAD() const; 00326 virtual Function<typename FunctionTraits<T>::BaseType> 00327 *cloneNonAD() const; 00328 // </group> 00329 00330 protected: 00331 //# Data 00332 // The parameters and masks 00333 FunctionParam<T> param_p; 00334 // Aid for non-contiguous argument storage 00335 mutable Vector<ArgType> arg_p; 00336 // Indicate parameter written 00337 mutable Bool parset_p; 00338 // Indicate that parameters are expected to be locked from changing 00339 mutable Bool locked_p; 00340 }; 00341 00342 //# Global functions 00343 // <summary> Global functions </summary> 00344 // <group name=Output> 00345 // Output declaration 00346 template<class T, class U> 00347 ostream &operator<<(ostream &os, const Function<T,U> &fun); 00348 // </group> 00349 00350 //# Inlines 00351 template<class T, class U> 00352 inline ostream &operator<<(ostream &os, const Function<T,U> &fun) { 00353 return fun.print(os); } 00354 00355 } //# NAMESPACE CASA - END 00356 00357 #ifndef AIPS_NO_TEMPLATE_SRC 00358 #include <scimath/Functionals/Function.cc> 00359 #endif //# AIPS_NO_TEMPLATE_SRC 00360 #endif
1.5.1