Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathadfun.cpp
More file actions
539 lines (469 loc) · 15.3 KB
/
Copy pathadfun.cpp
File metadata and controls
539 lines (469 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
---------------------------------------------------------------------------
$begin forward$$
$spell
numpy
adfun
Taylor
$$
$section Forward Mode: Derivative in One Domain Direction$$
$index forward$$
$index domain, direction derivative$$
$index derivative, domain direction$$
$index direction, domain derivative$$
$head Syntax$$
$icode%y_p% = %f%.forward(%p%, %x_p%)%$$
$head Purpose$$
We use $latex F : \B{R}^n \rightarrow \B{R}^m$$ to denote the
function corresponding to the $code adfun$$ object $cref/f/adfun/f/$$.
Given the $th p$$ order Taylor expansion for a function
$latex X : \B{R} \rightarrow \B{R}^n$$, this function can be used
to compute the $th p$$ order Taylor expansion for the function
$latex Y : \B{R} \rightarrow \B{R}^m$$ defined by
$latex \[
Y(t) = F [ X(t) ]
\] $$
$head x_k$$
For $latex k = 0 , \ldots , p$$,
we use $latex x^{(k)}$$ to denote the value of $icode x_k$$ in the
most recent call to
$codei%
%f%.forward(%k%, %x_k%)
%$$
including $latex x^{(p)}$$ as the value $icode x_p$$ in this call.
We define the function $latex X(t)$$ by
$latex \[
X(t) = x^{(0)} + x^{(1)} * t + \cdots + x^{(p)} * t^p
\] $$
$head y_k$$
For $latex k = 0 , \ldots , p$$,
we use $latex y^{(k)}$$ to denote the Taylor coefficients
for $latex Y(t) = F[ X(t) ]$$ expanded about zero; i.e.,
$latex \[
\begin{array}{rcl}
y^{(k)} & = & Y^{(k)} (0) / k !
\\
Y(t) & = & y^{(0)} + y^{(1)} * t + \cdots + y^{(p)} * t^p + o( t^p )
\end{array}
\] $$
where $latex o( t^p ) / t^p \rightarrow 0$$ as $latex t \rightarrow 0$$.
The coefficient $latex y^{(p)}$$ is equal to
the value $icode y_p$$ returned by this call.
$head f$$
The object $icode f$$ must be an $cref adfun$$ object.
We use $cref/level/adfun/f/level/$$ for the AD $cref ad$$ level of
this object.
$head p$$
The argument $icode p$$ is a non-negative $code int$$.
It specifies the order of the Taylor coefficient for $latex Y(t)$$
that is computed.
$head x_p$$
The argument $icode x_p$$ is a $code numpy.array$$ with one dimension
(i.e., a vector) with length equal to the domain size $cref/n/adfun/f/n/$$
for the function $icode f$$.
It specifies the $th p$$ order Taylor coefficient for $latex X(t)$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode x_p$$ must be either $code int$$ or instances
of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode x_p$$ must be $code a_float$$ objects.
$head y_p$$
The return value $icode y_p$$ is a $code numpy.array$$ with one dimension
(i.e., a vector) with length equal to the range size $cref/m/adfun/f/m/$$
for the function $icode f$$.
It is set to the $th p$$ order Taylor coefficient for $latex Y(t)$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode y_p$$ will be instances of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode y_p$$ will be $code a_float$$ objects.
$children%
example/forward_0.py%
example/forward_1.py
%$$
$head Example$$
$table
$rref forward_0.py$$
$rref forward_1.py$$
$tend
$end
---------------------------------------------------------------------------
$begin reverse$$
$spell
dw
numpy
adfun
Taylor
$$
$section Reverse Mode: Derivative in One Range Direction$$
$index reverse$$
$index derivative, range direction$$
$index range, direction derivative$$
$index direction, range derivative$$
$head Syntax$$
$icode%dw% = %f%.forward(%p%, %w%)%$$
$head Purpose$$
Reverse mode computes the derivative of the $cref forward$$ more
Taylor coefficients with respect to the domain variable $latex x$$.
$head x_k$$
For $latex k = 0 , \ldots , p$$,
we use $latex x^{(k)}$$ to denote the value of $icode x_k$$ in the
most recent call to
$codei%
%f%.forward(%k%, %x_k%)
%$$
We use $latex F : \B{R}^n \rightarrow \B{R}^m$$ to denote the
function corresponding to the $code adfun$$ object $cref/f/adfun/f/$$.
$head X(t, u)$$
We define the function $latex X : \B{R} \times \B{R}^n \rightarrow \B{R}^n$$ by
$latex \[
X(t, u) = u + x^{(0)} + x^{(1)} * t + \cdots + x^{(p-1)} * t^{p-1}
\] $$
Note that for $latex k = 0 , \ldots , p - 1$$,
$latex \[
x^{(k)} = \frac{1}{k !} \frac{\partial^k}{\partial t^k} X(0, 0)
\] $$
$head W(t, u)$$
The function $latex W : \B{R} \times \B{R}^n \rightarrow \B{R}$$
is defined by
$latex \[
W(t, u) = w_0 * F_0 [ X(t, u) ] + \cdots + w_{m-1} * F_{m-1} [ X(t, u) ]
\] $$
We define the function $latex W_k : \B{R}^n \rightarrow \B{R}$$ by
$latex \[
W_k ( u ) = \frac{1}{k !} \frac{\partial^k}{\partial t^k} W(0, u)
\] $$
It follows that
$latex \[
W(t, u ) = W_0 ( u ) + W_1 ( u ) * t + \cdots + W_{p-1} (u) * t^{p-1}
+ o( t^{p-1} )
\] $$
where $latex o( t^{p-1} ) / t^{p-1} \rightarrow 0$$
as $latex t \rightarrow 0$$.
$head f$$
The object $icode f$$ must be an $cref adfun$$ object.
We use $cref/level/adfun/f/level/$$ for the AD $cref ad$$ level of
this object.
$head p$$
The argument $icode p$$ is a non-negative $code int$$.
It specifies the order of the Taylor coefficient $latex W_{p-1} ( u )$$
that is differentiated.
Note that $latex W_{p-1} (u)$$ corresponds a derivative of order
$latex p-1$$ of $latex F(x)$$,
so the derivative of $latex W_{p-1} (u)$$ corresponds to a derivative
of order $latex p$$ of $latex F(x)$$.
$head w$$
The argument $icode w$$ is a $code numpy.array$$ with one dimension
(i.e., a vector) with length equal to the range size $cref/m/adfun/f/m/$$
for the function $icode f$$.
It specifies the weighting vector $latex w$$ used in the definition of
$latex W(t, u)$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode w$$ must be either $code int$$ or instances
of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode w$$ must be $code a_float$$ objects.
$head dw$$
The return value $icode v$$ is a $code numpy.array$$ with one dimension
(i.e., a vector) with length equal to the domain size $cref/n/adfun/f/n/$$
for the function $icode f$$.
It is set to the derivative
$latex \[
\begin{array}{rcl}
dw & = & W_{p-1}^{(1)} ( 0 ) \\
& = &
\partial_u \frac{1}{(p-1) !} \frac{\partial^{p-1}}{\partial t^{p-1}} W(0, 0)
\end{array}
\] $$
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode dw$$ will be instances of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode dw$$ will be $code a_float$$ objects.
$head First Order$$
In the case where $latex p = 1$$, we have
$latex \[
\begin{array}{rcl}
dw
& = & \partial_u \frac{1}{0 !} \frac{\partial^0}{\partial t^0} W(0, 0)
\\
& = & \partial_u W(0, 0)
\\
& = &
\partial_u \left[
w_0 * F_0 ( u + x^{(0)} ) + \cdots + w_{m-1} F_{m-1} ( u + x^{(0)} )
\right]_{u = 0}
\\
& = &
w_0 * F_0^{(1)} ( x^{(0)} ) + \cdots + w_{m-1} * F_{m-1}^{(1)} ( x^{(0)} )
\end{array}
\] $$
$head Second Order$$
In the case where $latex p = 2$$, we have
$latex \[
\begin{array}{rcl}
dw
& = & \partial_u \frac{1}{1 !} \frac{\partial^1}{\partial t^1} W (0, 0)
\\
& = &
\partial_u \left[
w_0 * F_0^{(1)} ( u + x^{(0)} ) * x^{(1)}
+ \cdots +
w_{m-1} * F_{m-1}^{(1)} ( u + x^{(0)} ) * x^{(1)}
\right]_{u = 0}
\\
& = &
w_0 * ( x^{(1)} )^\R{T} * F_0^{(2)} ( x^{(0)} )
+ \cdots +
w_{m-1} * ( x^{(1)} )^\R{T} * F_{m-1}^{(2)} ( x^{(0)} )
\end{array}
\] $$
$children%
example/reverse_1.py%
example/reverse_2.py
%$$
$head Example$$
$table
$rref reverse_1.py$$
$rref reverse_2.py$$
$tend
$end
---------------------------------------------------------------------------
$begin jacobian$$
$spell
jacobian
numpy
adfun
$$
$section Driver for Computing Entire Derivative$$
$index jacobian$$
$index driver, entire derivative$$
$index entire, derivative driver$$
$index derivative, entire driver$$
$head Syntax$$
$icode%J% = %f%.jacobian(%x%)%$$
$head Purpose$$
This routine computes the entire derivative $latex F^{(1)} (x)$$
where $latex F : \B{R}^n \rightarrow \B{R}^m$$ is the
function corresponding to the $code adfun$$ object $cref/f/adfun/f/$$.
$head f$$
The object $icode f$$ must be an $cref adfun$$ object.
We use $cref/level/adfun/f/level/$$ for the AD $cref ad$$ level of
this object.
$head x$$
The argument $icode x$$ is a $code numpy.array$$ with one dimension
(i.e., a vector) with length equal to the domain size $cref/n/adfun/f/n/$$
for the function $icode f$$.
It specifies the argument value at which the derivative is computed.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode x$$ must be either $code int$$ or instances
of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode x$$ must be $code a_float$$ objects.
$head J$$
The return value $icode J$$ is a $code numpy.array$$ with two dimensions
(i.e., a matrix).
The first dimension (row size) is equal to $cref/m/adfun/f/m/$$
(the number of range components in the function $icode f$$).
The second dimension (column size) is equal to $cref/n/adfun/f/n/$$
(the number of domain components in the function $icode f$$).
It is set to the derivative; i.e.,
$latex \[
J = F^{(1)} (x)
\] $$
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode J$$ will be instances of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode J$$ will be $code a_float$$ objects.
$children%
example/jacobian.py
%$$
$head Example$$
The file $cref jacobian.py$$ contains an example and test of this operation.
$end
---------------------------------------------------------------------------
$begin hessian$$
$spell
hessian
numpy
adfun
$$
$section Driver for Computing Hessian in a Range Direction$$
$index hessian, driver$$
$index driver, hessian$$
$index Lagrangian, hessian$$
$index hessian, Lagrangian$$
$head Syntax$$
$icode%H% = %f%.hessian(%x%, %w%)%$$
$head Purpose$$
This routine computes the Hessian of the weighted sum
$latex \[
w_0 * F_0 (x) + \cdots + w_{m-1} * F_{m-1} (x)
\] $$
where $latex F : \B{R}^n \rightarrow \B{R}^m$$ is the
function corresponding to the $code adfun$$ object $cref/f/adfun/f/$$.
$head f$$
The object $icode f$$ must be an $cref adfun$$ object.
We use $cref/level/adfun/f/level/$$ for the AD $cref ad$$ level of
this object.
$head x$$
The argument $icode x$$ is a $code numpy.array$$ with one dimension
(i.e., a vector) with length equal to the domain size $cref/n/adfun/f/n/$$
for the function $icode f$$.
It specifies the argument value at which the derivative is computed.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode x$$ must be either $code int$$ or instances
of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode x$$ must be $code a_float$$ objects.
$head w$$
The argument $icode w$$ is a $code numpy.array$$ with one dimension
(i.e., a vector) with length equal to the range size $cref/m/adfun/f/m/$$
for the function $icode f$$.
It specifies the argument value at which the derivative is computed.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode w$$ must be either $code int$$ or instances
of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode w$$ must be $code a_float$$ objects.
$head H$$
The return value $icode H$$ is a $code numpy.array$$ with two dimensions
(i.e., a matrix).
Both its first and second dimension size
(row and column size) are equal to $cref/n/adfun/f/n/$$
(the number of domain components in the function $icode f$$).
It is set to the Hessian; i.e.,
$latex \[
H = w_0 * F_0^{(2)} (x) + \cdots + w_{m-1} * F_{m-1}^{(2)} (x)
\] $$
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is zero,
all the elements of $icode H$$ will be instances of $code float$$.
If the AD $cref/level/adfun/f/level/$$ for $icode f$$ is one,
all the elements of $icode H$$ will be $code a_float$$ objects.
$children%
example/hessian.py
%$$
$head Example$$
The file $cref hessian.py$$ contains an example and test of this operation.
$end
---------------------------------------------------------------------------
$begin optimize$$
$spell
Taylor
var
$$
$section Optimize an AD Function Object Tape$$
$index optimize$$
$index tape, optimize$$
$index sequence, optimize operations$$
$index operations, optimize sequence$$
$index speed, optimize$$
$index memory, optimize$$
$head Syntax$$
$icode%f%.optimize()%$$
$head Purpose$$
The operation sequence corresponding to an $cref ADFun$$ object can
be very large and involve many operations.
The $icode%f%.optimize%$$ procedure reduces the number of operations,
and thereby the time and memory, required to
compute function and derivative values.
$head f$$
The object $icode f$$ is an $cref adfun$$ object.
$head Efficiency$$
The $code optimize$$ member function
may greatly reduce the size of the operation sequence corresponding to
$icode f$$.
$children%
example/optimize.py
%$$
$head Example$$
The file $cref optimize.py$$ contains an example and test of this operation.
$end
---------------------------------------------------------------------------
*/
# include "adfun.hpp"
# include "vector.hpp"
# include "vec2array.hpp"
namespace pycppad {
// -------------------------------------------------------------
// constructor for python class ADFun<Base>
template <class Base>
ADFun<Base>::ADFun(array& x_array, array& y_array)
{ vec< CppAD::AD<Base> > x_vec(x_array);
vec< CppAD::AD<Base> > y_vec(y_array);
f_.Dependent(x_vec, y_vec);
}
// Domain
template <class Base>
int ADFun<Base>::Domain(void)
{ return static_cast<int>( f_.Domain() ); }
// Range
template <class Base>
int ADFun<Base>::Range(void)
{ return static_cast<int>( f_.Range() ); }
// Forward
template <class Base>
array ADFun<Base>::Forward(int p, array& xp)
{ size_t p_sz(p);
vec<Base> xp_vec(xp);
vec<Base> result = f_.Forward(p_sz, xp_vec);
return vec2array(result);
}
# ifdef NDEBUG
template <class Base>
int ADFun<Base>::CompareChange(void)
{ bool known = true;
CppAD::ErrorHandler::Call(
known,
__LINE__,
__FILE__,
"# ifndef NDEBUG",
"Cannot use CompareChange when NDEBUG is defined"
);
return 0;
}
# else
// CompareChange
template <class Base>
int ADFun<Base>::CompareChange(void)
{ return static_cast<int>( f_.CompareChange() ); }
# endif
// Reverse
template <class Base>
array ADFun<Base>::Reverse(int p, array& w)
{ size_t p_sz(p);
vec<Base> w_vec(w);
vec<Base> dw_vec = f_.Reverse(p_sz, w_vec);
size_t n = f_.Domain();
vec<Base> result(n);
for(size_t j = 0; j < n; j++)
result[j] = dw_vec[j*p + p - 1];
return vec2array(result);
}
// Jacobian
template <class Base>
array ADFun<Base>::Jacobian(array& x)
{ vec<Base> x_vec(x);
vec<Base> result = f_.Jacobian(x_vec);
// Kludge: return a vector which is reshaped by cppad.py
return vec2array(result);
}
// Hessian
template <class Base>
array ADFun<Base>::Hessian(array& x, array& w)
{ vec<Base> x_vec(x);
vec<Base> w_vec(w);
vec<Base> result = f_.Hessian(x_vec, w_vec);
// Kludge: return a vector which is reshaped by cppad.py
return vec2array(result);
}
// optimize
template <class Base>
void ADFun<Base>::optimize(void)
{ f_.optimize(); }
// -------------------------------------------------------------
// instantiate instances of ADFun<Base>
template class ADFun<double>;
template class ADFun<AD_double>;
// -------------------------------------------------------------
void adfun_avoid_warning_that_import_array_not_used(void)
{ import_array(); }
}
You can’t perform that action at this time.
