casa
$Rev:20696$
|
00001 //# CombiFunction.h: Form a linear combination of Functions 00002 //# Copyright (C) 2001,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: CombiFunction.h 21024 2011-03-01 11:46:18Z gervandiepen $ 00028 00029 #ifndef SCIMATH_COMBIFUNCTION_H 00030 #define SCIMATH_COMBIFUNCTION_H 00031 00032 //# Includes 00033 #include <casa/aips.h> 00034 #include <scimath/Functionals/CombiParam.h> 00035 #include <scimath/Mathematics/AutoDiff.h> 00036 #include <scimath/Mathematics/AutoDiffMath.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="UNKNOWN" date="before2004/08/25" tests="tCombiFunction" demos=""> 00049 // </reviewed> 00050 // 00051 // <prerequisite> 00052 // <li> <linkto class="Function">Function</linkto> class 00053 // </prerequisite> 00054 // 00055 // <synopsis> 00056 // Given N function objects, the class describes a linear combination of the 00057 // form: 00058 // <srcblock> 00059 // f(x) = a(0)*f(0)(x) + a(1)*f(1)(x) + ... + a(N-1)*f(N-1)(x) 00060 // </srcblock> 00061 // where a = {a(n)} are parameters. If the combi function is used in 00062 // a functional fitting process (see 00063 // <linkto class=LinearFit>LinearFit</linkto>) these parameters canm be 00064 // solved for. In all aspects they behave as 00065 // <linkto class=FunctionParam>FunctionParam</linkto> values. 00066 // 00067 // Member functions are added with the <src>addFunction()</src> method. 00068 // 00069 // <note role=tip> 00070 // Check <linkto class=CompoundFunction>CompoundFunction</linkto> class 00071 // for a combination of functions behaving as one object. </note> 00072 // </synopsis> 00073 // 00074 // <example> 00075 // In the following example a second order polynomial is built from 3 separate 00076 // polynomials. 00077 // <srcblock> 00078 // Polynomial<Double> constant(0); 00079 // Polynomial<Double> linear(1); 00080 // Polynomial<Double> square(2); 00081 // 00082 // constant.setCoefficient(0, 1.0); // 1 00083 // linear.setCoefficient(1, 1.0); // x 00084 // square[2] = 1.0; // x^2 00085 // 00086 // CombiFunction<Double> combination; 00087 // 00088 // // form function, e0 + e1*x + e2*x^2 00089 // combination.addFunction(constant); 00090 // combination.addFunction(linear); 00091 // combination.addFunction(square); 00092 // </srcblock> 00093 // </example> 00094 00095 // <templating arg=T> 00096 // <li> T should have standard numerical operators and exp() function. Current 00097 // implementation only tested for real types. 00098 // <li> To obtain derivatives, the derivatives should be defined. 00099 // </templating> 00100 00101 // <thrown> 00102 // <li> AipsError in debug mode if incorrect function index 00103 // </thrown> 00104 // 00105 // <motivation> 00106 // This class was created to allow specialization of the evaluation in 00107 // a simple way. 00108 // </motivation> 00109 // 00110 // <todo asof="2001/10/22"> 00111 // <li> Nothing I know of 00112 // </todo> 00113 00114 template <class T> class CombiFunction : public CombiParam<T> { 00115 public: 00116 //# Constructors 00117 // The default constructor -- no functions, no parameters, nothing, the 00118 // function operator returns a 0. 00119 CombiFunction() : CombiParam<T>() {} 00120 // Make this object a (deep) copy of other. 00121 // <group> 00122 CombiFunction(const CombiFunction<T> &other) : 00123 CombiParam<T>(other) {} 00124 CombiFunction(const CombiFunction<T> &other, Bool) : 00125 CombiParam<T>(other, True) {} 00126 template <class W> 00127 CombiFunction(const CombiFunction<W> &other) : CombiParam<T>(other) {} 00128 template <class W> 00129 CombiFunction(const CombiFunction<W> &other, Bool) : 00130 CombiParam<T>(other, True) {} 00131 // </group> 00132 // Make this object a (deep) copy of other. 00133 CombiFunction<T> &operator=(const CombiFunction<T> &other) { 00134 CombiParam<T>::operator=(other); return *this; } 00135 00136 // Destructor 00137 virtual ~CombiFunction() {} 00138 00139 //# Operators 00140 // Evaluate the function at <src>x</src>. 00141 virtual T eval(typename Function<T>::FunctionArg x) const; 00142 00143 //# Member functions 00144 // Return a copy of this object from the heap. The caller is responsible for 00145 // deleting the pointer. 00146 // <group> 00147 virtual Function<T> *clone() const { return new CombiFunction<T>(*this); } 00148 virtual Function<typename FunctionTraits<T>::DiffType> *cloneAD() const { 00149 return new CombiFunction<typename FunctionTraits<T>::DiffType>(*this); } 00150 virtual Function<typename FunctionTraits<T>::BaseType> *cloneNonAD() const { 00151 return new CombiFunction<typename FunctionTraits<T>::BaseType> 00152 (*this, True); } 00153 // </group> 00154 00155 //# Make members of parent classes known. 00156 public: 00157 using CombiParam<T>::nparameters; 00158 }; 00159 00160 #define CombiFunction_PS CombiFunction 00161 00162 // <summary> Partial specialization of CombiFunction for <src>AutoDiff</src> 00163 // </summary> 00164 00165 // <synopsis> 00166 // <note role=warning> The name <src>CombiFunction_PS</src> is only for cxx2html 00167 // documentation problems. Use <src>CombiFunction</src> in your code.</note> 00168 // </synopsis> 00169 00170 template <class T> class CombiFunction_PS<AutoDiff<T> > : 00171 public CombiParam<AutoDiff<T> > { 00172 public: 00173 //# Constructors 00174 // The default constructor -- no functions, no parameters, nothing, the 00175 // function operator returns a 0. 00176 CombiFunction_PS() : CombiParam<AutoDiff<T> >() {} 00177 // Make this object a (deep) copy of other. 00178 // <group> 00179 CombiFunction_PS(const CombiFunction_PS<AutoDiff<T> > &other) : 00180 CombiParam<AutoDiff<T> >(other) {} 00181 template <class W> 00182 CombiFunction_PS(const CombiFunction_PS<W> &other) : 00183 CombiParam<AutoDiff<T> >(other) {} 00184 // </group> 00185 // Make this object a (deep) copy of other. 00186 CombiFunction_PS<AutoDiff<T> > & 00187 operator=(const CombiFunction_PS<AutoDiff<T> > &other) { 00188 CombiParam<AutoDiff<T> >::operator=(other); return *this; } 00189 00190 // Destructor 00191 virtual ~CombiFunction_PS() {} 00192 00193 //# Operators 00194 // Evaluate the function and its derivatives at <src>x</src> <em>wrt</em> 00195 // to the coefficients. 00196 virtual AutoDiff<T> eval(typename Function<AutoDiff<T> >::FunctionArg x) const; 00197 00198 //# Member functions 00199 // Return a copy of this object from the heap. The caller is responsible for 00200 // deleting the pointer. 00201 // <group> 00202 virtual Function<AutoDiff<T> > *clone() const { 00203 return new CombiFunction_PS<AutoDiff<T> >(*this); } 00204 virtual Function<typename FunctionTraits<AutoDiff<T> >::DiffType> 00205 *cloneAD() const { 00206 return new CombiFunction<typename FunctionTraits<AutoDiff<T> >::DiffType> 00207 (*this); } 00208 virtual Function<typename FunctionTraits<AutoDiff<T> >::BaseType> 00209 *cloneNonAD() const { 00210 return new CombiFunction<typename FunctionTraits<AutoDiff<T> >::BaseType> 00211 (*this, True); } 00212 // </group> 00213 00214 //# Make members of parent classes known. 00215 public: 00216 using CombiParam<AutoDiff<T> >::nparameters; 00217 }; 00218 00219 #undef CombiFunction_PS 00220 00221 00222 } //# NAMESPACE CASA - END 00223 00224 #ifndef CASACORE_NO_AUTO_TEMPLATES 00225 #include <scimath/Functionals/CombiFunction.tcc> 00226 #include <scimath/Functionals/Combi2Function.tcc> 00227 #endif //# CASACORE_NO_AUTO_TEMPLATES 00228 #endif