geomc 1.0
A c++ linear algebra template library
Loading...
Searching...
No Matches
SimpleMatrix< T, M, N, Lyt, P > Class Template Reference

A basic matrix with M x N elements. More...

#include <geomc/linalg/mtxtypes/SimpleMatrix.h>

Inheritance diagram for SimpleMatrix< T, M, N, Lyt, P >:
WriteableMatrixBase< T, M, N, SimpleMatrix< T, M, N, Lyt, P > > MatrixBase< T, M, N, SimpleMatrix< T, M, N, Lyt, P > >

Public Types

typedef FlatMatrixLayout< T, Lyt >::col_iterator col_iterator
 Writeable iterator over the elements of a column. This is a T* if the matrix is column-major; a proxy otherwise.
 
typedef FlatMatrixLayout< T, Lyt >::const_col_iterator const_col_iterator
 Read-only iterator over the elements of a column. This is a T* if the matrix is column-major; a proxy otherwise.
 
typedef FlatMatrixLayout< T, Lyt >::const_row_iterator const_iterator
 Read-only row-major iterator over the elements of the matrix. This is a T* if the matrix is row-major; a proxy otherwise.
 
typedef const_iterator const_region_iterator
 Read-only row-major iterator over the matrix elements in a rectangular region.
 
typedef FlatMatrixLayout< T, Lyt >::const_row_iterator const_row_iterator
 Read-only iterator over the elements of a row. This is a T* if the matrix is row-major; a proxy otherwise.
 
typedef T elem_t
 Element type.
 
typedef FlatMatrixLayout< T, Lyt >::row_iterator iterator
 Writeable row-major iterator over the elements of the matrix. This is a T* if the matrix is row-major; a proxy otherwise.
 
typedef SimpleMatrix< T, M, N, Lyt, P > recurring_t
 
typedef T & reference
 Reference to element type.
 
typedef iterator region_iterator
 Writeable row-major region iterator.
 
typedef FlatMatrixLayout< T, Lyt >::row_iterator row_iterator
 Writeable iterator over the elements of a row. This is a T* if the matrix is row-major; a proxy otherwise.
 
typedef Storage< storage_token_t, _ImplStorageObjCount< SimpleMatrix< T, M, N, Lyt, P > >::count > storagebuffer_t
 

Public Member Functions

template<typename Mx>
 SimpleMatrix (const Mx &mtx)
 
 SimpleMatrix (index_t nrows, index_t ncols)
 Construct a matrix of size (nrows x ncols).
 
 SimpleMatrix (T *src_data, index_t nrows, index_t ncols)
 Construct a matrix of size (nrows x ncols), initialized with src_data.
 
WrapperMatrix< T, N, M,(Lyt==ROW_MAJOR) ? COL_MAJOR :ROW_MAJOR > as_transpose () const
 
iterator begin ()
 
const_iterator begin () const
 
col_iterator col (index_t i)
 
const_col_iterator col (index_t i) const
 
index_t cols () const
 
T * data_begin ()
 
const T * data_begin () const
 
T * data_end ()
 
const T * data_end () const
 
iterator end ()
 
const_iterator end () const
 
storagebuffer_t get_storage_token_buffer () const
 
void get_storage_tokens (storage_token_t *buf) const
 
reference operator() (index_t r, index_t c)
 
operator() (index_t r, index_t c) const
 
template<typename U>
SimpleMatrix< T, M, N > & operator*= (U k)
 
derived_row_iterator operator[] (index_t i)
 
derived_const_row_iterator operator[] (index_t i) const
 
derived_const_row_iterator operator[] (index_t i) const
 
region_iterator region_begin (const MatrixRegion &r)
 
const_region_iterator region_begin (const MatrixRegion &r) const
 
const_region_iterator region_begin (const MatrixRegion &r) const
 
region_iterator region_end (const MatrixRegion &r)
 
const_region_iterator region_end (const MatrixRegion &r) const
 
const_region_iterator region_end (const MatrixRegion &r) const
 
row_iterator row (index_t i)
 
const_row_iterator row (index_t i) const
 
index_t rows () const
 
reference set (index_t r, index_t c, T val)
 
void set_identity ()
 
void set_zero ()
 
constexpr index_t storage_token_count () const
 

Static Public Attributes

static constexpr index_t COLDIM
 Column dimension template parameter.
 
static constexpr MatrixLayout Layout = Lyt
 The data layout of this matrix.
 
static constexpr index_t ROWDIM
 Row dimension template parameter.
 

Detailed Description

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
class geom::SimpleMatrix< T, M, N, Lyt, P >

A basic matrix with M x N elements.

Template Parameters
TElement type.
MRow dimension.
NColumn dimension.
LytLayout of the underlying array (ROW_MAJOR or COL_MAJOR). ROW_MAJOR is the default.
PPolicy for memory ownership of the backing array.

