primatologist-gpl  5.0.5
matrix.h
Go to the documentation of this file.
1 /* Copyright (C) 2000-2013 CEA
2  *
3  * This software and supporting documentation were developed by
4  * bioPICSEL
5  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
6  * 18, route du Panorama
7  * 92265 Fontenay-aux-Roses
8  * France
9  */
10 
11 #ifndef PRIMATOLOGIST_MATH_MATRIX_H
12 #define PRIMATOLOGIST_MATH_MATRIX_H
13 
14 #include <cartodata/volume/volume.h> // carto::VolumeRef
15 #include <cartobase/type/string_conversion.h> // carto::toString
16 #include <exception> // std::logic_error
17 #include <string> // std::string
18 #include <vector> // std::vector
19 
20 namespace aims {
21 namespace math {
22 
23  //==========================================================================
24  // MATRIX TYPEDEFS
25  //==========================================================================
26  template <typename T> class MatrixBase;
27  typedef MatrixBase<float> Matrix;
31 
32  //==========================================================================
33  // UTILITY FUNCTIONS
34  //==========================================================================
37  template <typename T>
38  MatrixBase<T> asMatrix( carto::VolumeRef<T> & volume );
39 
40  //==========================================================================
41  // BASE CLASS FOR MATRICES
42  //==========================================================================
48  template <typename T>
49  class MatrixBase: public carto::VolumeRef<T>
50  {
51  public:
52  //------------------------------------------------------------------------
53  // CONSTRUCTOR / DESTRUCTOR / COPY
54  //------------------------------------------------------------------------
57  MatrixBase( int nrow = 1, int ncol = 1 );
64  template <typename U>
65  MatrixBase( const carto::VolumeRef<U> & volume );
67  template <typename U>
68  MatrixBase( const std::vector<U> & vector );
70  ~MatrixBase();
73  template <typename U>
74  MatrixBase<T> & operator= ( const carto::VolumeRef<U> & matrix );
76  template <typename U>
77  MatrixBase<T> & operator= ( const std::vector<U> & vector );
78  //------------------------------------------------------------------------
79  // CONVERSIONS
80  //------------------------------------------------------------------------
82  template <typename U>
83  operator MatrixBase<U>() const;
84  //------------------------------------------------------------------------
85  // MATRIX OPERATIONS
86  //------------------------------------------------------------------------
90  template <typename U>
102  MatrixBase<T> & invert();
105  double trace() const;
106  //------------------------------------------------------------------------
107  // SIZE
108  //------------------------------------------------------------------------
110  int nrow() const;
112  int ncol() const;
114  int size() const;
115 
116  //------------------------------------------------------------------------
117  // PRIVATE SHARED MEMORY CONSTRUCTOR
118  //------------------------------------------------------------------------
119  friend MatrixBase<T> asMatrix<>( carto::VolumeRef<T> & );
120  private:
121  MatrixBase( carto::VolumeRef<T> & );
122  };
123 
124  //==========================================================================
125  // MATRIX OPERATIONS
126  //==========================================================================
130  template <typename T, typename U>
132  operator* ( const MatrixBase<T> & a, const MatrixBase<U> & b );
135  template <typename T>
140  template <typename T>
142 
143  //==========================================================================
144  // OVERLOAD OPERATORS
145  //==========================================================================
148  template <typename T, typename U>
149  bool operator== ( const MatrixBase<T> & vol, const MatrixBase<U> & other );
152  template <typename T, typename U>
153  bool operator!= ( const MatrixBase<T> & vol, const MatrixBase<U> & other );
155  template <typename T>
156  MatrixBase<T> operator- ( const MatrixBase<T> & vol );
158  template <typename T, typename U>
160  operator+ ( const MatrixBase<T> & vol, const U & value );
162  template <typename T, typename U>
164  operator- ( const MatrixBase<T> & vol, const U & value );
166  template <typename T, typename U>
168  operator* ( const MatrixBase<T> & vol, const U & value );
170  template <typename T, typename U>
172  operator/ ( const MatrixBase<T> & vol, const U & value );
173  // Scalar + Matrix
174  template <typename T, typename U>
176  operator+ ( const U & value, const MatrixBase<T> & vol );
177  // Scalar - Matrix
178  template <typename T, typename U>
180  operator- ( const U & value, const MatrixBase<T> & vol );
181  // Scalar * Matrix
182  template <typename T, typename U>
184  operator* ( const U & value, const MatrixBase<T> & vol );
185  // Scalar / Matrix
186  template <typename T, typename U>
188  operator/ ( const U & value, const MatrixBase<T> & vol );
189  // Matrix + Matrix
190  template <typename T, typename U>
192  operator+ ( const MatrixBase<T> & vol, const MatrixBase<U> & other );
193  // Matrix - Matrix
194  template <typename T, typename U>
196  operator- ( const MatrixBase<T> & vol, const MatrixBase<U> & other );
197 
198  //==========================================================================
199  // STREAM / WRITE
200  //==========================================================================
202  template <typename T>
203  std::ostream & operator<< ( std::ostream & out,
204  const MatrixBase<T> & matrix );
206  template <typename T>
207  void write( const MatrixBase<T> & matrix, const std::string & file );
208 
209  //==========================================================================
210  // EXCEPTION CLASS
211  //==========================================================================
214  class incompatible_matrix_exception: public std::logic_error
215  {
216  public:
218  std::logic_error( std::string("[aims::math::MatrixBase] Matrices "
219  "should have compatible numbers of rows and columns for a matrix "
220  "product. (") + carto::toString(ncol) + std::string(" and ") +
221  carto::toString(nrow) + std::string(").") )
222  {}
223  };
224 
225 } // namespace math
226 } // namespace aims
227 
228 #endif // PRIMATOLOGIST_MATH_MATRIX_H
MatrixBase< T > & invert()
Matrix inversion.
Definition: matrix_d.h:175
int size() const
Number of elements.
Definition: matrix_d.h:317
STL namespace.
MatrixBase< T > asMatrix(carto::VolumeRef< T > &volume)
Interprets the volume as a matrix.
Definition: matrix_d.h:341
MatrixBase< T > operator-(const MatrixBase< T > &vol)
Returns a Matrix filled with opposite elements.
Definition: matrix_d.h:214
MatrixBase< double > VectorD
Definition: matrix.h:30
int nrow() const
Number of rows.
Definition: matrix_d.h:305
Matrix class implementing matrix operations.
Definition: matrix.h:26
This exception is thrown when an attempt is made at multiplying two matrices with incompatible dimens...
Definition: matrix.h:214
MatrixBase< typename carto::volumeutil::divides< T, U >::result_type > operator/(const MatrixBase< T > &vol, const U &value)
Matrix / Scalar.
Definition: matrix_d.h:246
MatrixBase< float > Vector
Definition: matrix.h:29
int ncol() const
Number of columns.
Definition: matrix_d.h:311
MatrixBase< double > MatrixD
Definition: matrix.h:28
MatrixBase< T > & transpose()
Matrix transposition.
Definition: matrix_d.h:158
double trace() const
Returns the trace of the matrix, i.e.
bool operator==(const MatrixBase< T > &vol, const MatrixBase< U > &other)
Returns true if the dimensions are the same and if all elements are equal.
Definition: matrix_d.h:196
~MatrixBase()
Destructor.
Definition: matrix_d.h:53
MatrixBase< T > & operator=(const carto::VolumeRef< U > &matrix)
Assignment operator The content from other is entirely copied.
Definition: matrix_d.h:84
MatrixBase(int nrow=1, int ncol=1)
Standard constructor Allocate a matrix of size nrow x ncol.
Definition: matrix_d.h:30
MatrixBase< float > Matrix
Definition: matrix.h:26
MatrixBase< typename carto::volumeutil::plus< T, U >::result_type > operator+(const MatrixBase< T > &vol, const U &value)
Matrix + Scalar.
Definition: matrix_d.h:222
bool operator!=(const MatrixBase< T > &vol, const MatrixBase< U > &other)
Returns false if dimensions are not the same or if any couple of elements is not equal.
Definition: matrix_d.h:208
MatrixBase< T > & operator*=(const MatrixBase< U > &)
Matrix product.
Definition: matrix_d.h:129
incompatible_matrix_exception(int nrow, int ncol)
Definition: matrix.h:217
MatrixBase< typename carto::volumeutil::multiplies< T, U >::result_type > operator*(const MatrixBase< T > &a, const MatrixBase< U > &b)
Matrix product.
Definition: matrix_d.h:137
void write(const MatrixBase< T > &matrix, const std::string &file)
Write the matrix content in an image file.
std::ostream & operator<<(std::ostream &out, const MatrixBase< T > &matrix)
Print the matrix content on the standard output.
Definition: matrix_d.h:327