Stratax
Scientific computing containers and operations
Loading...
Searching...
No Matches
stratax::container::Matrix< T > Class Template Reference

#include <Matrix.hpp>

Public Types

using value_type = T
template<typename U>
using rebind = Matrix<U>

Public Member Functions

 Matrix ()
 Creates a default rank-2 empty matrix.
 Matrix (std::size_t rows, std::size_t cols)
 Creates a rank-2 matrix with the given number of rows and columns.
 Matrix (const core::Shape &shape)
 Creates a matrix from a validated rank-2 shape.
 Matrix (std::size_t rows, std::size_t cols, const T &value)
 Creates a matrix and fills it with a value.
 Matrix (std::initializer_list< std::initializer_list< T > > list)
 Creates a matrix from a nested initializer list.
 Matrix (const Matrix &)=default
 Creates a copy of another matrix.
 Matrix (Matrix &&) noexcept=default
 Transfers ownership from another matrix.
Matrixoperator= (const Matrix &)=default
 Replaces this matrix with a copy of another matrix.
Matrixoperator= (Matrix &&) noexcept=default
 Replaces this matrix by taking ownership from another matrix.
 ~Matrix ()=default
 Destroys the matrix.
std::size_t size () const noexcept
 Returns the total number of elements in the matrix.
bool empty () const noexcept
 Returns whether the matrix contains no elements.
std::size_t rows () const noexcept
 Returns the number of rows.
std::size_t cols () const noexcept
 Returns the number of columns.
const stratax::core::Shapeshape () const noexcept
 Returns the matrix shape.
const stratax::core::Stridesstrides () const noexcept
 Returns the matrix strides.
std::size_t rank () const noexcept
 Returns the matrix rank.
T & operator() (std::size_t row, std::size_t col)
 Returns an element by row and column with bounds checking.
const T & operator() (std::size_t row, std::size_t col) const
 Returns an element by row and column with bounds checking.
T & operator[] (std::size_t index) noexcept
 Returns a flat element without bounds checking.
const T & operator[] (std::size_t index) const noexcept
 Returns a flat element without bounds checking.
T & at (std::size_t row, std::size_t col)
 Returns an element by row and column.
const T & at (std::size_t row, std::size_t col) const
 Returns an element by row and column.
T & front ()
 Returns the first element.
const T & front () const
 Returns the first element as a const reference.
T & back ()
 Returns the last element.
const T & back () const
 Returns the last element as a const reference.
T * data () noexcept
 Returns the raw data pointer.
const T * data () const noexcept
 Returns the raw data pointer as a const pointer.
T * begin () noexcept
 Returns an iterator to the first element.
const T * begin () const noexcept
 Returns a const iterator to the first element.
const T * cbegin () const noexcept
 Returns a const iterator to the first element.
T * end () noexcept
 Returns an iterator one past the last element.
const T * end () const noexcept
 Returns a const iterator one past the last element.
const T * cend () const noexcept
 Returns a const iterator one past the last element.
std::reverse_iterator< T * > rbegin () noexcept
 Returns a reverse iterator to the last element.
std::reverse_iterator< const T * > rbegin () const noexcept
 Returns a const reverse iterator to the last element.
std::reverse_iterator< const T * > crbegin () const noexcept
 Returns a const reverse iterator to the last element.
std::reverse_iterator< T * > rend () noexcept
 Returns a reverse iterator before the first element.
std::reverse_iterator< const T * > rend () const noexcept
 Returns a const reverse iterator before the first element.
std::reverse_iterator< const T * > crend () const noexcept
 Returns a const reverse iterator before the first element.
void fill (const T &value)
 Fills every element with the same value.
void swap (Matrix &other) noexcept
 Swaps the contents of two matrices.

Detailed Description

template<typename T>
requires Numeric<T>
class stratax::container::Matrix< T >

Definition at line 28 of file Matrix.hpp.

Member Typedef Documentation

◆ rebind

template<typename T>
template<typename U>
using stratax::container::Matrix< T >::rebind = Matrix<U>

Definition at line 39 of file Matrix.hpp.

