Prev Next Index-> contents reference index search external Up-> CppAD ADFun drivers Jacobian jacobian.cpp ADFun-> record_adfun drivers Forward Reverse sparsity_pattern sparse_derivative optimize abs_normal FunCheck check_for_nan drivers-> Jacobian Hessian ForOne RevOne ForTwo RevTwo Jacobian-> jacobian.cpp jacobian.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}} }$
Jacobian: Example and Test

namespace { // ---------------------------------------------------------
// define the template function JacobianCases<Vector> in empty namespace
template <typename Vector>
bool JacobianCases()
{     bool ok = true;
double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

// domain space vector
size_t n = 2;
X[0] = 1.;
X[1] = 2.;

// declare independent variables and starting recording

// a calculation between the domain and range values
AD<double> Square = X[0] * X[0];

// range space vector
size_t m = 3;
Y[0] = Square * exp( X[1] );
Y[1] = Square * sin( X[1] );
Y[2] = Square * cos( X[1] );

// create f: X -> Y and stop tape recording

// new value for the independent variable vector
Vector x(n);
x[0] = 2.;
x[1] = 1.;

// compute the derivative at this x
Vector jac( m * n );
jac = f.Jacobian(x);

/*
F'(x) = [ 2 * x[0] * exp(x[1]) ,  x[0] * x[0] * exp(x[1]) ]
[ 2 * x[0] * sin(x[1]) ,  x[0] * x[0] * cos(x[1]) ]
[ 2 * x[0] * cos(x[1]) , -x[0] * x[0] * sin(x[i]) ]
*/
ok &=  NearEqual( 2.*x[0]*exp(x[1]), jac[0*n+0], eps99, eps99);
ok &=  NearEqual( 2.*x[0]*sin(x[1]), jac[1*n+0], eps99, eps99);
ok &=  NearEqual( 2.*x[0]*cos(x[1]), jac[2*n+0], eps99, eps99);

ok &=  NearEqual( x[0] * x[0] *exp(x[1]), jac[0*n+1], eps99, eps99);
ok &=  NearEqual( x[0] * x[0] *cos(x[1]), jac[1*n+1], eps99, eps99);
ok &=  NearEqual(-x[0] * x[0] *sin(x[1]), jac[2*n+1], eps99, eps99);

return ok;
}
} // End empty namespace
# include <vector>
# include <valarray>
bool Jacobian(void)
{     bool ok = true;
// Run with Vector equal to three different cases
// all of which are Simple Vectors with elements of type double.
ok &= JacobianCases< CppAD::vector  <double> >();
ok &= JacobianCases< std::vector    <double> >();
ok &= JacobianCases< std::valarray  <double> >();
return ok;
}

Input File: example/general/jacobian.cpp