OpenMS
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- 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
13
14#include <vector>
15#include <algorithm>
16#include <iostream>
17#include <iomanip>
18#include <stdexcept>
19
20namespace OpenMS
21{
33 template <typename Value>
34 class Matrix
35 {
36 public:
39 using value_type = Value;
40 using iterator = typename std::vector<Value>::iterator;
41 using const_iterator = typename std::vector<Value>::const_iterator;
43
46
48 Matrix() : rows_(0), cols_(0) {}
49
57 Matrix(Size rows, Size cols, Value value = Value())
58 : data_(rows * cols, value), rows_(rows), cols_(cols) {}
59
61 Matrix(const Matrix&) = default;
62
64 Matrix(Matrix&&) noexcept = default;
65
67 Matrix& operator=(const Matrix&) = default;
68
70 Matrix& operator=(Matrix&&) noexcept = default;
71
73 ~Matrix() = default;
74
76
79
81 Size rows() const { return rows_; }
82
84 Size cols() const { return cols_; }
85
87 Size size() const { return data_.size(); }
88
90 bool empty() const { return data_.empty(); }
91
93
96
106 const Value& getValue(size_t const row, size_t const col) const
107 {
108 return data_[col * rows_ + row]; // Column-major
109 }
110
120 Value& getValue(size_t const row, size_t const col)
121 {
122 return data_[col * rows_ + row]; // Column-major
123 }
124
134 void setValue(size_t const row, size_t const col, const Value& value)
135 {
136 data_[col * rows_ + row] = value; // Column-major
137 }
138
140 Value& operator()(size_t row, size_t col)
141 {
142 return data_[col * rows_ + row];
143 }
144
146 const Value& operator()(size_t row, size_t col) const
147 {
148 return data_[col * rows_ + row];
149 }
150
152
155
157 Value* data() { return data_.data(); }
158
160 const Value* data() const { return data_.data(); }
161
163 int innerStride() const { return 1; }
164
166 int outerStride() const { return static_cast<int>(rows_); }
167
169 static constexpr bool rowMajor() { return false; }
170
172
175
177 void resize(size_t rows, size_t cols)
178 {
179 rows_ = rows;
180 cols_ = cols;
181 data_.resize(rows * cols);
182 }
183
185 void fill(Value value)
186 {
187 std::fill(data_.begin(), data_.end(), value);
188 }
189
191 void clear()
192 {
193 data_.clear();
194 rows_ = 0;
195 cols_ = 0;
196 }
197
210 template <typename T, long int ROWS, long int COLS>
211 void setMatrix(T const (&array)[ROWS][COLS])
212 {
213 resize(ROWS, COLS);
214 for (long int i = 0; i < ROWS; ++i)
215 {
216 for (long int j = 0; j < COLS; ++j)
217 {
218 (*this)(i, j) = array[i][j];
219 }
220 }
221 }
222
224
227 iterator begin() { return data_.begin(); }
228 iterator end() { return data_.end(); }
229 const_iterator begin() const { return data_.begin(); }
230 const_iterator end() const { return data_.end(); }
231 const_iterator cbegin() const { return data_.cbegin(); }
232 const_iterator cend() const { return data_.cend(); }
234
237
243 Value maxValue() const
244 {
245 if (data_.empty()) return Value();
246 return *std::max_element(data_.begin(), data_.end());
247 }
248
254 Value minValue() const
255 {
256 if (data_.empty()) return Value();
257 return *std::min_element(data_.begin(), data_.end());
258 }
259
261
264
273 bool operator==(const Matrix& rhs) const
274 {
275 OPENMS_PRECONDITION(rows_ == rhs.rows_ && cols_ == rhs.cols_,
276 "Matrices must have the same dimensions for comparison.");
277 return data_ == rhs.data_;
278 }
279
280 bool operator!=(const Matrix& rhs) const
281 {
282 return !(*this == rhs);
283 }
284
286
294 friend std::ostream& operator<<(std::ostream& os, const Matrix<Value>& matrix)
295 {
296 for (Size i = 0; i < matrix.rows(); ++i)
297 {
298 for (Size j = 0; j < matrix.cols(); ++j)
299 {
300 os << std::setprecision(6) << std::setw(6) << matrix(i, j) << ' ';
301 }
302 os << '\n';
303 }
304 return os;
305 }
306
307 private:
308 std::vector<Value> data_;
311 };
312
313} // namespace OpenMS
A 2D matrix class with efficient buffer access for NumPy interoperability.
Definition Matrix.h:35
bool operator==(const Matrix &rhs) const
Equality operator. Compares two matrices for equality.
Definition Matrix.h:273
void resize(size_t rows, size_t cols)
Resize matrix (contents become undefined after resize)
Definition Matrix.h:177
friend std::ostream & operator<<(std::ostream &os, const Matrix< Value > &matrix)
Friend function to output the matrix to an output stream.
Definition Matrix.h:294
Value value_type
Definition Matrix.h:39
std::vector< Value > data_
Column-major storage.
Definition Matrix.h:308
typename std::vector< Value >::iterator iterator
Definition Matrix.h:40
const_iterator begin() const
Definition Matrix.h:229
const Value & getValue(size_t const row, size_t const col) const
Get element at (row, col)
Definition Matrix.h:106
typename std::vector< Value >::const_iterator const_iterator
Definition Matrix.h:41
const_iterator cbegin() const
Definition Matrix.h:231
Matrix()
Default constructor creates empty matrix.
Definition Matrix.h:48
Size cols() const
Number of columns.
Definition Matrix.h:84
const Value & operator()(size_t row, size_t col) const
Unchecked const element access (row, col)
Definition Matrix.h:146
Size rows_
Number of rows.
Definition Matrix.h:309
const Value * data() const
Const pointer to raw data buffer.
Definition Matrix.h:160
void fill(Value value)
Fill all elements with value.
Definition Matrix.h:185
bool empty() const
Check if matrix is empty.
Definition Matrix.h:90
Value minValue() const
Returns the minimum value in the matrix.
Definition Matrix.h:254
Matrix(Size rows, Size cols, Value value=Value())
Constructor to create a matrix with specified dimensions and fill value.
Definition Matrix.h:57
Size cols_
Number of columns.
Definition Matrix.h:310
Size rows() const
Number of rows.
Definition Matrix.h:81
Value * data()
Pointer to raw data buffer (column-major storage)
Definition Matrix.h:157
int innerStride() const
Stride between consecutive elements in same column (always 1 for column-major)
Definition Matrix.h:163
const_iterator cend() const
Definition Matrix.h:232
Matrix(Matrix &&) noexcept=default
Move constructor.
void setMatrix(T const (&array)[ROWS][COLS])
Sets the matrix values using a 2D array.
Definition Matrix.h:211
int outerStride() const
Stride between consecutive columns.
Definition Matrix.h:166
bool operator!=(const Matrix &rhs) const
Definition Matrix.h:280
Value & operator()(size_t row, size_t col)
Unchecked element access (row, col)
Definition Matrix.h:140
Matrix(const Matrix &)=default
Copy constructor.
void clear()
Clear matrix (set to 0x0)
Definition Matrix.h:191
Value & getValue(size_t const row, size_t const col)
Get mutable element at (row, col)
Definition Matrix.h:120
iterator end()
Definition Matrix.h:228
Size size() const
Total number of elements.
Definition Matrix.h:87
const_iterator end() const
Definition Matrix.h:230
Value maxValue() const
Returns the maximum value in the matrix.
Definition Matrix.h:243
iterator begin()
Definition Matrix.h:227
static constexpr bool rowMajor()
Returns false (column-major storage, not row-major)
Definition Matrix.h:169
void setValue(size_t const row, size_t const col, const Value &value)
Set element at (row, col)
Definition Matrix.h:134
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition openms/include/OpenMS/CONCEPT/Macros.h:94
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19