|
Prev
| Next
|
|
|
|
|
|
var2par.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
.
Convert a Variable or Dynamic Parameter a Constant: Example and Test
# include <cppad/cppad.hpp>
bool Var2Par(void)
{ bool ok = true;
using CppAD::AD;
using CppAD::Value;
using CppAD::Var2Par;
// independent variables
size_t nx = 2;
CPPAD_TESTVECTOR(AD<double>) ax(nx);
ax[0] = 3.;
ax[1] = 4.;
// independent dynamic paramers
size_t np = 1;
CPPAD_TESTVECTOR(AD<double>) ap(np);
ap[0] = 5.;
// declare independent variables and dynamic parameters
CppAD::Independent(ax, ap);
// range space vector
size_t ny = 2;
CPPAD_TESTVECTOR(AD<double>) ay(ny);
ay[0] = - ax[1] * Var2Par(ax[0]); // same as ay[0] = -ax[1] * 3.;
ay[1] = - ax[1] * Var2Par(ap[0]); // same as ay[1] = -ax[1] * 5.;
// Must convert these objects to constants before calling Value
ok &= ( Value( Var2Par(ax[0]) ) == 3. );
ok &= ( Value( Var2Par(ax[1]) ) == 4. );
ok &= ( Value( Var2Par(ap[0]) ) == 5. );
ok &= ( Value( Var2Par(ay[0]) ) == -12. );
ok &= ( Value( Var2Par(ay[1]) ) == -20. );
// create f: x -> y and stop tape recording
CppAD::ADFun<double> f(ax, ay);
// All AD object are currently constants
ok &= (Value(ax[0]) == 3.);
ok &= (Value(ax[1]) == 4.);
ok &= (Value(ap[0]) == 5.);
ok &= (Value(ay[0]) == -12.);
ok &= (Value(ay[1]) == -20.);
// evaluate zero order forward mode
// (note that the only real variable in this recording is x[1])
CPPAD_TESTVECTOR(double) x(nx), p(np), y(ny);
x[0] = 6.;
x[1] = 7.;
p[0] = 8.;
f.new_dynamic(p);
y = f.Forward(0, x);
ok &= y[0] == - x[1] * 3.0;
ok &= y[1] == - x[1] * 5.0;
return ok;
}
Input File: example/general/var2par.cpp