◆ value_type

template<typename T>
using stratax::container::Matrix< T >::value_type = T

Definition at line 36 of file Matrix.hpp.

Constructor & Destructor Documentation

◆ Matrix() [1/7]

template<typename T>
stratax::container::Matrix< T >::Matrix ( )
inline

Creates a default rank-2 empty matrix.

The default matrix has shape (0, 0) and owns no element storage.

Definition at line 46 of file Matrix.hpp.

◆ Matrix() [2/7]

template<typename T>
stratax::container::Matrix< T >::Matrix ( std::size_t rows,
std::size_t cols )
inline

Creates a rank-2 matrix with the given number of rows and columns.

Parameters
rowsNumber of rows.
colsNumber of columns.

Definition at line 54 of file Matrix.hpp.

◆ Matrix() [3/7]

template<typename T>
stratax::container::Matrix< T >::Matrix ( const core::Shape & shape)
inlineexplicit

Creates a matrix from a validated rank-2 shape.

Parameters
shapeSource shape.
Exceptions
Exceptions::DimensionErrorIf the shape is not rank 2.

Definition at line 68 of file Matrix.hpp.

◆ Matrix() [4/7]

template<typename T>
stratax::container::Matrix< T >::Matrix ( std::size_t rows,
std::size_t cols,
const T & value )
inline

Creates a matrix and fills it with a value.

Parameters
rowsNumber of rows.
colsNumber of columns.
valueValue to copy into each element.

Definition at line 85 of file Matrix.hpp.

◆ Matrix() [5/7]

template<typename T>
stratax::container::Matrix< T >::Matrix ( std::initializer_list< std::initializer_list< T > > list)
inline

Creates a matrix from a nested initializer list.

Rows are interpreted in row-major order and must all have the same length.

Parameters
listRow-major source values.
Exceptions
Exceptions::StrataxErrorIf the rows are not rectangular.

Definition at line 101 of file Matrix.hpp.

◆ Matrix() [6/7]

template<typename T>
stratax::container::Matrix< T >::Matrix ( const Matrix< T > & )
default

Creates a copy of another matrix.

◆ Matrix() [7/7]

template<typename T>
stratax::container::Matrix< T >::Matrix ( Matrix< T > && )
defaultnoexcept

Transfers ownership from another matrix.

◆ ~Matrix()

template<typename T>
stratax::container::Matrix< T >::~Matrix ( )
default

Destroys the matrix.

Member Function Documentation

◆ at() [1/2]

template<typename T>
T & stratax::container::Matrix< T >::at ( std::size_t row,
std::size_t col )
inline

Returns an element by row and column.

Parameters
rowRow index.
colColumn index.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf either index is out of bounds.

Definition at line 293 of file Matrix.hpp.

◆ at() [2/2]

template<typename T>
const T & stratax::container::Matrix< T >::at ( std::size_t row,
std::size_t col ) const
inline

Returns an element by row and column.

Parameters
rowRow index.
colColumn index.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf either index is out of bounds.

Definition at line 307 of file Matrix.hpp.

◆ back() [1/2]

template<typename T>
T & stratax::container::Matrix< T >::back ( )
inline

Returns the last element.

Returns
Mutable reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the matrix is empty.

Definition at line 350 of file Matrix.hpp.

◆ back() [2/2]

template<typename T>
const T & stratax::container::Matrix< T >::back ( ) const
inline

Returns the last element as a const reference.

Returns
Const reference to the last stored element.
Exceptions
Exceptions::IndexErrorIf the matrix is empty.

Definition at line 366 of file Matrix.hpp.

◆ begin() [1/2]

template<typename T>
const T * stratax::container::Matrix< T >::begin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the matrix.

Definition at line 411 of file Matrix.hpp.

◆ begin() [2/2]

template<typename T>
T * stratax::container::Matrix< T >::begin ( )
inlinenodiscardnoexcept

Returns an iterator to the first element.

Returns
Iterator to the beginning of the matrix.

Definition at line 401 of file Matrix.hpp.

◆ cbegin()

