iRoCS Toolbox  1.1.0
SparseMatrix.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 /*======================================================================*/
28 /*======================================================================*/
29 
30 #ifndef ATBSPARSEMATRIX_LEGACY_HH
31 #define ATBSPARSEMATRIX_LEGACY_HH
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.hh>
35 #endif
36 
37 #include <map>
38 #include <iostream>
39 #include <limits>
40 #include <cassert>
41 
43 
44 #include "RuntimeError.hh"
45 #include "SparseVector.hh"
46 
47 namespace atb
48 {
49 
50 /*======================================================================*/
56 /*======================================================================*/
58  {
59  public:
60 
61 /*======================================================================*/
67 /*======================================================================*/
68  SparseMatrixError(std::string const &message = "")
69  : RuntimeError(message)
70  {}
71 
72 /*======================================================================*/
76 /*======================================================================*/
78  {}
79 
80  };
81 
82 /*======================================================================*/
88 /*======================================================================*/
89  template <typename T>
90  class SparseMatrix
91  {
92 
93  public:
94 
95 /*======================================================================*/
99 /*======================================================================*/
100  SparseMatrix();
101 
102 /*======================================================================*/
110 /*======================================================================*/
111  SparseMatrix(size_t r, size_t c);
112 
113 /*======================================================================*/
119 /*======================================================================*/
120  SparseMatrix(SparseMatrix const &m);
121 
122 /*======================================================================*/
131 /*======================================================================*/
132  SparseMatrix(blitz::Array<T,2> const &m);
133 
134 /*======================================================================*/
144 /*======================================================================*/
145  template<int NRows, int NCols>
146  SparseMatrix(blitz::TinyMatrix<T,NRows,NCols> const &m);
147 
148 /*======================================================================*/
155 /*======================================================================*/
156  ~SparseMatrix();
157 
158 /*======================================================================*/
165 /*======================================================================*/
166  void resize(size_t r, size_t c);
167 
168 /*======================================================================*/
174 /*======================================================================*/
175  size_t nRows() const;
176 
177 /*======================================================================*/
183 /*======================================================================*/
184  size_t nColumns() const;
185 
186 /*======================================================================*/
192 /*======================================================================*/
193  blitz::TinyVector<size_t,2> shape() const;
194 
195 /*======================================================================*/
201 /*======================================================================*/
202  size_t nNonZeroEntries() const;
203 
204 /*======================================================================*/
208 /*======================================================================*/
209  void clear();
210 
211 /*======================================================================*/
217 /*======================================================================*/
218  void clearRow(size_t r);
219 
220 /*======================================================================*/
226 /*======================================================================*/
227  void clearColumn(size_t c);
228 
229 /*======================================================================*/
239 /*======================================================================*/
240  SparseVector<T> const &row(size_t r) const;
241 
242 /*======================================================================*/
259 /*======================================================================*/
260  SparseVector<T> const &column(size_t c) const;
261 
262 /*======================================================================*/
272 /*======================================================================*/
273  bool nonZero(size_t r, size_t c) const;
274 
275 /*======================================================================*/
285 /*======================================================================*/
286  T const &operator()(size_t r, size_t c) const;
287 
288 /*======================================================================*/
303 /*======================================================================*/
304  T &operator()(size_t r, size_t c);
305 
306 /*======================================================================*/
316 /*======================================================================*/
317  typename SparseVector<T>::const_iterator erase(size_t r, size_t c);
318 
319 /*======================================================================*/
325 /*======================================================================*/
326  blitz::Array<T,2> toBlitz() const;
327 
328 /*======================================================================*/
334 /*======================================================================*/
335  void operator+=(SparseMatrix<T> const &m);
336 
337 /*======================================================================*/
343 /*======================================================================*/
344  void operator-=(SparseMatrix<T> const &m);
345 
346 /*======================================================================*/
352 /*======================================================================*/
353  void operator*=(T const &alpha);
354 
355 /*======================================================================*/
361 /*======================================================================*/
362  void operator/=(T const &alpha);
363 
364 /*======================================================================*/
376 /*======================================================================*/
377  void save(std::string const &fileName, std::string const &groupName,
378  bool throwErrors = false);
379 
380 /*======================================================================*/
395 /*======================================================================*/
396  void load(std::string const &fileName, std::string const &groupName,
397  bool throwErrors = false);
398 
399  private:
400 
401  void updateColumn(size_t c) const;
402 
403  size_t _nRows, _nColumns;
404  std::vector< SparseVector<T> > _rows;
405 
406  // Internal structures that need to be modified by const methods
407  mutable std::vector<bool> _columnsValid;
408  mutable std::vector< SparseVector<T> > _columns;
409 
410  };
411 
412 /*======================================================================*/
422 /*======================================================================*/
423  template<typename T>
425 
426 /*======================================================================*/
437 /*======================================================================*/
438  template<typename T>
439  blitz::Array<T,1> operator*(
440  SparseMatrix<T> const &A, blitz::Array<T,1> const &x);
441 
442 /*======================================================================*/
454 /*======================================================================*/
455  template<typename T, int Dim>
456  blitz::TinyVector<T,Dim> operator*(
457  SparseMatrix<T> const &A, blitz::TinyVector<T,Dim> const &x);
458 
459 /*======================================================================*/
470 /*======================================================================*/
471  template<typename T>
473 
474 /*======================================================================*/
485 /*======================================================================*/
486  template<typename T>
488  blitz::Array<T,2> const &A, SparseMatrix<T> const &B);
489 
490 /*======================================================================*/
501 /*======================================================================*/
502  template<typename T>
504  SparseMatrix<T> const &A, blitz::Array<T,2> const &B);
505 
506 }
507 
508 #include "SparseMatrix.icc"
509 
510 #endif
Exception specialization for error handling within libArrayToolbox.
std::map< size_t, T >::const_iterator const_iterator
Definition: SparseVector.hh:64
The SparseMatrixError class is the exception type thrown when SparseMatrix operations fail...
Definition: SparseMatrix.hh:57
The SparseMatrix class provides a sparse matrix data structure and corresponding arithmetics.
Definition: SparseMatrix.hh:90
Objects of the RuntimeError class are thrown in case of errors while processing ArrayToolbox function...
Definition: RuntimeError.hh:50
~SparseMatrixError()
Destructor.
Definition: SparseMatrix.hh:77
Lightweight alternative to libBlitzHDF5 providing its basic functionality.
Polynomial< CoeffT > operator*(const Polynomial< CoeffT > &p1, const Polynomial< CoeffT > &p2)
Multiplication operator.
SparseMatrixError(std::string const &message="")
Create a new SparseMatrixError object with given error message.
Definition: SparseMatrix.hh:68