chsvlib
chsv helper source code
Matrix< Val, Alloc, Derived_t, Policy > Class Template Reference

A class template for matrices of elements of various types and semantics, including types which do not have any numeric semantics, as well as types satisfying algebraic_type requirements. More...

#include <chsvmath.h>

Public Types

typedef std::conditional< std::is_void< Derived_t >::value, Matrix< Val, Alloc, void, Policy >, Derived_t >::type derived_type
 A type alias for a most-derived class inheriting and implementing the behaviour of the matrix, if any, or the Matrix class otherwise (i.e. if Derived_t is void).
 
typedef MyBase::value_type value_type
 A type of a matrix element.
 
typedef MyBase::allocator_type allocator_type
 A type of an allocator used to manage storage for matrix elements.
 
typedef MyBase::policy_type policy_type
 A type of an execution policy specified for the matrix. To access specific sub-policies use matrix_execution_policy_traits.
 
typedef MyBase::size_type size_type
 An unsigned integral type for sizes within the matrix storage.
 
typedef MyBase::difference_type difference_type
 A signed integral type used for specifying differences between memory elements.
 
typedef MyBase::pointer pointer
 A type of a pointer to a matrix element.
 
typedef MyBase::const_pointer const_pointer
 A type of a read-only pointer to a matrix element.
 
typedef MyBase::reference reference
 A type of a reference to a matrix element.
 
typedef MyBase::const_reference const_reference
 A type of a read-only reference to a matrix element.
 
typedef MyBase::iterator iterator
 A type of a random-access iterator associated with columns of the matrix.
 
typedef MyBase::const_iterator const_iterator
 A type of a read-only random-access iterator associated with columns of the matrix.
 
typedef MyBase::reverse_iterator reverse_iterator
 A type of a reverse random-access iterator associated with columns of the matrix.
 
typedef MyBase::const_reverse_iterator const_reverse_iterator
 A type of a read-only reverse random-access iterator associated with columns of the matrix.
 
typedef MyBase::interpret_array_as_matrix_rows_set_t interpret_array_as_matrix_rows_set_t
 A type of a mark used in constructors to specify how to interpret a set of array elements: as an array of rows or an array of columns. The type is used to specify the former (default) case.
 
typedef MyBase::interpret_array_as_matrix_columns_set_t interpret_array_as_matrix_columns_set_t
 A type of a mark used in constructors to specify how to interpret a set of array elements: as an array of rows or an array of columns. The type is used to spesify the latter case.
 

Public Member Functions

 Matrix ()=default
 Constructs an empty 0x0 matrix.
 
 Matrix (const value_type &value)
 A constructor which creates a 1-dimensional square matrix with its only element to be initialized with a given value. More...
 
 Matrix (const value_type &value, size_type cColumns, size_type cRows)
 Creates a matrix with a specified number of columns and rows such that all elements appertaining to the main diagonal of the created matrix are initialized with a given value. More...
 
 Matrix (const value_type &value, size_type cColumns, size_type cRows, const allocator_type &alloc)
 Creates a matrix with a specified allocator object as well as numbers of columns and rows such that all elements which appertain to the main diagonal of the created matrix are initialized with a given value. More...
 
template<class Val2 , class Alloc2 , class Derived2_t , class Policy2 >
 Matrix (const Matrix< Val2, Alloc2, Derived2_t, Policy2 > &r)
 Constructs a copy of given matrix transformed from some other instantiation of the Matrix template. More...
 
template<class Derived2 , class Policy2 >
 Matrix (Matrix< Val, Alloc, Derived2, Policy2 > &&r)
 A moving constructor from a matrix with arbitrary implementing type and policy but same types of elements and an allocator. More...
 
 Matrix (std::initializer_list< std::initializer_list< value_type >> lst, const allocator_type &alloc=allocator_type())
 Creates a matrix from an initializer list of rows which, in turn, are also specified using initializer list mechanism with elements of a type convertible to value_type. More...
 
template<class InputIterator >
 Matrix (InputIterator itBegin, InputIterator itEnd, const allocator_type &alloc=allocator_type())
 Constructs a matrix from a range of matrices or matrix columns specified by a pair of input iterators. More...
 
