casa
$Rev:20696$
|
00001 //# AutoDiffA.h: An automatic differentiating class for functions 00002 //# Copyright (C) 2001,2002 00003 //# Associated Universities, Inc. Washington DC, USA. 00004 //# 00005 //# This library is free software; you can redistribute it and/or modify it 00006 //# under the terms of the GNU Library General Public License as published by 00007 //# the Free Software Foundation; either version 2 of the License, or (at your 00008 //# option) any later version. 00009 //# 00010 //# This library is distributed in the hope that it will be useful, but WITHOUT 00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 //# License for more details. 00014 //# 00015 //# You should have received a copy of the GNU Library General Public License 00016 //# along with this library; if not, write to the Free Software Foundation, 00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 00018 //# 00019 //# Correspondence concerning AIPS++ should be addressed as follows: 00020 //# Internet email: aips2-request@nrao.edu. 00021 //# Postal address: AIPS++ Project Office 00022 //# National Radio Astronomy Observatory 00023 //# 520 Edgemont Road 00024 //# Charlottesville, VA 22903-2475 USA 00025 //# 00026 //# 00027 //# $Id: AutoDiffA.h 21024 2011-03-01 11:46:18Z gervandiepen $ 00028 00029 #ifndef SCIMATH_AUTODIFFA_H 00030 #define SCIMATH_AUTODIFFA_H 00031 00032 //# Includes 00033 #include <casa/aips.h> 00034 #include <scimath/Mathematics/AutoDiff.h> 00035 00036 namespace casa { //# NAMESPACE CASA - BEGIN 00037 00038 //# Forward declarations 00039 template <class T> class Vector; 00040 00041 // <summary> 00042 // Class that computes partial derivatives by automatic differentiation. 00043 // </summary> 00044 // 00045 // <use visibility=export> 00046 // 00047 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tAutoDiff.cc" demos="dAutoDiff.cc"> 00048 // </reviewed> 00049 // 00050 // <prerequisite> 00051 // <li> <linkto class=AutoDiff>AutoDiff</linkto> 00052 // </prerequisite> 00053 // 00054 // <etymology> 00055 // Class that computes partial derivatives by automatic differentiation, thus 00056 // AutoDiff. 00057 // </etymology> 00058 // 00059 // <synopsis> 00060 // AutoDiffA is an <linkto class=AutoDiff>AutoDiff</linkto>. It is used 00061 // to be able to distinguish between two template incarnations; e.g. to 00062 // have one or more specializations, in addition to the general template 00063 // version. 00064 // </synopsis> 00065 // 00066 // <example> 00067 // See for an extensive example the demo program dAutoDiff. It is 00068 // based on the example given in the <linkto class=AutoDiff>AutoDiff</linkto> 00069 // class, and shows how to have both an automatic and a specific version 00070 // of a function object. 00071 // <srcblock> 00072 // // The function, with fixed parameters a,b: 00073 // template <class T> class f { 00074 // public: 00075 // T operator()(const T& x) { return a_p*a_p*a_p*b_p*b_p*x; } 00076 // void set(const T& a, const T& b) { a_p = a; b_p = b; } 00077 // private: 00078 // T a_p; 00079 // T b_p; 00080 // }; 00081 // // The specialized function 00082 // template <> class f<AutoDiffA<Double> > { 00083 // public: 00084 // T operator()(const T& x) { return a_p*a_p*a_p*b_p*b_p*x; } 00085 // void set(const T& a, const T& b) { a_p = a; b_p = b; } 00086 // private: 00087 // T a_p; 00088 // T b_p; 00089 // }; 00090 // // Call it with different template arguments: 00091 // AutoDiff<Double> a1(2,2,0), b1(3,2,1), x1(7); 00092 // f<AutoDiff<Double> > f1; f1.set(a1, b1); 00093 // cout << "Diff a,b: " << f1(x1) << endl; 00094 // 00095 // f<AutoDiffA<Double> > f12; f12.set(a1, b1); 00096 // cout << "Same....: " << f12(x1) << endl; 00097 // 00098 // // Result will be: 00099 // // Diff a,b: (504, [756, 336]) 00100 // // Same....: (504, [756, 336]) 00101 // 00102 // // It needed the template instantiations definitions: 00103 // template class f<AutoDiff<Double> >; 00104 // </srcblock> 00105 // </example> 00106 // 00107 // <motivation> 00108 // The class was created to enable separate calculations of the same 00109 // function. 00110 // </motivation> 00111 // 00112 // <templating arg=T> 00113 // <li> any class that has the standard mathematical and comparisons 00114 // defined 00115 // </templating> 00116 // 00117 // <todo asof="2001/06/07"> 00118 // <li> Nothing I know 00119 // </todo> 00120 00121 template <class T> class AutoDiffA : public AutoDiff<T> { 00122 public: 00123 //# Constructors 00124 // Construct a constant with a value of zero. Zero derivatives. 00125 AutoDiffA() : AutoDiff<T>() {} 00126 00127 // Construct a constant with a value of v. Zero derivatives. 00128 AutoDiffA(const T &v) : AutoDiff<T>(v) {} 00129 00130 // A function f(x0,x1,...,xn,...) with a value of v. The 00131 // total number of derivatives is ndiffs, the nth derivative is one, and all 00132 // others are zero. 00133 AutoDiffA(const T &v, const uInt ndiffs, const uInt n) : 00134 AutoDiff<T>(v, ndiffs, n) {} 00135 00136 // A function f(x0,x1,...,xn,...) with a value of v. The 00137 // total number of derivatives is ndiffs. 00138 // All derivatives are zero. 00139 AutoDiffA(const T &v, const uInt ndiffs) : AutoDiff<T>(v, ndiffs) {} 00140 00141 // Construct one from another 00142 AutoDiffA(const AutoDiff<T> &other) : AutoDiff<T>(other) {} 00143 00144 // Construct a function f(x0,x1,...,xn) of a value v and a vector of 00145 // derivatives derivs(0) = df/dx0, derivs(1) = df/dx1, ... 00146 AutoDiffA(const T &v, const Vector<T> &derivs) : AutoDiff<T>(v, derivs) {} 00147 00148 ~AutoDiffA() {} 00149 00150 // Assignment operator. Assign a constant to variable. All derivatives 00151 // are zero. 00152 AutoDiffA<T> &operator=(const T &v) { 00153 AutoDiff<T>::operator=(v); 00154 return *this; 00155 } 00156 00157 // Assign one to another. 00158 AutoDiffA<T> &operator=(const AutoDiff<T> &other) { 00159 AutoDiff<T>::operator=(other); 00160 return *this; 00161 } 00162 00163 private: 00164 //# Data 00165 00166 }; 00167 00168 00169 } //# NAMESPACE CASA - END 00170 00171 #endif