1 # ifndef CPPAD_CORE_SPARSE_HES_HPP
2 # define CPPAD_CORE_SPARSE_HES_HPP
316 template <
class Base>
317 template <
class SizeVector,
class BaseVector>
319 const BaseVector& x ,
320 const BaseVector& w ,
323 const std::string& coloring ,
325 {
size_t n = Domain();
329 "sparse_hes: subset.nr() not equal domain dimension for f"
333 "sparse_hes: subset.nc() not equal domain dimension for f"
336 size_t( x.size() ) == n,
337 "sparse_hes: x.size() not equal domain dimension for f"
340 size_t( w.size() ) == Range(),
341 "sparse_hes: w.size() not equal range dimension for f"
351 const SizeVector& subset_row( subset.
row() );
352 const SizeVector& subset_col( subset.
col() );
358 size_t K = subset.
nnz();
366 if( color.
size() != 0 )
369 "sparse_hes: work is non-empty and conditions have changed"
373 "sparse_hes: work is non-empty and conditions have changed"
377 "sparse_hes: work is non-empty and conditions have changed"
380 for(
size_t k = 0; k < K; k++)
381 {
bool ok = row[k] == subset_row[k] && col[k] == subset_col[k];
382 ok |= row[k] == subset_col[k] && col[k] == subset_row[k];
385 "sparse_hes: work is non-empty and conditions have changed"
392 if( color.
size() == 0 )
396 "sparse_hes: pattern.nr() not equal domain dimension for f"
400 "sparse_hes: pattern.nc() not equal domain dimension for f"
408 for(
size_t k = 0; k < K; k++)
409 { row[k] = subset_row[k];
410 col[k] = subset_col[k];
415 for(
size_t j = 0; j < n; j++)
416 internal_index[j] = j;
417 bool transpose =
true;
418 bool zero_empty =
false;
419 bool input_empty =
true;
421 internal_pattern.
resize(n, n);
423 transpose, internal_index, internal_pattern, pattern
429 if( coloring ==
"cppad.general" )
431 else if( coloring ==
"cppad.symmetric" )
433 else if( coloring ==
"colpack.general" )
435 # if CPPAD_HAS_COLPACK
436 local::color_general_colpack(internal_pattern, col, row, color);
440 "sparse_hes: coloring = colpack.star "
441 "and colpack_prefix not in cmake command line."
446 coloring ==
"colpack.symmetric" ||
447 coloring ==
"colpack.star"
450 # if CPPAD_HAS_COLPACK
455 "sparse_hes: coloring = colpack.symmetic or colpack.star "
456 "and colpack_prefix not in cmake command line."
462 "sparse_hes: coloring is not valid."
468 for(
size_t k = 0; k < K; k++)
469 key[k] = color[ col[k] ];
477 for(
size_t j = 0; j < n; j++)
if( color[j] < n )
478 n_color = std::max(n_color, color[j] + 1);
481 for(
size_t k = 0; k < K; k++)
488 BaseVector ddw(2 * n);
492 for(
size_t ell = 0; ell < n_color; ell++)
497 coloring ==
"colpack.general" ||
498 coloring ==
"colpack.symmetric" ||
499 coloring ==
"colpack.star"
502 else if( color[ col[ order[k] ] ] != ell )
506 coloring ==
"colpack.general" ||
507 coloring ==
"colpack.symmetic" ||
508 coloring ==
"colpack.star"
515 for(
size_t j = 0; j < n; j++)
517 if( color[j] == ell )
527 while( k < K && color[ col[order[k]] ] == ell )
528 {
size_t index = row[ order[k] ] * 2 + 1;
529 subset.
set(order[k], ddw[index] );
#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
sparse_hes_work(void)
constructor
Coloring algorithm for a symmetric sparse matrix.
CppAD::vector< size_t > order
indices that sort the row and col arrays by color
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
size_t size(void) const
number of elements currently in this vector.
CppAD::vector< size_t > col
sparsity pattern for a matrix with indices of type size_t
CppAD::vector< size_t > color
results of the coloring algorithm
void index_sort(const VectorKey &keys, VectorSize &ind)
Compute the indices that sort a vector of keys.
Class used to hold information used by Sparse Hessian routine in this file, so it does not need to be...
void clear(void)
inform CppAD that this information needs to be recomputed
size_t sparse_hes(const BaseVector &x, const BaseVector &w, sparse_rcv< SizeVector, BaseVector > &subset, const sparse_rc< SizeVector > &pattern, const std::string &coloring, sparse_hes_work &work)
Calculate sparse Hessians using forward mode.
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
void color_symmetric_cppad(const VectorSet &pattern, CppAD::vector< size_t > &row, CppAD::vector< size_t > &col, CppAD::vector< size_t > &color)
CppAD algorithm for determining which rows of a symmetric sparse matrix can be computed together...
void resize(size_t n_set, size_t end)
Start a new vector of sets.
void color_symmetric_colpack(const VectorSet &pattern, CppAD::vector< size_t > &row, CppAD::vector< size_t > &col, CppAD::vector< size_t > &color)
Colpack algorithm for determining which rows of a symmetric sparse matrix can be computed together...
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
CppAD::vector< size_t > row
row and column indicies for return values (some may be reflected by symmetric coloring algorithms) ...