$\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}} }$

Specifications

Implementation
# include <cppad/cppad.hpp>

// Note that CppAD uses global_option["memory"] at the main program level
# include <map>
extern std::map<std::string, bool> global_option;

namespace {
typedef vector<size_t>  s_vector;
typedef vector<bool>    b_vector;

void calc_sparsity(
{     bool reverse       = global_option["revsparsity"];
bool transpose     = false;
bool internal_bool = global_option["boolsparsity"];
bool dependency    = false;
bool subgraph      = global_option["subsparsity"];
size_t n = f.Domain();
size_t m = f.Range();
if( subgraph )
{     b_vector select_domain(n), select_range(m);
for(size_t j = 0; j < n; ++j)
select_domain[j] = true;
for(size_t i = 0; i < m; ++i)
select_range[i] = true;
f.subgraph_sparsity(
select_domain, select_range, transpose, sparsity
);
}
else
{     size_t q = n;
if( reverse )
q = m;
//
identity.resize(q, q, q);
for(size_t k = 0; k < q; k++)
identity.set(k, k, k);
//
if( reverse )
{     f.rev_jac_sparsity(
identity, transpose, dependency, internal_bool, sparsity
);
}
else
{     f.for_jac_sparsity(
identity, transpose, dependency, internal_bool, sparsity
);
}
}
}
}

size_t                           size     ,
size_t                           repeat   ,
size_t                           m        ,
size_t&                    n_sweep  )
{
// --------------------------------------------------------------------
// check global options
const char* valid[] = {
"memory", "onetape", "optimize", "subgraph",
"boolsparsity", "revsparsity", "subsparsity", "colpack"
# else
"boolsparsity", "revsparsity", "subsparsity"
# endif
};
size_t n_valid = sizeof(valid) / sizeof(valid[0]);
typedef std::map<std::string, bool>::iterator iterator;
//
for(iterator itr=global_option.begin(); itr!=global_option.end(); ++itr)
{     if( itr->second )
{     bool ok = false;
for(size_t i = 0; i < n_valid; i++)
ok |= itr->first == valid[i];
if( ! ok )
return false;
}
}
if( global_option["subsparsity"] )
{     if( global_option["boolsparisty"] || global_option["revsparsity"] )
return false;
}
// ---------------------------------------------------------------------
// optimization options: no conditional skips or compare operators
std::string options="no_compare_op";
// -----------------------------------------------------
// setup
typedef vector<double>       d_vector;
//
size_t order = 0;         // derivative order corresponding to function
size_t n     = size;      // number of independent variables
//
// declare sparsity pattern
//
// declare subset where Jacobian is evaluated
size_t nr  = m;
size_t nc  = n;
size_t nnz = row.size();
subset_pattern.resize(nr, nc, nnz);
for(size_t k = 0; k < nnz; k++)
subset_pattern.set(k, row[k], col[k]);
const d_vector& subset_val( subset.val() );
//
// coloring method
if( global_option["colpack"] )
coloring = "colpack";
# endif
//
// maximum number of colors at once
size_t group_max = 25;
// ------------------------------------------------------
if( ! global_option["onetape"] ) while(repeat--)
{     // choose a value for x
for(size_t j = 0; j < n; j++)
a_x[j] = x[j];
//
// declare independent variables
Independent(a_x);
//
CppAD::sparse_jac_fun<a_double>(m, n, a_x, row, col, order, a_y);
//
// create function object f : X -> Y
f.Dependent(a_x, a_y);
//
if( global_option["optimize"] )
f.optimize(options);
//
// skip comparison operators
f.compare_change_count(0);
//
// calculate the Jacobian sparsity pattern for this function
calc_sparsity(sparsity, f);
//
if( global_option["subgraph"] )
{     // user reverse mode becasue forward not yet implemented
f.subgraph_jac_rev(x, subset);
n_sweep = 0;
}
else
{     // structure that holds some of the work done by sparse_jac_for
//
// calculate the Jacobian at this x
// (use forward mode because m > n ?)
n_sweep = f.sparse_jac_for(
group_max, x, subset, sparsity, coloring, work
);
}
for(size_t k = 0; k < nnz; k++)
jacobian[k] = subset_val[k];
}
else
{     // choose a value for x
for(size_t j = 0; j < n; j++)
a_x[j] = x[j];
//
// declare independent variables
Independent(a_x);
//
CppAD::sparse_jac_fun<a_double>(m, n, a_x, row, col, order, a_y);
//
// create function object f : X -> Y
f.Dependent(a_x, a_y);
//
if( global_option["optimize"] )
f.optimize(options);
//
// skip comparison operators
f.compare_change_count(0);
//
// calculate the Jacobian sparsity pattern for this function
calc_sparsity(sparsity, f);
//
// structure that holds some of the work done by sparse_jac_for
//
while(repeat--)
{     // choose a value for x
//
// calculate the Jacobian at this x
if( global_option["subgraph"] )
{     // user reverse mode becasue forward not yet implemented
f.subgraph_jac_rev(x, subset);
n_sweep = 0;
}
else
{     // (use forward mode because m > n ?)
n_sweep = f.sparse_jac_for(
group_max, x, subset, sparsity, coloring, work
);
}
for(size_t k = 0; k < nnz; k++)
jacobian[k] = subset_val[k];
}
}
return true;
}