template<class InputIterator >
 Matrix (const Chusov::Memory::iterator_range< InputIterator > &range, const allocator_type &alloc=allocator_type())
 Constructs a matrix from a range of matrices or matrix columns specified by an instantiation of the Chusov::Memory::iterator_range template for input iterators. More...
 
template<class input_iterator_t , class array_interpretation_t >
 Matrix (input_iterator_t itBegin, input_iterator_t itEnd, std::size_t cColumns, std::size_t cRows, array_interpretation_t array_interpretation, const allocator_type &alloc=allocator_type())
 Constructs a matrix from a specified set of elements given by a pair of iterators and an interpretation mark. More...
 
template<class value_t , std::size_t cColumns, std::size_t cRows>
 Matrix (value_t(&Arr2)[cRows][cColumns], const allocator_type &alloc=allocator_type())
 Constructs a matrix from a two dimensional array of elements. More...
 
template<class value_t , class interpretation_t >
 Matrix (value_t *pVector, std::size_t cColumns, std::size_t cRows, interpretation_t vector_interpretation, const allocator_type &alloc=allocator_type())
 Creates a matrix from a vector of data pointed to by parameters of the constructor. The data is a set of adjacent matrix elements which are treated either as a set of rows or as a set of columns depending on in interpretation mark. More...
 
template<class value_t , class alloc_t , class interpretation_t >
 Matrix (const std::vector< value_t, alloc_t > &v, typename std::vector< value_t, alloc_t >::size_type cColumns, typename std::vector< value_t, alloc_t >::size_type cRows, interpretation_t vector_interpretation, const allocator_type &alloc=allocator_type())
 Creates a matrix from an std::vector of elements copied to the constructed matrix as a set of rows or as a set of columns depending on the interpretation mark. More...
 
template<class matrix_t >
 Matrix (const matrix_column< matrix_t > &column, const allocator_type &alloc=allocator_type())
 Creates a matrix with one column from a column of another matrix. . More...
 
template<class value_t >
 Matrix (value_t *pVector, std::size_t cColumns, std::size_t cRows, const allocator_type &alloc=allocator_type())
 Creates a matrix from a vector of raw data pointed to by parameters of the constructor. More...
 
template<class value_t , class alloc_t >
 Matrix (const std::vector< value_t, alloc_t > &v, typename std::vector< value_t, alloc_t >::size_type cColumns, typename std::vector< value_t, alloc_t >::size_type cRows, const allocator_type &alloc=allocator_type())
 Creates a matrix from a set of its elements given by an std::vector object. More...
 
template<class value_t , class alloc_t >
 Matrix (const std::vector< value_t, alloc_t > &v, const allocator_type &alloc=allocator_type())
 Construct a one-dimensional matrix from an algebraic vector represented as an std::vector object. . More...
 
derived_typeRemoveColumnsMe (size_type iColumnBegin, size_type iColumnEnd)
 Removes a specified range of columns from a matrix. . More...
 
derived_typeRemoveColumnsMe (size_type iColumnBegin=0)
 Removes trailing columns of a matrix starting from a specified column. More...
 
derived_typeRemoveColumnsMe (iterator itBegin, iterator itEnd)
 Removes a specified range of columns from a matrix. . More...
 
derived_typeRemoveColumnsMe (iterator itBegin)
 Removes trailing columns of a matrix starting from a specified column. More...
 
derived_typeRemoveColumnsMe (reverse_iterator itBegin, reverse_iterator itEnd)
 Removes a specified range of columns from a matrix. . More...
 
derived_typeRemoveColumnsMe (reverse_iterator itBegin)
 Removes the leftmost columns of a matrix starting from a specified column. More...
 
derived_typeRemoveRowsMe (size_type iRowBegin, size_type iRowEnd)
 Removes a specified range of rows from a matrix. . More...
 
derived_typeRemoveRowsMe (size_type iRowBegin=0)
 Removes the lowest rows of a matrix starting from a specified row. More...
 
derived_type RemoveColumns (size_type iColumnBegin, size_type iColumnEnd) const
 Removes a specified range of columns from a copy of a matrix and returns it. . More...
 
