casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
FunctionParam.h
Go to the documentation of this file.
00001 //# FunctionParam.h: Container of function parameters with masking flags
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 //# $Id: FunctionParam.h 21024 2011-03-01 11:46:18Z gervandiepen $
00027 
00028 #ifndef SCIMATH_FUNCTIONPARAM_H
00029 #define SCIMATH_FUNCTIONPARAM_H
00030 
00031 //# Include files
00032 #include <casa/aips.h>
00033 #include <casa/Arrays/Vector.h>
00034 #include <scimath/Functionals/FunctionTraits.h>
00035 
00036 //# Forward declarations
00037 #include <casa/iosfwd.h>
00038 
00039 namespace casa { //# NAMESPACE CASA - BEGIN
00040 
00041 // <summary>Container of function parameters with masking flags
00042 // </summary>
00043 //
00044 // <use visibility=export>
00045 //
00046 // <reviewed reviewer="tcornwel" date="1996/02/22" tests="tGaussian2D"
00047 //       demos="">
00048 // </reviewed>
00049 //
00050 // <synopsis>
00051 // <src>FunctionParam</src> is used to provide an interface to an entity which
00052 // has parameters that can be flagged.
00053 // This is useful, for example, in implementing parameter
00054 // fitting which operates on generic function objects.
00055 // 
00056 // Each parameter can be masked. The mask can, e.g., be used to indicate to a
00057 // generic least-squares fitting routine to only adjust parameters with
00058 // a <em>True</em> mask (the default). For that reason methods that only
00059 // handle <em>True</em> data items have names with <em>Adjust</em> in
00060 // the names. In general the user should not be concerned with these
00061 // methods, but should only manipulate the parameter <src>flags</src> and
00062 // <src>values</src>.
00063 //
00064 // </synopsis>
00065 //
00066 // <example>
00067 // See the <linkto class=Function>Function</linkto> class for a usage
00068 // interface.
00069 // </example>
00070 //
00071 // <motivation>
00072 // Generically manipulatable adjustable parameters are important for fitting.
00073 // </motivation>
00074 //
00075 // <templating arg=T>
00076 //  <li> <src>T</src> must have a default constructor, assignment operator,
00077 //       and copy constructor (for the Vector interface). 
00078 //  <li> all standard mathematical should be applicable if the 
00079 //      parameter interface is used for the calculation of
00080 //      <src>Functions</src>.
00081 // </templating>
00082 //
00083 // <todo asof="2001/08/28">
00084 //   <li> Nothing I know of
00085 // </todo>
00086 
00087 template<class T> class FunctionParam {
00088  public:
00089   //# Constructors
00090   // Construct a default FunctionParam with 0 parameters
00091   FunctionParam();
00092   // Construct a FunctionParam with <src>n</src> parameters with zero value and
00093   // all masks <em>True</em>
00094   explicit FunctionParam(const uInt n);
00095   // Construct a FunctionParam from the given vector, with all masks 
00096   // <em>True</em>
00097   explicit FunctionParam(const Vector<T> &in);
00098   // Copy constructor (deep copy)
00099   FunctionParam(const FunctionParam<T> &other);
00100   // Copy from different type (deep copy)
00101   template <class W>
00102   FunctionParam(const FunctionParam<W> &other) 
00103     : npar_p(other.getParameters().nelements()),
00104     param_p(npar_p), mask_p(npar_p),
00105     maskedPtr_p(0) {
00106     for (uInt i=0; i<npar_p; ++i) {
00107       FunctionTraits<T>::
00108 	setValue(param_p[i],
00109                  FunctionTraits<W>::getValue(other.getParameters()[i]),
00110                  npar_p, i);
00111     }
00112     mask_p = other.getParamMasks();
00113   }
00114 
00115   // Destructor
00116   virtual ~FunctionParam();
00117 
00118   //# Operators
00119   // Copy assignment (deep copy)
00120   FunctionParam &operator=(const FunctionParam<T> &other);
00121   // Manipulate the nth parameter (0-based) with no index check
00122   // <group>
00123   T &operator[](const uInt n) { return param_p[n]; }
00124   const T &operator[](const uInt n) const { return param_p[n]; }
00125   // </group>
00126   // Compare two parameter sets for equal size, values and masks.
00127   // <group>
00128   Bool operator==(const FunctionParam<T> &other) const;
00129   Bool operator!=(const FunctionParam<T> &other) const;
00130   // </group>
00131 
00132   //# Member functions
00133   // Return the number of parameters
00134   uInt nelements() const { return param_p.nelements(); }
00135   // Manipulate the nth parameter (0-based) with no index check
00136   // <group>
00137   T &parameter(const uInt n) { return param_p[n]; }
00138   const T &parameter(const uInt n) const{ return param_p[n]; }
00139   // </group>
00140 
00141   // Manipulate the mask associated with the nth parameter
00142   // (e.g. to indicate whether the parameter is adjustable or nonadjustable).
00143   // Note no index check.
00144   // <group>
00145   Bool &mask(const uInt n);
00146   const Bool &mask(const uInt n) const { return mask_p[n]; }
00147   // </group>
00148 
00149   // Get all parameters at once.  Returns zero length
00150   // Vector if there are no parameters.
00151   const Vector<T> &getParameters() const { return param_p; }
00152   
00153   // Set all the parameters at once. Only the minimum of the input number and
00154   // the object number of parameters will be set.
00155   void setParameters(const Vector<T> &params);
00156 
00157   // Get all parameter masks at once.  Returns zero length
00158   // Vector if there are no parameters.
00159   const Vector<Bool> &getParamMasks() const { return mask_p; }
00160     
00161   // Set all parameter masks at once. Only the minimum of the input number and
00162   // the object number of parameters will be set.
00163   void setParamMasks(const Vector<Bool> &masks);
00164 
00165   // Operations on the masked parameters only. For possible re-use the
00166   // results are cached.
00167   // <group>
00168   // Number of masked (<src>=True</src>) parameters
00169   uInt nMaskedParameters() const;
00170   // All masked parameters only
00171   // <group>
00172   Vector<T> &getMaskedParameters() const;
00173   void setMaskedParameters(Vector<T> &in);
00174   // </group>
00175   // </group>
00176 
00177   // Output the parameters
00178   ostream &print(ostream &os) const;
00179 
00180  private:
00181   //# Data
00182   // Number of parameters
00183   uInt npar_p;
00184   // Parameters
00185   Vector<T> param_p;
00186   // Masks
00187   Vector<Bool> mask_p;
00188   // Cached masked data
00189   mutable Vector<T> *maskedPtr_p;
00190 
00191   //# Methods
00192   // Create a cached version of the masked parameter list
00193   void createMaskedPtr() const;
00194   // Clear the masked parameter list
00195   void clearMaskedPtr() const;
00196 
00197 };
00198 
00199 //# Global functions
00200 // <summary> Global functions </summary>
00201 // <group name=Output>
00202 // Output declaration
00203 template<class T>
00204 ostream &operator<<(ostream &os, const FunctionParam<T> &par);
00205 // </group>
00206 
00207 //# Inlines
00208 template<class T>
00209 inline ostream &operator<<(ostream &os, const FunctionParam<T> &par) {
00210   return par.print(os); }
00211 
00212 
00213 } //# NAMESPACE CASA - END
00214 
00215 #ifndef CASACORE_NO_AUTO_TEMPLATES
00216 #include <scimath/Functionals/FunctionParam.tcc>
00217 #endif //# CASACORE_NO_AUTO_TEMPLATES
00218 #endif
00219