casa
$Rev:20696$
|
00001 //# FunctionTraits.h: Function data types for parameters and arguments 00002 //# Copyright (C) 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: FunctionTraits.h 21024 2011-03-01 11:46:18Z gervandiepen $ 00027 00028 #ifndef SCIMATH_FUNCTIONTRAITS_H 00029 #define SCIMATH_FUNCTIONTRAITS_H 00030 00031 //# Includes 00032 #include <casa/aips.h> 00033 #include <scimath/Mathematics/AutoDiff.h> 00034 #include <scimath/Mathematics/AutoDiffA.h> 00035 #include <scimath/Mathematics/AutoDiffX.h> 00036 00037 namespace casa { //# NAMESPACE CASA - BEGIN 00038 00039 // 00040 // <summary> Function data types for parameters and arguments 00041 // </summary> 00042 // <use visibility=local> 00043 // 00044 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="t"> 00045 // </reviewed> 00046 00047 // <prerequisite> 00048 // <li> <linkto class="Function">Function</linkto> 00049 // <li> <linkto class="AutoDiff">AutoDiff</linkto> 00050 // </prerequisite> 00051 00052 // <etymology> 00053 // A trait is a characteristic feature. FunctionTraits defines relationships 00054 // between the data types of result, parameters and arguments of function 00055 // objects. 00056 // </etymology> 00057 // 00058 // <synopsis> 00059 // This templated class contains a number of typedefs that 00060 // describe the relationship between different numeric data types used for 00061 // the calculation of <src>Function</src> and the function parameter and 00062 // arguments. 00063 // 00064 // Its main use is to optimize the speed of the calculation of function 00065 // values and derivatives in the case of <src>AutoDiff</src> use and 00066 // manual calculation of derivatives, by allowing the data type of the 00067 // the arguments and/or parameters to be plain numeric in cases where the 00068 // derivatives wrt these are not needed. 00069 // To enable this, the following definitions are used for the use of the 00070 // Function template. Bear in mind that the Function operator is defined 00071 // as <src>result = f<T>(x; parameters)</src>. 00072 // <ol> 00073 // <li> Simple numeric type (Double, Complex, etc): result, parameters and 00074 // arguments: all the one defined templated type. 00075 // <li> <src>AutoDiff<T></src> indicates the calculation (either automatic or 00076 // with specialized implementations) of the result with a <src>T</src> 00077 // function value for <src>T</src> arg, and <src>AutoDiff<T></src> 00078 // parameters. 00079 // <li> <src>AutoDiffA</src> calculate form <src>AutoDiff<T></src> 00080 // arguments and parameters (note that either could be simple 00081 // values with zero derivatives) 00082 // <li> <src>AutoDiffX<T></src> : calculate only with respect to 00083 // the arguments the derivatives, by using <src>T</src> 00084 // parameters 00085 // </ol> 00086 // The following types are defined: 00087 // <dl> 00088 // <dt> <src>Type</src> 00089 // <dd> The template argument 00090 // <dt> <src>BaseType</src> 00091 // <dd> One down in the template hierarchy if possible (e.g. <src>Double</src> 00092 // for <src>AutoDiff<Double></src>) 00093 // <dt> <src>NumericType</src> 00094 // <dd> Ultimate numeric type (e.g. <src>Double</src> for 00095 // <src>AutoDiff<AutoDiff<Double> ></src> 00096 // <dt> <src>ParamType</src> 00097 // <dd> Type used for parameters 00098 // <dt> <src>ArgType</src> 00099 // <dd> Type used for arguments 00100 // <dt> <src>DiffType</src> 00101 // <dd> The default differentiation type (e.g. <src>AutoDiff<Double></src> 00102 // for <src>AutoDiff<Double></src>) 00103 // <dt> <src>getValue()</src> 00104 // <dd> get the value of a simple numeric or of an <src>AutoDiff</src> 00105 // <dt> <src>setValue()</src> 00106 // <dd> set the value of a simple numeric or of an <src>AutoDiff</src> 00107 // </dl> 00108 // 00109 // The specializations are done in such a way that higher order 00110 // derivatives (e.g. <src>AutoDiff<AutoDiff<Double> ></src>) are catered for. 00111 // 00112 // Note that the class names in the following definitions are extended with 00113 // some individual id (like <src>_PA</src>): do not use them in programming, 00114 // they are only necessary for the <src>cxx2html</src> interpreter) 00115 // 00116 // This class is implemented as a number of specializations for the 00117 // following data types. 00118 // <ul> 00119 // <li> <src>T</src> 00120 // <li> <src>AutoDiff<T></src> 00121 // <li> <src>AutoDiffA<T></src> 00122 // <li> <src>AutoDiffX<T></src> 00123 // </ul> 00124 // </synopsis> 00125 // 00126 // <example> 00127 // See the <linkto class=Function>Function</linkto> class code. 00128 // </example> 00129 // 00130 // <motivation> 00131 // To keep the Function class single templated 00132 // </motivation> 00133 // 00134 // <todo asof="2002/06/19"> 00135 // <li> Additional <src>AutoDiff*</src> classes if and when needed 00136 // </todo> 00137 // 00138 00139 template <class T> class FunctionTraits { 00140 public: 00141 // Actual template type 00142 typedef T Type; 00143 // Template base type 00144 typedef T BaseType; 00145 // Numeric type of template 00146 typedef T NumericType; 00147 // Type for parameters 00148 typedef T ParamType; 00149 // Type for arguments 00150 typedef T ArgType; 00151 // Default type for differentiation 00152 typedef AutoDiff<T> DiffType; 00153 // Get the value 00154 static const T &getValue(const T &in) { return in; } 00155 // Set a value (and possible derivative) 00156 static void setValue(T &out, const T &val, const uInt, 00157 const uInt) { out = val; } 00158 }; 00159 00160 //# Following are specializations. Naming only for documentation 00161 //# purposes (a problem with cxx2html) 00162 00163 #define FunctionTraits_P FunctionTraits 00164 00165 // <summary> FunctionTraits specialization for AutoDiff 00166 // </summary> 00167 00168 template <class T> class FunctionTraits_P<AutoDiff<T> > { 00169 public: 00170 // Actual template type 00171 typedef AutoDiff<T> Type; 00172 // Template base type 00173 typedef T BaseType; 00174 // Template numeric type 00175 typedef typename FunctionTraits_P<T>::NumericType NumericType; 00176 // Type for parameters 00177 typedef AutoDiff<T> ParamType; 00178 // Type for arguments 00179 typedef T ArgType; 00180 // Default type for differentiation 00181 typedef AutoDiff<T> DiffType; 00182 // Get the value 00183 static const T &getValue(const Type &in) { 00184 return FunctionTraits<T>::getValue(in.value()); } 00185 // Set a value (and possible derivative) 00186 static void setValue(Type &out, const T &val, const uInt nder, 00187 const uInt i) { out = Type(val, nder, i); } 00188 }; 00189 00190 #undef FunctionTraits_P 00191 00192 #define FunctionTraits_PA FunctionTraits 00193 00194 // <summary> FunctionTraits specialization for AutoDiffA 00195 // </summary> 00196 00197 template <class T> class FunctionTraits_PA<AutoDiffA<T> > { 00198 public: 00199 // Actual template type 00200 typedef AutoDiffA<T> Type; 00201 // Template base type 00202 typedef T BaseType; 00203 // Template numeric type 00204 typedef typename FunctionTraits_PA<T>::NumericType NumericType; 00205 // Type for parameters 00206 typedef AutoDiffA<T> ParamType; 00207 // Type for arguments 00208 typedef AutoDiffA<T> ArgType; 00209 // Default type for differentiation 00210 typedef AutoDiffA<T> DiffType; 00211 // Get the value 00212 static const T &getValue(const Type &in) { 00213 return FunctionTraits<T>::getValue(in.value()); } 00214 // Set a value (and possible derivative) 00215 static void setValue(Type &out, const T &val, const uInt nder, 00216 const uInt i) { out = Type(val, nder, i); } 00217 }; 00218 00219 #undef FunctionTraits_PA 00220 00221 #define FunctionTraits_PX FunctionTraits 00222 00223 // <summary> FunctionTraits specialization for AutoDiffX 00224 // </summary> 00225 00226 template <class T> class FunctionTraits_PX<AutoDiffX<T> > { 00227 public: 00228 // Actual template type 00229 typedef AutoDiffX<T> Type; 00230 // Template base type 00231 typedef T BaseType; 00232 // Template numeric type 00233 typedef typename FunctionTraits_PX<T>::NumericType NumericType; 00234 // Type for parameters 00235 typedef T ParamType; 00236 // Type for arguments 00237 typedef AutoDiffX<T> ArgType; 00238 // Default type for differentiation 00239 typedef AutoDiffX<T> DiffType; 00240 // Get the value 00241 static const T &getValue(const Type &in) { 00242 return FunctionTraits<T>::getValue(in.value()); } 00243 // Set a value (and possible derivative) 00244 static void setValue(Type &out, const T &val, const uInt nder, 00245 const uInt i) { out = Type(val, nder, i); } 00246 }; 00247 00248 #undef FunctionTraits_PX 00249 00250 00251 } //# NAMESPACE CASA - END 00252 00253 #endif