casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Functionals.h
Go to the documentation of this file.
00001 //# Functionals.h: A module that represents various function-like classes.
00002 //# Copyright (C) 1995,1996,1998,1999,2001,2002
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: Functionals.h 20670 2009-07-10 01:43:43Z Malte.Marquarding $
00027 
00028 
00029 #ifndef SCIMATH_FUNCTIONALS_H
00030 #define SCIMATH_FUNCTIONALS_H
00031 
00032 //# Base classes
00033 #include <casa/BasicMath/Functional.h>
00034 #include <scimath/Functionals/FunctionTraits.h>
00035 #include <scimath/Functionals/FunctionParam.h>
00036 #include <scimath/Functionals/Function.h>
00037 #include <scimath/Functionals/Function1D.h>
00038 
00039 //# Combination methods
00040 #include <scimath/Functionals/FunctionWrapper.h>
00041 #include <scimath/Functionals/CombiFunction.h>
00042 #include <scimath/Functionals/CompoundFunction.h>
00043 
00044 //# remainder will be removed
00045 #include <scimath/Functionals/SampledFunctional.h>
00046 
00047 //# 1-D Functions
00048 #include <scimath/Functionals/Interpolate1D.h>
00049 #include <scimath/Functionals/ArraySampledFunctional.h>
00050 #include <scimath/Functionals/ScalarSampledFunctional.h>
00051 
00052 namespace casa { //# NAMESPACE CASA - BEGIN
00053 
00054 // <module>
00055 //
00056 // <summary>A module that represents various function-like classes.</summary>
00057 
00058 // <reviewed reviewer="tcornwel" date="1996/02/13" demos=""></reviewed>
00059 
00060 // <etymology>
00061 // The term <src>Functional</src> was chosen to roughly follow the usage in
00062 // Barton and Nackman's <em>Scientific and Engineering C++</em>.
00063 // Functional classes map a Domain object into a Range object, rather like a
00064 // mathematical <src>function</src>. They use <src>operator()</src>,
00065 // so they look much like single argument C++ <src>functions</src>.
00066 // </etymology>
00067 //
00068 // <synopsis>
00069 // <src>Functionals</src> and their derived classes map an input
00070 // <src>Domain</src> object into an output <src>Range</src> object using the 
00071 // <src>operator()</src>.
00072 // Often the input and output types are numeric, but it can be of any type. 
00073 // <srcblock>
00074 // class Offspring : public Functional<List<Parents>, List<Children> > {
00075 //  public:
00076 //    List<Children> operator()(List<Parents>);
00077 // };
00078 // </srcblock>
00079 // would be a legal Functional.
00080 // 
00081 // The <src>Functions</src> and their derived classes map, again using the
00082 // <src>operator()</src>, numeric value(s) into a numeric value. Since they are
00083 // numeric, the <src>Domain</src> and <src>Range</src> base type can be of type
00084 // <src>AutoDiff<T></src> (where <src>T</src> is numeric base type) or one
00085 // of its derivations, in which case the value and its derivatives will be
00086 // calculated.
00087 // 
00088 // <note role=warning> In the current version the <src>Domain</src> and 
00089 // <src>Range</src> are the same for Functions </note>
00090 //
00091 // The basic classes are:
00092 // <dl>
00093 // <dt> <linkto class=Functional><src>Functional<Domain, Range></src></linkto>
00094 // <dd>
00095 // A base class that maps a <src>Domain</src> object into a <src>Range</src>
00096 // object using the <src>Range operator(const Domain &)</src>. All
00097 // information necessary to convert the <src>Domain</src> into a
00098 // <src>Range</src> will be available in the class
00099 // or in the input information. No variable class state (<em>parameters</em>)
00100 // are available.
00101 // 
00102 // <dt> <linkto class=FunctionParam><src>FunctionParam<T></src></linkto>
00103 // <dd> A helper base class that acts as a container for <em>parameters</em>
00104 // (<em>state</em>) used in <src>Function</src> classes. The class contains
00105 // a list of parameters, and a list of flags associated with the parameters.
00106 // Methods to set and obtain the parameters (using <src>operator[]</src>)
00107 // and their flags (using methods <src>mask()</src>) are available. The flags
00108 // can e.g. be used to indicate to <src>Fitting</src> routines if a certain
00109 // parameter has to be updated ('fitted') or not.
00110 // <note role=tip>
00111 // The FunctionParam class does not assume anything about the uses of the
00112 // class, but leaves that to the final users. This means that a lot of
00113 // copying between intermediate and final users is not necessary
00114 // (like between a Gaussian fitter with fixed parameters
00115 // and the Fitting routines: the Gaussian fitter just sets a flag to False, and
00116 // let the Fitting worry about what to do internally).
00117 // </note>
00118 // 
00119 // <dt> <linkto class=Function><src>Function<T></src></linkto>
00120 // <dd> Base class for function objects with zero or more parameters (i.e.
00121 // Functionals with state).
00122 // All parameters should be of the same type <em>T</em> as the <src>
00123 // Function<T></src>. <src>Function</src> objects are specifically geared
00124 // towards use in the <linkto module=Fitting>Fitting</linkto> classes, but
00125 // can be used anywhere where the value (and/or derivatives) of functions
00126 // are needed.
00127 //
00128 // The <src>Function<T></src> class is derived from <src>Functional</src>
00129 // and contains a <src>FunctionParam<T></src> object.
00130 // The parameters act as state for the function
00131 // (e.g. a width for a Gaussian). A function object is called using the
00132 // <src>T operator(const T&)</src> (<em>ndim=1</em>), or the
00133 // <src>T operator(const Vector<T>&)</src> (all values of <em>ndim</em>), or
00134 // <src>T operator(const T&, const T&)</src> (for <em>ndim=2</em> only).
00135 // If the template argument is <src>AutoDiff<T></src>, the parameters and the
00136 // returned value will be <src>AutoDiff<T></src>; the arguments of the
00137 // <src>operator()</src> will be of type <src>T</src>. The returned value
00138 // of the function will be the function value at <em>x</em> (and the
00139 // derivatives w.r.t. the non-masked parameters) Using <src>AutoDiffA<T></src>
00140 // the derivatives can be calculated w.r.t. parameters and/or arguments, see
00141 // <linkto class=AutoDiff>AutoDiff</linkto> and <linkto class=FunctionTraits>
00142 // FunctionTraits</linkto> for details. 
00143 // 
00144 // <note role=tip>
00145 // A <src>Function1D</src> is provided for 1-dimensional function objects
00146 // </note>
00147 // </dl>
00148 // 
00149 //  Actual functional classes:
00150 // <dl>
00151 // <dt> e.g. <linkto
00152 // class=Gaussian1D><src>Gaussian1D<T></src></linkto>
00153 // <dd> An actual function object will be derived from 
00154 // <src>Function<T></src>. The minimum functionality of a Function
00155 // object will be support for the <src>operator()</src> methods (through a
00156 // single, hidden, <src>eval()</src> method); for the manipulation of the
00157 // associated parameters (using <src>operator[index]</src> and
00158 // <src>mask(index)</src>) and some administrative aids (<src>ndim()</src>,
00159 // <src>nparameters()</src> and the like.
00160 //
00161 // In most cases it is advantageous to have a special parameter handling
00162 // class (e.g. <src>Gaussian1DParam</src>), to separate the (template
00163 // independent) parameter handling from the possible specialization of
00164 // the <src>eval()</src> method, and to more easily incorporate
00165 // special parameter handling (e.g. using <em>flux</em> rather than amplitude
00166 // of a Gaussian). All of this is transparent to the end-user.
00167 // </dl>
00168 // Combinatory Function objects are provided to easily combine and create
00169 // function objects:
00170 // <dl> 
00171 // <dt> <linkto class=CompoundFunction>CompoundFunction</linkto>
00172 // <dd> creates
00173 // a new, compound, function object from one or more other function objects
00174 // (including compounds...). The new function will have the sum of the 
00175 // parameters of the input functions as the new parameters (i.e the compound
00176 // function created from a 1-dimensional Gaussian (with 3 parameters) and a 
00177 // third-order polynomial (with 4 parameters) will have 7 parameters).
00178 // <dt> <linkto class=CombiFunction>CombiFunction</linkto> 
00179 // <dd> creates
00180 // a (linear) combination of a number of input functions. The number of
00181 // parameters of the newly created function will be equal to the number of
00182 // input functions (i.e. the combi 
00183 // function created from a 1-dimensional Gaussian (with 3 parameters) and a 
00184 // third-order polynomial (with 4 parameters) will have 2 parameters). The
00185 // function will be <src>param0*gauss(x) + param1*poly(x)</src>
00186 // <dt> <linkto class=FunctionWrapper>FunctionWrapper</linkto>
00187 // <dd> will take
00188 // a global function (or by the use of the <em>STL</em> function adapters
00189 // <src>mem_fun*</src> also member functions) of any dimension, and with
00190 // any number of parameters. The function is assumed to be called as
00191 // <src>f(x, p)</src>, and is wrapped like 
00192 // <src>FunctionWrapper(&func, param&, ndim)</src> (see example). 
00193 //   
00194 // </dl>
00195 //
00196 // </synopsis>
00197 
00198 // <example>
00199 // A function to find a bracketed root by bisection could be written
00200 // as follows:
00201 // <srcblock>
00202 //    template <class Domain, class Range> 
00203 //      Domain findRoot(const Functional<Domain,Range> &func, Domain left, 
00204 //                      Domain right, Domain tol) {
00205 //          Range fr = func(right);
00206 //          Range fl = func(left);
00207 //          Range sign = fr > 0 ? 1 : -1 ;
00208 //          AlwaysAssertExit(fl*fr < 0.0 && right > left);
00209 //          while (right - left > tol) {
00210 //              Domain mid = (left + right) / 2;
00211 //              Range fmid = func(mid);
00212 //              if (sign*fmid > 0.0) right = mid;
00213 //              else left = mid;
00214 //          };
00215 //          return (left + right)/2;
00216 //      }
00217 // </srcblock>
00218 // Since Function1D is derived from Functional, the
00219 // above function will also work with classes derived from Function1D. To
00220 // behave sensibly, the Domain and Range types should be real, <em>i.e.</em>,
00221 // Float or Double.
00222 //
00223 // To calculate the value of a polynomial
00224 // <srcblock>2 + 4x<sup>2</sup> + 6x<sup>4</sup></srcblock>
00225 // at <src>x=5.1</src>:
00226 // <srcblock>
00227 //      Polynomial<Double> pol(4);
00228 //      pol[0] = 2; pol[2] = 4; pol[4] = 6;
00229 //      cout << "Polynomial value at 5.1: " << pol(5.1) << endl;
00230 // </srcblock>
00231 //
00232 // Create a simple function (1-dimensional) with 2 parameters (A and B):
00233 // <srcblock>
00234 //      Double myf(const Double x, const Vector<Double> p) {
00235 //        return p[0]*sin(p[1]*x); }
00236 // </srcblock>
00237 // make it into a function object for initial parameters 2 and pi:
00238 // <srcblock>
00239 //      Vector<Double> p(2);
00240 //      p[0] = 2; p[1] = C::pi;
00241 //      FunctionWrapper<Double> f0(myf, p, 2);
00242 // </srcblock>
00243 // Make the first parameter 3:
00244 // <srcblock>
00245 //      f0[0] = 3;
00246 // </srcblock>
00247 // (for the global function you have to change <src>p[0]</src>).
00248 // Calculate the value of the function:
00249 // <srcblock>
00250 //      cout << "The value " << f0(3) << " should be 1.5 times the value " <<
00251 //        myf(3) << endl;
00252 // </srcblock>
00253 // A function object could be created as:
00254 // <srcblock>
00255 //      template<class T> class objf : public Function<T> {
00256 //       public:
00257 //         objf() : Function<T>(2) {};          // 2 parameters
00258 //         objf(const objf<T> &other) : Function<T>(other) {};
00259 //         virtual ~objf() {};
00260 //         // The actual method called for the evaluation operator():
00261 //         virtual T eval(typename Function<T>::FunctionArg x) const {
00262 //           return param_p[0] * sin(param_p[1] * x[0]); };
00263 //         // Return a copy of function (used for combination e.g.)
00264 //         virtual Function<T> *clone() const {
00265 //           return new objf<T>(*this); };
00266 //     };
00267 // </srcblock>
00268 // Which can be called as:
00269 // <srcblock>
00270 //      objf<Double> f1;
00271 //      f1[0] = 2; f1[1] = C::pi;
00272 //      cout << "The value " << myf(3) << " should be equal to the value " <<
00273 //        f1(3) << endl;
00274 // </srcblock>
00275 // </example>
00276 
00277 // <motivation>
00278 // The immediate motivations for this module were:
00279 // <ol>
00280 //    <li> To represent functions which are used in linear and non-linear least
00281 //         squares fitting
00282 // </ol>
00283 // </motivation>
00284 
00285 // <todo asof="2001/12/30">
00286 //   <li> It could be convenient to have a letter/envelope class, and to 
00287 //        define ``function arithmetic.''
00288 // </todo>
00289 
00290 // </module>
00291 
00292 
00293 } //# NAMESPACE CASA - END
00294 
00295 #endif
00296