aimsdata  5.0.5
Neuroimaging data handling
sparseMatrix.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 #ifndef AIMS_SPARSEMATRIX_SPARSEMATRIX_H
35 #define AIMS_SPARSEMATRIX_SPARSEMATRIX_H
36 
37 #include <aims/data/pheader.h>
38 #include <boost/numeric/ublas/matrix_sparse.hpp>
39 #include <boost/numeric/ublas/vector_sparse.hpp>
40 #include <boost/numeric/ublas/io.hpp>
41 #include <boost/version.hpp>
42 #include <string>
43 
44 namespace aims
45 {
46 
47 
48 #if BOOST_VERSION >= 103300
49  typedef boost::numeric::ublas::mapped_matrix< double > boost_sparse_matrix;
50  typedef boost::numeric::ublas::mapped_vector< double > boost_sparse_vector;
51 #else
52  typedef boost::numeric::ublas::sparse_matrix< double > boost_sparse_matrix;
53  typedef boost::numeric::ublas::sparse_vector< double > boost_sparse_vector;
54 #endif
55 
56 
57  class SparseMatrix : public virtual carto::RCObject
58  {
59 
60  public:
61 
62  typedef boost_sparse_matrix::reference
64  typedef boost_sparse_matrix::const_reference
66 
67  typedef boost_sparse_matrix::iterator1 iterator1;
68  typedef boost_sparse_matrix::iterator2 iterator2;
69 
70  typedef boost_sparse_matrix::const_iterator1
72  typedef boost_sparse_matrix::const_iterator2
74 
75  SparseMatrix( int32_t size1 = 1, int32_t size2 = 1 );
76  SparseMatrix( const SparseMatrix& other );
77  virtual ~SparseMatrix();
78 
79  SparseMatrix& operator=( const SparseMatrix& other );
80 
81  void reallocate( int32_t size1, int32_t size2 );
82 
83  iterator1 begin1();
84  iterator1 end1();
85 
86  const_iterator1 begin1() const;
87  const_iterator1 end1() const;
88 
89  iterator2 begin2();
90  iterator2 end2();
91 
92  const_iterator2 begin2() const;
93  const_iterator2 end2() const;
94 
95  int32_t getSize1() const;
96  int32_t getSize2() const;
97  int32_t getNonZeroElementCount() const;
98 
99  bool hasElement( int32_t i, int32_t j ) const;
100 
101  const_reference operator()( int32_t i, int32_t j ) const;
102  reference operator()( int32_t i, int32_t j );
103 
104  bool isSquare() const;
105 
106  void setZero();
107  void setIdentity();//bizarre ?
108  void setRow( int32_t s1, const std::vector<double>& row );
109  void setColumn( int32_t s2, const std::vector<double>& column );
110  void fill( const double& value );
111  void fill( int32_t offset1,
112  int32_t offset2,
113  int32_t size1,
114  int32_t size2,
115  const double& value );
116  void fill( int32_t offset1,
117  int32_t offset2,
118  const SparseMatrix& other,
119  int32_t size1 = 0,
120  int32_t size2 = 0 );
121  void setDiagonal( const double& value );
122  void transpose();
123 
124  double getTrace() const;
126  SparseMatrix getComposition( const SparseMatrix& other ) const;
127  std::vector<double> getComposition( const std::vector<double>& other ) const;
128  std::vector<double> toVector() const;
129 
130  std::vector<double> getRow( int32_t i ) const;
131  std::vector<double> getColumn( int32_t j ) const;
132 
133  template <typename VectorType>
134  VectorType getSparseRow( int32_t i ) const;
135  template <typename VectorType>
136  VectorType getSparseColumn( int32_t i ) const;
137 
138  void erase_element( int32_t i, int32_t j );
139 
143  void read( const std::string& filename,
144  const std::string & openmode = "binar",
145  bool bswap = false );
149  void write( const std::string& filename,
150  const std::string & openmode = "binar",
151  bool bswap = false ) const;
153  operator += ( const aims::SparseMatrix& thing );
155  operator -= ( const aims::SparseMatrix& thing );
157  operator *= ( double x );
159  operator /= ( double x );
160 
162  boost_sparse_matrix & boostMatrix() { return _matrix; }
163  const boost_sparse_matrix & boostMatrix() const { return _matrix; }
164 
166  const carto::Object header() const { return _header; }
167  void setHeader( carto::Object ph );
168 
169  protected:
170 
171  boost_sparse_matrix _matrix;
173 
174  };
175 
176 
177  template <typename VectorType>
178  inline
179  VectorType aims::SparseMatrix::getSparseRow( int32_t i ) const
180  {
181 
182  try
183  {
184 
185  VectorType row( getSize2() );
186  boost_sparse_matrix::const_iterator1
187  ir = _matrix.find1( 0, i, 0 );
188  if( (ir != _matrix.end1()) && ((int)ir.index1() == i ))
189  {
190  boost_sparse_matrix::const_iterator2
191  ic, ec = ir.end();
192  long n = 0;
193  for( ic=ir.begin(); ic!=ec; ++ic, ++n )
194  row[ic.index2()] = *ic;
195  }
196  return row;
197 
198  }
199  catch( std::exception & e )
200  {
201  throw std::runtime_error( std::string(
202  "aims::SparseMatrix::getSparseRow( "
203  "int32_t i ) const" ) + e.what() );
204  }
205 
206  }
207 
208 
209  template <typename VectorType>
210  inline
211  VectorType aims::SparseMatrix::getSparseColumn( int32_t j ) const
212  {
213 
214  try
215  {
216 
217  VectorType column( getSize1() );
218  for ( int32_t s = 0; s < getSize1(); s++ )
219  {
220 
221  column[ s ] = ( *this )( s, j );
222 
223  }
224  return column;
225 
226  }
227  catch( std::exception & e )
228  {
229  throw std::runtime_error( std::string(
230  "aims::SparseMatrix::getSparseColumn( "
231  "int32_t j ) const" ) + e.what() );
232  }
233 
234  }
235 
236 //
237 // unary operators + & -
238 //
239 
240 // + SparseMatrix
242  operator + ( const aims::SparseMatrix& thing );
243 
244 // - SparseMatrix
246  operator - ( const aims::SparseMatrix& thing );
247 
248 //
249 // binary operator +
250 //
251 
252 // SparseMatrix + SparseMatrix
254  operator + ( const aims::SparseMatrix& thing1,
255  const aims::SparseMatrix& thing2 );
256 
257 
258 //
259 // binary operator -
260 //
261 
262 // SparseMatrix - SparseMatrix
264  operator - ( const aims::SparseMatrix& thing1,
265  const aims::SparseMatrix& thing2 );
266 
267 
268 //
269 // binary operator *
270 //
271 
272 // SparseMatrix * SparseMatrix
274  operator * ( const aims::SparseMatrix& thing1,
275  const aims::SparseMatrix& thing2 );
276 
277 // SparseMatrix * std::vector
278 std::vector<double>
279  operator * ( const aims::SparseMatrix& thing1,
280  const std::vector<double>& thing2 );
281 
282 // SparseMatrix * double
284  operator * ( const aims::SparseMatrix& thing1,
285  const double& thing2 );
286 
287 
288 //
289 // binary operator /
290 //
291 
292 // SparseMatrix / double
294  operator / ( const aims::SparseMatrix& thing1,
295  const double& thing2 );
296 
297 }
298 
299 namespace std
300 {
301 
302  std::ostream& operator << ( ostream& os, const aims::SparseMatrix& thing );
303  bool operator == ( const aims::SparseMatrix& thing1,
304  const aims::SparseMatrix& thing2 );
305 
306 }
307 
308 
309 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
310 
311 namespace carto
312 {
313 
314  template<> class DataTypeCode<aims::SparseMatrix>
315  {
316  public:
317  static std::string objectType()
318  { return "SparseMatrix"; }
319  static std::string dataType()
320  { return "DOUBLE"; }
321  static std::string name()
322  {
323  return std::string("SparseMatrix_") + dataType();
324  }
325  };
326 
327 }
328 
329 #endif // DOXYGEN_HIDE_INTERNAL_CLASSES
330 
331 namespace carto
332 {
333 
335 
336 }
337 
338 #endif
#define DECLARE_GENERIC_OBJECT_TYPE(T)
void read(const std::string &filename, const std::string &openmode="binar", bool bswap=false)
Avoid using this function, prefer the more standard Reader<SparseMatrix> instead. ...
aims::SparseMatrix & operator*=(double x)
const_reference operator()(int32_t i, int32_t j) const
iterator1 begin1()
aims::SparseMatrix & operator+=(const aims::SparseMatrix &thing)
boost_sparse_matrix _matrix
Definition: sparseMatrix.h:171
SparseMatrix & operator=(const SparseMatrix &other)
iterator1 end1()
void erase_element(int32_t i, int32_t j)
iterator2 begin2()
aims::SparseMatrix & operator/=(double x)
STL namespace.
bool hasElement(int32_t i, int32_t j) const
The class for EcatSino data write operation.
Definition: border.h:44
boost_sparse_matrix::const_iterator1 const_iterator1
Definition: sparseMatrix.h:71
void setDiagonal(const double &value)
boost_sparse_matrix::reference reference
Definition: sparseMatrix.h:63
virtual ~SparseMatrix()
SparseMatrix getTransposition() const
const carto::Object header() const
Definition: sparseMatrix.h:166
boost_sparse_matrix::const_reference const_reference
Definition: sparseMatrix.h:65
int32_t getNonZeroElementCount() const
void reallocate(int32_t size1, int32_t size2)
int32_t getSize2() const
aims::SparseMatrix & operator-=(const aims::SparseMatrix &thing)
const boost_sparse_matrix & boostMatrix() const
Definition: sparseMatrix.h:163
VectorType getSparseColumn(int32_t i) const
Definition: sparseMatrix.h:211
carto::Object header()
Definition: sparseMatrix.h:165
void write(const std::string &filename, const std::string &openmode="binar", bool bswap=false) const
Avoid using this function, prefer the more standard Writer<SparseMatrix> instead. ...
boost_sparse_matrix::const_iterator2 const_iterator2
Definition: sparseMatrix.h:73
void setRow(int32_t s1, const std::vector< double > &row)
void setHeader(carto::Object ph)
boost::numeric::ublas::sparse_matrix< double > boost_sparse_matrix
Definition: sparseMatrix.h:52
boost_sparse_matrix::iterator1 iterator1
Definition: sparseMatrix.h:67
boost::numeric::ublas::sparse_vector< double > boost_sparse_vector
Definition: sparseMatrix.h:53
SparseMatrix getComposition(const SparseMatrix &other) const
int32_t getSize1() const
void setColumn(int32_t s2, const std::vector< double > &column)
boost_sparse_matrix & boostMatrix()
for low-level boost operations
Definition: sparseMatrix.h:162
aims::SparseMatrix operator/(const aims::SparseMatrix &thing1, const double &thing2)
aims::SparseMatrix operator+(const aims::SparseMatrix &thing)
aims::SparseMatrix operator-(const aims::SparseMatrix &thing)
double getTrace() const
bool isSquare() const
MotionWriter & operator<<(MotionWriter &writer, const AffineTransformation3d &thing) __attribute__((__deprecated__("OBSOLETE")))
— OBSOLETE —
Definition: motionW.h:87
std::vector< double > getRow(int32_t i) const
std::vector< double > toVector() const
iterator2 end2()
carto::Object _header
Definition: sparseMatrix.h:172
boost_sparse_matrix::iterator2 iterator2
Definition: sparseMatrix.h:68
void fill(const double &value)
SparseMatrix(int32_t size1=1, int32_t size2=1)
std::vector< double > getColumn(int32_t j) const
Quaternion operator*(const Quaternion &a, const Quaternion &b)
VectorType getSparseRow(int32_t i) const
Definition: sparseMatrix.h:179