casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SparseDiffA.h
Go to the documentation of this file.
1 //# SparseDiff!A.h: An automatic differentiating class for functions
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 //#
27 //# $Id: SparseDiffA.h,v 1.1 2007/11/16 04:34:46 wbrouw Exp $
28 
29 #ifndef SCIMATH_SPARSEDIFFA_H
30 #define SCIMATH_SPARSEDIFFA_H
31 
32 //# Includes
33 #include <casacore/casa/aips.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39  // <summary>
40  // Class that computes partial derivatives by automatic differentiation.
41  // </summary>
42  //
43  // <use visibility=export>
44  //
45  // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tSparseDiff.cc" demos="dSparseDiff.cc">
46  // </reviewed>
47  //
48  // <prerequisite>
49  // <li> <linkto class=SparseDiff>SparseDiff</linkto>
50  // </prerequisite>
51  //
52  // <etymology>
53  // Class that computes partial derivatives by automatic differentiation, thus
54  // SparseDiff.
55  // </etymology>
56  //
57  // <synopsis>
58  // SparseDiffA is an <linkto class=SparseDiff>SparseDiff</linkto>. It is used
59  // to be able to distinguish between two template incarnations; e.g. to
60  // have one or more specializations, in addition to the general template
61  // version.
62  // </synopsis>
63  //
64  // <example>
65  // See for an extensive example the demo program dSparseDiff. It is
66  // based on the example given in the <linkto class=SparseDiff>SparseDiff</linkto>
67  // class, and shows how to have both an automatic and a specific version
68  // of a function object.
69  // <srcblock>
70  // // The function, with fixed parameters a,b:
71  // template <class T> class f {
72  // public:
73  // T operator()(const T& x) { return a_p*a_p*a_p*b_p*b_p*x; }
74  // void set(const T& a, const T& b) { a_p = a; b_p = b; }
75  // private:
76  // T a_p;
77  // T b_p;
78  // };
79  // // The specialized function
80  // template <> class f<SparseDiffA<Double> > {
81  // public:
82  // T operator()(const T& x) { return a_p*a_p*a_p*b_p*b_p*x; }
83  // void set(const T& a, const T& b) { a_p = a; b_p = b; }
84  // private:
85  // T a_p;
86  // T b_p;
87  // };
88  // // Call it with different template arguments:
89  // SparseDiff<Double> a1(2,0), b1(3,1), x1(7);
90  // f<SparseDiff<Double> > f1; f1.set(a1, b1);
91  // cout << "Diff a,b: " << f1(x1) << endl;
92  //
93  // f<SparseDiffA<Double> > f12; f12.set(a1, b1);
94  // cout << "Same....: " << f12(x1) << endl;
95  //
96  // // Result will be:
97  // // Diff a,b: (504, [756, 336])
98  // // Same....: (504, [756, 336])
99  //
100  // // It needed the template instantiations definitions:
101  // template class f<SparseDiff<Double> >;
102  // </srcblock>
103  // </example>
104  //
105  // <motivation>
106  // The class was created to enable separate calculations of the same
107  // function.
108  // </motivation>
109  //
110  // <templating arg=T>
111  // <li> any class that has the standard mathematical and comparisons
112  // defined
113  // </templating>
114  //
115  // <todo asof="2001/06/07">
116  // <li> Nothing I know
117  // </todo>
118 
119  template <class T> class SparseDiffA : public SparseDiff<T> {
120  public:
121  //# Constructors
122  // Construct a constant with a value of zero. Zero derivatives.
124 
125  // Construct a constant with a value of v. Zero derivatives.
126  SparseDiffA(const T &v) : SparseDiff<T>(v) {}
127 
128  // A function f(x0,x1,...,xn,...) with a value of v.
129  // The nth derivative is one, and all others are zero.
130  SparseDiffA(const T &v, const uInt n) :
131  SparseDiff<T>(v, n) {}
132 
133  // A function f(x0,x1,...,xn,...) with a value of v. The
134  // nth derivative is der, and all other derivatives are zero.
135  SparseDiffA(const T &v, const uInt n, const T &der) :
136  SparseDiff<T>(v, n, der) {}
137 
138  // Construct one from another
139  SparseDiffA(const SparseDiff<T> &other) : SparseDiff<T>(other) {}
140 
142 
143  // Assignment operator. Assign a constant to variable. All derivatives
144  // are zero.
145  SparseDiffA<T> &operator=(const T &v) {
147  return *this;
148  }
149 
150  // Assignment operator. Add a gradient to variable.
151  SparseDiffA<T> &operator=(const pair<uInt, T> &der) {
153  return *this;
154  }
155 
156  // Assignment operator. Assign gradients to variable.
157  SparseDiffA<T> &operator=(const vector<pair<uInt, T> > &der) {
159  return *this;
160  }
161 
162  // Assign one to another (deep copy).
165  return *this;
166  }
167 
168  private:
169  //# Data
170 
171  };
172 
173 
174 } //# NAMESPACE CASACORE - END
175 
176 #endif
Class that computes partial derivatives by automatic differentiation.
Definition: SparseDiffA.h:119
Class that computes partial derivatives by automatic differentiation.
Definition: SparseDiff.h:45
SparseDiffA< T > & operator=(const SparseDiff< T > &other)
Assign one to another (deep copy).
Definition: SparseDiffA.h:163
SparseDiffA< T > & operator=(const vector< pair< uInt, T > > &der)
Assignment operator.
Definition: SparseDiffA.h:157
SparseDiff< T > & operator=(const T &v)
Assignment operator.
SparseDiffA(const SparseDiff< T > &other)
Construct one from another.
Definition: SparseDiffA.h:139
SparseDiffA(const T &v)
Construct a constant with a value of v.
Definition: SparseDiffA.h:126
SparseDiffA()
Construct a constant with a value of zero.
Definition: SparseDiffA.h:123
SparseDiffA< T > & operator=(const T &v)
Assignment operator.
Definition: SparseDiffA.h:145
SparseDiffA(const T &v, const uInt n, const T &der)
A function f(x0,x1,...,xn,...) with a value of v.
Definition: SparseDiffA.h:135
SparseDiffA< T > & operator=(const pair< uInt, T > &der)
Assignment operator.
Definition: SparseDiffA.h:151
SparseDiffA(const T &v, const uInt n)
A function f(x0,x1,...,xn,...) with a value of v.
Definition: SparseDiffA.h:130
unsigned int uInt
Definition: aipstype.h:51