derived_type RemoveColumns (size_type iColumnBegin=0) const
 Removes trailing columns of a copy of a matrix starting from a specified column and returns the resulting matrix. More...
 
derived_type RemoveColumns (const_iterator itBegin, const_iterator itEnd) const
 Removes a specified range of columns from a matrix. . More...
 
derived_type RemoveColumns (const_iterator itBegin) const
 Removes trailing columns from a copy of a current matrix starting from a specified column. More...
 
derived_type RemoveColumns (const_reverse_iterator itBegin, const_reverse_iterator itEnd) const
 Removes a specified range of columns from a copy of a matrix. . More...
 
derived_type RemoveColumns (const_reverse_iterator itBegin) const
 Removes the leftmost columns of a copy of a current matrix starting from a specified column. More...
 
derived_type RemoveRows (size_type iRowBegin, size_type iRowEnd) const
 Removes a specified range of rows from a returned copy of a matrix. . More...
 
derived_type RemoveRows (size_type iRowBegin=0) const
 Removes the lowest rows from a returned copy of a matrix starting from a specified row. More...
 
template<class ParallelismPolicy , class FunctorType , class... Args>
void ForEachElementSetPolicy (FunctorType &&fn, Args...args) const
 Performs parallel policy-based enumeration of elements of a matrix calling a client's Callable object. More...
 
template<class FunctorType , class... Args>
void ForEachElement (FunctorType &&fn, Args &&...args) const
 Performs enumeration of elements of a matrix with respect to its parallelism policy, and calls a client's Callable object for each element of the matrix. More...
 
template<class ParallelismPolicy , class FunctorType , class... Args>
void ForEachColumnSetPolicy (FunctorType &&fn, Args...args) const
 Performs parallel policy-based enumeration of columns of a matrix calling a client's Callable object. More...
 
template<class FunctorType , class... Args>
void ForEachColumn (FunctorType &&fn, Args &&...args) const
 Performs enumeration of columns of a matrix with respect to its parallelism policy, and calls a client's Callable object for each of the columns. More...
 
template<class ParallelismPolicy , class FunctorType , class... Args>
void ForEachRowSetPolicy (FunctorType &&fn, Args...args) const
 Performs parallel policy-based enumeration of rows of a matrix calling a client's Callable object. More...
 
template<class FunctorType , class... Args>
void ForEachRow (FunctorType &&fn, Args &&...args) const
 Performs enumeration of rows of a matrix with respect to its parallelism policy, and calls a client's Callable object for each of the rows. More...
 
allocator_type get_allocator () const
 Returns a copy of the current allocator object used to manage memory for matrix elements.
 
size_type Rows () const
 Returns a number of rows in the current matrix.
 
size_type Columns () const
 Returns a number of columns in the current matrix.
 
unspecified_column_type operator[] (size_type iColumn)
 Returns a matrix column reference by the specified column index. More...
 
unspecified_const_column_type operator[] (size_type iColumn) const
 Returns a read-only matrix column reference by the specified column index. . More...
 
unspecified_column_type at (size_type iColumn)
 Returns a matrix column reference by the specified column index. More...
 
unspecified_const_column_type at (size_type iColumn) const
 Returns a read-only matrix column reference by the specified column index. . More...
 
void SwapRows (size_type iRow1, size_type iRow2)
 Swaps the specified rows within the current matrix. More...
 
void SwapColumns (size_type iColumn1, size_type iColumn2)
 Swaps the specified columns within the current matrix. More...
 
derived_type Transpose () const
 Returns a copy of the current matrix in the transposed form.
 
derived_typeTransposeMe ()
 Transposes the current matrix and returns a reference to it.
 
template<class rvalue_t , class ralloc_t , class rderived_t , class rpolicy_t >
derived_typeAugmentMe (const Matrix< rvalue_t, ralloc_t, rderived_t, rpolicy_t > &refRight)
 Adds columns of a specified matrix to the right side of the current matrix. More...
 
template<class rmatrix_t >
derived_typeAugmentMe (const unspecified_column_type &refRight)
 Adds a specified matrix column to the right side of the current matrix. More...
 
