casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FunctionTraits.h
Go to the documentation of this file.
1 //# FunctionTraits.h: Function data types for parameters and arguments
2 //# Copyright (C) 2001,2002
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef SCIMATH_FUNCTIONTRAITS_H
29 #define SCIMATH_FUNCTIONTRAITS_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //
40 // <summary> Function data types for parameters and arguments
41 // </summary>
42 // <use visibility=local>
43 //
44 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="t">
45 // </reviewed>
46 
47 // <prerequisite>
48 // <li> <linkto class="Function">Function</linkto>
49 // <li> <linkto class="AutoDiff">AutoDiff</linkto>
50 // </prerequisite>
51 
52 // <etymology>
53 // A trait is a characteristic feature. FunctionTraits defines relationships
54 // between the data types of result, parameters and arguments of function
55 // objects.
56 // </etymology>
57 //
58 // <synopsis>
59 // This templated class contains a number of typedefs that
60 // describe the relationship between different numeric data types used for
61 // the calculation of <src>Function</src> and the function parameter and
62 // arguments.
63 //
64 // Its main use is to optimize the speed of the calculation of function
65 // values and derivatives in the case of <src>AutoDiff</src> use and
66 // manual calculation of derivatives, by allowing the data type of the
67 // the arguments and/or parameters to be plain numeric in cases where the
68 // derivatives wrt these are not needed.
69 // To enable this, the following definitions are used for the use of the
70 // Function template. Bear in mind that the Function operator is defined
71 // as <src>result = f<T>(x; parameters)</src>.
72 // <ol>
73 // <li> Simple numeric type (Double, Complex, etc): result, parameters and
74 // arguments: all the one defined templated type.
75 // <li> <src>AutoDiff<T></src> indicates the calculation (either automatic or
76 // with specialized implementations) of the result with a <src>T</src>
77 // function value for <src>T</src> arg, and <src>AutoDiff<T></src>
78 // parameters.
79 // <li> <src>AutoDiffA</src> calculate form <src>AutoDiff<T></src>
80 // arguments and parameters (note that either could be simple
81 // values with zero derivatives)
82 // <li> <src>AutoDiffX<T></src> : calculate only with respect to
83 // the arguments the derivatives, by using <src>T</src>
84 // parameters
85 // </ol>
86 // The following types are defined:
87 // <dl>
88 // <dt> <src>Type</src>
89 // <dd> The template argument
90 // <dt> <src>BaseType</src>
91 // <dd> One down in the template hierarchy if possible (e.g. <src>Double</src>
92 // for <src>AutoDiff<Double></src>)
93 // <dt> <src>NumericType</src>
94 // <dd> Ultimate numeric type (e.g. <src>Double</src> for
95 // <src>AutoDiff<AutoDiff<Double> ></src>
96 // <dt> <src>ParamType</src>
97 // <dd> Type used for parameters
98 // <dt> <src>ArgType</src>
99 // <dd> Type used for arguments
100 // <dt> <src>DiffType</src>
101 // <dd> The default differentiation type (e.g. <src>AutoDiff<Double></src>
102 // for <src>AutoDiff<Double></src>)
103 // <dt> <src>getValue()</src>
104 // <dd> get the value of a simple numeric or of an <src>AutoDiff</src>
105 // <dt> <src>setValue()</src>
106 // <dd> set the value of a simple numeric or of an <src>AutoDiff</src>
107 // </dl>
108 //
109 // The specializations are done in such a way that higher order
110 // derivatives (e.g. <src>AutoDiff<AutoDiff<Double> ></src>) are catered for.
111 //
112 // Note that the class names in the following definitions are extended with
113 // some individual id (like <src>_PA</src>): do not use them in programming,
114 // they are only necessary for the <src>cxx2html</src> interpreter)
115 //
116 // This class is implemented as a number of specializations for the
117 // following data types.
118 // <ul>
119 // <li> <src>T</src>
120 // <li> <src>AutoDiff<T></src>
121 // <li> <src>AutoDiffA<T></src>
122 // <li> <src>AutoDiffX<T></src>
123 // </ul>
124 // </synopsis>
125 //
126 // <example>
127 // See the <linkto class=Function>Function</linkto> class code.
128 // </example>
129 //
130 // <motivation>
131 // To keep the Function class single templated
132 // </motivation>
133 //
134 // <todo asof="2002/06/19">
135 // <li> Additional <src>AutoDiff*</src> classes if and when needed
136 // </todo>
137 //
138 
139 template <class T> class FunctionTraits {
140 public:
141  // Actual template type
142  typedef T Type;
143  // Template base type
144  typedef T BaseType;
145  // Numeric type of template
146  typedef T NumericType;
147  // Type for parameters
148  typedef T ParamType;
149  // Type for arguments
150  typedef T ArgType;
151  // Default type for differentiation
153  // Get the value
154  static const T &getValue(const T &in) { return in; }
155  // Set a value (and possible derivative)
156  static void setValue(T &out, const T &val, const uInt,
157  const uInt) { out = val; }
158 };
159 
160 //# Following are specializations. Naming only for documentation
161 //# purposes (a problem with cxx2html)
162 
163 #define FunctionTraits_P FunctionTraits
164 
165 // <summary> FunctionTraits specialization for AutoDiff
166 // </summary>
167 
168 template <class T> class FunctionTraits_P<AutoDiff<T> > {
169 public:
170  // Actual template type
171  typedef AutoDiff<T> Type;
172  // Template base type
173  typedef T BaseType;
174  // Template numeric type
175  typedef typename FunctionTraits_P<T>::NumericType NumericType;
176  // Type for parameters
178  // Type for arguments
179  typedef T ArgType;
180  // Default type for differentiation
182  // Get the value
183  static const T &getValue(const Type &in) {
184  return FunctionTraits<T>::getValue(in.value()); }
185  // Set a value (and possible derivative)
186  static void setValue(Type &out, const T &val, const uInt nder,
187  const uInt i) { out = Type(val, nder, i); }
188 };
189 
190 #undef FunctionTraits_P
191 
192 #define FunctionTraits_PA FunctionTraits
193 
194 // <summary> FunctionTraits specialization for AutoDiffA
195 // </summary>
196 
197 template <class T> class FunctionTraits_PA<AutoDiffA<T> > {
198 public:
199  // Actual template type
200  typedef AutoDiffA<T> Type;
201  // Template base type
202  typedef T BaseType;
203  // Template numeric type
204  typedef typename FunctionTraits_PA<T>::NumericType NumericType;
205  // Type for parameters
207  // Type for arguments
209  // Default type for differentiation
211  // Get the value
212  static const T &getValue(const Type &in) {
213  return FunctionTraits<T>::getValue(in.value()); }
214  // Set a value (and possible derivative)
215  static void setValue(Type &out, const T &val, const uInt nder,
216  const uInt i) { out = Type(val, nder, i); }
217 };
218 
219 #undef FunctionTraits_PA
220 
221 #define FunctionTraits_PX FunctionTraits
222 
223 // <summary> FunctionTraits specialization for AutoDiffX
224 // </summary>
225 
226 template <class T> class FunctionTraits_PX<AutoDiffX<T> > {
227 public:
228  // Actual template type
229  typedef AutoDiffX<T> Type;
230  // Template base type
231  typedef T BaseType;
232  // Template numeric type
233  typedef typename FunctionTraits_PX<T>::NumericType NumericType;
234  // Type for parameters
235  typedef T ParamType;
236  // Type for arguments
238  // Default type for differentiation
240  // Get the value
241  static const T &getValue(const Type &in) {
242  return FunctionTraits<T>::getValue(in.value()); }
243  // Set a value (and possible derivative)
244  static void setValue(Type &out, const T &val, const uInt nder,
245  const uInt i) { out = Type(val, nder, i); }
246 };
247 
248 #undef FunctionTraits_PX
249 
250 
251 } //# NAMESPACE CASACORE - END
252 
253 #endif
AutoDiffA< T > Type
Actual template type.
#define FunctionTraits_PA
FunctionTraits_P< T >::NumericType NumericType
Template numeric type.
#define FunctionTraits_P
AutoDiffX< T > DiffType
Default type for differentiation.
static void setValue(Type &out, const T &val, const uInt nder, const uInt i)
Set a value (and possible derivative)
AutoDiffX< T > ArgType
Type for arguments.
static const T & getValue(const Type &in)
Get the value.
FunctionTraits_PA< T >::NumericType NumericType
Template numeric type.
AutoDiff< T > ParamType
Type for parameters.
#define FunctionTraits_PX
T BaseType
Template base type.
AutoDiffA< T > DiffType
Default type for differentiation.
static const T & getValue(const T &in)
Get the value.
AutoDiff< T > DiffType
Default type for differentiation.
FunctionTraits_PX< T >::NumericType NumericType
Template numeric type.
static void setValue(T &out, const T &val, const uInt, const uInt)
Set a value (and possible derivative)
T ParamType
Type for parameters.
static void setValue(Type &out, const T &val, const uInt nder, const uInt i)
Set a value (and possible derivative)
Class that computes partial derivatives by automatic differentiation.
Definition: AutoDiff.h:257
static const T & getValue(const Type &in)
Get the value.
AutoDiffX< T > Type
Actual template type.
static void setValue(Type &out, const T &val, const uInt nder, const uInt i)
Set a value (and possible derivative)
T ArgType
Type for arguments.
AutoDiffA< T > ArgType
Type for arguments.
AutoDiff< T > Type
Actual template type.
Class that computes partial derivatives by automatic differentiation.
Definition: AutoDiffX.h:119
Function data types for parameters and arguments.
T Type
Actual template type.
Class that computes partial derivatives by automatic differentiation.
Definition: AutoDiffA.h:119
AutoDiffA< T > ParamType
Type for parameters.
AutoDiff< T > DiffType
Default type for differentiation.
T NumericType
Numeric type of template.
static const T & getValue(const Type &in)
Get the value.
T & value()
Returns the value of the function.
Definition: AutoDiff.h:313
unsigned int uInt
Definition: aipstype.h:51