Main MRPT website > C++ reference for MRPT 1.5.7
CSparseMatrixTemplate.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CSparseMatrixTemplate_H
10 #define CSparseMatrixTemplate_H
11 
12 #include <mrpt/utils/utils_defs.h>
13 #include <map>
14 
15 namespace mrpt {
16 namespace math {
17 
18  /** A sparse matrix container (with cells of any type), with iterators.
19  * This class stores only those elements created by assigning them a value, for example: "M(2,3)=8;".
20  *
21  * This class doesn't implement math operations since it's a generic sparse container, but it can be
22  * used to initialize the contents of a CSparse library-based matrix of type mrpt::math::CSparseMatrix.
23  *
24  * Note that reading non-existing cell elements will return the default value (0 for numbers)
25  * and that cell will remain non-created in the matrix.
26  *
27  * There is an additional method "exists(i,j)" to check whether a given element exists in the matrix.
28  *
29  * \sa mrpt::math::MatrixBlockSparseCols, mrpt::math::CSparseMatrix, CSparseSymmetricalMatrix
30  * \note Methods marked as "Doesn't check bounds" mean that if an access to an element out of the matrix size is tried, an empty element will be assumed, but this will not raise any invalid memory access.
31  * \ingroup mrpt_base_grp
32  */
33  template<class T>
35  //Public typedefs
36  public:
37  /**
38  * Internal map type, used to store the actual matrix.
39  */
40  typedef typename std::map<std::pair<size_t,size_t>,T> SparseMatrixMap;
41  /**
42  * Const iterator to move through the matrix.
43  * \sa CSparseMatrixTemplate::const_reverse_iterator
44  */
46  /**
47  * Const reverse iterator to move through the matrix.
48  * \sa CSparseMatrixTemplate::const_iterator
49  */
50  typedef typename SparseMatrixMap::const_reverse_iterator const_reverse_iterator;
51  protected:
52  /**
53  * Size of the matrix.
54  */
55  size_t mRows,mColumns;
56  /**
57  * Actual matrix.
58  */
60  public:
61  /**
62  * Basic constructor with no data. Size is set to (0,0).
63  */
65  /**
66  * Constructor with default size.
67  */
68  CSparseMatrixTemplate(size_t nR,size_t nC):mRows(nR),mColumns(nC) {}
69  /**
70  * Element access operator. Doesn't check bounds.
71  */
72  inline T operator()(size_t r,size_t c) const {
73  const_iterator it=objectList.find(std::make_pair(r,c));
74  if (it==objectList.end()) return T();
75  else return it->second;
76  }
77 
78  /** Element access operator. Checks bounds.
79  */
80  inline bool exists(size_t r,size_t c) const {
81 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
82  if (r>=mRows||c>=mColumns) throw std::logic_error("Out of range");
83 #endif
84  return (objectList.find(std::make_pair(r,c)) != objectList.end());
85  }
86 
87  /**
88  * Reference access operator. Checks for bounds.
89  */
90  inline T& operator()(size_t r,size_t c) { //-V659
91 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
92  if (r>=mRows||c>=mColumns) throw std::logic_error("Out of range");
93 #endif
94  return objectList[std::make_pair(r,c)];
95  }
96  /**
97  * Returns the amount of rows in this matrix.
98  * \sa getColCount,getRow
99  */
100  inline size_t getRowCount() const {
101  return mRows;
102  }
103  /**
104  * Returns the amount of columns in this matrix.
105  * \sa getRowCount
106  */
107  inline size_t getColCount() const {
108  return mColumns;
109  }
110  /**
111  * Extracts a full row from the matrix.
112  * \sa getRowCount,getColumn,setRow
113  * \throw std::logic_error on out of range.
114  */
115  void getRow(size_t nRow,std::vector<T> &vec) const {
116 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
117  if (nRow>=mRows) throw std::logic_error("Out of range");
118 #endif
119  vec.resize(mColumns);
120  size_t nextIndex=0;
121  for (typename SparseMatrixMap::const_iterator it=objectList.begin();it!=objectList.end();++it) {
122  const std::pair<size_t,size_t> &index=it->first;
123  if (index.first<nRow) continue;
124  else if (index.first==nRow) {
125  for (size_t i=nextIndex;i<index.second;i++) vec[i]=T();
126  vec[index.second]=it->second;
127  nextIndex=index.second+1;
128  } else {
129  for (size_t i=nextIndex;i<mColumns;i++) vec[i]=T();
130  break;
131  }
132  }
133  }
134  /**
135  * Extracts a full column from the matrix.
136  * \sa getColCount,getRow,setColumn
137  * \throw std::logic_error on out of range.
138  */
139  void getColumn(size_t nCol,std::vector<T> &vec) const {
140 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
141  if (nCol>=mColumns) throw std::logic_error("Out of range");
142 #endif
143  vec.resize(mRows);
144  size_t nextIndex=0;
145  for (typename SparseMatrixMap::const_iterator it=objectList.begin();it!=objectList.end();++it) {
146  const std::pair<size_t,size_t> &index=it->first;
147  if (index.second==nCol) {
148  for (size_t i=nextIndex;i<index.first;i++) vec[i]=T();
149  vec[index.first]=it->second;
150  nextIndex=index.first+1;
151  }
152  }
153  for (size_t i=nextIndex;i<mRows;i++) vec[i]=T();
154  }
155  /**
156  * Inserts an element into the matrix.
157  * \sa operator()(size_t,size_t)
158  */
159  inline void insert(size_t row,size_t column,const T& obj) {
161  }
162 
163  /** Inserts submatrix at a given location */
164  template <class MATRIX_LIKE>
165  inline void insertMatrix(size_t row,size_t column,const MATRIX_LIKE& mat)
166  {
167  for (size_t nr=0;nr<mat.getRowCount();nr++)
168  for (size_t nc=0;nc<mat.getColCount();nc++)
169  operator()(row+nr,column+nc)=mat(nr,nc);
170  }
171 
172  //Public interface only supports const iterators. This way, no user of this class will be able to freely modify it contents.
173  /**
174  * Returns an iterator which points to the starting point of the matrix. It's a const_iterator, so that the usar isn't able to modify the matrix content into an invalid state.
175  * \sa end,rbegin,rend
176  */
177  inline const_iterator begin() const {
178  return objectList.begin();
179  }
180  /**
181  * Returns an iterator which points to the end of the matrix. It's a const_iterator, so that the usar isn't able to modify the matrix content into an invalid state.
182  * \sa begin,rbegin,rend
183  */
184  inline const_iterator end() const {
185  return objectList.end();
186  }
187  /**
188  * Returns an iterator which points to the end of the matrix, and can be used to move backwards. It's a const_reverse_iterator, so that the usar isn't able to modify the matrix content into an invalid state.
189  * \sa begin,end,rend
190  */
192  return objectList.rbegin();
193  }
194  /**
195  * Returns an iterator which points to the starting point of the matrix, although it's the upper limit of the matrix since it's a reverse iterator. Also, it's a const_reverse_iterator, so that the usar isn't able to modify the matrix content into an invalid state.
196  * \sa begin,end,rbegin
197  */
198  inline const_reverse_iterator rend() const {
199  return objectList.rend();
200  }
201  /**
202  * Inserts a full row into the matrix. The third argument is used to specify a null object (which won't be inserted, since the matrix is sparse).
203  * \sa getRow
204  * \throw std::logic_error on out of range or wrong sized vector.
205  */
206  void setRow(size_t nRow,const std::vector<T> &vec,const T& nullObject=T()) {
207 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
208  if (nRow>=mRows) throw std::logic_error("Out of range");
209 #endif
210  size_t N=vec.size();
211  if (N!=mColumns) throw std::logic_error("Wrong-sized vector");
212  for (size_t i=0;i<N;i++) {
213  const T &obj=vec[i];
214  std::pair<size_t,size_t> index=std::make_pair(nRow,i);
215  if (obj==nullObject) objectList.erase(index);
216  else objectList[index]=obj;
217  }
218  }
219  /**
220  * Inserts a full column into the matrix. The third argument is used to specify a null object (which won't be inserted, since the matrix is sparse).
221  * \sa getColumn
222  * \throw std::logic_error on out of range or wrong sized vector.
223  */
224  void setColumn(size_t nCol,const std::vector<T> &vec,const T& nullObject=T()) {
225 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
226  if (nCol>=mColumns) throw std::logic_error("Out of range");
227 #endif
228  size_t N=vec.size();
229  if (N!=mRows) throw std::logic_error("Wrong-sized vector");
230  for (size_t i=0;i<N;i++) {
231  const T &obj=vec[i];
232  std::pair<size_t,size_t> index=std::make_pair(i,nCol);
233  if (obj==nullObject) objectList.erase(index);
234  else objectList[index]=obj;
235  }
236  }
237  /**
238  * Changes the size of the matrix.
239  */
240  void resize(size_t nRows,size_t nCols) {
241  // if (mRows<0||mColumns<0) throw std::logic_error("Invalid range"); // This case never happens!
242  if (mRows==nRows && mColumns==nCols) return;
243  mRows=nRows;
244  mColumns=nCols;
245  std::vector<std::pair<size_t,size_t> > toErase;
246  for (const_iterator it=objectList.begin();it!=objectList.end();++it) {
247  const std::pair<size_t,size_t> &i=it->first;
248  if (i.first>=nRows||i.second>=nCols) toErase.push_back(it->first);
249  }
250  for (std::vector<std::pair<size_t,size_t> >::const_iterator it=toErase.begin();it!=toErase.end();++it) objectList.erase(*it);
251  }
252  /**
253  * Extracts a submatrix form the matrix.
254  * \sa operator()(size_t,size_t)
255  * \throw std::logic_error on invalid bounds.
256  */
257  CSparseMatrixTemplate<T> operator()(size_t firstRow,size_t lastRow,size_t firstColumn,size_t lastColumn) const {
258 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
259  if (lastRow>=mRows||lastColumn>=mColumns) throw std::logic_error("Out of range");
260  if (firstRow>lastRow||firstColumn>lastColumn) throw std::logic_error("Invalid size");
261 #endif
262  CSparseMatrixTemplate<T> res=CSparseMatrixTemplate<T>(lastRow+1-firstRow,lastColumn+1-firstColumn);
263  for (typename SparseMatrixMap::const_iterator it=begin();it!=end();++it) {
264  const std::pair<size_t,size_t> &i=it->first;
265  if (i.first>=firstRow&&i.first<=lastRow&&i.second>=firstColumn&&i.second<=lastColumn) res(i.first-firstRow,i.second-firstColumn)=it->second;
266  }
267  return res;
268  }
269  /**
270  * Gets a vector containing all the elements of the matrix, ignoring their position.
271  */
272  void getAsVector(std::vector<T> &vec) const {
273  size_t N=objectList.size();
274  vec.resize(0);
275  vec.reserve(N);
276  for (const_iterator it=objectList.begin();it!=objectList.end();++it) vec.push_back(it->second);
277  }
278  /**
279  * Gets the amount of non-null elements inside the matrix.
280  * \sa getNullElements,isNull,isNotNull
281  */
282  inline size_t getNonNullElements() const {
283  return objectList.size();
284  }
285  /** Are there no elements set to !=0 ?
286  * \sa getNullElements,isNull,isNotNull
287  */
288  inline bool empty() const { return objectList.empty(); }
289 
290  /**
291  * Gets the amount of null elements inside the matrix.
292  * \sa getNonNullElements,isNull,isNotNull
293  */
294  inline size_t getNullElements() const {
296  }
297  /**
298  * Checks whether an element of the matrix is the default object.
299  * \sa getNonNullElements,getNullElements,isNotNull
300  * \throw std::logic_error on out of range
301  */
302  inline bool isNull(size_t nRow,size_t nCol) const {
303  if (nRow>=mRows||nCol>=mColumns) throw std::logic_error("Out of range");
304  return objectList.count(std::make_pair(nRow,nCol))==0;
305  }
306  /**
307  * Checks whether an element of the matrix is not the default object.
308  * \sa getNonNullElements,getNullElements,isNull
309  */
310  inline bool isNotNull(size_t nRow,size_t nCol) const {
311  if (nRow>=mRows||nCol>=mColumns) throw std::logic_error("Out of range");
312  return objectList.count(std::make_pair(nRow,nCol))>0;
313  }
314  /**
315  * Completely removes all elements, although maintaining the matrix's size.
316  */
317  inline void clear() {
318  objectList.clear();
319  }
320  /**
321  * Checks each non-null elements against the basic objects, erasing unnecesary references to it.
322  */
323  void purge(T nullObject=T()) {
324  std::vector<std::pair<size_t,size_t> > nulls;
325  for (const_iterator it=begin();it!=end();++it) if (it->second==nullObject) nulls.push_back(it->first);
326  for (std::vector<std::pair<size_t,size_t> >::const_iterator it=nulls.begin();it!=nulls.end();++it) objectList.erase(*it);
327  }
328  }; // end of sparse matrix
329 
330  /** A sparse matrix container for square symmetrical content around the main diagonal.
331  * This class saves half of the space with respect to CSparseMatrixTemplate since only those entries (c,r) such as c>=r are really stored,
332  * but both (c,r) and (r,c) can be retrieved or set and both redirect to the same internal cell container.
333  * \sa CSparseMatrixTemplate
334  */
335  template<class T>
337  public:
342 
343  void resize(size_t matrixSize) {
344  CSparseMatrixTemplate<T>::resize(matrixSize,matrixSize);
345  }
346 
347  inline T operator()(size_t r,size_t c) const {
348  if (c<r) std::swap(r,c); // Symmetrical matrix
350  if (it==CSparseMatrixTemplate<T>::objectList.end()) return T();
351  else return it->second;
352  }
353  inline T& operator()(size_t r,size_t c) { //-V659
354  if (c<r) std::swap(r,c); // Symmetrical matrix
355  if (r>=CSparseMatrixTemplate<T>::mRows||c>=CSparseMatrixTemplate<T>::mColumns) throw std::logic_error("Out of range");
356  return CSparseMatrixTemplate<T>::objectList[std::make_pair(r,c)];
357  }
358 
359  }; // end of CSparseSymmetricalMatrix
360 
361 }
362 }
363 #endif
void insertMatrix(size_t row, size_t column, const MATRIX_LIKE &mat)
Inserts submatrix at a given location.
const_iterator begin() const
Returns an iterator which points to the starting point of the matrix.
bool isNotNull(size_t nRow, size_t nCol) const
Checks whether an element of the matrix is not the default object.
size_t getNullElements() const
Gets the amount of null elements inside the matrix.
size_t getNonNullElements() const
Gets the amount of non-null elements inside the matrix.
size_t getColCount() const
Returns the amount of columns in this matrix.
void setColumn(size_t nCol, const std::vector< T > &vec, const T &nullObject=T())
Inserts a full column into the matrix.
void insert(size_t row, size_t column, const T &obj)
Inserts an element into the matrix.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
SparseMatrixMap::const_iterator const_iterator
Const iterator to move through the matrix.
T & operator()(size_t r, size_t c)
Reference access operator.
GLsizei GLsizei GLuint * obj
Definition: glext.h:3902
CSparseSymmetricalMatrix(const CSparseSymmetricalMatrix &o)
A sparse matrix container (with cells of any type), with iterators.
bool empty() const
Are there no elements set to !=0 ?
void purge(T nullObject=T())
Checks each non-null elements against the basic objects, erasing unnecesary references to it...
size_t getRowCount() const
Returns the amount of rows in this matrix.
CSparseMatrixTemplate(size_t nR, size_t nC)
Constructor with default size.
T operator()(size_t r, size_t c) const
Element access operator.
GLuint index
Definition: glext.h:3891
const GLubyte * c
Definition: glext.h:5590
SparseMatrixMap::const_reverse_iterator const_reverse_iterator
Const reverse iterator to move through the matrix.
void getColumn(size_t nCol, std::vector< T > &vec) const
Extracts a full column from the matrix.
CSparseMatrixTemplate()
Basic constructor with no data.
size_t mRows
Size of the matrix.
const_reverse_iterator rend() const
Returns an iterator which points to the starting point of the matrix, although it&#39;s the upper limit o...
void clear()
Completely removes all elements, although maintaining the matrix&#39;s size.
CSparseSymmetricalMatrix(const CSparseMatrixTemplate< T > &o)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const_iterator end() const
Returns an iterator which points to the end of the matrix.
bool isNull(size_t nRow, size_t nCol) const
Checks whether an element of the matrix is the default object.
GLdouble GLdouble GLdouble r
Definition: glext.h:3618
T operator()(size_t r, size_t c) const
CSparseMatrixTemplate< T > operator()(size_t firstRow, size_t lastRow, size_t firstColumn, size_t lastColumn) const
Extracts a submatrix form the matrix.
SparseMatrixMap objectList
Actual matrix.
GLenum GLenum GLvoid * row
Definition: glext.h:3533
void resize(size_t nRows, size_t nCols)
Changes the size of the matrix.
bool exists(size_t r, size_t c) const
Element access operator.
std::map< std::pair< size_t, size_t >, T > SparseMatrixMap
Internal map type, used to store the actual matrix.
GLuint res
Definition: glext.h:6298
const_reverse_iterator rbegin() const
Returns an iterator which points to the end of the matrix, and can be used to move backwards...
void getAsVector(std::vector< T > &vec) const
Gets a vector containing all the elements of the matrix, ignoring their position. ...
GLenum GLenum GLvoid GLvoid * column
Definition: glext.h:3533
void getRow(size_t nRow, std::vector< T > &vec) const
Extracts a full row from the matrix.
void setRow(size_t nRow, const std::vector< T > &vec, const T &nullObject=T())
Inserts a full row into the matrix.
A sparse matrix container for square symmetrical content around the main diagonal.



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019