casacore
Loading...
Searching...
No Matches
AutoDiffX.h
Go to the documentation of this file.
1// # AutoDiffX.h: An automatic differentiating class for functions
2// # Copyright (C) 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_AUTODIFFX_H
27#define SCIMATH_AUTODIFFX_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/ArrayFwd.h>
32#include <casacore/scimath/Mathematics/AutoDiff.h>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// <summary>
37// Class that computes partial derivatives by automatic differentiation.
38// </summary>
39//
40// <use visibility=export>
41//
42// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tAutoDiff.cc" demos="dAutoDiff.cc">
43// </reviewed>
44//
45// <prerequisite>
46// <li> <linkto class=AutoDiff>AutoDiff</linkto>
47// </prerequisite>
48//
49// <etymology>
50// Class that computes partial derivatives by automatic differentiation, thus
51// AutoDiff.
52// </etymology>
53//
54// <synopsis>
55// AutoDiffX is an <linkto class=AutoDiff>AutoDiff</linkto>. It is used
56// to be able to distinguish between two template incarnations; e.g. to
57// have one or more specializations, in addition to the general template
58// version.
59// </synopsis>
60//
61// <example>
62// See for an extensive example the demo program dAutoDiff. It is
63// based on the example given in the <linkto class=AutoDiff>AutoDiff</linkto>
64// class, and shows how to have both an automatic and a specific version
65// of a function object.
66// <srcblock>
67// // The function, with fixed parameters a,b:
68// template <class T> class f {
69// public:
70// T operator()(const T& x) { return a_p*a_p*a_p*b_p*b_p*x; }
71// void set(const T& a, const T& b) { a_p = a; b_p = b; }
72// private:
73// T a_p;
74// T b_p;
75// };
76// // The specialized function
77// template <> class f<AutoDiffX<Double> > {
78// public:
79// T operator()(const T& x) { return a_p*a_p*a_p*b_p*b_p*x; }
80// void set(const T& a, const T& b) { a_p = a; b_p = b; }
81// private:
82// T a_p;
83// T b_p;
84// };
85// // Call it with different template arguments:
86// AutoDiff<Double> a1(2,2,0), b1(3,2,1), x1(7);
87// f<AutoDiff<Double> > f1; f1.set(a1, b1);
88// cout << "Diff a,b: " << f1(x1) << endl;
89//
90// f<AutoDiffX<Double> > f12; f12.set(a1, b1);
91// cout << "Same....: " << f12(x1) << endl;
92//
93// // Result will be:
94// // Diff a,b: (504, [756, 336])
95// // Same....: (504, [756, 336])
96//
97// // It needed the template instantiations definitions:
98// template class f<AutoDiff<Double> >;
99// </srcblock>
100// </example>
101//
102// <motivation>
103// The class was created to enable separate calculations of the same
104// function.
105// </motivation>
106//
107// <templating arg=T>
108// <li> any class that has the standard mathematical and comparisons
109// defined
110// </templating>
111//
112// <todo asof="2001/06/07">
113// <li> Nothing I know
114// </todo>
115
116template <class T>
117class AutoDiffX : public AutoDiff<T> {
118 public:
119 // # Constructors
120 // Construct a constant with a value of zero. Zero derivatives.
121 AutoDiffX() : AutoDiff<T>() {}
122
123 // Construct a constant with a value of v. Zero derivatives.
124 AutoDiffX(const T &v) : AutoDiff<T>(v) {}
125
126 // A function f(x0,x1,...,xn,...) with a value of v. The
127 // total number of derivatives is ndiffs, the nth derivative is one, and all
128 // others are zero.
129 AutoDiffX(const T &v, const uInt ndiffs, const uInt n) : AutoDiff<T>(v, ndiffs, n) {}
130
131 // A function f(x0,x1,...,xn,...) with a value of v. The
132 // total number of derivatives is ndiffs.
133 // All derivatives are zero.
134 AutoDiffX(const T &v, const uInt ndiffs) : AutoDiff<T>(v, ndiffs) {}
135
136 // Construct one from another
137 AutoDiffX(const AutoDiff<T> &other) : AutoDiff<T>(other) {}
138
139 // Construct a function f(x0,x1,...,xn) of a value v and a vector of
140 // derivatives derivs(0) = df/dx0, derivs(1) = df/dx1, ...
141 AutoDiffX(const T &v, const Vector<T> &derivs) : AutoDiff<T>(v, derivs) {}
142
144
145 // Assignment operator. Assign a constant to variable. All derivatives
146 // are zero.
147 AutoDiffX<T> &operator=(const T &v) {
149 return *this;
150 }
151
152 // Assign one to another.
155 return *this;
156 }
157
158 private:
159 // # Data
160};
161
162} // namespace casacore
163
164#endif
AutoDiffX(const T &v, const uInt ndiffs)
A function f(x0,x1,...,xn,...) with a value of v.
Definition AutoDiffX.h:134
AutoDiffX(const T &v)
Construct a constant with a value of v.
Definition AutoDiffX.h:124
AutoDiffX< T > & operator=(const T &v)
Assignment operator.
Definition AutoDiffX.h:147
AutoDiffX(const T &v, const uInt ndiffs, const uInt n)
A function f(x0,x1,...,xn,...) with a value of v.
Definition AutoDiffX.h:129
AutoDiffX(const T &v, const Vector< T > &derivs)
Construct a function f(x0,x1,...,xn) of a value v and a vector of derivatives derivs(0) = df/dx0,...
Definition AutoDiffX.h:141
AutoDiffX(const AutoDiff< T > &other)
Construct one from another.
Definition AutoDiffX.h:137
AutoDiffX< T > & operator=(const AutoDiff< T > &other)
Assign one to another.
Definition AutoDiffX.h:153
AutoDiffX()
Construct a constant with a value of zero.
Definition AutoDiffX.h:121
AutoDiff< T > & operator=(const T &v)
Assignment operator.
AutoDiff()
Construct a constant with a value of zero.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49