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

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:02 using doxygen 1.8.13