<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Forward Mode Jacobian Sparsity: Example and Test</title> <meta name="description" id="description" content="Forward Mode Jacobian Sparsity: Example and Test"/> <meta name="keywords" id="keywords" content=" Forsparsejac example sparsity forward test "/> <style type='text/css'> body { color : black } body { background-color : white } A:link { color : blue } A:visited { color : purple } A:active { color : purple } </style> <script type='text/javascript' language='JavaScript' src='_forsparsejac.cpp_xml.js'> </script> </head> <body> <table><tr> <td> <a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a> </td> <td><a href="forsparsejac.xml" target="_top">Prev</a> </td><td><a href="revsparsejac.xml" target="_top">Next</a> </td><td> <select onchange='choose_across0(this)'> <option>Index-></option> <option>contents</option> <option>reference</option> <option>index</option> <option>search</option> <option>external</option> </select> </td> <td> <select onchange='choose_up0(this)'> <option>Up-></option> <option>CppAD</option> <option>ADFun</option> <option>FunEval</option> <option>Sparse</option> <option>ForSparseJac</option> <option>ForSparseJac.cpp</option> </select> </td> <td> <select onchange='choose_down3(this)'> <option>FunEval-></option> <option>Forward</option> <option>Reverse</option> <option>Sparse</option> </select> </td> <td> <select onchange='choose_down2(this)'> <option>Sparse-></option> <option>ForSparseJac</option> <option>RevSparseJac</option> <option>RevSparseHes</option> </select> </td> <td> <select onchange='choose_down1(this)'> <option>ForSparseJac-></option> <option>ForSparseJac.cpp</option> </select> </td> <td>ForSparseJac.cpp</td> <td>Headings</td> </tr></table><br/> <center><b><big><big>Forward Mode Jacobian Sparsity: Example and Test</big></big></b></center> <code><font color="blue"><pre style='display:inline'> # include <set> # include <cppad/cppad.hpp> namespace { // ------------------------------------------------------------- // define the template function BoolCases<Vector> template <typename Vector> // vector class, elements of type bool bool BoolCases(void) { bool ok = true; using CppAD::AD; // domain space vector size_t n = 2; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > X(n); X[0] = 0.; X[1] = 1.; // declare independent variables and start recording CppAD::<a href="independent.xml" target="_top">Independent</a>(X); // range space vector size_t m = 3; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > Y(m); Y[0] = X[0]; Y[1] = X[0] * X[1]; Y[2] = X[1]; // create f: X -> Y and stop tape recording CppAD::<a href="funconstruct.xml" target="_top">ADFun</a><double> f(X, Y); // sparsity pattern for the identity matrix Vector r(n * n); size_t i, j; for(i = 0; i < n; i++) { for(j = 0; j < n; j++) r[ i * n + j ] = (i == j); } // sparsity pattern for F'(x) Vector s(m * n); s = f.ForSparseJac(n, r); // check values ok &= (s[ 0 * n + 0 ] == true); // Y[0] does depend on X[0] ok &= (s[ 0 * n + 1 ] == false); // Y[0] does not depend on X[1] ok &= (s[ 1 * n + 0 ] == true); // Y[1] does depend on X[0] ok &= (s[ 1 * n + 1 ] == true); // Y[1] does depend on X[1] ok &= (s[ 2 * n + 0 ] == false); // Y[2] does not depend on X[0] ok &= (s[ 2 * n + 1 ] == true); // Y[2] does depend on X[1] // check that values are stored ok &= (f.size_forward_bool() > 0); // free values from forward calculation f.size_forward_bool(0); ok &= (f.size_forward_bool() == 0); return ok; } // define the template function SetCases<Vector> template <typename Vector> // vector class, elements of type std::set<size_t> bool SetCases(void) { bool ok = true; using CppAD::AD; // domain space vector size_t n = 2; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > X(n); X[0] = 0.; X[1] = 1.; // declare independent variables and start recording CppAD::<a href="independent.xml" target="_top">Independent</a>(X); // range space vector size_t m = 3; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > Y(m); Y[0] = X[0]; Y[1] = X[0] * X[1]; Y[2] = X[1]; // create f: X -> Y and stop tape recording CppAD::<a href="funconstruct.xml" target="_top">ADFun</a><double> f(X, Y); // sparsity pattern for the identity matrix Vector r(n); size_t i; for(i = 0; i < n; i++) { assert( r[i].empty() ); r[i].insert(i); } // sparsity pattern for F'(x) Vector s(m); s = f.ForSparseJac(n, r); // an interator to a standard set std::set<size_t>::iterator itr; bool found; // Y[0] does depend on X[0] found = s[0].find(0) != s[0].end(); ok &= ( found == true ); // Y[0] does not depend on X[1] found = s[0].find(1) != s[0].end(); ok &= ( found == false ); // Y[1] does depend on X[0] found = s[1].find(0) != s[1].end(); ok &= ( found == true ); // Y[1] does depend on X[1] found = s[1].find(1) != s[1].end(); ok &= ( found == true ); // Y[2] does not depend on X[0] found = s[2].find(0) != s[2].end(); ok &= ( found == false ); // Y[2] does depend on X[1] found = s[2].find(1) != s[2].end(); ok &= ( found == true ); return ok; } } // End empty namespace # include <vector> # include <valarray> bool ForSparseJac(void) { bool ok = true; // Run with Vector equal to four different cases // all of which are Simple Vectors with elements of type bool. ok &= BoolCases< CppAD::vectorBool >(); ok &= BoolCases< CppAD::vector <bool> >(); ok &= BoolCases< std::vector <bool> >(); ok &= BoolCases< std::valarray <bool> >(); // Run with Vector equal to two different cases both of which are // Simple Vectors with elements of type std::set<size_t> typedef std::set<size_t> set; ok &= SetCases< CppAD::vector <set> >(); ok &= SetCases< std::vector <set> >(); // Do not use valarray because its element access in the const case // returns a copy instead of a reference // ok &= SetCases< std::valarray <set> >(); return ok; } </pre> </font></code> <hr/>Input File: example/for_sparse_jac.cpp </body> </html>