Prev Next base2vec_ad.cpp

@(@\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 .
Using base2ad and VecAD together: Example and Test

Purpose
This example demonstrate that base2ad does not convert VecAD operations as might be expected. To be specific, this indexing into a VecAD object gets fixed when zero order forward mode is run for a base2ad result; e.g. af below.
# include <cppad/cppad.hpp>

bool base2vec_ad(void)
{   using CppAD::vector;
    using CppAD::AD;
    //
    bool ok = true;
    //
    // f(x) = x[0] if 0 <= x[2] < 1
    //      = x[1] if 1 <= x[2] < 2
    vector< AD<double> > ax(3), ay(1);
    ax[0] = 3.0;
    ax[1] = 2.0;
    ax[2] = 1.0;
    Independent(ax);
    CppAD::VecAD<double> av(2);
    av[ AD<double>(0) ] = ax[0];
    av[ AD<double>(1) ] = ax[1];
    ay[0] = av[ ax[2] ];
    CppAD::ADFun<double> f(ax, ay);
    //
    // ok
    // value during recording of f
    ok &= ay[0] == ax[1];
    //
    // ok
    // f zero order forward mode, the VecAD commands give expected result
    vector<double> x(3), y(1);
    x[0] = 2.0;
    x[1] = 1.0;
    x[2] = 0.0;
    y = f.Forward(0, x);
    ok &= y[0] == x[0];
    //
    // af
    CppAD::ADFun< AD<double>, double > af = f.base2ad();
    //
    // g
    ax[0] = 3.0;
    ax[1] = 2.0;
    ax[2] = 1.0;
    Independent(ax);
    ay = af.Forward(0, ax);
    CppAD::ADFun<double> g(ax, ay);
    //
    // ok
    // value during recording of g
    ok &= ay[0] == ax[1];
    //
    // ok
    // g zero order forward mode
    // Note that this index does not change, but the value does
    x[0] = 5.0;
    x[1] = 4.0;
    x[2] = 0.0;
    y = g.Forward(0, x);
    ok &= y[0] == x[1];
    //
    return ok;
}

Input File: example/general/base2vec_ad.cpp