OpenMS  2.5.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-2020.
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  {
331  OPENMS_PRECONDITION(cols_ == rhs.cols_,
332  "Matrices have different row sizes.");
333  OPENMS_PRECONDITION(rows_ == rhs.rows_,
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  {
345  OPENMS_PRECONDITION(cols_ == rhs.cols_,
346  "Matrices have different row sizes.");
347  OPENMS_PRECONDITION(rows_ == rhs.rows_,
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 
OpenMS::Matrix::operator()
reference operator()(size_type const i, size_type const j)
Definition: Matrix.h:160
OpenMS::Matrix::iterator
Base::iterator iterator
Definition: Matrix.h:92
OpenMS::Matrix::Pointer
pointer Pointer
Definition: Matrix.h:115
OpenMS::Matrix::resize
void resize(size_type i, size_type j, value_type value=value_type())
Definition: Matrix.h:243
OpenMS::Matrix::container_type
Base container_type
Definition: Matrix.h:85
OpenMS::Matrix::cols_
SizeType cols_
Number of columns (width of a row)
Definition: Matrix.h:378
OpenMS::Matrix::Matrix
Matrix(const SizeType rows, const SizeType cols, ValueType value=ValueType())
Definition: Matrix.h:130
OpenMS::Matrix::reference
Base::reference reference
Definition: Matrix.h:97
OpenMS::Matrix::operator=
Matrix & operator=(const Matrix &rhs)
Definition: Matrix.h:142
OpenMS::Matrix::Matrix
Matrix(const Matrix &source)
Definition: Matrix.h:136
OpenMS::Matrix::col
container_type col(size_type const i) const
Return the i-th column of the matrix as a vector.
Definition: Matrix.h:195
OpenMS::Matrix::setMatrix
void setMatrix(const ValueType matrix[ROWS][COLS])
set matrix to 2D arrays values
Definition: Matrix.h:354
OpenMS::Matrix::const_iterator
Base::const_iterator const_iterator
Definition: Matrix.h:90
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::Matrix::Base
std::vector< Value > Base
Definition: Matrix.h:79
seqan::assign
void assign(char &c_target, AAcid const &source)
Definition: AhoCorasickAmbiguous.h:179
OpenMS::Matrix::sizePair
std::pair< Size, Size > sizePair() const
Definition: Matrix.h:269
OPENMS_PRECONDITION
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition: openms/include/OpenMS/CONCEPT/Macros.h:136
OpenMS::Matrix::getValue
const_reference getValue(size_type const i, size_type const j) const
Definition: Matrix.h:165
OpenMS::Matrix::const_reference
Base::const_reference const_reference
Definition: Matrix.h:95
OpenMS::Matrix::Iterator
iterator Iterator
Definition: Matrix.h:111
OpenMS::Matrix::size_type
Base::size_type size_type
Definition: Matrix.h:88
OpenMS::Matrix::rowIndex
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
OpenMS::Matrix::ConstReverseIterator
const_reverse_iterator ConstReverseIterator
Definition: Matrix.h:110
OpenMS::Matrix::row
container_type row(size_type const i) const
Return the i-th row of the matrix as a vector.
Definition: Matrix.h:181
OpenMS::Matrix::value_type
Base::value_type value_type
Definition: Matrix.h:98
Macros.h
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::Matrix::ReverseIterator
reverse_iterator ReverseIterator
Definition: Matrix.h:112
OpenMS::Matrix::cols
SizeType cols() const
Number of columns.
Definition: Matrix.h:264
OpenMS::Matrix
A two-dimensional matrix. Similar to std::vector, but uses a binary operator(,) for element access.
Definition: IsobaricQuantitationMethod.h:49
OpenMS::Matrix::rows
SizeType rows() const
Number of rows.
Definition: Matrix.h:258
OpenMS::Matrix::allocator_type
Base::allocator_type allocator_type
Definition: Matrix.h:100
OpenMS::Matrix::ConstReference
const_reference ConstReference
Definition: Matrix.h:114
OpenMS::Matrix::clear
void clear()
Definition: Matrix.h:236
OpenMS::Matrix::AllocatorType
allocator_type AllocatorType
Definition: Matrix.h:119
OpenMS::Matrix::index
const SizeType 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
OpenMS::Matrix::rows_
SizeType rows_
Number of rows (height of a column)
Definition: Matrix.h:376
OpenMS::Matrix::pointer
Base::pointer pointer
Definition: Matrix.h:96
OpenMS::Matrix::const_reverse_iterator
Base::const_reverse_iterator const_reverse_iterator
Definition: Matrix.h:91
OpenMS::Matrix::Matrix
Matrix()
Definition: Matrix.h:124
OpenMS::Matrix::ContainerType
Base ContainerType
Definition: Matrix.h:105
OpenMS::Matrix::getValue
reference getValue(size_type const i, size_type const j)
Definition: Matrix.h:170
OpenMS::operator<<
std::ostream & operator<<(std::ostream &os, const AccurateMassSearchResult &amsr)
OpenMS::Matrix::setValue
void setValue(size_type const i, size_type const j, value_type value)
Definition: Matrix.h:175
OpenMS::Exception::IndexOverflow
Int overflow exception.
Definition: Exception.h:254
OpenMS::Matrix::asVector
const Base & asVector()
Definition: Matrix.h:366
OpenMS::Matrix::colIndex
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
OpenMS::Matrix::Reference
reference Reference
Definition: Matrix.h:116
OpenMS::Matrix::difference_type
Base::difference_type difference_type
Definition: Matrix.h:87
OpenMS::Matrix::operator()
const_reference operator()(size_type const i, size_type const j) const
Definition: Matrix.h:155
OpenMS::Matrix::ValueType
value_type ValueType
Definition: Matrix.h:117
OpenMS::Matrix::ConstIterator
const_iterator ConstIterator
Definition: Matrix.h:109
OpenMS::Matrix::reverse_iterator
Base::reverse_iterator reverse_iterator
Definition: Matrix.h:93
OpenMS::Matrix::DifferenceType
difference_type DifferenceType
Definition: Matrix.h:106
OpenMS::Matrix::SizeType
size_type SizeType
Definition: Matrix.h:107
OpenMS::Matrix::operator==
bool operator==(Matrix const &rhs) const
Equality comparator.
Definition: Matrix.h:329
OpenMS::Matrix::indexPair
const std::pair< Size, Size > 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
OpenMS::Matrix::~Matrix
~Matrix()
Definition: Matrix.h:150
OpenMS::Matrix::operator<
bool operator<(Matrix const &rhs) const
Less-than comparator. Comparison is done lexicographically: first by row, then by column.
Definition: Matrix.h:343
OpenMS::Matrix::resize
void resize(std::pair< Size, Size > const &size_pair, value_type value=value_type())
Definition: Matrix.h:250