casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef SCIMATH_FUNCTIONTRAITS_H
27#define SCIMATH_FUNCTIONTRAITS_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/scimath/Mathematics/AutoDiff.h>
32#include <casacore/scimath/Mathematics/AutoDiffA.h>
33#include <casacore/scimath/Mathematics/AutoDiffX.h>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37//
38// <summary> Function data types for parameters and arguments
39// </summary>
40// <use visibility=local>
41//
42// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="t">
43// </reviewed>
44
45// <prerequisite>
46// <li> <linkto class="Function">Function</linkto>
47// <li> <linkto class="AutoDiff">AutoDiff</linkto>
48// </prerequisite>
49
50// <etymology>
51// A trait is a characteristic feature. FunctionTraits defines relationships
52// between the data types of result, parameters and arguments of function
53// objects.
54// </etymology>
55//
56// <synopsis>
57// This templated class contains a number of typedefs that
58// describe the relationship between different numeric data types used for
59// the calculation of <src>Function</src> and the function parameter and
60// arguments.
61//
62// Its main use is to optimize the speed of the calculation of function
63// values and derivatives in the case of <src>AutoDiff</src> use and
64// manual calculation of derivatives, by allowing the data type of the
65// the arguments and/or parameters to be plain numeric in cases where the
66// derivatives wrt these are not needed.
67// To enable this, the following definitions are used for the use of the
68// Function template. Bear in mind that the Function operator is defined
69// as <src>result = f<T>(x; parameters)</src>.
70// <ol>
71// <li> Simple numeric type (Double, Complex, etc): result, parameters and
72// arguments: all the one defined templated type.
73// <li> <src>AutoDiff<T></src> indicates the calculation (either automatic or
74// with specialized implementations) of the result with a <src>T</src>
75// function value for <src>T</src> arg, and <src>AutoDiff<T></src>
76// parameters.
77// <li> <src>AutoDiffA</src> calculate form <src>AutoDiff<T></src>
78// arguments and parameters (note that either could be simple
79// values with zero derivatives)
80// <li> <src>AutoDiffX<T></src> : calculate only with respect to
81// the arguments the derivatives, by using <src>T</src>
82// parameters
83// </ol>
84// The following types are defined:
85// <dl>
86// <dt> <src>Type</src>
87// <dd> The template argument
88// <dt> <src>BaseType</src>
89// <dd> One down in the template hierarchy if possible (e.g. <src>Double</src>
90// for <src>AutoDiff<Double></src>)
91// <dt> <src>NumericType</src>
92// <dd> Ultimate numeric type (e.g. <src>Double</src> for
93// <src>AutoDiff<AutoDiff<Double> ></src>
94// <dt> <src>ParamType</src>
95// <dd> Type used for parameters
96// <dt> <src>ArgType</src>
97// <dd> Type used for arguments
98// <dt> <src>DiffType</src>
99// <dd> The default differentiation type (e.g. <src>AutoDiff<Double></src>
100// for <src>AutoDiff<Double></src>)
101// <dt> <src>getValue()</src>
102// <dd> get the value of a simple numeric or of an <src>AutoDiff</src>
103// <dt> <src>setValue()</src>
104// <dd> set the value of a simple numeric or of an <src>AutoDiff</src>
105// </dl>
106//
107// The specializations are done in such a way that higher order
108// derivatives (e.g. <src>AutoDiff<AutoDiff<Double> ></src>) are catered for.
109//
110// Note that the class names in the following definitions are extended with
111// some individual id (like <src>_PA</src>): do not use them in programming,
112// they are only necessary for the <src>cxx2html</src> interpreter)
113//
114// This class is implemented as a number of specializations for the
115// following data types.
116// <ul>
117// <li> <src>T</src>
118// <li> <src>AutoDiff<T></src>
119// <li> <src>AutoDiffA<T></src>
120// <li> <src>AutoDiffX<T></src>
121// </ul>
122// </synopsis>
123//
124// <example>
125// See the <linkto class=Function>Function</linkto> class code.
126// </example>
127//
128// <motivation>
129// To keep the Function class single templated
130// </motivation>
131//
132// <todo asof="2002/06/19">
133// <li> Additional <src>AutoDiff*</src> classes if and when needed
134// </todo>
135//
136
137template <class T>
139 public:
140 // Actual template type
141 typedef T Type;
142 // Template base type
143 typedef T BaseType;
144 // Numeric type of template
145 typedef T NumericType;
146 // Type for parameters
147 typedef T ParamType;
148 // Type for arguments
149 typedef T ArgType;
150 // Default type for differentiation
152 // Get the value
153 static const T &getValue(const T &in) { return in; }
154 // Set a value (and possible derivative)
155 static void setValue(T &out, const T &val, const uInt, const uInt) { out = val; }
156};
157
158// # Following are specializations. Naming only for documentation
159// # purposes (a problem with cxx2html)
160
161#define FunctionTraits_P FunctionTraits
162
163// <summary> FunctionTraits specialization for AutoDiff
164// </summary>
165
166template <class T>
168 public:
169 // Actual template type
171 // Template base type
172 typedef T BaseType;
173 // Template numeric type
175 // Type for parameters
177 // Type for arguments
178 typedef T ArgType;
179 // Default type for differentiation
181 // Get the value
182 static const T &getValue(const Type &in) { return FunctionTraits<T>::getValue(in.value()); }
183 // Set a value (and possible derivative)
184 static void setValue(Type &out, const T &val, const uInt nder, const uInt i) {
185 out = Type(val, nder, i);
186 }
187};
188
189#undef FunctionTraits_P
190
191#define FunctionTraits_PA FunctionTraits
192
193// <summary> FunctionTraits specialization for AutoDiffA
194// </summary>
195
196template <class T>
198 public:
199 // Actual template type
201 // Template base type
202 typedef T BaseType;
203 // Template numeric type
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) { return FunctionTraits<T>::getValue(in.value()); }
213 // Set a value (and possible derivative)
214 static void setValue(Type &out, const T &val, const uInt nder, const uInt i) {
215 out = Type(val, nder, i);
216 }
217};
218
219#undef FunctionTraits_PA
220
221#define FunctionTraits_PX FunctionTraits
222
223// <summary> FunctionTraits specialization for AutoDiffX
224// </summary>
225
226template <class T>
228 public:
229 // Actual template type
231 // Template base type
232 typedef T BaseType;
233 // Template numeric type
235 // Type for parameters
236 typedef T ParamType;
237 // Type for arguments
239 // Default type for differentiation
241 // Get the value
242 static const T &getValue(const Type &in) { 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, const uInt i) {
245 out = Type(val, nder, i);
246 }
247};
248
249#undef FunctionTraits_PX
250
251} // namespace casacore
252
253#endif
#define FunctionTraits_PA
#define FunctionTraits_P
#define FunctionTraits_PX
T & value()
Returns the value of the function.
Definition AutoDiff.h:311
AutoDiffA< T > ArgType
Type for arguments.
static void setValue(Type &out, const T &val, const uInt nder, const uInt i)
Set a value (and possible derivative).
static const T & getValue(const Type &in)
Get the value.
AutoDiffA< T > DiffType
Default type for differentiation.
AutoDiffA< T > Type
Actual template type.
AutoDiffA< T > ParamType
Type for parameters.
FunctionTraits_PA< T >::NumericType NumericType
Template numeric type.
static const T & getValue(const Type &in)
Get the value.
AutoDiffX< T > Type
Actual template type.
AutoDiffX< T > DiffType
Default type for differentiation.
FunctionTraits_PX< T >::NumericType NumericType
Template numeric type.
AutoDiffX< T > ArgType
Type for arguments.
static void setValue(Type &out, const T &val, const uInt nder, const uInt i)
Set a value (and possible derivative).
AutoDiff< 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).
AutoDiff< T > DiffType
Default type for differentiation.
static const T & getValue(const Type &in)
Get the value.
AutoDiff< T > ParamType
Type for parameters.
FunctionTraits_P< T >::NumericType NumericType
Template numeric type.
static void setValue(T &out, const T &val, const uInt, const uInt)
Set a value (and possible derivative).
AutoDiff< T > DiffType
static const T & getValue(const T &in)
Get the value.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49