00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CSparseMatrixTemplate_H
00029 #define CSparseMatrixTemplate_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032
00033 namespace mrpt {
00034 namespace math {
00035 using namespace std;
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 template<class T>
00052 class CSparseMatrixTemplate {
00053
00054 public:
00055
00056
00057
00058 typedef typename std::map<std::pair<size_t,size_t>,T> SparseMatrixMap;
00059
00060
00061
00062
00063 typedef typename SparseMatrixMap::const_iterator const_iterator;
00064
00065
00066
00067
00068 typedef typename SparseMatrixMap::const_reverse_iterator const_reverse_iterator;
00069 protected:
00070
00071
00072
00073 size_t mRows,mColumns;
00074
00075
00076
00077 SparseMatrixMap objectList;
00078 public:
00079
00080
00081
00082 CSparseMatrixTemplate():mRows(0),mColumns(0) {}
00083
00084
00085
00086 CSparseMatrixTemplate(size_t nR,size_t nC):mRows(nR),mColumns(nC) {}
00087
00088
00089
00090 inline T operator()(size_t r,size_t c) const {
00091 const_iterator it=objectList.find(make_pair(r,c));
00092 if (it==objectList.end()) return T();
00093 else return it->second;
00094 }
00095
00096
00097
00098 inline bool exists(size_t r,size_t c) const {
00099 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
00100 if (r>=mRows||c>=mColumns) throw std::logic_error("Out of range");
00101 #endif
00102 return (objectList.find(make_pair(r,c)) != objectList.end());
00103 }
00104
00105
00106
00107
00108 inline T& operator()(size_t r,size_t c) {
00109 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
00110 if (r>=mRows||c>=mColumns) throw std::logic_error("Out of range");
00111 #endif
00112 return objectList[make_pair(r,c)];
00113 }
00114
00115
00116
00117
00118 inline size_t getRowCount() const {
00119 return mRows;
00120 }
00121
00122
00123
00124
00125 inline size_t getColCount() const {
00126 return mColumns;
00127 }
00128
00129
00130
00131
00132
00133 void getRow(size_t nRow,std::vector<T> &vec) const {
00134 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
00135 if (nRow>=mRows) throw std::logic_error("Out of range");
00136 #endif
00137 vec.resize(mColumns);
00138 size_t nextIndex=0;
00139 for (typename SparseMatrixMap::const_iterator it=objectList.begin();it!=objectList.end();++it) {
00140 const pair<size_t,size_t> &index=it->first;
00141 if (index.first<nRow) continue;
00142 else if (index.first==nRow) {
00143 for (size_t i=nextIndex;i<index.second;i++) vec[i]=T();
00144 vec[index.second]=it->second;
00145 nextIndex=index.second+1;
00146 } else {
00147 for (size_t i=nextIndex;i<mColumns;i++) vec[i]=T();
00148 break;
00149 }
00150 }
00151 }
00152
00153
00154
00155
00156
00157 void getColumn(size_t nCol,std::vector<T> &vec) const {
00158 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
00159 if (nCol>=mColumns) throw std::logic_error("Out of range");
00160 #endif
00161 vec.resize(mRows);
00162 size_t nextIndex=0;
00163 for (typename SparseMatrixMap::const_iterator it=objectList.begin();it!=objectList.end();++it) {
00164 const pair<size_t,size_t> &index=it->first;
00165 if (index.second==nCol) {
00166 for (size_t i=nextIndex;i<index.first;i++) vec[i]=T();
00167 vec[index.first]=it->second;
00168 nextIndex=index.first+1;
00169 }
00170 }
00171 for (size_t i=nextIndex;i<mRows;i++) vec[i]=T();
00172 }
00173
00174
00175
00176
00177 inline void insert(size_t row,size_t column,const T& obj) {
00178 operator()(row,column)=obj;
00179 }
00180
00181
00182
00183
00184
00185 inline const_iterator begin() const {
00186 return objectList.begin();
00187 }
00188
00189
00190
00191
00192 inline const_iterator end() const {
00193 return objectList.end();
00194 }
00195
00196
00197
00198
00199 inline const_reverse_iterator rbegin() const {
00200 return objectList.rbegin();
00201 }
00202
00203
00204
00205
00206 inline const_reverse_iterator rend() const {
00207 return objectList.rend();
00208 }
00209
00210
00211
00212
00213
00214 void setRow(size_t nRow,const std::vector<T> &vec,const T& nullObject=T()) {
00215 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
00216 if (nRow>=mRows) throw std::logic_error("Out of range");
00217 #endif
00218 size_t N=vec.size();
00219 if (N!=mColumns) throw std::logic_error("Wrong-sized vector");
00220 for (size_t i=0;i<N;i++) {
00221 const T &obj=vec[i];
00222 pair<size_t,size_t> index=make_pair(nRow,i);
00223 if (obj==nullObject) objectList.erase(index);
00224 else objectList[index]=obj;
00225 }
00226 }
00227
00228
00229
00230
00231
00232 void setColumn(size_t nCol,const std::vector<T> &vec,const T& nullObject=T()) {
00233 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
00234 if (nCol>=mColumns) throw std::logic_error("Out of range");
00235 #endif
00236 size_t N=vec.size();
00237 if (N!=mRows) throw std::logic_error("Wrong-sized vector");
00238 for (size_t i=0;i<N;i++) {
00239 const T &obj=vec[i];
00240 pair<size_t,size_t> index=make_pair(i,nCol);
00241 if (obj==nullObject) objectList.erase(index);
00242 else objectList[index]=obj;
00243 }
00244 }
00245
00246
00247
00248 void resize(size_t nRows,size_t nCols) {
00249
00250 if (mRows==nRows && mColumns==nCols) return;
00251 mRows=nRows;
00252 mColumns=nCols;
00253 std::vector<pair<size_t,size_t> > toErase;
00254 for (const_iterator it=objectList.begin();it!=objectList.end();++it) {
00255 const pair<size_t,size_t> &i=it->first;
00256 if (i.first>=nRows||i.second>=nCols) toErase.push_back(it->first);
00257 }
00258 for (vector<pair<size_t,size_t> >::const_iterator it=toErase.begin();it!=toErase.end();++it) objectList.erase(*it);
00259 }
00260
00261
00262
00263
00264
00265 CSparseMatrixTemplate<T> operator()(size_t firstRow,size_t lastRow,size_t firstColumn,size_t lastColumn) const {
00266 #if defined(_DEBUG) || (MRPT_ALWAYS_CHECKS_DEBUG_MATRICES)
00267 if (lastRow>=mRows||lastColumn>=mColumns) throw std::logic_error("Out of range");
00268 if (firstRow>lastRow||firstColumn>lastColumn) throw std::logic_error("Invalid size");
00269 #endif
00270 CSparseMatrixTemplate<T> res=CSparseMatrixTemplate<T>(lastRow+1-firstRow,lastColumn+1-firstColumn);
00271 for (typename SparseMatrixMap::const_iterator it=begin();it!=end();++it) {
00272 const pair<size_t,size_t> &i=it->first;
00273 if (i.first>=firstRow&&i.first<=lastRow&&i.second>=firstColumn&&i.second<=lastColumn) res(i.first-firstRow,i.second-firstColumn)=it->second;
00274 }
00275 return res;
00276 }
00277
00278
00279
00280 void getAsVector(std::vector<T> &vec) const {
00281 size_t N=objectList.size();
00282 vec.resize(0);
00283 vec.reserve(N);
00284 for (const_iterator it=objectList.begin();it!=objectList.end();++it) vec.push_back(it->second);
00285 }
00286
00287
00288
00289
00290 inline size_t getNonNullElements() const {
00291 return objectList.size();
00292 }
00293
00294
00295
00296 inline bool empty() const { return objectList.empty(); }
00297
00298
00299
00300
00301
00302 inline size_t getNullElements() const {
00303 return mRows*mColumns-getNonNullElements();
00304 }
00305
00306
00307
00308
00309
00310 inline bool isNull(size_t nRow,size_t nCol) const {
00311 if (nRow>=mRows||nCol>=mColumns) throw std::logic_error("Out of range");
00312 return objectList.count(make_pair(nRow,nCol))==0;
00313 }
00314
00315
00316
00317
00318 inline bool isNotNull(size_t nRow,size_t nCol) const {
00319 if (nRow>=mRows||nCol>=mColumns) throw std::logic_error("Out of range");
00320 return objectList.count(make_pair(nRow,nCol))>0;
00321 }
00322
00323
00324
00325 inline void clear() {
00326 objectList.clear();
00327 }
00328
00329
00330
00331 void purge(T nullObject=T()) {
00332 std::vector<std::pair<size_t,size_t> > nulls;
00333 for (const_iterator it=begin();it!=end();++it) if (it->second==nullObject) nulls.push_back(it->first);
00334 for (std::vector<std::pair<size_t,size_t> >::const_iterator it=nulls.begin();it!=nulls.end();++it) objectList.erase(*it);
00335 }
00336 };
00337
00338
00339
00340
00341
00342
00343 template<class T>
00344 class CSparseSymmetricalMatrix : public CSparseMatrixTemplate<T> {
00345 public:
00346 CSparseSymmetricalMatrix() : CSparseMatrixTemplate<T>() { }
00347 explicit CSparseSymmetricalMatrix(const CSparseSymmetricalMatrix &o) : CSparseMatrixTemplate<T>(o) { }
00348 explicit CSparseSymmetricalMatrix(const CSparseMatrixTemplate<T> &o) : CSparseMatrixTemplate<T>(o) { }
00349 virtual ~CSparseSymmetricalMatrix() { }
00350
00351 void resize(size_t matrixSize) {
00352 CSparseMatrixTemplate<T>::resize(matrixSize,matrixSize);
00353 }
00354
00355 inline T operator()(size_t r,size_t c) const {
00356 if (c<r) std::swap(r,c);
00357 typename CSparseMatrixTemplate<T>::const_iterator it=CSparseMatrixTemplate<T>::objectList.find(make_pair(r,c));
00358 if (it==CSparseMatrixTemplate<T>::objectList.end()) return T();
00359 else return it->second;
00360 }
00361 inline T& operator()(size_t r,size_t c) {
00362 if (c<r) std::swap(r,c);
00363 if (r>=CSparseMatrixTemplate<T>::mRows||c>=CSparseMatrixTemplate<T>::mColumns) throw std::logic_error("Out of range");
00364 return CSparseMatrixTemplate<T>::objectList[make_pair(r,c)];
00365 }
00366
00367 };
00368
00369 }
00370 }
00371 #endif