CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
for_one.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_FOR_ONE_HPP
2 # define CPPAD_CORE_FOR_ONE_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
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.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 
15 /*
16 $begin ForOne$$
17 $spell
18  dy
19  typename
20  Taylor
21  const
22 $$
23 
24 
25 
26 
27 $section First Order Partial Derivative: Driver Routine$$
28 $mindex easy$$
29 
30 $head Syntax$$
31 $icode%dy% = %f%.ForOne(%x%, %j%)%$$
32 
33 
34 $head Purpose$$
35 We use $latex F : B^n \rightarrow B^m$$ to denote the
36 $cref/AD function/glossary/AD Function/$$ corresponding to $icode f$$.
37 The syntax above sets $icode dy$$ to the
38 partial of $latex F$$ with respect to $latex x_j$$; i.e.,
39 $latex \[
40 dy
41 = \D{F}{ x_j } (x)
42 = \left[
43  \D{ F_0 }{ x_j } (x) , \cdots , \D{ F_{m-1} }{ x_j } (x)
44 \right]
45 \] $$
46 
47 $head f$$
48 The object $icode f$$ has prototype
49 $codei%
50  ADFun<%Base%> %f%
51 %$$
52 Note that the $cref ADFun$$ object $icode f$$ is not $code const$$
53 (see $cref/ForOne Uses Forward/ForOne/ForOne Uses Forward/$$ below).
54 
55 $head x$$
56 The argument $icode x$$ has prototype
57 $codei%
58  const %Vector% &%x%
59 %$$
60 (see $cref/Vector/ForOne/Vector/$$ below)
61 and its size
62 must be equal to $icode n$$, the dimension of the
63 $cref/domain/seq_property/Domain/$$ space for $icode f$$.
64 It specifies
65 that point at which to evaluate the partial derivative.
66 
67 $head j$$
68 The argument $icode j$$ has prototype
69 $codei%
70  size_t %j%
71 %$$
72 an is less than $icode n$$,
73 $cref/domain/seq_property/Domain/$$ space for $icode f$$.
74 It specifies the component of $icode F$$
75 for which we are computing the partial derivative.
76 
77 $head dy$$
78 The result $icode dy$$ has prototype
79 $codei%
80  %Vector% %dy%
81 %$$
82 (see $cref/Vector/ForOne/Vector/$$ below)
83 and its size is $latex m$$, the dimension of the
84 $cref/range/seq_property/Range/$$ space for $icode f$$.
85 The value of $icode dy$$ is the partial of $latex F$$ with respect to
86 $latex x_j$$ evaluated at $icode x$$; i.e.,
87 for $latex i = 0 , \ldots , m - 1$$
88 $latex \[.
89  dy[i] = \D{ F_i }{ x_j } ( x )
90 \] $$
91 
92 
93 $head Vector$$
94 The type $icode Vector$$ must be a $cref SimpleVector$$ class with
95 $cref/elements of type/SimpleVector/Elements of Specified Type/$$
96 $icode Base$$.
97 The routine $cref CheckSimpleVector$$ will generate an error message
98 if this is not the case.
99 
100 $head ForOne Uses Forward$$
101 After each call to $cref Forward$$,
102 the object $icode f$$ contains the corresponding
103 $cref/Taylor coefficients/glossary/Taylor Coefficient/$$.
104 After a call to $code ForOne$$,
105 the zero order Taylor coefficients correspond to
106 $icode%f%.Forward(0,%x%)%$$
107 and the other coefficients are unspecified.
108 
109 $head Example$$
110 $children%
111  example/general/for_one.cpp
112 %$$
113 The routine
114 $cref/ForOne/for_one.cpp/$$ is both an example and test.
115 It returns $code true$$, if it succeeds and $code false$$ otherwise.
116 
117 $end
118 -----------------------------------------------------------------------------
119 */
120 
121 // BEGIN CppAD namespace
122 namespace CppAD {
123 
124 template <typename Base>
125 template <typename Vector>
126 Vector ADFun<Base>::ForOne(const Vector &x, size_t j)
127 { size_t j1;
128 
129  size_t n = Domain();
130  size_t m = Range();
131 
132  // check Vector is Simple Vector class with Base type elements
133  CheckSimpleVector<Base, Vector>();
134 
136  x.size() == n,
137  "ForOne: Length of x not equal domain dimension for f"
138  );
140  j < n,
141  "ForOne: the index j is not less than domain dimension for f"
142  );
143 
144  // point at which we are evaluating the second partials
145  Forward(0, x);
146 
147  // direction in which are are taking the derivative
148  Vector dx(n);
149  for(j1 = 0; j1 < n; j1++)
150  dx[j1] = Base(0.0);
151  dx[j] = Base(1.0);
152 
153  // dimension the return value
154  Vector dy(m);
155 
156  // compute the return value
157  dy = Forward(1, dx);
158 
159  return dy;
160 }
161 
162 } // END CppAD namespace
163 
164 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
VectorBase ForOne(const VectorBase &x, size_t j)
forward mode calculation of partial w.r.t one domain component