CppAD: A C++ Algorithmic Differentiation Package  20171217
rev_one.hpp
Go to the documentation of this file.
3
4 /* --------------------------------------------------------------------------
6
8 the terms of the
9  Eclipse Public License Version 1.0.
10
11 A copy of this license is included in the COPYING file of this distribution.
13 -------------------------------------------------------------------------- */
14
15 /*
16 $begin RevOne$$17 spell 18 dw 19 Taylor 20 const 21$$ 22 23 24 25 26$section First Order Derivative: Driver Routine$$27 mindex derivative easy$$
28
29 $head Syntax$$30 icode%dw% = %f%.RevOne(%x%, %i%)%$$ 31 32 33$head Purpose$$34 We use latex F : B^n \rightarrow B^m$$ to denote the
35 $cref/AD function/glossary/AD Function/$$corresponding to icode f$$. 36 The syntax above sets$icode dw$$to the 37 derivative of latex F_i$$ with respect to $latex x$$; i.e., 38 latex $39 dw = 40 F_i^{(1)} (x) 41 = \left[ 42 \D{ F_i }{ x_0 } (x) , \cdots , \D{ F_i }{ x_{n-1} } (x) 43 \right] 44$$$ 45 46$head f$$47 The object icode f$$ has prototype
48 $codei% 49 ADFun<%Base%> %f% 50 %$$51 Note that the cref ADFun$$ object$icode f$$is not code const$$
52 (see $cref/RevOne Uses Forward/RevOne/RevOne Uses Forward/$$below). 53 54 head x$$ 55 The argument$icode x$$has prototype 56 codei% 57 const %Vector% &%x% 58 %$$
59 (see $cref/Vector/RevOne/Vector/$$below) 60 and its size 61 must be equal to icode n$$, the dimension of the 62$cref/domain/seq_property/Domain/$$space for icode f$$.
63 It specifies
64 that point at which to evaluate the derivative.
65
66 $head i$$67 The index icode i$$ has prototype 68$codei%
69  size_t %i%
70 %$$71 and is less than latex m$$, the dimension of the
72 $cref/range/seq_property/Range/$$space for icode f$$. 73 It specifies the 74 component of$latex F$$that we are computing the derivative of. 75 76 head dw$$
77 The result $icode dw$$has prototype 78 codei% 79 %Vector% %dw% 80 %$$ 81 (see$cref/Vector/RevOne/Vector/$$below) 82 and its size is icode n$$, the dimension of the
83 $cref/domain/seq_property/Domain/$$space for icode f$$. 84 The value of$icode dw$$is the derivative of latex F_i$$
85 evaluated at $icode x$$; i.e., 86 for latex j = 0 , \ldots , n - 1$$ 87$latex $. 88 dw[ j ] = \D{ F_i }{ x_j } ( x ) 89$ $$90 91 head Vector$$
92 The type $icode Vector$$must be a cref SimpleVector$$ class with 93$cref/elements of type/SimpleVector/Elements of Specified Type/$$94 icode Base$$.
95 The routine $cref CheckSimpleVector$$will generate an error message 96 if this is not the case. 97 98 head RevOne Uses Forward$$ 99 After each call to$cref Forward$$, 100 the object icode f$$ contains the corresponding
101 $cref/Taylor coefficients/glossary/Taylor Coefficient/$$. 102 After a call to code RevOne$$, 103 the zero order Taylor coefficients correspond to 104$icode%f%.Forward(0, %x%)%$$105 and the other coefficients are unspecified. 106 107 head Example$$
108 $children% 109 example/general/rev_one.cpp 110 %$$111 The routine 112 cref/RevOne/rev_one.cpp/$$ is both an example and test. 113 It returns$code true$$, if it succeeds and code false$$ otherwise.
114
115 \$end
116 -----------------------------------------------------------------------------
117 */
118
121
122 template <typename Base>
123 template <typename Vector>
124 Vector ADFun<Base>::RevOne(const Vector &x, size_t i)
125 { size_t i1;
126
127  size_t n = Domain();
128  size_t m = Range();
129
130  // check Vector is Simple Vector class with Base type elements
131  CheckSimpleVector<Base, Vector>();
132
134  x.size() == n,
135  "RevOne: Length of x not equal domain dimension for f"
136  );
138  i < m,
139  "RevOne: the index i is not less than range dimension for f"
140  );
141
142  // point at which we are evaluating the derivative
143  Forward(0, x);
144
145  // component which are are taking the derivative of
146  Vector w(m);
147  for(i1 = 0; i1 < m; i1++)
148  w[i1] = 0.;
149  w[i] = Base(1.0);
150
151  // dimension the return value
152  Vector dw(n);
153
154  // compute the return value
155  dw = Reverse(1, w);
156
157  return dw;
158 }
159
160 } // END CppAD namespace
161
162 # endif