casa
$Rev:20696$
|
00001 //# FunctionWrapper.h: Construct function objects from C++ 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 //# $Id: FunctionWrapper.h 21024 2011-03-01 11:46:18Z gervandiepen $ 00027 00028 #ifndef SCIMATH_FUNCTIONWRAPPER_H 00029 #define SCIMATH_FUNCTIONWRAPPER_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <scimath/Functionals/WrapperParam.h> 00034 00035 namespace casa { //# NAMESPACE CASA - BEGIN 00036 00037 //# Forward declarations 00038 template <class T> class Vector; 00039 template <class T> class WrapperBase; 00040 00041 // <summary> Construct nD function objects from C++ functions 00042 // </summary> 00043 // 00044 // <use visibility=export> 00045 // 00046 // <reviewed reviewer="" date="1996/02/22" tests="" demos=""> 00047 // </reviewed> 00048 // 00049 // <prerequisite> 00050 // <li> <linkto class="Function">Function</linkto> class 00051 // <li> <linkto class="FunctionParam">FunctionParam</linkto> 00052 // </prerequisite> 00053 // 00054 // <synopsis> 00055 // This class is provided so that user can quickly construct a function 00056 // object from a C++ function pointer without having to write a function 00057 // class. The constructor constructs a function object from a function 00058 // pointer, and an optional parameter list. 00059 // Parameters are necessary if 00060 // the function has to be used in a functional fitting process (see 00061 // <linkto class=GenericL2Fit>GenericL2Fit</linkto>). 00062 // 00063 // The general function signature is <src>f(x;p)</src>, where <src>x</src> 00064 // represents the <em>arguments</em>, and <src>p</src> the parameters. 00065 // The allowed signatures of the function include all combinations of 00066 // arguments and parameters, and are: 00067 // <ul> 00068 // <li> <src>f()</src> no arguments e.g. random number or constant 00069 // <li> <src>f(x)</src> 1-dimensional, e.g. <src>sin(x)</src> 00070 // <li> <src>f(Vectorx)</src> n-dimensional, e.g. <src>sin(x+2y)</src> 00071 // </ul> 00072 // 00073 // </synopsis> 00074 // 00075 // <example> 00076 // <srcblock> 00077 // Float func(const Vector<Float>& x) {return x(0)*x(1);} // x*y 00078 // // Convert C++ functions to Functionals 00079 // FunctionWrapper<Float> Func(func,2); 00080 // </srcblock> 00081 // 00082 00083 template <class T> 00084 class FunctionWrapper : public WrapperParam<T> 00085 { 00086 public: 00087 //# Constructors 00088 // Default constructor, to enable arrays 00089 FunctionWrapper(); 00090 // A function with no parameters and no arguments. 00091 FunctionWrapper(T(*f)()); 00092 // A function with parameter and no arguments 00093 // (Note value of isPar irrelevant) 00094 FunctionWrapper(T(*f)( const T&), const Bool isPar); 00095 // A function with parameters and no arguments. 00096 // (Note value of isPar irrelevant) 00097 FunctionWrapper(T(*f)(const Vector<T>&), const Bool isPar); 00098 // Construct a 1-dimensional function with no parameters. 00099 FunctionWrapper(T(*f)(const T&)); 00100 // Construct a 1-dimensional function with parameter. 00101 FunctionWrapper(T(*f)(const T&, const T&), const T &par); 00102 // Construct a 1-dimensional function with parameters. 00103 FunctionWrapper(T(*f)(const T&, const Vector<T>&), 00104 const Vector<T> &par); 00105 // Construct an n-dimensional function with no parameters. 00106 FunctionWrapper(T(*f)(const Vector<T>&), const Int dim=1); 00107 // Construct an n-dimensional function with parameter. 00108 FunctionWrapper(T(*f)(const Vector<T>&, const T&), 00109 const T &par, const uInt dim=1); 00110 // Construct an n-dimensional function with parameters. 00111 FunctionWrapper(T(*f)(const Vector<T>&, const Vector<T>&), 00112 const Vector<T> &par, const uInt dim=1); 00113 // Copy constructor (deep copy) 00114 // <group> 00115 FunctionWrapper(const FunctionWrapper<T> &other); 00116 // </group> 00117 // Copy assignment (deep copy) 00118 FunctionWrapper<T> &operator=(const FunctionWrapper<T> &other); 00119 00120 // Destructor 00121 virtual ~FunctionWrapper() {} 00122 00123 //# Operators 00124 // Evaluate the function at <src>x</src>. 00125 // <group> 00126 virtual T eval(typename Function<T>::FunctionArg x) const; 00127 // </group> 00128 00129 //# Member functions 00130 // Get the dimensionality 00131 virtual uInt ndim() const; 00132 // Return a copy of this object from the heap. The caller is responsible 00133 // for deleting this pointer. 00134 // <group> 00135 virtual Function<T> *clone() const { 00136 return new FunctionWrapper<T>(*this); } 00137 // </group> 00138 00139 protected: 00140 //# Data 00141 // The function aid object 00142 WrapperBase<T> *doit_p; 00143 00144 //# Make members of parent classes known. 00145 protected: 00146 using WrapperParam<T>::param_p; 00147 }; 00148 00149 00150 } //# NAMESPACE CASA - END 00151 00152 #ifndef CASACORE_NO_AUTO_TEMPLATES 00153 #include <scimath/Functionals/FunctionWrapper.tcc> 00154 #endif //# CASACORE_NO_AUTO_TEMPLATES 00155 #endif