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> class FunctionTraits {
138public:
139 // Actual template type
140 typedef T Type;
141 // Template base type
142 typedef T BaseType;
143 // Numeric type of template
144 typedef T NumericType;
145 // Type for parameters
146 typedef T ParamType;
147 // Type for arguments
148 typedef T ArgType;
149 // Default type for differentiation
151 // Get the value
152 static const T &getValue(const T &in) { return in; }
153 // Set a value (and possible derivative)
154 static void setValue(T &out, const T &val, const uInt,
155 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> class FunctionTraits_P<AutoDiff<T> > {
167public:
168 // Actual template type
170 // Template base type
171 typedef T BaseType;
172 // Template numeric type
173 typedef typename FunctionTraits_P<T>::NumericType NumericType;
174 // Type for parameters
176 // Type for arguments
177 typedef T ArgType;
178 // Default type for differentiation
180 // Get the value
181 static const T &getValue(const Type &in) {
182 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,
185 const uInt i) { out = Type(val, nder, i); }
186};
187
188#undef FunctionTraits_P
189
190#define FunctionTraits_PA FunctionTraits
191
192// <summary> FunctionTraits specialization for AutoDiffA
193// </summary>
194
195template <class T> class FunctionTraits_PA<AutoDiffA<T> > {
196public:
197 // Actual template type
199 // Template base type
200 typedef T BaseType;
201 // Template numeric type
202 typedef typename FunctionTraits_PA<T>::NumericType NumericType;
203 // Type for parameters
205 // Type for arguments
207 // Default type for differentiation
209 // Get the value
210 static const T &getValue(const Type &in) {
211 return FunctionTraits<T>::getValue(in.value()); }
212 // Set a value (and possible derivative)
213 static void setValue(Type &out, const T &val, const uInt nder,
214 const uInt i) { out = Type(val, nder, i); }
215};
216
217#undef FunctionTraits_PA
218
219#define FunctionTraits_PX FunctionTraits
220
221// <summary> FunctionTraits specialization for AutoDiffX
222// </summary>
223
224template <class T> class FunctionTraits_PX<AutoDiffX<T> > {
225public:
226 // Actual template type
228 // Template base type
229 typedef T BaseType;
230 // Template numeric type
231 typedef typename FunctionTraits_PX<T>::NumericType NumericType;
232 // Type for parameters
233 typedef T ParamType;
234 // Type for arguments
236 // Default type for differentiation
238 // Get the value
239 static const T &getValue(const Type &in) {
240 return FunctionTraits<T>::getValue(in.value()); }
241 // Set a value (and possible derivative)
242 static void setValue(Type &out, const T &val, const uInt nder,
243 const uInt i) { out = Type(val, nder, i); }
244};
245
246#undef FunctionTraits_PX
247
248
249} //# NAMESPACE CASACORE - END
250
251#endif
#define FunctionTraits_PA
#define FunctionTraits_P
#define FunctionTraits_PX
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
Default type for differentiation.
T ArgType
Type for arguments.
T Type
Actual template type.
T BaseType
Template base type.
T ParamType
Type for parameters.
T NumericType
Numeric type of template.
static const T & getValue(const T &in)
Get the value.
this file contains all the compiler specific defines
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49