template<class InputIterator >
derived_typeAugmentMe (InputIterator itBegin, InputIterator itEnd)
 Adds a specified range of matrix columns to the right side of the current matrix. More...
 
template<class InputIterator >
derived_typeAugmentMe (const Chusov::Memory::iterator_range< InputIterator > &range)
 Adds a specified range of matrix columns to the right side of the current matrix. More...
 
template<class rderived_t , class rvalue_t , class ralloc_t , class rpolicy_t >
derived_typeStackMe (const Matrix< rderived_t, rvalue_t, ralloc_t, rpolicy_t > &refRight)
 Adds new rows to the bottom of a matrix. More...
 
bool empty () const
 Checks whether the current matrix is empty, which is true when at least on of its dimensions is zero.
 
bool square () const
 Checks whether the current matrix is square.
 
iterator begin ()
 Returns a mutable random-access iterator associated with the first column of the matrix.
 
const_iterator begin () const
 Returns a read-only random-access iterator associated with the first column of the matrix.
 
const_iterator cbegin () const
 Returns a read-only random-access iterator associated with the first column of the matrix.
 
iterator end ()
 Returns a mutable random-access past-the-end column iterator.
 
const_iterator end () const
 Returns a read-only random-access past-the-end column iterator.
 
const_iterator cend () const
 Returns a read-only random-access past-the-end column iterator.
 
reverse_iterator rbegin ()
 Returns a mutable reverse random-access iterator to the last column of the matrix.
 
const_reverse_iterator rbegin () const
 Returns a read-only reverse random-access iterator to the last column of the matrix.
 
const_reverse_iterator crbegin () const
 Returns a read-only reverse random-access iterator to the last column of the matrix.
 
reverse_iterator rend ()
 Returns a mutable reverse random-access iterator associated with pseudo column of the matrix before the first one.
 
const_reverse_iterator rend () const
 Returns a read-only reverse random-access iterator associated with pseudo column of the matrix before the first one.
 
const_reverse_iterator crend () const
 Returns a read-only reverse random-access iterator associated with pseudo column of the matrix before the first one.
 
const_pointer data () const
 Returns a pointer to a contiguous memory with matrix elements grouped columnwise.
 
pointer data () const
 Returns a pointer to a contiguous memory with matrix elements grouped columnwise.
 
size_type size () const noexcept
 Returns a total number of elements in the matrix.
 
Methods defined for additive group elements

Methods defined only for element types satisfying additive_group_concept.

template<class T = value_type>
std::enable_if< is_additive_group< T >::value, derived_type >::type operator- () const
 Returns an additive inversion of the matrix, i.e. a matrix with all elements to be an additive inversion of corresponding elements of the current matrix. More...
 
Methods defined for commutative ring elements

Methods defined only for element types satisfying commutative_ring_concept.

size_type Rank () const
 Calculates a rank of the current matrix. More...
 
value_type Det () const
 Calculates the determinant of the matrix. More...
 
value_type Minor (size_type iColumn, size_type iRow) const
 Calculates a minor for the specified element of the matrix. More...
 
value_type Cofactor (size_type iColumn, size_type iRow) const
 Returns a cofactor associated with the specified element. More...
 
derived_type Cofactor () const
 Returns a new matrix with each element being a cofactor value for the corresponding element in the current matrix. . More...
 
derived_typeCofactorMe ()
 Replaces each element within the current matrix with the corresponding cofactor element and returns a reference to the current matrix. . More...
 
derived_type Adjugate () const
 Calculates an adjugate form of the matrix and returns the result as a new matrix. More...
 
derived_typeAdjugateMe ()
 Calculates an adjugate form of the matrix in-place. More...
 
Methods defined for division ring elements

Methods defined only for element types satisfying division_ring_concept.

derived_typeReducedRowEchelonFormMe ()
 Calculates a reduced row echelon form of the current matrix using Gaussian elimination, replaces contents of the current matrix with the result, and returns a reference to the matrix. More...
 
derived_type ReducedRowEchelonForm () const
 Calculates a reduced row echelon form of the current matrix using Gaussian elimination and returns the result as a newly created matrix. More...
 
