- 3.0.2 core module.
SparsityPattern.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #ifdef CPPAD
9 
10 #include <cppad/cppad.hpp>
11 #include <Eigen/Core>
12 
13 namespace ct {
14 namespace core {
15 namespace internal {
16 
18 class SparsityPattern
19 {
20 public:
21  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
22 
23  SparsityPattern() = default;
24 
25  virtual ~SparsityPattern() = default;
26 
28 
35  template <int ROWS, int COLS>
36  void initPattern(const Eigen::Matrix<bool, ROWS, COLS>& sparsity)
37  {
38  clearWork();
39 
40  const size_t rows = sparsity.rows();
41  const size_t cols = sparsity.cols();
42 
43  sparsity_.resize(rows * cols);
44 
45  for (size_t i = 0; i < rows * cols; i++)
46  sparsity_[i] = sparsity.array()(i);
47 
48  size_t jacEntries = sparsity.template cast<int>().sum();
49 
50  row_.resize(jacEntries);
51  col_.resize(jacEntries);
52  size_t count = 0;
53  for (size_t i = 0; i < rows; i++)
54  {
55  for (size_t j = 0; j < cols; j++)
56  {
57  if (sparsity(i, j))
58  {
59  row_[count] = j;
60  col_[count] = i;
61  count++;
62  }
63  }
64  }
65  }
66 
68 
71  const CppAD::vector<bool>& sparsity() const { return sparsity_; }
73 
80  const CppAD::vector<size_t>& row() const { return row_; }
82 
89  const CppAD::vector<size_t>& col() const { return col_; }
91 
96  CppAD::sparse_jacobian_work& workJacobian() { return workJacobian_; }
97  CppAD::sparse_hessian_work& workHessian() { return workHessian_; }
101  void clearWork()
102  {
103  workJacobian_.clear();
104  workHessian_.clear();
105  }
106 
107 private:
108  CppAD::vector<bool> sparsity_;
109  CppAD::vector<size_t> row_;
110  CppAD::vector<size_t> col_;
111  CppAD::sparse_jacobian_work workJacobian_;
112  CppAD::sparse_hessian_work workHessian_;
113 };
114 
115 
116 } /* namespace internal */
117 } /* namespace core */
118 } /* namespace ct */
119 
120 #endif
for i