diff --git a/example/valvector/ad_join.cpp b/example/valvector/ad_join.cpp index 10b9f7b14..c4f6938aa 100644 --- a/example/valvector/ad_join.cpp +++ b/example/valvector/ad_join.cpp @@ -23,6 +23,9 @@ bool ad_join(void) { // ok bool ok = true; // + // sparsity_type + typedef CppAD::sparse_rc< CPPAD_TESTVECTOR(size_t) > sparsity_type; + // // scalar_type typedef valvector::scalar_type scalar_type; // @@ -74,6 +77,29 @@ bool ad_join(void) // ok for(size_t j = 0; j < n; ++j) ok &= dw[0][j] == scalar_type(2) * x[0][j]; + // + // jac_pattern + sparsity_type identity_pattern(n, n, n); + for(size_t k = 0; k < n; ++k) + identity_pattern.set(k, k, k); + bool transpose = false; + bool dependency = false; + bool internal_bool = false; + sparsity_type jac_pattern; + f.for_jac_sparsity( + identity_pattern, transpose, dependency, internal_bool, jac_pattern + ); + // + // ok + ok &= jac_pattern.nnz() == n; + ok &= jac_pattern.nr() == m; + ok &= jac_pattern.nc() == n; + CPPAD_TESTVECTOR(size_t) col_major = jac_pattern.col_major(); + for(size_t k = 0; k < n; ++k) + { ok &= jac_pattern.row()[k] == 0; + ok &= jac_pattern.col()[k] == k; + } + // return ok; } // END C++ diff --git a/include/cppad/example/valvector/ad_join.hpp b/include/cppad/example/valvector/ad_join.hpp index 66f69f566..6fbf0c850 100644 --- a/include/cppad/example/valvector/ad_join.hpp +++ b/include/cppad/example/valvector/ad_join.hpp @@ -162,6 +162,46 @@ class valvector_ad_join_atom : public CppAD::atomic_four { } return ok; } + // ------------------------------------------------------------------------ + // jac_sparsity + bool jac_sparsity( + size_t call_id , + bool dependency , + const CppAD::vector& ident_zero_x , + const CppAD::vector& select_x , + const CppAD::vector& select_y , + CppAD::sparse_rc< CppAD::vector >& pattern_out ) override + { // + // ok + bool ok = true; + // + // m, n + size_t m = select_y.size(); + size_t n = select_x.size(); + // + assert( call_id == 0 ); + assert( m == 1 ); + // + // nnz + size_t nnz = 0; + if( select_y[0] ) + { for(size_t j = 0; j < n; ++j) + { if( select_x[j] ) + ++nnz; + } + } + // + // pattern_out + pattern_out.resize(m, n, nnz); + size_t k = 0; + if( select_y[0] ) + { for(size_t j = 0; j < n; ++j) + { if( select_x[j] ) + pattern_out.set(k++, 0, j); + } + } + return ok; + } }; class valvector_ad_join { diff --git a/include/cppad/example/valvector/ad_split.hpp b/include/cppad/example/valvector/ad_split.hpp index f03f35136..96e7e264c 100644 --- a/include/cppad/example/valvector/ad_split.hpp +++ b/include/cppad/example/valvector/ad_split.hpp @@ -211,12 +211,13 @@ class valvector_ad_split_atom : public CppAD::atomic_four { // ok bool ok = true; // - // m + // m, n size_t m = select_y.size(); size_t n = select_x.size(); // assert( call_id == 0 ); assert( n == 1 ); + // // nnz size_t nnz = 0; if( select_x[0] )