template<typename T>
const T * stratax::container::Matrix< T >::cbegin ( ) const
inlinenodiscardnoexcept

Returns a const iterator to the first element.

Returns
Const iterator to the beginning of the matrix.

Definition at line 421 of file Matrix.hpp.

◆ cend()

template<typename T>
const T * stratax::container::Matrix< T >::cend ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the matrix.

Definition at line 451 of file Matrix.hpp.

◆ cols()

template<typename T>
std::size_t stratax::container::Matrix< T >::cols ( ) const
inlinenodiscardnoexcept

Returns the number of columns.

Returns
Column count.

Definition at line 191 of file Matrix.hpp.

Referenced by stratax::container::Matrix< U >::operator()(), stratax::container::Matrix< U >::operator()(), stratax::container::operator<<(), and slice().

◆ crbegin()

template<typename T>
std::reverse_iterator< const T * > stratax::container::Matrix< T >::crbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 481 of file Matrix.hpp.

◆ crend()

template<typename T>
std::reverse_iterator< const T * > stratax::container::Matrix< T >::crend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 511 of file Matrix.hpp.

◆ data() [1/2]

template<typename T>
const T * stratax::container::Matrix< T >::data ( ) const
inlinenodiscardnoexcept

Returns the raw data pointer as a const pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 391 of file Matrix.hpp.

◆ data() [2/2]

template<typename T>
T * stratax::container::Matrix< T >::data ( )
inlinenodiscardnoexcept

Returns the raw data pointer.

Returns
Pointer to the first stored element, or nullptr when empty.

Definition at line 381 of file Matrix.hpp.

◆ empty()

template<typename T>
bool stratax::container::Matrix< T >::empty ( ) const
inlinenodiscardnoexcept

Returns whether the matrix contains no elements.

Returns
true when the matrix size is zero.

Definition at line 171 of file Matrix.hpp.

Referenced by stratax::container::Matrix< U >::back(), stratax::container::Matrix< U >::back(), stratax::container::Matrix< U >::front(), and stratax::container::Matrix< U >::front().

◆ end() [1/2]

template<typename T>
const T * stratax::container::Matrix< T >::end ( ) const
inlinenodiscardnoexcept

Returns a const iterator one past the last element.

Returns
Const iterator to the end of the matrix.

Definition at line 441 of file Matrix.hpp.

◆ end() [2/2]

template<typename T>
T * stratax::container::Matrix< T >::end ( )
inlinenodiscardnoexcept

Returns an iterator one past the last element.

Returns
Iterator to the end of the matrix.

Definition at line 431 of file Matrix.hpp.

◆ fill()

template<typename T>
void stratax::container::Matrix< T >::fill ( const T & value)
inline

Fills every element with the same value.

Parameters
valueValue to assign to each element.

Definition at line 521 of file Matrix.hpp.

◆ front() [1/2]

template<typename T>
T & stratax::container::Matrix< T >::front ( )
inline

Returns the first element.

Returns
Mutable reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the matrix is empty.

Definition at line 318 of file Matrix.hpp.

◆ front() [2/2]

template<typename T>
const T & stratax::container::Matrix< T >::front ( ) const
inline

Returns the first element as a const reference.

Returns
Const reference to the first stored element.
Exceptions
Exceptions::IndexErrorIf the matrix is empty.

Definition at line 334 of file Matrix.hpp.

◆ operator()() [1/2]

template<typename T>
T & stratax::container::Matrix< T >::operator() ( std::size_t row,
std::size_t col )
inline

Returns an element by row and column with bounds checking.

Parameters
rowRow index.
colColumn index.
Returns
Mutable reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf either index is out of bounds.

Definition at line 235 of file Matrix.hpp.

◆ operator()() [2/2]

template<typename T>
const T & stratax::container::Matrix< T >::operator() ( std::size_t row,
std::size_t col ) const
inline

Returns an element by row and column with bounds checking.

Parameters
rowRow index.
colColumn index.
Returns
Const reference to the indexed element.
Exceptions
Exceptions::IndexErrorIf either index is out of bounds.

