@(@\newcommand{\W}[1]{ \; #1 \; }
\newcommand{\R}[1]{ {\rm #1} }
\newcommand{\B}[1]{ {\bf #1} }
\newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} }
\newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} }
\newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} }
\newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@This is cppad-20221105 documentation. Here is a link to its
current documentation
.
Atomic Linear ODE Reverse Mode: Example Implementation
Purpose
The reverse routine overrides the virtual functions
used by the atomic_four base; see
reverse
.
First Order Theory
We are given a vector @(@
w \in \B{R}^m
@)@ and need to compute
@[@
\partial_x w^\R{T} z(r, x)
@]@
see the definition of z(t, x)
.
Consider the Lagrangian corresponding to
@(@
w^\R{T} z(r, x)
@)@ as the objective and the ODE as the constraint:
@[@
L(x, \lambda)
=
w^\R{T} z(r, x) +
\int_0^r \lambda(t, x)^\R{T}
[ A(x) z(t, x) - z_t (t, x) ] \R{d} t
@]@
where @(@
\lambda : \R{R} \times \B{R}^n \rightarrow \B{R}^m
@)@
is a smooth function.
If @(@
z(t, x)
@)@ satisfies its ODE, then
@[@
\partial_x w^\R{T} z(r, x)
=
\partial_x L(x, \lambda)
@]@
We use the following integration by parts to replace the @(@
z_t (t, x)
@)@
term in the integral defining @(@
L(x, \lambda)
@)@:
@[@
- \int_0^r \lambda(t, x)^\R{T} z_t (t, x) \R{d} t
=
- \left. \lambda(t, x)^\R{T} z(t, x) \right|_0^r
+
\int_0^r \lambda_t (t, x)^\R{T} z(t, x) \R{d} t
@]@
Adding the condition @(@
\lambda(r, x) = w
@)@,
and noting that @(@
z(0, x) = b(x)
@)@, we have
@[@
L(x, \lambda)
=
\lambda(0, x)^\R{T} z(0, x)
+
\int_0^r \lambda_t (t, x)^\R{T} z(t, x) \R{d} t
+
\int_0^r \lambda(t, x)^\R{T} A(x) z(t, x) \R{d} t
@]@
@[@
L(x, \lambda)
=
\lambda(0, x)^\R{T} b (x)
+
\int_0^r [ \lambda_t (t, x)^\R{T} + \lambda(t, x)^\R{T} A(x) ]
z(t, x) \R{d} t
@]@
@[@
L(x, \lambda)
=
\lambda(0, x)^\R{T} b (x)
+
\int_0^r z(t, x)^\R{T}
[ \lambda_t (t, x) + A(x)^\R{T} \lambda(t, x) ] \R{d} t
@]@
The partial derivative
of @(@
L(x, \lambda)
@)@ with respect to @(@
b_j
@)@,
(not including the dependence of @(@
\lambda(t, x)
@)@ on @(@
x
@)@)
is :
@[@
\partial_{b(j)} L(x, \lambda)
=
\lambda_j (0, x)
@]@
The partial derivative
of @(@
L(x, \lambda)
@)@ with respect to @(@
A_{i,j}
@)@
(not including The dependence of @(@
\lambda(t, x)
@)@ on @(@
x
@)@)
is :
@[@
\partial_{A(i,j)} L(x, \lambda)
=
\int_0^r \partial_{A(i,j)} z(t, x)^\R{T}
[ \lambda_t (t, x) + A(x)^\R{T} \lambda(t, x) ] \R{d} t
+
\int_0^r z_j (t, x) \lambda_i (t, x) \R{d} t
@]@
If @(@
\lambda(t, x)
@)@ satisfies the ODE
@[@
0 = \lambda_t (t, x) + A(x)^\R{T} \lambda(t, x)
@]@
The partial derivative with respect to @(@
A_{i,j}
@)@ is
@[@
\partial_{A(i,j)} L(x, \lambda)
=
\int_0^r z_j (t, x) \lambda_i (t, x) \R{d} t
@]@
In summary, we can compute
an approximate solution for the initial value ODE:
@[@
z_t (t, x) = A(x) z(t, x) \W{,} z(0, x) = b(x)
@]@
and approximate solution for the final value ODE:
@[@
\lambda_t (t, x) = - A(x)^\R{T} \lambda(t, x)
\W{,}
\lambda(r, x) = w
@]@
Using the notation
nnz
,
row
, and
col
,
We can compute an approximation for
@[@
\partial_{x(k)} w^\R{T} z(r, x)
=
\left\{ \begin{array}{lll}
\int_0^r \lambda_i (t, x) z_j (r, x) \R{d} t
& \R{where} \; i = \R{row} [k] \W{,} j = \R{col}[k]
& \R{if} \; k < nnz
\\
\lambda_i (0, x)
& \R{where} \; i = k - nnz
& \R{otherwise}
%
\end{array} \right.
@]@
Simpson's Rule
This example uses Simpson's rule to approximate the integral
@[@
\int_0^r \lambda_i (t, x) z_j (t, x) \R{d} t
@]@
Any other approximation for this integral can be used.