OpenMS
Matrix.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- EKU Tuebingen, ETH Zurich, and FU Berlin
2 // SPDX-License-Identifier: BSD-3-Clause
3 //
4 // --------------------------------------------------------------------------
5 // $Maintainer: Timo Sachsenberg $
6 // $Authors: $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Macros.h>
12 
13 #include <cmath> // pow()
14 #include <iomanip>
15 #include <vector>
16 #include <iostream>
17 
18 namespace OpenMS
19 {
20 
48  template <typename Value>
49  class Matrix :
50  protected std::vector<Value>
51  {
52 protected:
53  typedef std::vector<Value> Base;
54 
55 public:
56 
58 
60 
61  typedef typename Base::difference_type difference_type;
62  typedef typename Base::size_type size_type;
63 
64  typedef typename Base::const_iterator const_iterator;
65  typedef typename Base::const_reverse_iterator const_reverse_iterator;
66  typedef typename Base::iterator iterator;
67  typedef typename Base::reverse_iterator reverse_iterator;
68 
69  typedef typename Base::const_reference const_reference;
70  typedef typename Base::pointer pointer;
71  typedef typename Base::reference reference;
72  typedef typename Base::value_type value_type;
73 
74  typedef typename Base::allocator_type allocator_type;
76 
78 
82 
85  typedef iterator Iterator;
87 
89  typedef pointer Pointer;
92 
95 
97 
98  Matrix() :
99  Base(),
100  rows_(0),
101  cols_(0)
102  {}
103 
104  Matrix(const SizeType rows, const SizeType cols, ValueType value = ValueType()) :
105  Base(rows * cols, value),
106  rows_(rows),
107  cols_(cols)
108  {}
109 
110  Matrix(const Matrix& source) :
111  Base(source),
112  rows_(source.rows_),
113  cols_(source.cols_)
114  {}
115 
116  Matrix& operator=(const Matrix& rhs)
117  {
118  Base::operator=(rhs);
119  rows_ = rhs.rows_;
120  cols_ = rhs.cols_;
121  return *this;
122  }
123 
124  ~Matrix() {}
126 
128 
130  {
131  return getValue(i, j);
132  }
133 
135  {
136  return getValue(i, j);
137  }
138 
139  const_reference getValue(size_type const i, size_type const j) const
140  {
141  return Base::operator[](index(i, j));
142  }
143 
145  {
146  return Base::operator[](index(i, j));
147  }
148 
149  void setValue(size_type const i, size_type const j, value_type value)
150  {
151  Base::operator[](index(i, j)) = value;
152  }
153 
155  container_type row(size_type const i) const
156  {
157 #ifdef OPENMS_DEBUG
158  if (i >= rows_) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, i, rows_);
159 #endif
160  container_type values(cols_);
161  for (size_type j = 0; j < cols_; j++)
162  {
163  values[j] = Base::operator[](index(i, j));
164  }
165  return values;
166  }
167 
169  container_type col(size_type const i) const
170  {
171 #ifdef OPENMS_DEBUG
172  if (i >= cols_) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, i, cols_);
173 #endif
174  container_type values(rows_);
175  for (size_type j = 0; j < rows_; j++)
176  {
177  values[j] = Base::operator[](index(j, i));
178  }
179  return values;
180  }
181 
183 
184 
191 public:
192 
193  using Base::begin;
194  using Base::end;
195  using Base::rbegin;
196  using Base::rend;
197 
198  using Base::front;
199  using Base::back;
200  using Base::assign;
201 
202  using Base::empty;
203  using Base::size;
204 
205  using Base::capacity;
206  using Base::max_size;
207 
209 
210  void clear()
211  {
212  Base::clear();
213  rows_ = 0;
214  cols_ = 0;
215  }
216 
218  {
219  rows_ = i;
220  cols_ = j;
221  Base::resize(rows_ * cols_, value);
222  }
223 
224  void resize(std::pair<Size, Size> const& size_pair, value_type value = value_type())
225  {
226  rows_ = size_pair.first;
227  cols_ = size_pair.second;
228  Base::resize(rows_ * cols_, value);
229  }
230 
232  SizeType rows() const
233  {
234  return rows_;
235  }
236 
238  SizeType cols() const
239  {
240  return cols_;
241  }
242 
243  std::pair<Size, Size> sizePair() const
244  {
245  return std::pair<Size, Size>(rows_, cols_);
246  }
247 
253  {
254 #ifdef OPENMS_DEBUG
255  if (row >= rows_) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, row, rows_);
256  if (col >= cols_) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, col, cols_);
257 #endif
258  return row * cols_ + col;
259  }
260 
265  std::pair<Size, Size> const indexPair(Size index) const
266  {
267 #ifdef OPENMS_DEBUG
268  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, index, size() - 1);
269 #endif
270  return std::pair<SizeType, SizeType>(index / cols_, index % cols_);
271  }
272 
278  {
279 #ifdef OPENMS_DEBUG
280  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, index, size() - 1);
281 #endif
282  return index % cols_;
283  }
284 
290  {
291 #ifdef OPENMS_DEBUG
292  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, index, size() - 1);
293 #endif
294 
295  return index / cols_;
296  }
297 
303  bool operator==(Matrix const& rhs) const
304  {
306  "Matrices have different row sizes.");
308  "Matrices have different column sizes.");
309  return static_cast<typename Matrix<Value>::Base const&>(*this) == static_cast<typename Matrix<Value>::Base const&>(rhs);
310  }
311 
317  bool operator<(Matrix const& rhs) const
318  {
320  "Matrices have different row sizes.");
322  "Matrices have different column sizes.");
323  return static_cast<typename Matrix<Value>::Base const&>(*this) < static_cast<typename Matrix<Value>::Base const&>(rhs);
324  }
325 
327  template <int ROWS, int COLS>
328  void setMatrix(const ValueType matrix[ROWS][COLS])
329  {
330  resize(ROWS, COLS);
331  for (SizeType i = 0; i < this->rows_; ++i)
332  {
333  for (SizeType j = 0; j < this->cols_; ++j)
334  {
335  setValue(i, j, matrix[i][j]);
336  }
337  }
338  }
339 
340  const Base& asVector()
341  {
342  return *this;
343  }
344 
345 protected:
346 
348 
349  SizeType rows_;
354 
355  }; // class Matrix
356 
362  template <typename Value>
363  std::ostream& operator<<(std::ostream& os, const Matrix<Value>& matrix)
364  {
365  typedef typename Matrix<Value>::size_type size_type;
366  for (size_type i = 0; i < matrix.rows(); ++i)
367  {
368  for (size_type j = 0; j < matrix.cols(); ++j)
369  {
370  os << std::setprecision(6) << std::setw(6) << matrix(i, j) << ' ';
371  }
372  os << std::endl;
373  }
374  return os;
375  }
376 
377 } // namespace OpenMS
378 
Int overflow exception.
Definition: Exception.h:221
A two-dimensional matrix. Similar to std::vector, but uses a binary operator(,) for element access.
Definition: Matrix.h:51
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:265
SizeType cols_
Number of columns (width of a row)
Definition: Matrix.h:352
Base::allocator_type allocator_type
Definition: Matrix.h:74
Base::iterator iterator
Definition: Matrix.h:66
const_reverse_iterator ConstReverseIterator
Definition: Matrix.h:84
Base::reverse_iterator reverse_iterator
Definition: Matrix.h:67
difference_type DifferenceType
Definition: Matrix.h:80
const Base & asVector()
Definition: Matrix.h:340
void resize(size_type i, size_type j, value_type value=value_type())
Definition: Matrix.h:217
std::ostream & operator<<(std::ostream &os, const Matrix< Value > &matrix)
Print the contents to a stream.
Definition: Matrix.h:363
~Matrix()
Definition: Matrix.h:124
container_type row(size_type const i) const
Return the i-th row of the matrix as a vector.
Definition: Matrix.h:155
Matrix(const SizeType rows, const SizeType cols, ValueType value=ValueType())
Definition: Matrix.h:104
Matrix()
Definition: Matrix.h:98
reverse_iterator ReverseIterator
Definition: Matrix.h:86
iterator Iterator
Definition: Matrix.h:85
reference Reference
Definition: Matrix.h:90
Base ContainerType
Definition: Matrix.h:79
container_type col(size_type const i) const
Return the i-th column of the matrix as a vector.
Definition: Matrix.h:169
reference operator()(size_type const i, size_type const j)
Definition: Matrix.h:134
Base::const_reverse_iterator const_reverse_iterator
Definition: Matrix.h:65
SizeType rows() const
Number of rows.
Definition: Matrix.h:232
const_iterator ConstIterator
Definition: Matrix.h:83
bool operator<(Matrix const &rhs) const
Less-than comparator. Comparison is done lexicographically: first by row, then by column.
Definition: Matrix.h:317
const_reference operator()(size_type const i, size_type const j) const
Definition: Matrix.h:129
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:252
Base::value_type value_type
Definition: Matrix.h:72
allocator_type AllocatorType
Definition: Matrix.h:93
value_type ValueType
Definition: Matrix.h:91
void resize(std::pair< Size, Size > const &size_pair, value_type value=value_type())
Definition: Matrix.h:224
bool operator==(Matrix const &rhs) const
Equality comparator.
Definition: Matrix.h:303
Base container_type
Definition: Matrix.h:59
Base::pointer pointer
Definition: Matrix.h:70
Base::difference_type difference_type
Definition: Matrix.h:61
std::pair< Size, Size > sizePair() const
Definition: Matrix.h:243
Matrix(const Matrix &source)
Definition: Matrix.h:110
size_type SizeType
Definition: Matrix.h:81
const_reference getValue(size_type const i, size_type const j) const
Definition: Matrix.h:139
Base::const_reference const_reference
Definition: Matrix.h:69
const_reference ConstReference
Definition: Matrix.h:88
std::vector< Value > Base
Definition: Matrix.h:53
SizeType cols() const
Number of columns.
Definition: Matrix.h:238
Base::reference reference
Definition: Matrix.h:71
SizeType rows_
Number of rows (height of a column)
Definition: Matrix.h:350
void clear()
Definition: Matrix.h:210
pointer Pointer
Definition: Matrix.h:89
void setValue(size_type const i, size_type const j, value_type value)
Definition: Matrix.h:149
Base::size_type size_type
Definition: Matrix.h:62
SizeType colIndex(SizeType index) const
Calculate the column from an index into the underlying vector. Note that Matrix uses the (row,...
Definition: Matrix.h:277
Matrix & operator=(const Matrix &rhs)
Definition: Matrix.h:116
Base::const_iterator const_iterator
Definition: Matrix.h:64
SizeType rowIndex(SizeType index) const
Calculate the row from an index into the underlying vector. Note that Matrix uses the (row,...
Definition: Matrix.h:289
void setMatrix(const ValueType matrix[ROWS][COLS])
set matrix to 2D arrays values
Definition: Matrix.h:328
reference getValue(size_type const i, size_type const j)
Definition: Matrix.h:144
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:94
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22