CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
hessian.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_HESSIAN_HPP
2 # define CPPAD_CORE_HESSIAN_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 Hessian$$
17 $spell
18  hes
19  typename
20  Taylor
21  HesLuDet
22  const
23 $$
24 
25 
26 $section Hessian: Easy Driver$$
27 $mindex second derivative$$
28 
29 $head Syntax$$
30 $icode%hes% = %f%.Hessian(%x%, %w%)
31 %$$
32 $icode%hes% = %f%.Hessian(%x%, %l%)
33 %$$
34 
35 
36 $head Purpose$$
37 We use $latex F : B^n \rightarrow B^m$$ to denote the
38 $cref/AD function/glossary/AD Function/$$ corresponding to $icode f$$.
39 The syntax above sets $icode hes$$ to the Hessian
40 The syntax above sets $icode h$$ to the Hessian
41 $latex \[
42  hes = \dpow{2}{x} \sum_{i=1}^m w_i F_i (x)
43 \] $$
44 The routine $cref sparse_hessian$$ may be faster in the case
45 where the Hessian is sparse.
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/Hessian Uses Forward/Hessian/Hessian 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/Hessian/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 Hessian.
66 
67 $head l$$
68 If the argument $icode l$$ is present, it has prototype
69 $codei%
70  size_t %l%
71 %$$
72 and is less than $icode m$$, the dimension of the
73 $cref/range/seq_property/Range/$$ space for $icode f$$.
74 It specifies the component of $icode F$$
75 for which we are evaluating the Hessian.
76 To be specific, in the case where the argument $icode l$$ is present,
77 $latex \[
78  w_i = \left\{ \begin{array}{ll}
79  1 & i = l \\
80  0 & {\rm otherwise}
81  \end{array} \right.
82 \] $$
83 
84 $head w$$
85 If the argument $icode w$$ is present, it has prototype
86 $codei%
87  const %Vector% &%w%
88 %$$
89 and size $latex m$$.
90 It specifies the value of $latex w_i$$ in the expression
91 for $icode h$$.
92 
93 $head hes$$
94 The result $icode hes$$ has prototype
95 $codei%
96  %Vector% %hes%
97 %$$
98 (see $cref/Vector/Hessian/Vector/$$ below)
99 and its size is $latex n * n$$.
100 For $latex j = 0 , \ldots , n - 1 $$
101 and $latex \ell = 0 , \ldots , n - 1$$
102 $latex \[
103  hes [ j * n + \ell ] = \DD{ w^{\rm T} F }{ x_j }{ x_\ell } ( x )
104 \] $$
105 
106 $head Vector$$
107 The type $icode Vector$$ must be a $cref SimpleVector$$ class with
108 $cref/elements of type/SimpleVector/Elements of Specified Type/$$
109 $icode Base$$.
110 The routine $cref CheckSimpleVector$$ will generate an error message
111 if this is not the case.
112 
113 $head Hessian Uses Forward$$
114 After each call to $cref Forward$$,
115 the object $icode f$$ contains the corresponding
116 $cref/Taylor coefficients/glossary/Taylor Coefficient/$$.
117 After a call to $code Hessian$$,
118 the zero order Taylor coefficients correspond to
119 $icode%f%.Forward(0, %x%)%$$
120 and the other coefficients are unspecified.
121 
122 $head Example$$
123 $children%
124  example/general/hessian.cpp%
125  example/general/hes_lagrangian.cpp
126 %$$
127 The routines
128 $cref hessian.cpp$$ and
129 $cref hes_lagrangian.cpp$$
130 are examples and tests of $code Hessian$$.
131 They return $code true$$, if they succeed and $code false$$ otherwise.
132 
133 
134 $end
135 -----------------------------------------------------------------------------
136 */
137 
138 // BEGIN CppAD namespace
139 namespace CppAD {
140 
141 template <typename Base>
142 template <typename Vector>
143 Vector ADFun<Base>::Hessian(const Vector &x, size_t l)
144 { size_t i, m = Range();
146  l < m,
147  "Hessian: index i is not less than range dimension for f"
148  );
149 
150  Vector w(m);
151  for(i = 0; i < m; i++)
152  w[i] = Base(0.0);
153  w[l] = Base(1.0);
154 
155  return Hessian(x, w);
156 }
157 
158 
159 template <typename Base>
160 template <typename Vector>
161 Vector ADFun<Base>::Hessian(const Vector &x, const Vector &w)
162 { size_t j;
163  size_t k;
164 
165  size_t n = Domain();
166 
167  // check Vector is Simple Vector class with Base type elements
168  CheckSimpleVector<Base, Vector>();
169 
171  size_t(x.size()) == n,
172  "Hessian: length of x not equal domain dimension for f"
173  );
175  size_t(w.size()) == Range(),
176  "Hessian: length of w not equal range dimension for f"
177  );
178 
179  // point at which we are evaluating the Hessian
180  Forward(0, x);
181 
182  // define the return value
183  Vector hes(n * n);
184 
185  // direction vector for calls to forward
186  Vector u(n);
187  for(j = 0; j < n; j++)
188  u[j] = Base(0.0);
189 
190 
191  // location for return values from Reverse
192  Vector ddw(n * 2);
193 
194  // loop over forward directions
195  for(j = 0; j < n; j++)
196  { // evaluate partials of entire function w.r.t. j-th coordinate
197  u[j] = Base(1.0);
198  Forward(1, u);
199  u[j] = Base(0.0);
200 
201  // evaluate derivative of partial corresponding to F_i
202  ddw = Reverse(2, w);
203 
204  // return desired components
205  for(k = 0; k < n; k++)
206  hes[k * n + j] = ddw[k * 2 + 1];
207  }
208 
209  return hes;
210 }
211 
212 } // END CppAD namespace
213 
214 # endif
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
VectorBase Hessian(const VectorBase &x, const VectorBase &w)
calculate Hessian for one component of f