Definition at line 251 of file Matrix.hpp.

◆ operator=() [1/2]

template<typename T>
Matrix & stratax::container::Matrix< T >::operator= ( const Matrix< T > & )
default

Replaces this matrix with a copy of another matrix.

◆ operator=() [2/2]

template<typename T>
Matrix & stratax::container::Matrix< T >::operator= ( Matrix< T > && )
defaultnoexcept

Replaces this matrix by taking ownership from another matrix.

◆ operator[]() [1/2]

template<typename T>
const T & stratax::container::Matrix< T >::operator[] ( std::size_t index) const
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Const reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 279 of file Matrix.hpp.

◆ operator[]() [2/2]

template<typename T>
T & stratax::container::Matrix< T >::operator[] ( std::size_t index)
inlinenoexcept

Returns a flat element without bounds checking.

Parameters
indexFlat element index.
Returns
Mutable reference to the indexed element.
Warning
The caller must ensure that the index is in range.

Definition at line 266 of file Matrix.hpp.

◆ rank()

template<typename T>
std::size_t stratax::container::Matrix< T >::rank ( ) const
inlinenodiscardnoexcept

Returns the matrix rank.

Returns
The number of dimensions, always 2 for valid matrices.

Definition at line 221 of file Matrix.hpp.

◆ rbegin() [1/2]

template<typename T>
std::reverse_iterator< const T * > stratax::container::Matrix< T >::rbegin ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator to the last element.

Returns
Const reverse iterator starting at the last element.

Definition at line 471 of file Matrix.hpp.

◆ rbegin() [2/2]

template<typename T>
std::reverse_iterator< T * > stratax::container::Matrix< T >::rbegin ( )
inlinenodiscardnoexcept

Returns a reverse iterator to the last element.

Returns
Reverse iterator starting at the last element.

Definition at line 461 of file Matrix.hpp.

◆ rend() [1/2]

template<typename T>
std::reverse_iterator< const T * > stratax::container::Matrix< T >::rend ( ) const
inlinenodiscardnoexcept

Returns a const reverse iterator before the first element.

Returns
Const reverse iterator representing the end sentinel.

Definition at line 501 of file Matrix.hpp.

◆ rend() [2/2]

template<typename T>
std::reverse_iterator< T * > stratax::container::Matrix< T >::rend ( )
inlinenodiscardnoexcept

Returns a reverse iterator before the first element.

Returns
Reverse iterator representing the end sentinel.

Definition at line 491 of file Matrix.hpp.

◆ rows()

template<typename T>
std::size_t stratax::container::Matrix< T >::rows ( ) const
inlinenodiscardnoexcept

Returns the number of rows.

Returns
Row count.

Definition at line 181 of file Matrix.hpp.

Referenced by stratax::container::Matrix< U >::operator()(), stratax::container::Matrix< U >::operator()(), stratax::container::operator<<(), and slice().

◆ shape()

template<typename T>
const stratax::core::Shape & stratax::container::Matrix< T >::shape ( ) const
inlinenoexcept

Returns the matrix shape.

Returns
Shape describing the matrix.

Definition at line 201 of file Matrix.hpp.

Referenced by astype().

◆ size()

template<typename T>
std::size_t stratax::container::Matrix< T >::size ( ) const
inlinenodiscardnoexcept

Returns the total number of elements in the matrix.

Returns
Number of stored elements.

Definition at line 161 of file Matrix.hpp.

Referenced by astype().

◆ strides()

template<typename T>
const stratax::core::Strides & stratax::container::Matrix< T >::strides ( ) const
inlinenoexcept

Returns the matrix strides.

Returns
Strides describing the matrix layout.

Definition at line 211 of file Matrix.hpp.

◆ swap()

template<typename T>
void stratax::container::Matrix< T >::swap ( Matrix< T > & other)
inlinenoexcept

Swaps the contents of two matrices.

Parameters
otherMatrix to exchange state with.

Definition at line 531 of file Matrix.hpp.

Referenced by stratax::container::Matrix< U >::swap().


The documentation for this class was generated from the following file: