1 # ifndef CPPAD_CORE_SPARSE_HESSIAN_HPP
2 # define CPPAD_CORE_SPARSE_HESSIAN_HPP
417 template <
class VectorBase,
class VectorSet,
class VectorSize>
419 const VectorBase& x ,
420 const VectorBase& w ,
421 VectorSet& sparsity ,
422 const VectorSize& user_row ,
423 const VectorSize& user_col ,
442 CheckSimpleVector<Base, VectorBase>();
445 size_t K = hes.size();
464 if( color.
size() == 0 )
472 for(k = 0; k < K; k++)
473 { row[k] = user_row[k];
474 col[k] = user_col[k];
485 # if CPPAD_HAS_COLPACK
486 local::color_general_colpack(sparsity, row, col, color);
490 "SparseHessian: work.color_method = colpack.general "
491 "and colpack_prefix missing from cmake command line."
500 # if CPPAD_HAS_COLPACK
505 "SparseHessian: work.color_method is "
506 "colpack.symmetric or colpack.star\n"
507 "and colpack_prefix missing from cmake command line."
514 "SparseHessian: work.color_method is not valid."
521 for(k = 0; k < K; k++)
522 key[k] = color[ row[k] ];
527 for(ell = 0; ell < n; ell++)
if( color[ell] < n )
528 n_color = std::max(n_color, color[ell] + 1);
534 VectorBase ddw(2 * n);
537 for(k = 0; k < K; k++)
545 for(ell = 0; ell < n_color; ell++)
550 coloring ==
"colpack.general" ||
551 coloring ==
"colpack.symmetic" ||
552 coloring ==
"colpack.star"
555 else if( color[ row[ order[k] ] ] != ell )
559 coloring ==
"colpack.general" ||
560 coloring ==
"colpack.symmetic" ||
561 coloring ==
"colpack.star"
568 for(i = 0; i < n; i++)
570 if( color[i] == ell )
580 while( k < K && color[ row[ order[k] ] ] == ell )
581 { hes[ order[k] ] = ddw[ col[ order[k] ] * 2 + 1 ];
651 template <
class VectorBase,
class VectorSet,
class VectorSize>
653 const VectorBase& x ,
654 const VectorBase& w ,
656 const VectorSize& row ,
657 const VectorSize& col ,
662 size_t K = hes.size();
666 size_t(x.size()) == n ,
667 "SparseHessian: size of x not equal domain dimension for f."
670 size_t(row.size()) == K &&
size_t(col.size()) == K ,
671 "SparseHessian: either r or c does not have the same size as ehs."
675 "SparseHessian: invalid value in work."
677 for(k = 0; k < K; k++)
680 "SparseHessian: invalid value in r."
684 "SparseHessian: invalid value in c."
690 "SparseHessian: invalid value in work."
702 {
bool transpose =
false;
703 const char* error_msg =
"SparseHessian: sparsity pattern"
704 " does not have proper row or column dimension";
707 n_sweep = SparseHessianCompute(x, w, s, row, col, hes, work);
747 template <
class Base>
748 template <
class VectorBase,
class VectorSet>
750 const VectorBase& x,
const VectorBase& w,
const VectorSet& p
755 VectorBase hes(n * n);
758 size_t(x.size()) == n,
759 "SparseHessian: size of x not equal domain size for f."
767 for(i = 0; i < n; i++)
768 for(j = 0; j < n; j++)
769 hes[i * n + j] = zero;
776 bool transpose =
false;
777 const char* error_msg =
"SparseHessian: sparsity pattern"
778 " does not have proper row or column dimension";
781 for(i = 0; i < n; i++)
782 {
typename Pattern_type::const_iterator itr(s, i);
784 while( j != s.end() )
795 SparseHessianCompute(x, w, s, row, col, H, work);
798 for(k = 0; k < K; k++)
799 hes[ row[k] * n + col[k] ] = H[k];
833 template <
class Base>
834 template <
class VectorBase>
844 for(j = 0; j < n; j++)
845 {
for(k = 0; k < n; k++)
846 r[j * n + k] =
false;
852 for(i = 0; i < m; i++)
854 VectorBool p = RevSparseHes(n, s);
857 return SparseHessian(x, w, p);
class used by SparseHessian to hold information so it does not need to be recomputed.
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
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 clear(void)
inform CppAD that this information needs to be recomputed
CppAD::vector< size_t > row
row and column indicies for return values (some may be reflected by star coloring algorithm) ...
Coloring algorithm for a symmetric sparse matrix.
void clear(void)
free memory and set number of elements to zero
Coloring algorithm for a general sparse matrix.
void resize(size_t n)
change the number of elements in this vector.
std::string color_method
Coloring method: "cppad", or "colpack" (this field is set by user)
size_t size(void) const
number of elements currently in this vector.
Two constant standard sets (currently used for concept checking).
void index_sort(const VectorKey &keys, VectorSize &ind)
Compute the indices that sort a vector of keys.
void push_back(const Type &s)
add an element to the back of this vector
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
CppAD::vector< size_t > order
indices that sort the user row and col arrays by color
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 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...
VectorBase SparseHessian(const VectorBase &x, const VectorBase &w)
calculate sparse Hessians
size_t SparseHessianCompute(const VectorBase &x, const VectorBase &w, VectorSet &sparsity, const VectorSize &row, const VectorSize &col, VectorBase &hes, sparse_hessian_work &work)
Private helper function that does computation for all Sparse Hessian cases.
CppAD::vector< size_t > col
sparse_hessian_work(void)
constructor
CppAD::vector< size_t > color
results of the coloring algorithm
void sparsity_user2internal(sparse_list &internal, const VectorSet &user, size_t n_set, size_t end, bool transpose, const char *error_msg)
Copy a user vector of sets sparsity pattern to an internal sparse_list object.
Template structure used obtain the internal sparsity pattern type form the corresponding element type...