1 # ifndef CPPAD_CORE_SPARSE_JAC_HPP
2 # define CPPAD_CORE_SPARSE_JAC_HPP
284 template <
class Base>
285 template <
class SizeVector,
class BaseVector>
288 const BaseVector& x ,
291 const std::string& coloring ,
293 {
size_t m = Range();
298 "sparse_jac_for: subset.nr() not equal range dimension for f"
302 "sparse_jac_for: subset.nc() not equal domain dimension for f"
306 const SizeVector& row( subset.
row() );
307 const SizeVector& col( subset.
col() );
312 color.size() == 0 || color.size() == n,
313 "sparse_jac_for: work is non-empty and conditions have changed"
320 size_t K = subset.
nnz();
328 if( color.size() == 0 )
332 "sparse_jac_for: pattern.nr() not equal range dimension for f"
336 "sparse_jac_for: pattern.nc() not equal domain dimension for f"
341 for(
size_t j = 0; j < n; j++)
342 internal_index[j] = j;
343 bool transpose =
true;
344 bool zero_empty =
false;
345 bool input_empty =
true;
347 pattern_transpose.
resize(n, m);
349 transpose, internal_index, pattern_transpose, pattern
355 if( coloring ==
"cppad" )
357 else if( coloring ==
"colpack" )
359 # if CPPAD_HAS_COLPACK
360 local::color_general_colpack(pattern_transpose, col, row, color);
364 "sparse_jac_for: coloring = colpack "
365 "and colpack_prefix missing from cmake command line."
371 "sparse_jac_for: coloring is not valid."
377 for(
size_t k = 0; k < K; k++)
378 key[k] = color[ col[k] ];
386 for(
size_t j = 0; j < n; j++)
if( color[j] < n )
387 n_color = std::max(n_color, color[j] + 1);
390 for(
size_t k = 0; k < K; k++)
396 size_t color_count = 0;
398 while( color_count < n_color )
400 size_t group_size = std::min(group_max, n_color - color_count);
403 BaseVector dx(n * group_size), dy(m * group_size);
406 for(
size_t ell = 0; ell < group_size; ell++)
408 for(
size_t j = 0; j < n; j++)
409 { dx[j * group_size + ell] = zero;
410 if( color[j] == ell + color_count )
411 dx[j * group_size + ell] = one;
414 if( group_size == 1 )
417 dy = Forward(1, group_size, dx);
420 for(
size_t ell = 0; ell < group_size; ell++)
422 while(k < K && color[ col[ order[k] ] ] == ell + color_count )
424 size_t r = row[ order[k] ];
425 subset.
set( order[k], dy[ r * group_size + ell ] );
430 color_count += group_size;
478 template <
class Base>
479 template <
class SizeVector,
class BaseVector>
481 const BaseVector& x ,
484 const std::string& coloring ,
486 {
size_t m = Range();
491 "sparse_jac_rev: subset.nr() not equal range dimension for f"
495 "sparse_jac_rev: subset.nc() not equal domain dimension for f"
499 const SizeVector& row( subset.
row() );
500 const SizeVector& col( subset.
col() );
505 color.size() == 0 || color.size() == m,
506 "sparse_jac_rev: work is non-empty and conditions have changed"
513 size_t K = subset.
nnz();
521 if( color.size() == 0 )
525 "sparse_jac_rev: pattern.nr() not equal range dimension for f"
529 "sparse_jac_rev: pattern.nc() not equal domain dimension for f"
534 for(
size_t i = 0; i < m; i++)
535 internal_index[i] = i;
536 bool transpose =
false;
537 bool zero_empty =
false;
538 bool input_empty =
true;
540 internal_pattern.
resize(m, n);
542 transpose, internal_index, internal_pattern, pattern
547 if( coloring ==
"cppad" )
549 else if( coloring ==
"colpack" )
551 # if CPPAD_HAS_COLPACK
552 local::color_general_colpack(internal_pattern, row, col, color);
556 "sparse_jac_rev: coloring = colpack "
557 "and colpack_prefix missing from cmake command line."
563 "sparse_jac_rev: coloring is not valid."
569 for(
size_t k = 0; k < K; k++)
570 key[k] = color[ row[k] ];
578 for(
size_t i = 0; i < m; i++)
if( color[i] < m )
579 n_color = std::max(n_color, color[i] + 1);
582 for(
size_t k = 0; k < K; k++)
586 BaseVector w(m), dw(n);
590 for(
size_t ell = 0; ell < n_color; ell++)
596 else if( color[ row[ order[k] ] ] != ell )
605 for(
size_t i = 0; i < m; i++)
607 if( color[i] == ell )
614 while( k < K && color[ row[order[k]] ] == ell )
615 { subset.
set(order[k], dw[col[order[k]]] );
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
Vector of sets of positive integers, each set stored as a singly linked list.
void color_general_cppad(const VectorSet &pattern, const VectorSize &row, const VectorSize &col, CppAD::vector< size_t > &color)
Determine which rows of a general sparse matrix can be computed together; i.e., do not have non-zero ...
void set(size_t k, const value_type &v)
size_t nnz(void) const
number of possibly non-zero elements in matrix
const SizeVector & col(void) const
column indices
CppAD::vector< size_t > color
results of the coloring algorithm
void clear(void)
free memory and set number of elements to zero
Define the CppAD error checking macros (all of which begin with CPPAD_ASSERT_)
size_t nc(void) const
number of columns in matrix
Sparse matrices with elements of type Scalar.
Coloring algorithm for a general sparse matrix.
void resize(size_t n)
change the number of elements in this vector.
size_t nr(void) const
number of rows in matrix
size_t nr(void) const
number of rows in matrix
CppAD::vector< size_t > order
indices that sort the user row and col arrays by color
sparsity pattern for a matrix with indices of type size_t
void index_sort(const VectorKey &keys, VectorSize &ind)
Compute the indices that sort a vector of keys.
size_t sparse_jac_for(size_t group_max, const BaseVector &x, sparse_rcv< SizeVector, BaseVector > &subset, const sparse_rc< SizeVector > &pattern, const std::string &coloring, sparse_jac_work &work)
Calculate sparse Jacobains using forward mode.
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
void resize(size_t n_set, size_t end)
Start a new vector of sets.
Class used to hold information used by Sparse Jacobian routines in this file, so they do not need to ...
File used to define CppAD::vector and CppAD::vectorBool.
size_t nc(void) const
number of columns in matrix
void set_internal_sparsity(bool zero_empty, bool input_empty, bool transpose, const vector< size_t > &internal_index, InternalSparsity &internal_pattern, const sparse_rc< SizeVector > &pattern_in)
Update the internal sparsity pattern for a sub-set of rows.
Routines that enable code to be independent of which internal spasity pattern is used.
const SizeVector & row(void) const
row indices
size_t sparse_jac_rev(const BaseVector &x, sparse_rcv< SizeVector, BaseVector > &subset, const sparse_rc< SizeVector > &pattern, const std::string &coloring, sparse_jac_work &work)
Calculate sparse Jacobains using reverse mode.
sparse_jac_work(void)
constructor
void clear(void)
reset work to empty. This informs CppAD that color and order need to be recomputed ...