void ForSparseJacSet( bool transpose , size_t q , const VectorSet& r , VectorSet& s , size_t total_num_var , CppAD::vector<size_t>& dep_taddr , CppAD::vector<size_t>& ind_taddr , CppAD::player<Base>& play , CPPAD_INTERNAL_SPARSE_SET& for_jac_sparsity ) { // temporary indices size_t i, j; std::set<size_t>::const_iterator itr; // range and domain dimensions for F size_t m = dep_taddr.size(); size_t n = ind_taddr.size(); CPPAD_ASSERT_KNOWN( q > 0, "RevSparseJac: q is not greater than zero" ); CPPAD_ASSERT_KNOWN( size_t(r.size()) == n || transpose, "RevSparseJac: size of r is not equal to n and transpose is false." ); CPPAD_ASSERT_KNOWN( size_t(r.size()) == q || ! transpose, "RevSparseJac: size of r is not equal to q and transpose is true." ); // allocate memory for the requested sparsity calculation for_jac_sparsity.resize(total_num_var, q); // set values corresponding to independent variables if( transpose ) { for(i = 0; i < q; i++) { // add the elements that are present itr = r[i].begin(); while( itr != r[i].end() ) { j = *itr++; CPPAD_ASSERT_KNOWN( j < n, "ForSparseJac: transpose is true and element of the set\n" "r[j] has value greater than or equal n." ); CPPAD_ASSERT_UNKNOWN( ind_taddr[j] < total_num_var ); // operator for j-th independent variable CPPAD_ASSERT_UNKNOWN( play.GetOp( ind_taddr[j] ) == InvOp ); for_jac_sparsity.add_element( ind_taddr[j], i); } } } else { for(i = 0; i < n; i++) { CPPAD_ASSERT_UNKNOWN( ind_taddr[i] < total_num_var ); // ind_taddr[i] is operator taddr for i-th independent variable CPPAD_ASSERT_UNKNOWN( play.GetOp( ind_taddr[i] ) == InvOp ); // add the elements that are present itr = r[i].begin(); while( itr != r[i].end() ) { j = *itr++; CPPAD_ASSERT_KNOWN( j < q, "ForSparseJac: an element of the set r[i] " "has value greater than or equal q." ); for_jac_sparsity.add_element( ind_taddr[i], j); } } } // evaluate the sparsity patterns ForJacSweep( n, total_num_var, &play, for_jac_sparsity ); // return values corresponding to dependent variables CPPAD_ASSERT_UNKNOWN( size_t(s.size()) == m || transpose ); CPPAD_ASSERT_UNKNOWN( size_t(s.size()) == q || ! transpose ); for(i = 0; i < m; i++) { CPPAD_ASSERT_UNKNOWN( dep_taddr[i] < total_num_var ); // extract results from for_jac_sparsity // and add corresponding elements to sets in s CPPAD_ASSERT_UNKNOWN( for_jac_sparsity.end() == q ); for_jac_sparsity.begin( dep_taddr[i] ); j = for_jac_sparsity.next_element(); while( j < q ) { if( transpose ) s[j].insert(i); else s[i].insert(j); j = for_jac_sparsity.next_element(); } } }
void RevSparseHesSet( bool transpose , size_t q , const VectorSet& s , VectorSet& h , size_t num_var , CppAD::vector<size_t>& dep_taddr , CppAD::vector<size_t>& ind_taddr , CppAD::player<Base>& play , CPPAD_INTERNAL_SPARSE_SET& for_jac_sparsity ) { // temporary indices size_t i, j; std::set<size_t>::const_iterator itr; // check VectorSet is Simple Vector class with sets for elements CheckSimpleVector<std::set<size_t>, VectorSet>( one_element_std_set<size_t>(), two_element_std_set<size_t>() ); // range and domain dimensions for F # ifndef NDEBUG size_t m = dep_taddr.size(); # endif size_t n = ind_taddr.size(); CPPAD_ASSERT_KNOWN( q == for_jac_sparsity.end(), "RevSparseHes: q is not equal to its value\n" "in the previous call to ForSparseJac with this ADFun object." ); CPPAD_ASSERT_KNOWN( s.size() == 1, "RevSparseHes: size of s is not equal to one." ); // Array that will hold reverse Jacobian dependency flag. // Initialize as true for the dependent variables. pod_vector<bool> RevJac; RevJac.extend(num_var); for(i = 0; i < num_var; i++) RevJac[i] = false; itr = s[0].begin(); while( itr != s[0].end() ) { i = *itr++; CPPAD_ASSERT_KNOWN( i < m, "RevSparseHes: an element of the set s[0] has value " "greater than or equal m" ); CPPAD_ASSERT_UNKNOWN( dep_taddr[i] < num_var ); RevJac[ dep_taddr[i] ] = true; } // vector of sets that will hold reverse Hessain values CPPAD_INTERNAL_SPARSE_SET rev_hes_sparsity; rev_hes_sparsity.resize(num_var, q); // compute the Hessian sparsity patterns RevHesSweep( n, num_var, &play, for_jac_sparsity, RevJac.data(), rev_hes_sparsity ); // return values corresponding to independent variables // j is index corresponding to reverse mode partial CPPAD_ASSERT_UNKNOWN( size_t(h.size()) == q || transpose ); CPPAD_ASSERT_UNKNOWN( size_t(h.size()) == n || ! transpose ); for(j = 0; j < n; j++) { CPPAD_ASSERT_UNKNOWN( ind_taddr[j] < num_var ); CPPAD_ASSERT_UNKNOWN( ind_taddr[j] == j + 1 ); CPPAD_ASSERT_UNKNOWN( play.GetOp( ind_taddr[j] ) == InvOp ); // extract the result from rev_hes_sparsity // and add corresponding elements to result sets in h CPPAD_ASSERT_UNKNOWN( rev_hes_sparsity.end() == q ); rev_hes_sparsity.begin(j+1); i = rev_hes_sparsity.next_element(); while( i < q ) { if( transpose ) h[j].insert(i); else h[i].insert(j); i = rev_hes_sparsity.next_element(); } } return; }
void ADFun<Base>::RevSparseJacCheckpoint( size_t q , CPPAD_INTERNAL_SPARSE_SET& r , bool transpose , bool dependency , CPPAD_INTERNAL_SPARSE_SET& s ) { size_t n = Domain(); size_t m = Range(); # ifndef NDEBUG if( transpose ) { CPPAD_ASSERT_UNKNOWN( r.n_set() == m ); CPPAD_ASSERT_UNKNOWN( r.end() == q ); } else { CPPAD_ASSERT_UNKNOWN( r.n_set() == q ); CPPAD_ASSERT_UNKNOWN( r.end() == m ); } for(size_t i = 0; i < m; i++) CPPAD_ASSERT_UNKNOWN( dep_taddr_[i] < num_var_tape_ ); # endif // holds reverse Jacobian sparsity pattern for all variables CPPAD_INTERNAL_SPARSE_SET var_sparsity; var_sparsity.resize(num_var_tape_, q); // set sparsity pattern for dependent variables if( transpose ) { for(size_t i = 0; i < m; i++) { r.begin(i); size_t j = r.next_element(); while( j < q ) { var_sparsity.add_element( dep_taddr_[i], j ); j = r.next_element(); } } } else { for(size_t j = 0; j < q; j++) { r.begin(j); size_t i = r.next_element(); while( i < m ) { var_sparsity.add_element( dep_taddr_[i], j ); i = r.next_element(); } } } // evaluate the sparsity pattern for all variables RevJacSweep( dependency, n, num_var_tape_, &play_, var_sparsity ); // dimension the return value if( transpose ) s.resize(n, m); else s.resize(m, n); // return values corresponding to independent variables for(size_t j = 0; j < n; j++) { CPPAD_ASSERT_UNKNOWN( ind_taddr_[j] == (j+1) ); // ind_taddr_[j] is operator taddr for j-th independent variable CPPAD_ASSERT_UNKNOWN( play_.GetOp( ind_taddr_[j] ) == InvOp ); // extract the result from var_sparsity CPPAD_ASSERT_UNKNOWN( var_sparsity.end() == q ); var_sparsity.begin(j+1); size_t i = var_sparsity.next_element(); while( i < q ) { if( transpose ) s.add_element(j, i); else s.add_element(i, j); i = var_sparsity.next_element(); } } }