OpenMS
Loading...
Searching...
No Matches
Matrix< Value > Class Template Reference

A 2D matrix class with efficient buffer access for NumPy interoperability. More...

#include <OpenMS/DATASTRUCTURES/Matrix.h>

Inheritance diagram for Matrix< Value >:
[legend]
Collaboration diagram for Matrix< Value >:
[legend]

Public Types

Type definitions
using value_type = Value
 
using iterator = typename std::vector< Value >::iterator
 
using const_iterator = typename std::vector< Value >::const_iterator
 

Public Member Functions

Constructors and assignment
 Matrix ()
 Default constructor creates empty matrix.
 
 Matrix (Size rows, Size cols, Value value=Value())
 Constructor to create a matrix with specified dimensions and fill value.
 
 Matrix (const Matrix &)=default
 Copy constructor.
 
 Matrix (Matrix &&) noexcept=default
 Move constructor.
 
Matrixoperator= (const Matrix &)=default
 Copy assignment operator.
 
Matrixoperator= (Matrix &&) noexcept=default
 Move assignment operator.
 
 ~Matrix ()=default
 Destructor.
 
Dimension accessors
Size rows () const
 Number of rows.
 
Size cols () const
 Number of columns.
 
Size size () const
 Total number of elements.
 
bool empty () const
 Check if matrix is empty.
 
Element access
const Value & getValue (size_t const row, size_t const col) const
 Get element at (row, col)
 
Value & getValue (size_t const row, size_t const col)
 Get mutable element at (row, col)
 
void setValue (size_t const row, size_t const col, const Value &value)
 Set element at (row, col)
 
Value & operator() (size_t row, size_t col)
 Unchecked element access (row, col)
 
const Value & operator() (size_t row, size_t col) const
 Unchecked const element access (row, col)
 
Modifiers
void resize (size_t rows, size_t cols)
 Resize matrix (contents become undefined after resize)
 
void fill (Value value)
 Fill all elements with value.
 
void clear ()
 Clear matrix (set to 0x0)
 
template<typename T , long int ROWS, long int COLS>
void setMatrix (T const (&array)[ROWS][COLS])
 Sets the matrix values using a 2D array.
 
Iterators
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
Reduction operations
Value maxValue () const
 Returns the maximum value in the matrix.
 
Value minValue () const
 Returns the minimum value in the matrix.
 
Comparison
bool operator== (const Matrix &rhs) const
 Equality operator. Compares two matrices for equality.
 
bool operator!= (const Matrix &rhs) const
 

Private Attributes

std::vector< Value > data_
 Column-major storage.
 
Size rows_
 Number of rows.
 
Size cols_
 Number of columns.
 

Friends

std::ostream & operator<< (std::ostream &os, const Matrix< Value > &matrix)
 Friend function to output the matrix to an output stream.
 

Buffer access (for NumPy/Cython interoperability)

Value * data ()
 Pointer to raw data buffer (column-major storage)
 
const Value * data () const
 Const pointer to raw data buffer.
 
int innerStride () const
 Stride between consecutive elements in same column (always 1 for column-major)
 
int outerStride () const
 Stride between consecutive columns.
 
static constexpr bool rowMajor ()
 Returns false (column-major storage, not row-major)
 

Detailed Description

template<typename Value>
class OpenMS::Matrix< Value >

A 2D matrix class with efficient buffer access for NumPy interoperability.

Data is stored in column-major order (Fortran style) for compatibility with Eigen and most numerical libraries. Internal OpenMS code can obtain zero-copy Eigen views via the eigenView() function in MatrixEigen.h (internal header).

Template Parameters
ValueThe element type (typically double or float)

Member Typedef Documentation

◆ const_iterator

template<typename Value >
using const_iterator = typename std::vector<Value>::const_iterator

◆ iterator

template<typename Value >
using iterator = typename std::vector<Value>::iterator

◆ value_type

template<typename Value >
using value_type = Value

Constructor & Destructor Documentation

◆ Matrix() [1/4]

template<typename Value >
Matrix ( )
inline

Default constructor creates empty matrix.

◆ Matrix() [2/4]

template<typename Value >
Matrix ( Size  rows,
Size  cols,
Value  value = Value() 
)
inline

Constructor to create a matrix with specified dimensions and fill value.

Parameters
rowsNumber of rows in the matrix.
colsNumber of columns in the matrix.
valueInitial value to fill the matrix.

◆ Matrix() [3/4]

template<typename Value >
Matrix ( const Matrix< Value > &  )
default

Copy constructor.

◆ Matrix() [4/4]

template<typename Value >
Matrix ( Matrix< Value > &&  )
defaultnoexcept

Move constructor.

◆ ~Matrix()

template<typename Value >
~Matrix ( )
default

Destructor.

Member Function Documentation

◆ begin() [1/2]

template<typename Value >
iterator begin ( )
inline

◆ begin() [2/2]

template<typename Value >
const_iterator begin ( ) const
inline

◆ cbegin()

template<typename Value >
const_iterator cbegin ( ) const
inline

◆ cend()

template<typename Value >
const_iterator cend ( ) const
inline

◆ clear()

template<typename Value >
void clear ( )
inline

Clear matrix (set to 0x0)

References Matrix< Value >::cols_, Matrix< Value >::data_, and Matrix< Value >::rows_.

◆ cols()

◆ data() [1/2]

template<typename Value >
Value * data ( )
inline

Pointer to raw data buffer (column-major storage)

References Matrix< Value >::data_.

Referenced by OpenMS::eigenView(), and OpenMS::eigenView().

◆ data() [2/2]

template<typename Value >
const Value * data ( ) const
inline

