CppAD: A C++ Algorithmic Differentiation Package
20130918
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_REV_ONE_INCLUDED 00003 # define CPPAD_REV_ONE_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Eclipse Public License Version 1.0. 00011 00012 A copy of this license is included in the COPYING file of this distribution. 00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00014 -------------------------------------------------------------------------- */ 00015 00016 /* 00017 $begin RevOne$$ 00018 $spell 00019 dw 00020 Taylor 00021 const 00022 $$ 00023 00024 00025 $index derivative, first order driver$$ 00026 $index first, order derivative driver$$ 00027 $index driver, first order derivative$$ 00028 00029 $index easy, derivative$$ 00030 $index driver, easy derivative$$ 00031 $index derivative, easy$$ 00032 00033 $section First Order Derivative: Driver Routine$$ 00034 00035 $head Syntax$$ 00036 $icode%dw% = %f%.RevOne(%x%, %i%)%$$ 00037 00038 00039 $head Purpose$$ 00040 We use $latex F : B^n \rightarrow B^m$$ to denote the 00041 $cref/AD function/glossary/AD Function/$$ corresponding to $icode f$$. 00042 The syntax above sets $icode dw$$ to the 00043 derivative of $latex F_i$$ with respect to $latex x$$; i.e., 00044 $latex \[ 00045 dw = 00046 F_i^{(1)} (x) 00047 = \left[ 00048 \D{ F_i }{ x_0 } (x) , \cdots , \D{ F_i }{ x_{n-1} } (x) 00049 \right] 00050 \] $$ 00051 00052 $head f$$ 00053 The object $icode f$$ has prototype 00054 $codei% 00055 ADFun<%Base%> %f% 00056 %$$ 00057 Note that the $cref ADFun$$ object $icode f$$ is not $code const$$ 00058 (see $cref/RevOne Uses Forward/RevOne/RevOne Uses Forward/$$ below). 00059 00060 $head x$$ 00061 The argument $icode x$$ has prototype 00062 $codei% 00063 const %Vector% &%x% 00064 %$$ 00065 (see $cref/Vector/RevOne/Vector/$$ below) 00066 and its size 00067 must be equal to $icode n$$, the dimension of the 00068 $cref/domain/seq_property/Domain/$$ space for $icode f$$. 00069 It specifies 00070 that point at which to evaluate the derivative. 00071 00072 $head i$$ 00073 The index $icode i$$ has prototype 00074 $codei% 00075 size_t %i% 00076 %$$ 00077 and is less than $latex m$$, the dimension of the 00078 $cref/range/seq_property/Range/$$ space for $icode f$$. 00079 It specifies the 00080 component of $latex F$$ that we are computing the derivative of. 00081 00082 $head dw$$ 00083 The result $icode dw$$ has prototype 00084 $codei% 00085 %Vector% %dw% 00086 %$$ 00087 (see $cref/Vector/RevOne/Vector/$$ below) 00088 and its size is $icode n$$, the dimension of the 00089 $cref/domain/seq_property/Domain/$$ space for $icode f$$. 00090 The value of $icode dw$$ is the derivative of $latex F_i$$ 00091 evaluated at $icode x$$; i.e., 00092 for $latex j = 0 , \ldots , n - 1 $$ 00093 $latex \[. 00094 dw[ j ] = \D{ F_i }{ x_j } ( x ) 00095 \] $$ 00096 00097 $head Vector$$ 00098 The type $icode Vector$$ must be a $cref SimpleVector$$ class with 00099 $cref/elements of type/SimpleVector/Elements of Specified Type/$$ 00100 $icode Base$$. 00101 The routine $cref CheckSimpleVector$$ will generate an error message 00102 if this is not the case. 00103 00104 $head RevOne Uses Forward$$ 00105 After each call to $cref Forward$$, 00106 the object $icode f$$ contains the corresponding 00107 $cref/Taylor coefficients/glossary/Taylor Coefficient/$$. 00108 After a call to $code RevOne$$, 00109 the zero order Taylor coefficients correspond to 00110 $icode%f%.Forward(0, %x%)%$$ 00111 and the other coefficients are unspecified. 00112 00113 $head Example$$ 00114 $children% 00115 example/rev_one.cpp 00116 %$$ 00117 The routine 00118 $cref/RevOne/rev_one.cpp/$$ is both an example and test. 00119 It returns $code true$$, if it succeeds and $code false$$ otherwise. 00120 00121 $end 00122 ----------------------------------------------------------------------------- 00123 */ 00124 00125 // BEGIN CppAD namespace 00126 namespace CppAD { 00127 00128 template <typename Base> 00129 template <typename Vector> 00130 Vector ADFun<Base>::RevOne(const Vector &x, size_t i) 00131 { size_t i1; 00132 00133 size_t n = Domain(); 00134 size_t m = Range(); 00135 00136 // check Vector is Simple Vector class with Base type elements 00137 CheckSimpleVector<Base, Vector>(); 00138 00139 CPPAD_ASSERT_KNOWN( 00140 x.size() == n, 00141 "RevOne: Length of x not equal domain dimension for f" 00142 ); 00143 CPPAD_ASSERT_KNOWN( 00144 i < m, 00145 "RevOne: the index i is not less than range dimension for f" 00146 ); 00147 00148 // point at which we are evaluating the derivative 00149 Forward(0, x); 00150 00151 // component which are are taking the derivative of 00152 Vector w(m); 00153 for(i1 = 0; i1 < m; i1++) 00154 w[i1] = 0.; 00155 w[i] = Base(1); 00156 00157 // dimension the return value 00158 Vector dw(n); 00159 00160 // compute the return value 00161 dw = Reverse(1, w); 00162 00163 return dw; 00164 } 00165 00166 } // END CppAD namespace 00167 00168 # endif