|
Prev
| Next
|
|
|
|
|
|
forward_order.cpp |
Headings |
@(@\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
.
Forward Mode: Example and Test of Multiple Orders
# include <limits>
# include <cppad/cppad.hpp>
bool forward_order(void)
{ bool ok = true;
using CppAD::AD;
using CppAD::NearEqual;
double eps = 10. * std::numeric_limits<double>::epsilon();
// domain space vector
size_t n = 2;
CPPAD_TESTVECTOR(AD<double>) ax(n);
ax[0] = 0.;
ax[1] = 1.;
// declare independent variables and starting recording
CppAD::Independent(ax);
// range space vector
size_t m = 1;
CPPAD_TESTVECTOR(AD<double>) ay(m);
ay[0] = ax[0] * ax[0] * ax[1];
// create f: x -> y and stop tape recording
CppAD::ADFun<double> f(ax, ay);
// initially, the variable values during taping are stored in f
ok &= f.size_order() == 1;
// Compute three forward orders at once
size_t q = 2, q1 = q+1;
CPPAD_TESTVECTOR(double) xq(n*q1), yq;
xq[q1*0 + 0] = 3.; xq[q1*1 + 0] = 4.; // x^0 (order zero)
xq[q1*0 + 1] = 1.; xq[q1*1 + 1] = 0.; // x^1 (order one)
xq[q1*0 + 2] = 0.; xq[q1*1 + 2] = 0.; // x^2 (order two)
// X(t) = x^0 + x^1 * t + x^2 * t^2
// = [ 3 + t, 4 ]
yq = f.Forward(q, xq);
ok &= size_t( yq.size() ) == m*q1;
// Y(t) = F[X(t)]
// = (3 + t) * (3 + t) * 4
// = y^0 + y^1 * t + y^2 * t^2 + o(t^3)
//
// check y^0 (order zero)
CPPAD_TESTVECTOR(double) x0(n);
x0[0] = xq[q1*0 + 0];
x0[1] = xq[q1*1 + 0];
ok &= NearEqual(yq[q1*0 + 0] , x0[0]*x0[0]*x0[1], eps, eps);
//
// check y^1 (order one)
ok &= NearEqual(yq[q1*0 + 1] , 2.*x0[0]*x0[1], eps, eps);
//
// check y^2 (order two)
double F_00 = 2. * yq[q1*0 + 2]; // second partial F w.r.t. x_0, x_0
ok &= NearEqual(F_00, 2.*x0[1], eps, eps);
//
// check number of orders per variable
ok &= f.size_order() == 3;
//
return ok;
}
Input File: example/general/forward_order.cpp