derived_type SolveRightSLE (const Matrix &refResult) const
 Solves a system of linear equations with the current matrix being multiplied to the solution vector from the right such that the specified result is to be a product. More...
 
Methods defined for field elements

Methods defined only for element types satisfying field_concept.

derived_type MultiplicativeInverse () const
 Calculates an inverse form of the current matrix returning the result as a newly created matrix. More...
 
derived_typeMultiplicativeInverseMe ()
 Inverses the current matrix and returns a reference to it. More...
 
derived_type Inverse () const
 Same as MultiplicativeInverse.
 
derived_typeInverseMe ()
 Same as MultiplicativeInverseMe.
 

Static Public Attributes

static const interpret_array_as_matrix_rows_set_t interpret_array_as_matrix_rows_set = interpret_array_as_matrix_rows_set_t::interpret_array_as_matrix_rows_set
 An integral mark used in constructors to specify how to interpret a set of array elements: as an array of rows or an array of columns. The value is used to spesify the former (default) case.
 
static const interpret_array_as_matrix_columns_set_t interpret_array_as_matrix_columns_set = interpret_array_as_matrix_columns_set_t::interpret_array_as_matrix_columns_set
 An integral mark used in constructors to specify how to interpret a set of array elements: as an array of rows or an array of columns. The value is used to spesify the latter case.
 

Protected Member Functions

pointer GetColumnDirect (size_type iColumn)
 Retrieves a mutable pointer to contiguous elements of the specified matrix column. Neither presence of the column nor validity of the column index are validated. More...
 
const_pointer GetColumnDirect (size_type iColumn) const
 Retrieves a read-only pointer to contiguous elements of the specified matrix column. Neither presence of the column nor validity of the column index are validated. More...
 
reference GetElementDirect (size_type iColumn, size_type iRow)
 Retrieves a mutable reference to a matrix element at a specified position. No validity check for the indices is performed. More...
 
const_reference GetElementDirect (size_type iColumn, size_type iRow)
 Retrieves a read-only reference to a matrix element at a specified position. No validity check for the indices is performed. More...
 
derived_typeSetParallelismPolicy (unspecified_type policy)
 Sets parallelism policy, and, for the dynamically customizable parallelism policy - a policy flag, as a result of unifying policies of two operands of the make_matrix_parallelism_policy function. More...
 
bool set_parallel_mode (bool fParallel=true)
 For matrices with dynamic parallelism sets a mode of a matrix parallel execution. The function is thread safe. More...
 
bool reset_parallel_mode ()
 For matrices with dynamic parallelism resets a mode of a matrix parallel execution to sequential. This is a wrap over set_parallel_mode. More...
 
bool IsParallel () const
 Checks whether the matrix is being processed in parallel mode where possible. More...
 
Protected methods defined for Rng elements

Protected methods defined only for element types satisfying rng_concept.

template<class... transform_row_params_t>
bool RowEchelonMe (transform_row_params_t...transform_row_params)
 Returns one of the existing row echelon forms of the matrix. More...
 
template<class... transform_row_params_t>
bool RowEchelonTransformRow (size_type iPivotColumn, size_type iPivotRow, size_type iTransformRow, transform_row_params_t...transform_row_params)
 Performs a Gaussian row transformation to nullify a specified element located in the same column as a specified pivot element during a row echelon form transformation of the current matrix which is performed by the RowEchelonMe method. More...
 

Detailed Description

template<class Val, class Alloc = std::allocator<Val>, class Derived_t = void, class Policy = default_matrix_execution_policy>
class Chusov::Math::Matrix< Val, Alloc, Derived_t, Policy >

A class template for matrices of elements of various types and semantics, including types which do not have any numeric semantics, as well as types satisfying algebraic_type requirements.

