OpenMS
Matrix.h
Go to the documentation of this file.
1 // Copyright (c) 2002-present, 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: Timo Sachsenberg $
7 // --------------------------------------------------------------------------
8 
9 #pragma once
10 
11 #include <OpenMS/CONCEPT/Macros.h>
12 
13 #include <Eigen/Dense>
14 
15 #include <algorithm>
16 #include <iostream>
17 #include <iomanip>
18 
19 namespace OpenMS
20 {
30  template <typename Value>
31  class Matrix : public Eigen::Matrix<Value, Eigen::Dynamic, Eigen::Dynamic>
32  {
33  public:
38  using EigenMatrixType::fill;
39  using EigenMatrixType::innerStride;
40  using EigenMatrixType::outerStride;
41 
42  // Default constructor. Creates the "null" matrix.
43  Matrix() = default;
44 
45  // Destructor
46  ~Matrix() = default;
47 
48  // Copy constructor
49  Matrix(const Matrix& other) = default;
50 
51  // Copy assignment operator
52  Matrix& operator=(const Matrix& other) = default;
53 
54  // Move constructor
55  Matrix(Matrix&& other) noexcept = default;
56 
57  // Move assignment operator
58  Matrix& operator=(Matrix&& other) noexcept = default;
59 
67  Matrix(Size rows, Size cols, Value value = Value()) : EigenMatrixType(rows, cols)
68  {
69  fill(value);
70  }
71 
72  /*
73  @brief get matrix entry
74  Note: pyOpenMS can't easily wrap operator() so we provide additional getter / setter.
75  */
76  const Value& getValue(size_t const i, size_t const j) const
77  {
78  return *this(i, j);
79  }
80 
81  /*
82  @brief get matrix entry
83  Note: pyOpenMS can't easily wrap operator() so we provide additional getter / setter.
84  */
85  Value& getValue(size_t const i, size_t const j)
86  {
87  return this->operator()(i, j);
88  }
89 
90  /*
91  @brief set matrix entry
92  Note: pyOpenMS can't easily wrap operator() so we provide additional getter / setter.
93  */
94  void setValue(size_t const i, size_t const j, const Value& value)
95  {
96  this->operator()(i, j) = value;
97  }
98 
99  // apparently needed for cython
100  void resize(size_t rows, size_t cols)
101  {
102  EigenMatrixType::resize(rows, cols);
103  }
104 
105  int innerStride() const
106  {
107  return EigenMatrixType::innerStride();
108  }
109 
110  int outerStride() const
111  {
112  return EigenMatrixType::outerStride();
113  }
114 
115  bool rowMajor() const
116  {
117  return EigenMatrixType::IsRowMajor;
118  }
119 
132  template <typename T, long int ROWS, long int COLS>
133  void setMatrix(T const (&array)[ROWS][COLS])
134  {
135  resize(ROWS, COLS);
136  for (int i = 0; i < ROWS; ++i)
137  {
138  for (int j = 0; j < COLS; ++j)
139  {
140  this->operator()(i, j) = array[i][j];
141  }
142  }
143  }
144 
151  bool operator==(const Matrix& rhs) const {
152  return EigenMatrixType::operator==(rhs);
153  }
154 
162  friend std::ostream& operator<<(std::ostream& os, const Matrix<Value>& matrix)
163  {
164  for (long int i = 0; i < matrix.rows(); ++i)
165  {
166  for (long int j = 0; j < matrix.cols(); ++j)
167  {
168  os << std::setprecision(6) << std::setw(6) << matrix(i, j) << ' ';
169  }
170  os << '\n';
171  }
172  return os;
173  }
174 
181  {
182  return *this;
183  }
184 
191  {
192  return *this;
193  }
194  };
195 } // namespace OpenMS
Definition: IsobaricIsotopeCorrector.h:17
A class representing a thin wrapper around an Eigen matrix.
Definition: Matrix.h:32
bool operator==(const Matrix &rhs) const
Equality operator. Compares two matrices for equality.
Definition: Matrix.h:151
void resize(size_t rows, size_t cols)
Definition: Matrix.h:100
Matrix(const Matrix &other)=default
const Value & getValue(size_t const i, size_t const j) const
Definition: Matrix.h:76
void setValue(size_t const i, size_t const j, const Value &value)
Definition: Matrix.h:94
Matrix & operator=(Matrix &&other) noexcept=default
Matrix(Size rows, Size cols, Value value=Value())
Constructor to create a matrix with specified dimensions and fill value.
Definition: Matrix.h:67
int innerStride() const
Definition: Matrix.h:105
Matrix()=default
~Matrix()=default
void setMatrix(T const (&array)[ROWS][COLS])
Sets the matrix values using a 2D array.
Definition: Matrix.h:133
int outerStride() const
Definition: Matrix.h:110
EigenMatrixType & getEigenMatrix()
Get a reference to the underlying Eigen matrix.
Definition: Matrix.h:190
Matrix & operator=(const Matrix &other)=default
Value & getValue(size_t const i, size_t const j)
Definition: Matrix.h:85
const EigenMatrixType & getEigenMatrix() const
Get a const reference to the underlying Eigen matrix.
Definition: Matrix.h:180
friend std::ostream & operator<<(std::ostream &os, const Matrix< Value > &matrix)
Friend function to output the matrix to an output stream.
Definition: Matrix.h:162
Matrix(Matrix &&other) noexcept=default
bool rowMajor() const
Definition: Matrix.h:115
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:97
bool operator==(const IDBoostGraph::ProteinGroup &lhs, const IDBoostGraph::ProteinGroup &rhs)
Main OpenMS namespace.
Definition: openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19