OpenMS  2.8.0
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-2021.
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: Timo Sachsenberg $
32 // $Authors: $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Macros.h>
38 
39 #include <cmath> // pow()
40 #include <iomanip>
41 #include <vector>
42 #include <iostream>
43 
44 namespace OpenMS
45 {
46 
74  template <typename Value>
75  class Matrix :
76  protected std::vector<Value>
77  {
78 protected:
79  typedef std::vector<Value> Base;
80 
81 public:
82 
84 
86 
87  typedef typename Base::difference_type difference_type;
88  typedef typename Base::size_type size_type;
89 
90  typedef typename Base::const_iterator const_iterator;
91  typedef typename Base::const_reverse_iterator const_reverse_iterator;
92  typedef typename Base::iterator iterator;
93  typedef typename Base::reverse_iterator reverse_iterator;
94 
95  typedef typename Base::const_reference const_reference;
96  typedef typename Base::pointer pointer;
97  typedef typename Base::reference reference;
98  typedef typename Base::value_type value_type;
99 
100  typedef typename Base::allocator_type allocator_type;
102 
104 
108 
113 
115  typedef pointer Pointer;
118 
121 
123 
124  Matrix() :
125  Base(),
126  rows_(0),
127  cols_(0)
128  {}
129 
130  Matrix(const SizeType rows, const SizeType cols, ValueType value = ValueType()) :
131  Base(rows * cols, value),
132  rows_(rows),
133  cols_(cols)
134  {}
135 
136  Matrix(const Matrix& source) :
137  Base(source),
138  rows_(source.rows_),
139  cols_(source.cols_)
140  {}
141 
142  Matrix& operator=(const Matrix& rhs)
143  {
144  Base::operator=(rhs);
145  rows_ = rhs.rows_;
146  cols_ = rhs.cols_;
147  return *this;
148  }
149 
150  ~Matrix() {}
152 
154 
156  {
157  return getValue(i, j);
158  }
159 
161  {
162  return getValue(i, j);
163  }
164 
165  const_reference getValue(size_type const i, size_type const j) const
166  {
167  return Base::operator[](index(i, j));
168  }
169 
171  {
172  return Base::operator[](index(i, j));
173  }
174 
175  void setValue(size_type const i, size_type const j, value_type value)
176  {
177  Base::operator[](index(i, j)) = value;
178  }
179 
181  container_type row(size_type const i) const
182  {
183 #ifdef OPENMS_DEBUG
184  if (i >= rows_) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, i, rows_);
185 #endif
186  container_type values(cols_);
187  for (size_type j = 0; j < cols_; j++)
188  {
189  values[j] = Base::operator[](index(i, j));
190  }
191  return values;
192  }
193 
195  container_type col(size_type const i) const
196  {
197 #ifdef OPENMS_DEBUG
198  if (i >= cols_) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, i, cols_);
199 #endif
200  container_type values(rows_);
201  for (size_type j = 0; j < rows_; j++)
202  {
203  values[j] = Base::operator[](index(j, i));
204  }
205  return values;
206  }
207 
209 
210 
217 public:
218 
219  using Base::begin;
220  using Base::end;
221  using Base::rbegin;
222  using Base::rend;
223 
224  using Base::front;
225  using Base::back;
226  using Base::assign;
227 
228  using Base::empty;
229  using Base::size;
230 
231  using Base::capacity;
232  using Base::max_size;
233 
235 
236  void clear()
237  {
238  Base::clear();
239  rows_ = 0;
240  cols_ = 0;
241  }
242 
244  {
245  rows_ = i;
246  cols_ = j;
247  Base::resize(rows_ * cols_, value);
248  }
249 
250  void resize(std::pair<Size, Size> const& size_pair, value_type value = value_type())
251  {
252  rows_ = size_pair.first;
253  cols_ = size_pair.second;
254  Base::resize(rows_ * cols_, value);
255  }
256 
258  SizeType rows() const
259  {
260  return rows_;
261  }
262 
264  SizeType cols() const
265  {
266  return cols_;
267  }
268 
269  std::pair<Size, Size> sizePair() const
270  {
271  return std::pair<Size, Size>(rows_, cols_);
272  }
273 
279  {
280 #ifdef OPENMS_DEBUG
281  if (row >= rows_) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, row, rows_);
282  if (col >= cols_) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, col, cols_);
283 #endif
284  return row * cols_ + col;
285  }
286 
291  std::pair<Size, Size> const indexPair(Size index) const
292  {
293 #ifdef OPENMS_DEBUG
294  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, index, size() - 1);
295 #endif
296  return std::pair<SizeType, SizeType>(index / cols_, index % cols_);
297  }
298 
304  {
305 #ifdef OPENMS_DEBUG
306  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, index, size() - 1);
307 #endif
308  return index % cols_;
309  }
310 
316  {
317 #ifdef OPENMS_DEBUG
318  if (index >= size()) throw Exception::IndexOverflow(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, index, size() - 1);
319 #endif
320 
321  return index / cols_;
322  }
323 
329  bool operator==(Matrix const& rhs) const
330  {
332  "Matrices have different row sizes.");
334  "Matrices have different column sizes.");
335  return static_cast<typename Matrix<Value>::Base const&>(*this) == static_cast<typename Matrix<Value>::Base const&>(rhs);
336  }
337 
343  bool operator<(Matrix const& rhs) const
344  {
346  "Matrices have different row sizes.");
348  "Matrices have different column sizes.");
349  return static_cast<typename Matrix<Value>::Base const&>(*this) < static_cast<typename Matrix<Value>::Base const&>(rhs);
350  }
351 
353  template <int ROWS, int COLS>
354  void setMatrix(const ValueType matrix[ROWS][COLS])
355  {
356  resize(ROWS, COLS);
357  for (SizeType i = 0; i < this->rows_; ++i)
358  {
359  for (SizeType j = 0; j < this->cols_; ++j)
360  {
361  setValue(i, j, matrix[i][j]);
362  }
363  }
364  }
365 
366  const Base& asVector()
367  {
368  return *this;
369  }
370 
371 protected:
372 
374 
375  SizeType rows_;
380 
381  }; // class Matrix
382 
388  template <typename Value>
389  std::ostream& operator<<(std::ostream& os, const Matrix<Value>& matrix)
390  {
391  typedef typename Matrix<Value>::size_type size_type;
392  for (size_type i = 0; i < matrix.rows(); ++i)
393  {
394  for (size_type j = 0; j < matrix.cols(); ++j)
395  {
396  os << std::setprecision(6) << std::setw(6) << matrix(i, j) << ' ';
397  }
398  os << std::endl;
399  }
400  return os;
401  }
402 
403 } // namespace OpenMS
404 
Int overflow exception.
Definition: Exception.h:247
A two-dimensional matrix. Similar to std::vector, but uses a binary operator(,) for element access.
Definition: Matrix.h:77
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:291
SizeType cols_
Number of columns (width of a row)
Definition: Matrix.h:378
Base::allocator_type allocator_type
Definition: Matrix.h:100
Base::iterator iterator
Definition: Matrix.h:92
const_reverse_iterator ConstReverseIterator
Definition: Matrix.h:110
Base::reverse_iterator reverse_iterator
Definition: Matrix.h:93
difference_type DifferenceType
Definition: Matrix.h:106
const Base & asVector()
Definition: Matrix.h:366
void resize(size_type i, size_type j, value_type value=value_type())
Definition: Matrix.h:243
std::ostream & operator<<(std::ostream &os, const Matrix< Value > &matrix)
Print the contents to a stream.
Definition: Matrix.h:389
~Matrix()
Definition: Matrix.h:150
container_type row(size_type const i) const
Return the i-th row of the matrix as a vector.
Definition: Matrix.h:181
Matrix(const SizeType rows, const SizeType cols, ValueType value=ValueType())
Definition: Matrix.h:130
Matrix()
Definition: Matrix.h:124
reverse_iterator ReverseIterator
Definition: Matrix.h:112
iterator Iterator
Definition: Matrix.h:111
reference Reference
Definition: Matrix.h:116
Base ContainerType
Definition: Matrix.h:105
container_type col(size_type const i) const
Return the i-th column of the matrix as a vector.
Definition: Matrix.h:195
reference operator()(size_type const i, size_type const j)
Definition: Matrix.h:160
Base::const_reverse_iterator const_reverse_iterator
Definition: Matrix.h:91
SizeType rows() const
Number of rows.
Definition: Matrix.h:258
const_iterator ConstIterator
Definition: Matrix.h:109
bool operator<(Matrix const &rhs) const
Less-than comparator. Comparison is done lexicographically: first by row, then by column.
Definition: Matrix.h:343
const_reference operator()(size_type const i, size_type const j) const
Definition: Matrix.h:155
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:278
Base::value_type value_type
Definition: Matrix.h:98
allocator_type AllocatorType
Definition: Matrix.h:119
value_type ValueType
Definition: Matrix.h:117
void resize(std::pair< Size, Size > const &size_pair, value_type value=value_type())
Definition: Matrix.h:250
bool operator==(Matrix const &rhs) const
Equality comparator.
Definition: Matrix.h:329
Base container_type
Definition: Matrix.h:85
Base::pointer pointer
Definition: Matrix.h:96
Base::difference_type difference_type
Definition: Matrix.h:87
std::pair< Size, Size > sizePair() const
Definition: Matrix.h:269
Matrix(const Matrix &source)
Definition: Matrix.h:136
size_type SizeType
Definition: Matrix.h:107
const_reference getValue(size_type const i, size_type const j) const
Definition: Matrix.h:165
Base::const_reference const_reference
Definition: Matrix.h:95
const_reference ConstReference
Definition: Matrix.h:114
std::vector< Value > Base
Definition: Matrix.h:79
SizeType cols() const
Number of columns.
Definition: Matrix.h:264
Base::reference reference
Definition: Matrix.h:97
SizeType rows_
Number of rows (height of a column)
Definition: Matrix.h:376
void clear()
Definition: Matrix.h:236
pointer Pointer
Definition: Matrix.h:115
void setValue(size_type const i, size_type const j, value_type value)
Definition: Matrix.h:175
Base::size_type size_type
Definition: Matrix.h:88
SizeType colIndex(SizeType index) const
Calculate the column from an index into the underlying vector. Note that Matrix uses the (row,...
Definition: Matrix.h:303
Matrix & operator=(const Matrix &rhs)
Definition: Matrix.h:142
Base::const_iterator const_iterator
Definition: Matrix.h:90
SizeType rowIndex(SizeType index) const
Calculate the row from an index into the underlying vector. Note that Matrix uses the (row,...
Definition: Matrix.h:315
void setMatrix(const ValueType matrix[ROWS][COLS])
set matrix to 2D arrays values
Definition: Matrix.h:354
reference getValue(size_type const i, size_type const j)
Definition: Matrix.h:170
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:120
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47