Const pointer to raw data buffer.

References Matrix< Value >::data_.

◆ empty()

template<typename Value >
bool empty ( ) const
inline

Check if matrix is empty.

References Matrix< Value >::data_.

Referenced by BilinearInterpolation< Key, Value >::empty().

◆ end() [1/2]

template<typename Value >
iterator end ( )
inline

◆ end() [2/2]

template<typename Value >
const_iterator end ( ) const
inline

◆ fill()

template<typename Value >
void fill ( Value  value)
inline

Fill all elements with value.

References Matrix< Value >::data_.

◆ getValue() [1/2]

template<typename Value >
Value & getValue ( size_t const  row,
size_t const  col 
)
inline

Get mutable element at (row, col)

Note: pyOpenMS can't easily wrap operator() so we provide additional getter.

Parameters
[in]rowZero-based row index.
[in]colZero-based column index.
Returns
Mutable reference to the value at the given position.

References Matrix< Value >::data_, and Matrix< Value >::rows_.

◆ getValue() [2/2]

template<typename Value >
const Value & getValue ( size_t const  row,
size_t const  col 
) const
inline

Get element at (row, col)

Note: pyOpenMS can't easily wrap operator() so we provide additional getter.

Parameters
[in]rowZero-based row index.
[in]colZero-based column index.
Returns
Const reference to the value at the given position.

References Matrix< Value >::data_, and Matrix< Value >::rows_.

◆ innerStride()

template<typename Value >
int innerStride ( ) const
inline

Stride between consecutive elements in same column (always 1 for column-major)

◆ maxValue()

template<typename Value >
Value maxValue ( ) const
inline

Returns the maximum value in the matrix.

Returns
The maximum value. For empty matrices, returns Value() (default-constructed value).

References Matrix< Value >::data_.

◆ minValue()

template<typename Value >
Value minValue ( ) const
inline

Returns the minimum value in the matrix.

Returns
The minimum value. For empty matrices, returns Value() (default-constructed value).

References Matrix< Value >::data_.

◆ operator!=()

template<typename Value >
bool operator!= ( const Matrix< Value > &  rhs) const
inline

◆ operator()() [1/2]

template<typename Value >
Value & operator() ( size_t  row,
size_t  col 
)
inline

Unchecked element access (row, col)

References Matrix< Value >::data_, and Matrix< Value >::rows_.

◆ operator()() [2/2]

template<typename Value >
const Value & operator() ( size_t  row,
size_t  col 
) const
inline

Unchecked const element access (row, col)

References Matrix< Value >::data_, and Matrix< Value >::rows_.

◆ operator=() [1/2]

template<typename Value >
Matrix & operator= ( const Matrix< Value > &  )
default

Copy assignment operator.

◆ operator=() [2/2]

template<typename Value >
Matrix & operator= ( Matrix< Value > &&  )
defaultnoexcept

Move assignment operator.

◆ operator==()

template<typename Value >
bool operator== ( const Matrix< Value > &  rhs) const
inline

Equality operator. Compares two matrices for equality.

Parameters
[in]rhsThe matrix to be compared.
Returns
True if matrices are equal, false otherwise.
Exceptions
Exception::Preconditionif matrices have different dimensions (Debug mode only)

References Matrix< Value >::cols_, Matrix< Value >::data_, OPENMS_PRECONDITION, and Matrix< Value >::rows_.

◆ outerStride()

template<typename Value >
int outerStride ( ) const
inline

Stride between consecutive columns.

References Matrix< Value >::rows_.

◆ resize()

template<typename Value >
void resize ( size_t  rows,
size_t  cols 
)
inline

Resize matrix (contents become undefined after resize)

References Matrix< Value >::cols(), Matrix< Value >::cols_, Matrix< Value >::data_, Matrix< Value >::rows(), and Matrix< Value >::rows_.

Referenced by Matrix< Value >::setMatrix().

◆ rowMajor()

template<typename Value >
static constexpr bool rowMajor ( )
inlinestaticconstexpr

Returns false (column-major storage, not row-major)

◆ rows()

◆ setMatrix()

template<typename Value >
template<typename T , long int ROWS, long int COLS>
void setMatrix ( T const (&)  array[ROWS][COLS])
inline

Sets the matrix values using a 2D array.

This function resizes the matrix to the specified number of rows and columns, and then assigns the values from the 2D array to the corresponding elements in the matrix.

Template Parameters
TThe type of the matrix elements.
ROWSThe number of rows in the matrix.
COLSThe number of columns in the matrix.
Parameters
[in]arrayThe 2D array containing the values to be assigned to the matrix.

References Matrix< Value >::resize().

◆ setValue()

template<typename Value >
void setValue ( size_t const  row,
size_t const  col,
const Value &  value 
)
inline

Set element at (row, col)

Note: pyOpenMS can't easily wrap operator() so we provide additional setter.

Parameters
[in]rowZero-based row index.
[in]colZero-based column index.
[in]valueValue to set at the given position.

References Matrix< Value >::data_, and Matrix< Value >::rows_.

◆ size()

template<typename Value >
Size size ( ) const
inline

Total number of elements.

References Matrix< Value >::data_.

Friends And Related Symbol Documentation

◆ operator<<

template<typename Value >
std::ostream & operator<< ( std::ostream &  os,
const Matrix< Value > &  matrix 
)
friend

Friend function to output the matrix to an output stream.

Parameters
[in,out]osOutput stream.
[in]matrixMatrix to be output.
Returns
Reference to the output stream.

Member Data Documentation

◆ cols_

template<typename Value >
Size cols_
private

◆ data_

◆ rows_