Example:

SimpleMatrix<double,4,4> mx;

If M or N are 0, the matrix has runtime-chosen size.

The storage policy behavior is as follows:

  • If the StoragePolicy is STORAGE_UNIQUE (default), then copy-constructed duplicates of dynamically-sized matrices will make a full copy of the underlying array.
  • If the StoragePolicy is STORAGE_SHARED, then all copy-constructed duplicates of dynamically-sized matrixes should be treated as references to a common array.
  • If the StoragePolicy is STORAGE_WRAPPED, then the matrix will wrap a user-provided backing array, whose lifetime is managed manually.

Note that in c++11, array duplicatations will use rvalue references to avoid a performing full array-copy where possible.

Wrapping an array

A SimpleMatrix can be made into a wrapper around a user-owned array like so:

double myRowMajorArray[16] = { ... };
SimpleMatrix<double,4,4,STORAGE_WRAPPED> mtx(myRowMajorArray);

In the above example, no array duplication will occur, and myRowMajorArray is used direcly as the backing storage for the matrix. Note the use of STORAGE_WRAPPED for the SimpleMatrix's storage policy template parameter.

In c++11, a template alias is available for this construction, called WrapperMatrix:

WrapperMatrix<double,4,4> wmtx(myRowMajorArray);

For more on matrices, see the matrix module documentation.

Constructor & Destructor Documentation

◆ SimpleMatrix() [1/3]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
SimpleMatrix ( index_t nrows,
index_t ncols )
inlineexplicit

Construct a matrix of size (nrows x ncols).

For matrices with dynamic dimension, a size argument is required for that dimension. Size arguments will be ignored for dimensions that are statically-sized.

Examples:

SimpleMatrix<double, 3, 3> m1;
SimpleMatrix<double, 0, 0> m2(3, 3);
SimpleMatrix<double, 0, 0> m3; // XXX: Compiler error!

This constructor is not available if the storage policy is STORAGE_WRAPPED.

Parameters
nrowsNumber of rows in the matrix.
ncolsNumber of columns in the matrix.

◆ SimpleMatrix() [2/3]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
SimpleMatrix ( T * src_data,
index_t nrows,
index_t ncols )
inlineexplicit

Construct a matrix of size (nrows x ncols), initialized with src_data.

For matrices with dynamic dimension, a size argument is required for that dimension. Size arguments will be ignored for dimensions that are statically-sized.

If the storage policy is STORAGE_WRAPPED, src_data will be used directly as the backing storage, and its lifetime must exceed the lifetime of this matrix.

Parameters
src_dataArray of nrows * ncols elements, in row-major order.
nrowsNumber of rows in the matrix.
ncolsNumber of columns in the matrix.

◆ SimpleMatrix() [3/3]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
template<typename Mx>
SimpleMatrix ( const Mx & mtx)
inline

Construct and initialize this matrix with the contents of another.

This constructor is not available if the storage policy is STORAGE_WRAPPED.

Template Parameters
MxA matrix type with agreeing dimension.
Parameters
mtxMatrix containing source elements.

Member Function Documentation

◆ as_transpose()

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
WrapperMatrix< T, N, M,(Lyt==ROW_MAJOR) ? COL_MAJOR :ROW_MAJOR > as_transpose ( ) const
inline

Reinterpret this matrix as a transposed version of itself.

The returned matrix is backed by this matrix's underlying memory. As such, its lifetime must not be longer than that of this matrix. Changes to the elements of this matrix will also be reflected in the resultant matrix, and vice versa.

This operation is O(1), and fuctions by constructing a new WrapperMatrix with opposite layout (i.e., column-major if this matrix is row-major, or row-major if this matrix is column-major).

