aimsdata 6.0.0
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
44namespace 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
80
81 void reallocate( int32_t size1, int32_t size2 );
82
85
88
91
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;
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;
157 operator *= ( double x );
159 operator /= ( double x );
160
163 const boost_sparse_matrix & boostMatrix() const { return _matrix; }
164
166 const carto::Object header() const { return _header; }
168
169 protected:
170
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
243
244// - SparseMatrix
247
248//
249// binary operator +
250//
251
252// SparseMatrix + SparseMatrix
255 const aims::SparseMatrix& thing2 );
256
257
258//
259// binary operator -
260//
261
262// SparseMatrix - SparseMatrix
265 const aims::SparseMatrix& thing2 );
266
267
268//
269// binary operator *
270//
271
272// SparseMatrix * SparseMatrix
275 const aims::SparseMatrix& thing2 );
276
277// SparseMatrix * std::vector
278std::vector<double>
280 const std::vector<double>& thing2 );
281
282// SparseMatrix * double
285 const double& thing2 );
286
287
288//
289// binary operator /
290//
291
292// SparseMatrix / double
295 const double& thing2 );
296
297}
298
299namespace 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
311namespace 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
331namespace carto
332{
333
334DECLARE_GENERIC_OBJECT_TYPE( aims::SparseMatrix )
335
336}
337
338#endif
aims::SparseMatrix & operator/=(double x)
double getTrace() const
const boost_sparse_matrix & boostMatrix() const
boost_sparse_matrix & boostMatrix()
for low-level boost operations
carto::Object _header
const_iterator2 end2() const
const_iterator1 begin1() const
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.
void setHeader(carto::Object ph)
void reallocate(int32_t size1, int32_t size2)
SparseMatrix(int32_t size1=1, int32_t size2=1)
void fill(const double &value)
const_reference operator()(int32_t i, int32_t j) const
const carto::Object header() const
aims::SparseMatrix & operator-=(const aims::SparseMatrix &thing)
carto::Object header()
int32_t getSize1() const
iterator2 end2()
aims::SparseMatrix & operator*=(double x)
SparseMatrix getTransposition() const
std::vector< double > getRow(int32_t i) const
VectorType getSparseRow(int32_t i) const
boost_sparse_matrix::iterator2 iterator2
boost_sparse_matrix::const_reference const_reference
reference operator()(int32_t i, int32_t j)
const_iterator1 end1() const
void fill(int32_t offset1, int32_t offset2, const SparseMatrix &other, int32_t size1=0, int32_t size2=0)
void fill(int32_t offset1, int32_t offset2, int32_t size1, int32_t size2, const double &value)
boost_sparse_matrix::iterator1 iterator1
boost_sparse_matrix _matrix
void setColumn(int32_t s2, const std::vector< double > &column)
void setRow(int32_t s1, const std::vector< double > &row)
aims::SparseMatrix & operator+=(const aims::SparseMatrix &thing)
boost_sparse_matrix::reference reference
boost_sparse_matrix::const_iterator1 const_iterator1
virtual ~SparseMatrix()
std::vector< double > toVector() const
void setDiagonal(const double &value)
SparseMatrix & operator=(const SparseMatrix &other)
bool isSquare() const
boost_sparse_matrix::const_iterator2 const_iterator2
std::vector< double > getComposition(const std::vector< double > &other) const
bool hasElement(int32_t i, int32_t j) const
const_iterator2 begin2() const
std::vector< double > getColumn(int32_t j) const
iterator1 end1()
iterator2 begin2()
VectorType getSparseColumn(int32_t i) const
int32_t getSize2() const
void erase_element(int32_t i, int32_t j)
SparseMatrix(const SparseMatrix &other)
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.
iterator1 begin1()
SparseMatrix getComposition(const SparseMatrix &other) const
int32_t getNonZeroElementCount() const
std::string dataType()
The class for EcatSino data write operation.
aims::SparseMatrix operator+(const aims::SparseMatrix &thing)
boost::numeric::ublas::sparse_matrix< double > boost_sparse_matrix
aims::SparseMatrix operator/(const aims::SparseMatrix &thing1, const double &thing2)
boost::numeric::ublas::sparse_vector< double > boost_sparse_vector
Quaternion operator*(const Quaternion &a, const Quaternion &b)
aims::SparseMatrix operator-(const aims::SparseMatrix &thing)
STL namespace.
std::ostream & operator<<(ostream &os, const aims::SparseMatrix &thing)
bool operator==(const aims::SparseMatrix &thing1, const aims::SparseMatrix &thing2)
#define DECLARE_GENERIC_OBJECT_TYPE(T)