Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
Matrix.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2013.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Erhan Kenar $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_DATASTRUCTURES_MATRIX_H
36 #define OPENMS_DATASTRUCTURES_MATRIX_H
37 
38 #include <OpenMS/CONCEPT/Macros.h>
39 
40 #include <cmath> // pow()
41 #include <iomanip>
42 #include <vector>
43 
44 #include <gsl/gsl_matrix.h>
45 #include <gsl/gsl_vector.h>
46 #include <gsl/gsl_linalg.h>
47 
48 namespace OpenMS
49 {
50 
78  template <typename Value>
79  class Matrix :
80  protected std::vector<Value>
81  {
82 protected:
83  typedef std::vector<Value> Base;
84 
85 public:
86 
88 
90 
91  typedef typename Base::difference_type difference_type;
92  typedef typename Base::size_type size_type;
93 
94  typedef typename Base::const_iterator const_iterator;
95  typedef typename Base::const_reverse_iterator const_reverse_iterator;
96  typedef typename Base::iterator iterator;
97  typedef typename Base::reverse_iterator reverse_iterator;
98 
99  typedef typename Base::const_reference const_reference;
100  typedef typename Base::pointer pointer;
101  typedef typename Base::reference reference;
102  typedef typename Base::value_type value_type;
103 
104  typedef typename Base::allocator_type allocator_type;
106 
108 
112 
117 
119  typedef pointer Pointer;
122 
125 
127 
128  Matrix() :
129  Base(),
130  rows_(0),
131  cols_(0)
132  {}
133 
134  Matrix(const SizeType rows, const SizeType cols, ValueType value = ValueType()) :
135  Base(rows * cols, value),
136  rows_(rows),
137  cols_(cols)
138  {}
139 
140  Matrix(const Matrix& source) :
141  Base(source),
142  rows_(source.rows_),
143  cols_(source.cols_)
144  {}
145 
146  Matrix& operator=(const Matrix& rhs)
147  {
148  Base::operator=(rhs);
149  rows_ = rhs.rows_;
150  cols_ = rhs.cols_;
151  return *this;
152  }
153 
154  ~Matrix() {}
156 
158 
160  {
161  return getValue(i, j);
162  }
163 
165  {
166  return getValue(i, j);
167  }
168 
169  const_reference getValue(size_type const i, size_type const j) const
170  {
171  return Base::operator[](index(i, j));
172  }
173 
175  {
176  return Base::operator[](index(i, j));
177  }
178 
179  void setValue(size_type const i, size_type const j, value_type value)
180  {
181  Base::operator[](index(i, j)) = value;
182  }
183 
185  container_type row(size_type const i) const
186  {
187 #ifdef OPENMS_DEBUG
188  if (i >= rows_) throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__, i, rows_);
189 #endif
190  container_type values(cols_);
191  for (size_type j = 0; j < cols_; j++)
192  {
193  values[j] = Base::operator[](index(i, j));
194  }
195  return values;
196  }
197 
199  container_type col(size_type const i) const
200  {
201 #ifdef OPENMS_DEBUG
202  if (i >= cols_) throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__, i, cols_);
203 #endif
204  container_type values(rows_);
205  for (size_type j = 0; j < rows_; j++)
206  {
207  values[j] = Base::operator[](index(j, i));
208  }
209  return values;
210  }
211 
213 
214 
221 public:
222 
223  using Base::begin;
224  using Base::end;
225  using Base::rbegin;
226  using Base::rend;
227 
228  using Base::front;
229  using Base::back;
230  using Base::assign;
231 
232  using Base::empty;
233  using Base::size;
234 
235  using Base::capacity;
236  using Base::max_size;
237 
239 
240  void clear()
241  {
242  Base::clear();
243  rows_ = 0;
244  cols_ = 0;
245  }
246 
248  {
249  rows_ = i;
250  cols_ = j;
251  Base::resize(rows_ * cols_, value);
252  }
253 
254  void resize(std::pair<Size, Size> const& size_pair, value_type value = value_type())
255  {
256  rows_ = size_pair.first;
257  cols_ = size_pair.second;
258  Base::resize(rows_ * cols_, value);
259  }
260 
262  SizeType rows() const
263  {
264  return rows_;
265  }
266 
268  SizeType cols() const
269  {
270  return cols_;
271  }
272 
273  std::pair<Size, Size> sizePair() const
274  {
275  return std::pair<Size, Size>(rows_, cols_);
276  }
277 
283  {
284 #ifdef OPENMS_DEBUG
285  if (row >= rows_) throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__, row, rows_);
286  if (col >= cols_) throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__, col, cols_);
287 #endif
288  return row * cols_ + col;
289  }
290 
295  std::pair<Size, Size> const indexPair(Size index) const
296  {
297 #ifdef OPENMS_DEBUG
298  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__, index, size() - 1);
299 #endif
300  return std::pair<SizeType, SizeType>(index / cols_, index % cols_);
301  }
302 
308  {
309 #ifdef OPENMS_DEBUG
310  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__, index, size() - 1);
311 #endif
312  return index % cols_;
313  }
314 
320  {
321 #ifdef OPENMS_DEBUG
322  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__, index, size() - 1);
323 #endif
324 
325  return index / cols_;
326  }
327 
333  bool operator==(Matrix const& rhs) const
334  {
336  "Matrices have different row sizes.");
338  "Matrices have different column sizes.");
339  return static_cast<typename Matrix<Value>::Base const&>(*this) == static_cast<typename Matrix<Value>::Base const&>(rhs);
340  }
341 
347  bool operator<(Matrix const& rhs) const
348  {
350  "Matrices have different row sizes.");
352  "Matrices have different column sizes.");
353  return static_cast<typename Matrix<Value>::Base const&>(*this) < static_cast<typename Matrix<Value>::Base const&>(rhs);
354  }
355 
357  template <int ROWS, int COLS>
358  void setMatrix(const ValueType matrix[ROWS][COLS])
359  {
360  resize(ROWS, COLS);
361  for (SizeType i = 0; i < this->rows_; ++i)
362  {
363  for (SizeType j = 0; j < this->cols_; ++j)
364  {
365  setValue(i, j, matrix[i][j]);
366  }
367  }
368  }
369 
377  gsl_matrix* toGslMatrix()
378  {
379  gsl_matrix* m_ptr = gsl_matrix_alloc(rows_, cols_);
380 
381  for (size_type i = 0; i < this->rows_; ++i)
382  {
383  for (size_type j = 0; j < this->cols_; ++j)
384  {
385  gsl_matrix_set(m_ptr, i, j, (double) (*this)(i, j));
386  }
387  }
388 
389  return m_ptr;
390  }
391 
392 protected:
393 
395 
396  SizeType rows_;
401 
402  }; // class Matrix
403 
404  // template<> OPENMS_DLLAPI gsl_matrix* Matrix<double>::toGslMatrix();
405  // template<> OPENMS_DLLAPI gsl_matrix* Matrix<float>::toGslMatrix();
406 
412  template <typename Value>
413  std::ostream& operator<<(std::ostream& os, const Matrix<Value>& matrix)
414  {
415  typedef typename Matrix<Value>::size_type size_type;
416  for (size_type i = 0; i < matrix.rows(); ++i)
417  {
418  for (size_type j = 0; j < matrix.cols(); ++j)
419  {
420  os << std::setprecision(6) << std::setw(6) << matrix(i, j) << ' ';
421  }
422  os << std::endl;
423  }
424  return os;
425  }
426 
427 } // namespace OpenMS
428 
429 #endif // OPENMS_DATASTRUCTURES_MATRIX_H
Base::value_type value_type
Definition: Matrix.h:102
std::pair< Size, Size > sizePair() const
Definition: Matrix.h:273
Base ContainerType
Definition: Matrix.h:109
const_iterator ConstIterator
Definition: Matrix.h:113
allocator_type AllocatorType
Definition: Matrix.h:123
value_type ValueType
Definition: Matrix.h:121
std::pair< Size, Size > const indexPair(Size index) const
Calculate the row and column from an index into the underlying vector. Note that Matrix uses the (row...
Definition: Matrix.h:295
SizeType rowIndex(SizeType index) const
Calculate the row from an index into the underlying vector. Note that Matrix uses the (row...
Definition: Matrix.h:319
Base::const_reference const_reference
Definition: Matrix.h:99
const_reference ConstReference
Definition: Matrix.h:118
Int overflow exception.
Definition: Exception.h:255
iterator Iterator
Definition: Matrix.h:115
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: Macros.h:107
Base::pointer pointer
Definition: Matrix.h:100
SizeType colIndex(SizeType index) const
Calculate the column from an index into the underlying vector. Note that Matrix uses the (row...
Definition: Matrix.h:307
bool operator==(Matrix const &rhs) const
Equality comparator.
Definition: Matrix.h:333
pointer Pointer
Definition: Matrix.h:119
gsl_matrix * toGslMatrix()
create gsl_matrix*
Definition: Matrix.h:377
container_type col(size_type const i) const
Return the i-th column of the matrix as a vector.
Definition: Matrix.h:199
reverse_iterator ReverseIterator
Definition: Matrix.h:116
Base::difference_type difference_type
Definition: Matrix.h:91
size_type SizeType
Definition: Matrix.h:111
void resize(size_type i, size_type j, value_type value=value_type())
Definition: Matrix.h:247
~Matrix()
Definition: Matrix.h:154
const_reference getValue(size_type const i, size_type const j) const
Definition: Matrix.h:169
container_type row(size_type const i) const
Return the i-th row of the matrix as a vector.
Definition: Matrix.h:185
Base::const_iterator const_iterator
Definition: Matrix.h:94
Matrix(const SizeType rows, const SizeType cols, ValueType value=ValueType())
Definition: Matrix.h:134
Base::iterator iterator
Definition: Matrix.h:96
bool operator<(Matrix const &rhs) const
Less-than comparator. Comparison is done lexicographically: first by row, then by column...
Definition: Matrix.h:347
std::vector< Value > Base
Definition: Matrix.h:83
void resize(std::pair< Size, Size > const &size_pair, value_type value=value_type())
Definition: Matrix.h:254
Matrix & operator=(const Matrix &rhs)
Definition: Matrix.h:146
reference Reference
Definition: Matrix.h:120
SizeType cols_
Number of columns (width of a row)
Definition: Matrix.h:399
SizeType rows() const
Number of rows.
Definition: Matrix.h:262
SizeType cols() const
Number of columns.
Definition: Matrix.h:268
Base::reverse_iterator reverse_iterator
Definition: Matrix.h:97
difference_type DifferenceType
Definition: Matrix.h:110
Base::reference reference
Definition: Matrix.h:101
Base::allocator_type allocator_type
Definition: Matrix.h:104
const_reverse_iterator ConstReverseIterator
Definition: Matrix.h:114
reference operator()(size_type const i, size_type const j)
Definition: Matrix.h:164
Base::size_type size_type
Definition: Matrix.h:92
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:144
Base container_type
Definition: Matrix.h:89
void setMatrix(const ValueType matrix[ROWS][COLS])
set matrix to 2D arrays values
Definition: Matrix.h:358
void setValue(size_type const i, size_type const j, value_type value)
Definition: Matrix.h:179
Base::const_reverse_iterator const_reverse_iterator
Definition: Matrix.h:95
reference getValue(size_type const i, size_type const j)
Definition: Matrix.h:174
Matrix(const Matrix &source)
Definition: Matrix.h:140
SizeType const index(SizeType row, SizeType col) const
Calculate the index into the underlying vector from row and column. Note that Matrix uses the (row...
Definition: Matrix.h:282
Matrix()
Definition: Matrix.h:128
void clear()
Definition: Matrix.h:240
A two-dimensional matrix. Similar to std::vector, but uses a binary operator(,) for element access...
Definition: Matrix.h:79
const_reference operator()(size_type const i, size_type const j) const
Definition: Matrix.h:159
SizeType rows_
Number of rows (height of a column)
Definition: Matrix.h:397

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:17 using doxygen 1.8.5