Prev Next par_var.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}} }@)@
AD Parameter and Variable Functions: Example and Test

# include <cppad/cppad.hpp>

bool ParVar(void)
{     bool ok = true;

     using CppAD::AD;
     using CppAD::VecAD;
     using CppAD::Parameter;
     using CppAD::Variable;

     // declare independent variables and start tape recording
     size_t n = 1;
     CPPAD_TESTVECTOR(AD<double>) x(n);
     x[0]     = 0.;
     ok &= Parameter(x[0]);     // x[0] is a paraemter here
     CppAD::Independent(x);
     ok &= Variable(x[0]);      // now x[0] is a variable

     // dependent variable vector
     size_t m = 2;
     CPPAD_TESTVECTOR(AD<double>) y(m);
     y[0] = 2.;
     ok  &= Parameter(y[0]);    // y[0] does not depend on x[0]
     y[1] = fabs(x[0]);
     ok  &= Variable(y[1]);     // y[1] does depends on x[0]

     // VecAD objects
     VecAD<double> z(2);
     z[0] = 0.;
     z[1] = 1.;
     ok  &= Parameter(z);      // z does not depend on x[0]
     z[x[0]] = 2.;
     ok  &= Variable(z);       // z depends on x[0]


     // create f: x -> y and stop tape recording
     CppAD::ADFun<double> f(x, y);

     // check that now all AD<double> objects are parameters
     ok &= Parameter(x[0]); ok &= ! Variable(x[0]);
     ok &= Parameter(y[0]); ok &= ! Variable(y[0]);
     ok &= Parameter(y[1]); ok &= ! Variable(y[1]);

     // check that the VecAD<double> object is a parameter
     ok &= Parameter(z);

     return ok;
}

Input File: example/general/par_var.cpp