casa
$Rev:20696$
|
00001 //# CombiParam.h: Parameters for 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: CombiParam.h 21024 2011-03-01 11:46:18Z gervandiepen $ 00028 00029 #ifndef SCIMATH_COMBIPARAM_H 00030 #define SCIMATH_COMBIPARAM_H 00031 00032 //# Includes 00033 #include <casa/aips.h> 00034 #include <casa/BasicSL/String.h> 00035 #include <casa/Containers/Block.h> 00036 #include <casa/Utilities/Assert.h> 00037 #include <scimath/Functionals/Function.h> 00038 00039 namespace casa { //# NAMESPACE CASA - BEGIN 00040 00041 //# Forward declarations 00042 00043 // <summary> 00044 // Parameters for a linear combination of function objects. 00045 // </summary> 00046 // 00047 // <use visibility=local> 00048 // 00049 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tCombiFunction" demos=""> 00050 // </reviewed> 00051 // 00052 // <prerequisite> 00053 // <li> <linkto class="CombiFunction">CombiFunction</linkto> class 00054 // </prerequisite> 00055 // 00056 // <synopsis> 00057 // Given N function objects, the class describes a linear combination of the 00058 // form: 00059 // <srcblock> 00060 // f(x) = a(0)*f(0)(x) + a(1)*f(1)(x) + ... + a(N-1)*f(N-1)(x) 00061 // </srcblock> 00062 // where a = {a(n)} are parameters. If the combi function is used in 00063 // a functional fitting process (see 00064 // <linkto class=LinearFit>LinearFit</linkto>) these parameters canm be 00065 // solved for. In all aspects they behave as 00066 // <linkto class=FunctionParam>FunctionParam</linkto> values. 00067 // 00068 // Member functions are added with the <src>addFunction()</src> method. 00069 // </synopsis> 00070 // 00071 // <example> 00072 // In the following example a second order polynomial is built from 3 separate 00073 // polynomials. 00074 // <srcblock> 00075 // Polynomial<Double> constant(0); 00076 // Polynomial<Double> linear(1); 00077 // Polynomial<Double> square(2); 00078 // 00079 // constant.setCoefficient(0, 1.0); // 1 00080 // linear.setCoefficient(1, 1.0); // x 00081 // square[2] = 1.0; // x^2 00082 // 00083 // CombiParam<Double> combination; 00084 // 00085 // // form function, e0 + e1*x + e2*x^2 00086 // combination.addFunction(constant); 00087 // combination.addFunction(linear); 00088 // combination.addFunction(square); 00089 // </srcblock> 00090 // </example> 00091 00092 // <templating arg=T> 00093 // <li> T should have standard numerical operators and exp() function. Current 00094 // implementation only tested for real types. 00095 // <li> To obtain derivatives, the derivatives should be defined. 00096 // </templating> 00097 00098 // <thrown> 00099 // <li> AipsError if dimensions of functions added different 00100 // </thrown> 00101 00102 // <motivation> 00103 // This class was created to allow specialization of the evaluation in 00104 // a simple way. 00105 // </motivation> 00106 // 00107 // <todo asof="2001/10/22"> 00108 // <li> Nothing I know of 00109 // </todo> 00110 00111 template <class T> class CombiParam : public Function<T> 00112 { 00113 public: 00114 //# Constructors 00115 // The default constructor -- no functions, no parameters, nothing, the 00116 // function operator returns a 0. 00117 CombiParam(); 00118 // Make this object a (deep) copy of other. 00119 // <group> 00120 CombiParam(const CombiParam<T> &other); 00121 CombiParam(const CombiParam<T> &other, Bool) : 00122 Function<T>(other), ndim_p(other.ndim_p), 00123 functionPtr_p(other.functionPtr_p.nelements()) { 00124 for (uInt i=0; i<functionPtr_p.nelements(); ++i) { 00125 functionPtr_p[i] = (*(other.functionPtr_p[i])).clone(); 00126 } 00127 } 00128 template <class W> 00129 CombiParam(const CombiParam<W> &other) : 00130 Function<T>(other), ndim_p(other.ndim()), 00131 functionPtr_p(other.nFunctions()) { 00132 for (uInt i=0; i<nFunctions(); ++i) { 00133 functionPtr_p[i] = other.function(i).cloneAD(); 00134 } 00135 } 00136 template <class W> 00137 CombiParam(const CombiParam<W> &other, Bool) : 00138 Function<T>(other), ndim_p(other.ndim()), 00139 functionPtr_p(other.nFunctions()) { 00140 for (uInt i=0; i<nFunctions(); ++i) { 00141 functionPtr_p[i] = other.function(i).cloneNonAD(); 00142 } 00143 } 00144 // </group> 00145 // Make this object a (deep) copy of other. 00146 CombiParam<T> &operator=(const CombiParam<T> &other); 00147 // Destructor 00148 virtual ~CombiParam(); 00149 00150 //# Operators 00151 00152 //# Member functions 00153 // Give name of function 00154 virtual const String &name() const { static String x("combi"); 00155 return x; } 00156 00157 // Add a function. All functions must have the same <src>ndim()</src> 00158 // as the first one. Returns the (zero relative) number (<src>i</src>) 00159 // of the function just added. 00160 // The default initial parameter value (<src>a(i)</src>) is 00161 // initialized to 1. The parameter mask is set <src>True</src>. 00162 uInt addFunction(const Function<T> &newFunction); 00163 00164 // Return the total number of functions. The number is equal to the 00165 // number of functions that have been added. 00166 uInt nFunctions() const { return nparameters(); } 00167 00168 // Return a reference to a specific Function in the combination. 00169 // <group> 00170 const Function<T> &function(uInt which) const { 00171 DebugAssert(nFunctions() > which, AipsError); 00172 return *(functionPtr_p[which]); } 00173 const Function<T> &function(uInt which) { 00174 DebugAssert(nFunctions() > which, AipsError); 00175 return *(functionPtr_p[which]); } 00176 // </group> 00177 00178 // Returns the dimension of functions in the linear combination 00179 virtual uInt ndim() const { return ndim_p; } 00180 00181 protected: 00182 //# Data 00183 // Number of dimensions of underlying functions 00184 uInt ndim_p; 00185 00186 // Pointer to each added function 00187 PtrBlock<Function<T> *> functionPtr_p; 00188 00189 //# Make members of parent classes known. 00190 public: 00191 using Function<T>::nparameters; 00192 }; 00193 00194 00195 } //# NAMESPACE CASA - END 00196 00197 #ifndef CASACORE_NO_AUTO_TEMPLATES 00198 #include <scimath/Functionals/CombiParam.tcc> 00199 #endif //# CASACORE_NO_AUTO_TEMPLATES 00200 #endif