Template Parameters
Valis a type of an element of a matrix. The type is not required to be numeric, although if the type adheres algebraic_type requirements, then operations upon the matrix (i.e. its interface methods) are defined specially by the use of templates. If a type of an element does not satisfy the algebraic_type concept, the matrix is considered just as a 2-dimensional container with container-related operations defined for matrices, such as transposition, row and column positioning, addition and removal of matrix elements and so on, but not operations that require numeric operations upon matrix elements, such as calculating a determinant, or arithmetic operations upon a matrix and its elements. The type must be MoveConstructible and MoveInsertable to the matrix container using the allocator Alloc. Additionally, the type must either be algebraic_type or be DefaultConstructible.
Allocis a type of an allocator object used for managing a storage of the matrix elements. A type chosen by default is std::allocator instantiation for Val. Val must be [MoveInsertible] to a container managed by an Alloc allocator.
Derived_tis a statically defined most-derived class of the matrix. The class can redefine functions of the matrix and provide its qualified name for Derived_t on order for the Matrix template to exploit static polymorphism. The type also can be defined as void (default value) in order to use default implementations of the methods provided by the Matrix class. Hence, this static polymorphism is optional. The type must be MoveConstructible from other instances of Derived_t as well as from the Matrix<Val, Alloc, void, Policy> objects.
Policyis a class specifying policies for implementation of matrix methods. The Policy class is a traits class with type fields customizing behaviour of the matrix in various circumstances. See matrix_execution_policy_traits (which the matrix relies on) and matrix_execution_policy for more information on policies.

The template is built using meta-programming techniques in order to conditionally include interface methods for a specific matrix type depending upon a type of a matrix element, or more precisely, upon traits of the element type, retrieved by a use of algebraic_traits <Val> template instantiation.

The template is conditionally defined for the following types of matrix elements:

Type of an element Class definition
Any type that does not satisfy the algebraic_type concept A matrix is considered just as a two-dimensional array of elements with interface methods operating with their positioning and presence.
Any type that satisfies additive_group_concept In addition to container-related operations, some algebraic operations upon a matrix are defined, which implement addition with another matrix. See an algebraic group for more information. Note that if elements of two matrices are commutatively additive, then the addition of the two matrices is also commutative.
Any type that satisfies rng_concept In addition to addition (as defined for additive groups), the class also defines a multiplication with another matrix, and with a value convertible to the type of a matrix element which implements an Rng.
Any type that satisfies commutative_ring_concept In addition to the Rng-specific operations the interface also defines methods which implement those matrix operations that are well-defined for commutative ring elements, for which multiplication is commutative. The set of such operations over matrices includes calculations of a rank and of a determinant (with factorial complexity over a number of matrix columns/rows), minor and cofactor calculations and so on.
A type that implements a division ring with respect to the division_ring_concept requirements In addition to the Rng-specific operations the interface also defines methods which implement those matrix operations that are well-defined if the matrix elements appertain to a division ring, all elements of which except for additive identity are invertible over multiplication. The set of interface methods for a matrix of division ring elements includes calculating a reduced row echelon form of the matrix using a Gaussian elimination for rows, and a methods for finding any existent solutions of a given set of linear equations.
A type which implements an algebraic field, i.e. satisfies the requirements of the field_concept Such a type unifies definitions of a commutative ring and of a division ring, hence operations upon elements are operations well-defined for a commutative ring supplemented with operations defined for a division ring. Thus, a matrix of these elements provides well-defined operations as stated for commutative ring matrices and division ring matrices. In addition, such matrices are invertible (over multiplication). Also, since a matrix of commutative elements has a sole determinant and because elements of the matrix are invertible, there is a way to calculate determinant using the Gaussian elimination, rather than straightforward calculation. Hence, for field matrices, complexity of a determinant calculation is reduced from \(O\left(n!\right)\) to \(O\left({n}^{3}\right)\).
Note
A well-defined algebraic operation over matrix elements that appertain to a given algebraic structure \(G\) is an operation that makes a sense and that produces a predictable and deterministic result which is solely defined for a each possible set of input parameters from \(G\).
Remarks
If one implements a class C that inherits from
Matrix<value_type, allocator_type, C, policy_type>
or from
Specializes the Matrix template for fixed-sized matrices.
Definition: chsvmath.h:8765
, for given value_type, number_of_rows, number_of_columns, allocator_type, and policy_type, then the following conditions must be adhered to get access to the corresponding functionality:

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