◆ begin() [1/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
iterator begin ( )
inline
Returns
A writeable, random-access, row-major iterator over the elements of this matrix, pointing to the element at (0,0).

◆ begin() [2/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
const_iterator begin ( ) const
inline
Returns
A read-only, random-access, row-major iterator over the elements of this matrix, pointing to the element at (0,0).

◆ col() [1/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
col_iterator col ( index_t i)
inline
Parameters
iIndex of column (zero-indexed)
Returns
A writeable iterator over the elements of row i.

◆ col() [2/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
const_col_iterator col ( index_t i) const
inline
Parameters
iIndex of column (zero-indexed)
Returns
A read-only iterator over the elements of row i.

◆ cols()

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
index_t cols ( ) const
inline
Returns
number of columns in the matrix.

◆ data_begin() [1/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
T * data_begin ( )
inline
Returns
A pointer to the bare data array, in the layout policy of this matrix.

◆ data_begin() [2/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
const T * data_begin ( ) const
inline
Returns
A pointer to the read-only bare data array, in the layout policy of this matrix.

◆ data_end() [1/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
T * data_end ( )
inline
Returns
A pointer to the end of the bare data array.

◆ data_end() [2/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
const T * data_end ( ) const
inline
Returns
A pointer to the end of the read-only bare data array.

◆ end() [1/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
iterator end ( )
inline
Returns
A writeable, random-access, row-major iterator over the elements of this matrix, pointing to the element just beyond the last element in the matrix.

◆ end() [2/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
const_iterator end ( ) const
inline
Returns
A read-only, random-access, row-major iterator over the elements of this matrix, pointing to the just beyond the last element in the matrix.

◆ operator()() [1/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
reference operator() ( index_t r,
index_t c )
inline

Get the element at (row, col).

Parameters
rZero-indexed row coordinate
cZero-indexed column coordinate
Returns
A reference to the element at (row, col)

◆ operator()() [2/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
T operator() ( index_t r,
index_t c ) const
inline

Get the element at (row, col).

Parameters
rZero-indexed row coordinate
cZero-indexed column coordinate
Returns
The element at (row, col)

◆ operator*=()

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
template<typename U>
SimpleMatrix< T, M, N > & operator*= ( U k)
inline

Scalar in-place multiplication.

Template Parameters
UA type satisfying std::is_scalar<>.
Parameters
kScalar value.
Returns
A reference to this.

◆ operator[]() [1/3]

derived_row_iterator operator[] ( index_t i)
inlineinherited
Parameters
iIndex of row (zero-indexed)
Returns
A writeable iterator over the elements of row i.

◆ operator[]() [2/3]

( index_t i) const
inherited
Parameters
iIndex of row (zero-indexed)
Returns
A const iterator over the elements of row i

◆ operator[]() [3/3]

derived_const_row_iterator operator[] ( index_t i) const
inlineinherited
Parameters
iIndex of row (zero-indexed)
Returns
A const iterator over the elements of row i

◆ region_begin() [1/3]

region_iterator region_begin ( const MatrixRegion & r)
inlineinherited
Parameters
rThe zero-indexed region to iterate over. The upper extreme coordinates represent the index just beyond the last element to be iterated over.
Returns
A writeable, random-access, row-major iterator over the elements in region r, pointing at the first element in the region (upper left corner).

◆ region_begin() [2/3]

( const MatrixRegion & r) const
inherited
Parameters
rThe zero-indexed region to iterate over. The upper extreme coordinates represent the index just beyond the last element to be iterated over.
Returns
A read-only, random-access, row-major iterator over the elements in region r, pointing at the first element in the region (upper left corner).

◆ region_begin() [3/3]

const_region_iterator region_begin ( const MatrixRegion & r) const
inlineinherited
Parameters
rThe zero-indexed region to iterate over. The upper extreme coordinates represent the index just beyond the last element to be iterated over.
Returns
A read-only, random-access, row-major iterator over the elements in region r, pointing at the first element in the region (upper left corner).

◆ region_end() [1/3]

region_iterator region_end ( const MatrixRegion & r)
inlineinherited
Parameters
rThe zero-indexed region to iterate over. The upper extreme coordinates represent the index just beyond the last element to be iterated over.
Returns
A writeable, random-access, row-major iterator over the elements in region r, pointing at the element just beyond the last element in the region.

◆ region_end() [2/3]

( const MatrixRegion & r) const
inherited
Parameters
rThe zero-indexed region to iterate over. The upper extreme coordinates represent the index just beyond the last element to be iterated over.
Returns
A read-only, random-access, row-major iterator over the elements in region r, pointing at the element just beyond the last element in the region.

◆ region_end() [3/3]

const_region_iterator region_end ( const MatrixRegion & r) const
inlineinherited
Parameters
rThe zero-indexed region to iterate over. The upper extreme coordinates represent the index just beyond the last element to be iterated over.
Returns
A read-only, random-access, row-major iterator over the elements in region r, pointing at the element just beyond the last element in the region.

◆ row() [1/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
row_iterator row ( index_t i)
inline
Parameters
iIndex of row (zero-indexed)
Returns
A writeable iterator over the elements of row i.

◆ row() [2/2]

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
const_row_iterator row ( index_t i) const
inline
Parameters
iIndex of row (zero-indexed)
Returns
A read-only iterator over the elements of row i.

◆ rows()

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
index_t rows ( ) const
inline
Returns
Number of rows in the matrix.

◆ set()

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
reference set ( index_t r,
index_t c,
T val )
inline

Set the element at (row, col) to val.

Parameters
rZero-indexed row coordinate
cZero-indexed column coordinate
valNew value of element at (row, col)
Returns
A reference to the element at (row, col), for convenience.

◆ set_identity()

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
void set_identity ( )
inline

Clear this matrix and set its elements to the identity matrix.

◆ set_zero()

template<typename T, index_t M, index_t N, MatrixLayout Lyt, StoragePolicy P>
void set_zero ( )
inline

Set all the elements of this matrix to zero.


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