MRPT  2.0.4
MatrixBlockSparseCols.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
13 #include <mrpt/math/CMatrixDynamic.h> // For mrpt::math::CMatrixDouble
14 #include <map>
15 
16 namespace mrpt::math
17 {
18 /** A templated column-indexed efficient storage of block-sparse Jacobian or
19  * Hessian matrices, together with other arbitrary information.
20  * Columns are stored in a non-associative container, but the contents of each
21  * column are kept within an std::map<> indexed by row.
22  * All submatrix blocks have the same size, which allows dense storage of them
23  * in fixed-size matrices, avoiding costly memory allocations.
24  *
25  * \tparam NROWS Rows in each elementary matrix.
26  * \tparam NCOLS Cols in each elementary matrix.
27  * \tparam INFO Type of the extra data fields within each block
28  * \tparam HAS_REMAP Is true, an inverse mapping between column indices and
29  * "user IDs" is kept.
30  * \tparam INDEX_REMAP_MAP_IMPL Ignore if HAS_REMAP=false. Defaults to
31  * "mrpt::containers::map_as_vector<size_t,size_t>" for amortized O(1). Can be
32  * set to "std::map<size_t,size_t>" in very sparse systems to save memory at the
33  * cost of a O(log N) access time when using the remap indices.
34  *
35  * \ingroup mrpt_math_grp
36  */
37 template <
38  typename Scalar, int NROWS, int NCOLS, typename INFO, bool HAS_REMAP,
39  typename INDEX_REMAP_MAP_IMPL =
42 {
44  using symbolic_t = INFO;
45 
46  struct TEntry
47  {
48  /** Numeric matrix */
50  /** Extra symbolic info */
52  };
53 
54  /** Each compressed sparse column */
55  using col_t = std::map<size_t, TEntry>;
56 
57  private:
58  /** -> cols[i]: i'th column.
59  * -> Each column is a map [row] -> TEntry
60  */
61  std::deque<col_t> m_cols;
62  /** "remapped index" is the index of some global variable, interpreted by
63  * the external user of this class. */
64  // map<size_t,size_t> col_inverse_remapped_indices;
67  std::vector<size_t> col_remapped_indices;
68 
69  public:
70  inline MatrixBlockSparseCols() : m_cols(0) {}
71  inline col_t& getCol(const size_t idx) { return m_cols[idx]; }
72  inline const col_t& getCol(const size_t idx) const { return m_cols[idx]; }
75  {
76  if (!HAS_REMAP) assert(false);
78  }
79  inline const std::vector<size_t>& getColRemappedIndices() const
80  {
81  if (!HAS_REMAP) assert(false);
82  return col_remapped_indices;
83  }
84 
85  /** Append one column, returning a ref to the new col_t data */
86  inline col_t& appendCol(const size_t remapIndex)
87  {
88  const size_t idx = m_cols.size();
89  m_cols.push_back(col_t());
90 
91  if (HAS_REMAP)
92  {
93  col_remapped_indices.resize(idx + 1);
94  col_remapped_indices[idx] = remapIndex;
95 
96  col_inverse_remapped_indices[remapIndex] = idx;
97  }
98 
99  return *m_cols.rbegin();
100  }
101 
102  /** Change the number of columns (keep old contents) */
103  inline void setColCount(const size_t nCols) { m_cols.resize(nCols); }
104  /** Get current number of cols. \sa findCurrentNumberOfRows */
105  inline size_t cols() const { return m_cols.size(); }
106  /** Clear all the entries in each column (do not change the number of
107  * columns, though!) \sa getColCount */
108  inline void clearColEntries()
109  {
110  for (size_t i = 0; i < m_cols.size(); i++) m_cols[i].clear();
111  }
112 
113  /** Clear all the entries in each column (do not change the number of
114  * columns, though!) \sa getColCount */
115  inline void clearAll()
116  {
117  m_cols.clear();
118  if (HAS_REMAP)
119  {
120  col_remapped_indices.clear();
122  }
123  }
124 
125  /** Builds a dense representation of the matrix and saves to a text file. */
127  const std::string& filename, const bool force_symmetry = false,
128  const bool is_col_compressed = true) const
129  {
131  getAsDense(D, force_symmetry, is_col_compressed);
132  return D.saveToTextFile(filename);
133  }
134 
135  /** Builds a dense representation of the matrix and saves to a text file.
136  * \param is_col_compressed true: interpret this object as compressed by
137  * cols; false: compressed by rows
138  */
140  mrpt::math::CMatrixDouble& D, const bool force_symmetry = false,
141  const bool is_col_compressed = true) const
142  {
143  const size_t nCols = m_cols.size();
144  const size_t nRows = findCurrentNumberOfRows();
145 
146  if (is_col_compressed)
147  D.setSize(nRows * NROWS, nCols * NCOLS);
148  else
149  D.setSize(nCols * NROWS, nRows * NCOLS);
150 
151  for (size_t j = 0; j < nCols; j++)
152  {
153  for (typename col_t::const_iterator itRow = m_cols[j].begin();
154  itRow != m_cols[j].end(); ++itRow)
155  {
156  const size_t row = itRow->first;
157  const size_t row_idx =
158  is_col_compressed ? row * NROWS : j * NROWS;
159  const size_t col_idx =
160  is_col_compressed ? j * NCOLS : row * NCOLS;
161  D.block(row_idx, col_idx, NROWS, NCOLS) = itRow->second.num;
162  if (force_symmetry && row_idx != col_idx)
163  D.block(col_idx, row_idx, NCOLS, NROWS) =
164  itRow->second.num.transpose();
165  }
166  }
167  }
168 
169  /** Goes over all the columns and keep the largest column length. \sa
170  * cols() */
171  size_t findCurrentNumberOfRows() const
172  {
173  size_t nRows = 0;
174  const size_t nCols = m_cols.size();
175  for (size_t j = 0; j < nCols; j++)
176  for (typename col_t::const_iterator itRow = m_cols[j].begin();
177  itRow != m_cols[j].end(); ++itRow)
178  mrpt::keep_max(nRows, itRow->first);
179  return nRows +
180  1; // nRows was the max. row index, now it's the row count.
181  }
182 
183  /** Builds a binary matrix with 1s where an elementary matrix is stored, 0s
184  * elsewhere. */
185  template <class MATRIX>
186  void getBinaryBlocksRepresentation(MATRIX& out) const
187  {
188  const size_t nCols = m_cols.size();
189  const size_t nRows = findCurrentNumberOfRows();
190  out.setZero(nRows, nCols);
191  for (size_t j = 0; j < nCols; j++)
192  for (typename col_t::const_iterator itRow = m_cols[j].begin();
193  itRow != m_cols[j].end(); ++itRow)
194  {
195  const size_t row = itRow->first;
196  out(row, j) = 1;
197  }
198  }
199 
200  /** Clear the current contents of this objects and replicates the sparse
201  * structure and numerical values of \a o */
204  {
205  const size_t nC = o.m_cols.size();
206  if (m_cols.size() != nC)
207  {
208  // Just create an empty structure with the numerical matrices:
209  m_cols.resize(nC);
210  for (size_t i = 0; i < nC; i++)
211  {
212  m_cols[i].clear();
213  for (typename col_t::const_iterator it = o.m_cols[i].begin();
214  it != o.m_cols[i].end(); ++it)
215  m_cols[i][it->first].num = it->second.num;
216  }
217  }
218  else
219  {
220  // It might be that we're overwriting an existing data structure:
221  for (size_t i = 0; i < nC; i++)
222  {
223  // ASSERTMSG_(cols[i].size()>=o.cols[i].size(),
224  // "copyNumericalValuesFrom() invoked on dissimilar structures")
225  typename col_t::iterator it_dst = m_cols[i].begin();
226  typename col_t::const_iterator it_src = o.m_cols[i].begin();
227  while (it_src != o.m_cols[i].end())
228  {
229  if (it_dst->first < it_src->first)
230  {
231  it_dst->second.num.setZero();
232  it_dst++;
233  }
234  else if (it_dst->first > it_src->first)
235  {
236  m_cols[i][it_src->first].num = it_src->second.num;
237  it_src++;
238  }
239  else
240  {
241  it_dst->second.num = it_src->second.num;
242  ++it_dst;
243  ++it_src;
244  }
245  }
246  }
247  }
248  } // end copyNumericalValuesFrom()
249 
250 }; // end of MatrixBlockSparseCols
251 
252 } // namespace mrpt::math
void getAsDense(mrpt::math::CMatrixDouble &D, const bool force_symmetry=false, const bool is_col_compressed=true) const
Builds a dense representation of the matrix and saves to a text file.
double Scalar
Definition: KmUtils.h:43
symbolic_t sym
Extra symbolic info.
void getBinaryBlocksRepresentation(MATRIX &out) const
Builds a binary matrix with 1s where an elementary matrix is stored, 0s elsewhere.
const col_t & getCol(const size_t idx) const
col_t & appendCol(const size_t remapIndex)
Append one column, returning a ref to the new col_t data.
void saveToTextFile(const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat=mrpt::math::MATRIX_FORMAT_ENG, bool appendMRPTHeader=false, const std::string &userHeader=std::string()) const
Saves the vector/matrix to a file compatible with MATLAB/Octave text format.
matrix_t num
Numeric matrix.
void saveToTextFileAsDense(const std::string &filename, const bool force_symmetry=false, const bool is_col_compressed=true) const
Builds a dense representation of the matrix and saves to a text file.
std::deque< col_t > m_cols
-> cols[i]: i&#39;th column.
This base provides a set of functions for maths stuff.
auto block(int start_row, int start_col)
non-const block(): Returns an Eigen::Block reference to the block
void setColCount(const size_t nCols)
Change the number of columns (keep old contents)
size_t cols() const
Get current number of cols.
const mrpt::containers::map_as_vector< size_t, size_t > & getColInverseRemappedIndices() const
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
A templated column-indexed efficient storage of block-sparse Jacobian or Hessian matrices, together with other arbitrary information.
size_t findCurrentNumberOfRows() const
Goes over all the columns and keep the largest column length.
void clearColEntries()
Clear all the entries in each column (do not change the number of columns, though!) ...
const_iterator begin() const
Definition: ts_hash_map.h:240
const std::vector< size_t > & getColRemappedIndices() const
mrpt::containers::map_as_vector< size_t, size_t > col_inverse_remapped_indices
"remapped index" is the index of some global variable, interpreted by the external user of this class...
void clear()
Clear the contents of this container.
mrpt::vision::TStereoCalibResults out
void setSize(size_t row, size_t col, bool zeroNewElements=false)
Changes the size of matrix, maintaining the previous contents.
void clearAll()
Clear all the entries in each column (do not change the number of columns, though!) ...
void copyNumericalValuesFrom(const MatrixBlockSparseCols< Scalar, NROWS, NCOLS, INFO, HAS_REMAP > &o)
Clear the current contents of this objects and replicates the sparse structure and numerical values o...
std::vector< size_t > col_remapped_indices
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:183
std::map< size_t, TEntry > col_t
Each compressed sparse column.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020