PIP 5.6.1
Platform-Independent Primitives
Public Member Functions | Static Public Member Functions | List of all members
PIMathMatrix< Type > Class Template Reference

Dynamic-size math matrix. Uses PIVector2D as storage and keeps the matrix interface aligned with PIMathMatrixT. More...

#include <pimathmatrix.h>

Inheritance diagram for PIMathMatrix< Type >:
Inheritance graph
[legend]

Public Member Functions

 PIMathMatrix (const uint cols=0, const uint rows=0, const Type &f=Type())
 Constructor of class PIMathMatrix, which creates a matrix. More...
 
 PIMathMatrix (const uint cols, const uint rows, const PIVector< Type > &val)
 Constructor of class PIMathMatrix, which creates a matrix. More...
 
 PIMathMatrix (const PIVector< PIVector< Type > > &val)
 Constructor of class PIMathMatrix, which creates a matrix. More...
 
 PIMathMatrix (const PIVector2D< Type > &val)
 Constructor of class PIMathMatrix, which creates a matrix. More...
 
PIMathMatrix< Type > & setCol (uint index, const PIMathVector< Type > &v)
 Set the selected column in matrix. More...
 
PIMathMatrix< Type > & setRow (uint index, const PIMathVector< Type > &v)
 Set the selected row in matrix. More...
 
PIMathMatrix< Type > & swapCols (uint r0, uint r1)
 Method which swaps selected columns in a matrix. More...
 
PIMathMatrix< Type > & swapRows (uint c0, uint c1)
 Method which replace selected rows in a matrix. More...
 
PIMathMatrix< Type > & fill (const Type &v)
 Method which fills the matrix with selected value. More...
 
bool isSquare () const
 Method which checks if matrix is square. More...
 
bool isIdentity () const
 Method which checks if main diagonal of matrix consists of ones and another elements are zeros. More...
 
bool isNull () const
 Method which checks if every elements of matrix are zeros. More...
 
bool isValid () const
 Method which checks if matrix is empty. More...
 
void operator+= (const PIMathMatrix< Type > &sm)
 Addition assignment with matrix sm. More...
 
void operator-= (const PIMathMatrix< Type > &sm)
 Subtraction assignment with matrix sm. More...
 
void operator*= (const Type &v)
 Multiplication assignment with value v. More...
 
void operator/= (const Type &v)
 Division assignment with value v. More...
 
PIMathMatrix< Type > operator- () const
 Negation operation. More...
 
PIMathMatrix< Type > operator+ (const PIMathMatrix< Type > &sm) const
 Matrix addition. More...
 
PIMathMatrix< Type > operator- (const PIMathMatrix< Type > &sm) const
 Matrix substraction. More...
 
PIMathMatrix< Type > operator* (const Type &v) const
 Matrix multiplication. More...
 
PIMathMatrix< Type > operator/ (const Type &v) const
 Matrix division. More...
 
Type determinant (bool *ok=0) const
 â€‹Calculate Determinant of the matrix. More...
 
Type trace () const
 Calculate the trace of a matrix. More...
 
PIMathMatrix< Type > & toUpperTriangular (bool *ok=0)
 Transforming matrix to upper triangular. More...
 
PIMathMatrix< Type > & invert (bool *ok=0, PIMathVector< Type > *sv=0)
 Matrix inversion operation. More...
 
PIMathMatrix< Type > inverted (bool *ok=0) const
 Matrix inversion operation. More...
 
PIMathMatrix< Type > transposed () const
 Matrix transposition operation. More...
 
- Public Member Functions inherited from PIVector2D< Type >
 PIVector2D ()
 Constructs an empty 2D array. No memory is allocated. After this constructor, rows() and cols() return 0, and isEmpty() returns true. More...
 
 PIVector2D (size_t rows, size_t cols, const Type &f=Type())
 Constructs a 2D array with the given dimensions, filled with copies of f. The underlying storage is a single contiguous block of memory of size rows * cols. All elements are initialized with the value f. More...
 
 PIVector2D (size_t rows, size_t cols, const PIVector< Type > &v)
 Constructs a 2D array from an existing 1D vector, reshaping it. The constructor copies the data from v into the internal flat vector. If v is larger than rows * cols, the excess elements are ignored (the vector is truncated). If v is smaller, other values filled whith default cunstructor T() More...
 
 PIVector2D (size_t rows, size_t cols, PIVector< Type > &&v)
 Move constructs a 2D array from an existing 1D vector, reshaping it. The data is moved from v into the internal flat vector, avoiding a copy. After construction, v is left in a valid but unspecified state. More...
 
 PIVector2D (const PIVector< PIVector< Type > > &v)
 Constructs a 2D array from a vector of vectors (jagged array). Assumes all inner vectors have the same size. If the input is empty, the constructed array is also empty. Otherwise, the number of columns is taken from the size of the first inner vector. All inner vectors are concatenated in the internal flat storage.
 
size_t rows () const
 Returns the number of rows in the 2D array. The result is always non-negative. If the array is empty, returns 0. More...
 
size_t cols () const
 Returns the number of columns in the 2D array. The result is always non-negative. If the array is empty, returns 0. More...
 
size_t size () const
 Returns the total number of elements (rows * cols). This is equivalent to the size of the underlying flat vector. More...
 
ssize_t size_s () const
 Returns the total number of elements as a signed value. More...
 
size_t length () const
 Returns the total number of elements (same as size()). More...
 
size_t capacity () const
 Returns the number of elements that the underlying container has currently allocated space for. This value may be larger than size(). It indicates how many elements can be added before a reallocation is needed.
 
bool isEmpty () const
 Checks if the array has no elements. An empty array has both rows and columns equal to 0. More...
 
bool isNotEmpty () const
 Checks if the array has at least one element. More...
 
Type & element (size_t row, size_t col)
 Returns a reference to the element at the given row and column. No bounds checking is performed. More...
 
const Type & element (size_t row, size_t col) const
 Returns a const reference to the element at the given row and column.
 
Type & element (const Index &idx)
 Returns a reference to the element at the given Index.
 
const Type & element (const Index &idx) const
 Returns a const reference to the element at the given Index.
 
const Type & at (size_t row, size_t col) const
 Returns a const reference to the element at the given row and column No bounds checking is performed.
 
const Type & at (const Index &idx) const
 Returns a const reference to the element at the given Index (bounds-checked only in debug).
 
Type & operator[] (const Index &idx)
 Returns a reference to the element at the given Index.
 
const Type & operator[] (const Index &idx) const
 Returns a const reference to the element at the given Index.
 
Row operator[] (size_t index)
 Returns a proxy object for the row at the given index for modification. More...
 
RowConst operator[] (size_t index) const
 Returns a proxy object for the row at the given index for read-only access.
 
Type * data (size_t index=0)
 Returns a pointer to the underlying flat data starting at an optional offset. More...
 
const Type * data (size_t index=0) const
 Returns a const pointer to the underlying flat data starting at an optional offset.
 
Row row (size_t index)
 Returns a proxy object for the row at the given index for modification. More...
 
RowConst row (size_t index) const
 Returns a proxy object for the row at the given index for read-only access.
 
Col col (size_t index)
 Returns a proxy object for the column at the given index for modification. More...
 
ColConst col (size_t index) const
 Returns a proxy object for the column at the given index for read-only access.
 
PIVector2D< Type > & setRow (size_t row, const RowConst &other)
 Replaces a row with the contents of a read-only RowConst object.
 
PIVector2D< Type > & setRow (size_t row, const PIVector< Type > &other)
 Replaces a row with the contents of a PIVector.
 
PIVector2D< Type > & addRow (const RowConst &other)
 Appends a new row to the bottom of the array from another Row object. If the array was empty, its column count is set to the size of the source row. Otherwise, only min(cols(), other.size()) elements are copied; the rest of the new row is default-initialized. More...
 
PIVector2D< Type > & addRow (const PIVector< Type > &other)
 Appends a new row to the bottom of the array from a PIVector.
 
PIVector2D< Type > & appendRows (size_t count, const Type &f=Type())
 Appends count new empty rows to the bottom of the array, filled with value f. If the array was empty (no columns defined), the column count is set to 1. The new rows are filled with the default value f. More...
 
PIVector2D< Type > & appendColumns (size_t count, const Type &f=Type())
 Appends count new empty columns to the end of each row of the array. If the array was empty (rows not defined), the array becomes a single row with count columns. If the array already has rows, new elements are inserted at the end of each existing row. More...
 
PIVector2D< Type > & deleteRows (size_t row_start, size_t count)
 Deletes count rows starting from the specified row index. Removes the specified rows from the array and updates the row count. If all elements are deleted (array becomes empty), both rows and columns are set to 0. More...
 
PIVector2D< Type > & deleteColumns (size_t col_start, size_t count)
 Removes the specified columns from the array and updates the column count. Removes count columns starting from col_start. If col_start is out of range or count is 0, the function does nothing. If count extends beyond the last column, only available columns are deleted. More...
 
PIVector2D< Type > & addColumn (const ColConst &other)
 Appends a new column to the right of the array from a ColConst.
 
PIVector2D< Type > & addColumn (const PIVector< Type > &other)
 Appends a new column to the right of the array from a PIVector.
 
PIVector2D< Type > & resize (size_t rows, size_t cols, const Type &f=Type())
 Resizes the 2D array to new dimensions. If the new dimensions are larger, new elements are appended and filled with copies of f. If they are smaller, the array is truncated (excess elements are destroyed). The underlying memory may be reallocated. More...
 
bool operator== (const PIVector2D< Type > &t) const
 Equality operator. More...
 
bool operator!= (const PIVector2D< Type > &t) const
 Inequality operator.
 
PIVector< PIVector< Type > > toVectors () const
 Converts the 2D array to a vector of vectors (PIVector<PIVector<T>>). Each row vector is a copy of the corresponding row. More...
 
const PIVector< Type > & asPlainVector () const
 Returns a const reference to the underlying flat PIVector.
 
PIVector< Type > & asPlainVector ()
 Returns a reference to the underlying flat PIVector.
 
PIVector< Type > toPlainVector () const
 Returns a copy of the underlying flat PIVector.
 
void swap (PIVector2D< Type > &other)
 Swaps this 2D array with another. Swaps the flat vectors and the dimension members. Very fast, no memory allocation. More...
 
void clear ()
 Clears the array, removing all elements and setting dimensions to 0. The capacity of the underlying flat vector may remain unchanged. More...
 
bool contains (const Type &e) const
 Checks if the underlying flat vector contains the element e. More...
 
int entries (const Type &e) const
 Counts occurrences of e in the underlying flat vector. More...
 
int entries (std::function< bool(const Type &e)> test) const
 Counts elements in the flat vector that pass the test. More...
 
Index indexOf (const Type &e) const
 Returns the first index (row, col) of e in the 2D array. More...
 
Index indexWhere (std::function< bool(const Type &e)> test, ssize_t start=0) const
 Returns the first index (row, col) in the 2D array that passes the test. More...
 
Index lastIndexOf (const Type &e, ssize_t start=-1) const
 Returns the last index (row, col) of e in the 2D array. More...
 
Index lastIndexWhere (std::function< bool(const Type &e)> test, ssize_t start=-1) const
 Returns the last index (row, col) in the 2D array that passes the test. More...
 
bool any (std::function< bool(const Type &e)> test) const
 Tests if any element in the flat vector passes the test. More...
 
bool every (std::function< bool(const Type &e)> test) const
 Tests if all elements in the flat vector pass the test. More...
 
PIVector2D< Type > & fill (const Type &e=Type())
 Fills the entire 2D array with copies of e. More...
 
PIVector2D< Type > & fill (std::function< Type(size_t i)> f)
 Fills the entire 2D array using a generator function f based on flat index. More...
 
PIVector2D< Type > & assign (const Type &e=Type())
 Same as fill().
 
PIVector2D< Type > & assign (size_t rows, size_t cols, const Type &f=Type())
 Assigns new size and fills with value. More...
 
PIVector2D< Type > transposed () const
 Returns a transposed 2D array (rows become columns and vice versa). The element at (r, c) in the original becomes at (c, r) in the result.
 
PIVector2D< Type > & reverseRows ()
 Reverses the order of rows in place. More...
 
PIVector2D< Type > & reverseColumns ()
 Reverses the order of columns in each row in place. More...
 
PIVector2D< Type > getRange (size_t rowStart, size_t rowCount, size_t colStart, size_t colCount) const
 Returns a sub-2D array (a range of rows and columns). If the range exceeds the array boundaries, it is clipped. If rowCount or colCount is 0, an empty array is returned. More...
 
PIVector2D< ST > map (std::function< ST(const Type &e)> f) const
 Applies a function to each element and returns a new 2D array of a different type. The original array is not modified. More...
 
PIVector2D< ST > mapIndexed (std::function< ST(size_t row, size_t col, const Type &e)> f) const
 Applies a function (with row and col indices) to each element and returns a new 2D array. More...
 
PIVector2D< Type > & forEachRow (std::function< void(Row)> f)
 Applies a function to each row (modifiable). More...
 
void forEachRow (std::function< void(RowConst)> f) const
 Applies a function to each row (read-only).
 
PIVector2D< Type > & forEachColumn (std::function< void(Col)> f)
 Applies a function to each column (modifiable).
 
void forEachColumn (std::function< void(ColConst)> f) const
 Applies a function to each column (read-only).
 
ST reduce (std::function< ST(const Type &e, const ST &acc)> f, const ST &initial=ST()) const
 Accumulates a value across all elements. More...
 
ST reduceIndexed (std::function< ST(size_t row, size_t col, const Type &e, const ST &acc)> f, const ST &initial=ST()) const
 Accumulates a value across all elements with indices. More...
 
PIVector2D< Type > & removeRow (size_t row)
 Removes a row from the 2D array. If the last row is removed and the array becomes empty, cols() is set to 0. More...
 
PIVector2D< Type > & removeColumn (size_t col)
 Removes a column from the 2D array. This operation is more expensive than removing a row because elements must be moved. More...
 
PIVector2D< Type > & removeRowsWhere (std::function< bool(const RowConst &)> test)
 Removes all rows that satisfy a condition. Rows are removed from the bottom to avoid index shifting issues. More...
 
PIVector2D< Type > & removeColumnsWhere (std::function< bool(const ColConst &)> test)
 Removes all columns that satisfy a condition. More...
 
PIVector2D< Type > filterRows (std::function< bool(const RowConst &)> test) const
 Returns a new 2D array containing only the rows that pass the test. More...
 
PIVector2D< Type > filterColumns (std::function< bool(const ColConst &)> test) const
 Returns a new 2D array containing only the columns that pass the test. More...
 

Static Public Member Functions

static PIMathMatrix< Type > identity (const uint cols, const uint rows)
 Creates a matrix whose main diagonal is filled with ones and the remaining elements are zeros. More...
 
static PIMathMatrix< Type > matrixRow (const PIMathVector< Type > &val)
 Creates a row matrix of every element that is equal to every element of the vector. More...
 
static PIMathMatrix< Type > matrixCol (const PIMathVector< Type > &val)
 Creates a column matrix of every element that is equal to every element of the vector. More...
 

Detailed Description

template<typename Type>
class PIMathMatrix< Type >

Dynamic-size math matrix. Uses PIVector2D as storage and keeps the matrix interface aligned with PIMathMatrixT.

A class for dynamic size and fixed type matrix.

Template Parameters
`Type`There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /) of the C++ language are implemented.

Constructor & Destructor Documentation

◆ PIMathMatrix() [1/4]

template<typename Type >
PIMathMatrix< Type >::PIMathMatrix ( const uint  cols = 0,
const uint  rows = 0,
const Type &  f = Type() 
)
inline

Constructor of class PIMathMatrix, which creates a matrix.

Parameters
colsis number of matrix column uint type.
rowsis number of matrix row uint type.
fis type of matrix elements.

◆ PIMathMatrix() [2/4]

template<typename Type >
PIMathMatrix< Type >::PIMathMatrix ( const uint  cols,
const uint  rows,
const PIVector< Type > &  val 
)
inline

Constructor of class PIMathMatrix, which creates a matrix.

Parameters
colsis number of matrix column uint type
rowsis number of matrix row uint type
valis PIVector<Type> of matrix elements

◆ PIMathMatrix() [3/4]

template<typename Type >
PIMathMatrix< Type >::PIMathMatrix ( const PIVector< PIVector< Type > > &  val)
inline

Constructor of class PIMathMatrix, which creates a matrix.

Parameters
valis PIVector<Type> of PIVector, which creates matrix.

◆ PIMathMatrix() [4/4]

template<typename Type >
PIMathMatrix< Type >::PIMathMatrix ( const PIVector2D< Type > &  val)
inline

Constructor of class PIMathMatrix, which creates a matrix.

Parameters
valis PIVector2D<Type>, which creates matrix.

Member Function Documentation

◆ identity()

template<typename Type >
static PIMathMatrix< Type > PIMathMatrix< Type >::identity ( const uint  cols,
const uint  rows 
)
inlinestatic

Creates a matrix whose main diagonal is filled with ones and the remaining elements are zeros.

Parameters
colsis number of matrix column uint type
rowsis number of matrix row uint type
Returns
identity matrix(cols, rows)

◆ matrixRow()

template<typename Type >
static PIMathMatrix< Type > PIMathMatrix< Type >::matrixRow ( const PIMathVector< Type > &  val)
inlinestatic

Creates a row matrix of every element that is equal to every element of the vector.

Parameters
valis the vector type PIMathVector
Returns
row matrix of every element that is equal to every element of the vector

◆ matrixCol()

template<typename Type >
static PIMathMatrix< Type > PIMathMatrix< Type >::matrixCol ( const PIMathVector< Type > &  val)
inlinestatic

Creates a column matrix of every element that is equal to every element of the vector.

Parameters
valis the vector type PIMathVector
Returns
column matrix of every element that is equal to every element of the vector

◆ setCol()

template<typename Type >
PIMathMatrix< Type > & PIMathMatrix< Type >::setCol ( uint  index,
const PIMathVector< Type > &  v 
)
inline

Set the selected column in matrix.

If there are more elements of the vector than elements in the column of the matrix or index larger than the number of columns otherwise there will be "undefined behavior".

Parameters
indexis the number of the selected column.
vis a vector of the type PIMathVector<Type> that needs to fill the column.
Returns
matrix type PIMathMatrix<Type>

◆ setRow()

template<typename Type >
PIMathMatrix< Type > & PIMathMatrix< Type >::setRow ( uint  index,
const PIMathVector< Type > &  v 
)
inline

Set the selected row in matrix.

If there are more elements of the vector than elements in the row of the matrix, or index larger than the number of rows otherwise there will be "undefined behavior".

Parameters
indexis the number of the selected row.
vis a vector of the type PIMathVector<Type> that needs to fill the row.
Returns
matrix type PIMathMatrix<Type>.

◆ swapCols()

template<typename Type >
PIMathMatrix< Type > & PIMathMatrix< Type >::swapCols ( uint  r0,
uint  r1 
)
inline

Method which swaps selected columns in a matrix.

You cannot use an index larger than the number of columns, otherwise there will be "undefined behavior".

Parameters
r0is the number of the first selected column.
r1is the number of the second selected column.
Returns
matrix type PIMathMatrix<Type>.

◆ swapRows()

template<typename Type >
PIMathMatrix< Type > & PIMathMatrix< Type >::swapRows ( uint  c0,
uint  c1 
)
inline

Method which replace selected rows in a matrix.

You cannot use an index larger than the number of rows, otherwise there will be "undefined behavior"

Parameters
c0is the number of the first selected row.
c1is the number of the second selected row.
Returns
matrix type PIMathMatrix<Type>.

◆ fill()

template<typename Type >
PIMathMatrix< Type > & PIMathMatrix< Type >::fill ( const Type &  v)
inline

Method which fills the matrix with selected value.

Parameters
vis a parameter the type and value of which is selected and later filled into the matrix.
Returns
filled matrix type PIMathMatrix<Type>.

◆ isSquare()

template<typename Type >
bool PIMathMatrix< Type >::isSquare ( ) const
inline

Method which checks if matrix is square.

Returns
true if matrix is square, else false.

◆ isIdentity()

template<typename Type >
bool PIMathMatrix< Type >::isIdentity ( ) const
inline

Method which checks if main diagonal of matrix consists of ones and another elements are zeros.

Returns
true if matrix is identitied, else false.

◆ isNull()

template<typename Type >
bool PIMathMatrix< Type >::isNull ( ) const
inline

Method which checks if every elements of matrix are zeros.

Returns
true if matrix is null, else false.

◆ isValid()

template<typename Type >
bool PIMathMatrix< Type >::isValid ( ) const
inline

Method which checks if matrix is empty.

Returns
true if matrix is valid, else false.

◆ operator+=()

template<typename Type >
void PIMathMatrix< Type >::operator+= ( const PIMathMatrix< Type > &  sm)
inline

Addition assignment with matrix sm.

Parameters
smmatrix for the addition assigment.

◆ operator-=()

template<typename Type >
void PIMathMatrix< Type >::operator-= ( const PIMathMatrix< Type > &  sm)
inline

Subtraction assignment with matrix sm.

Parameters
smmatrix for the subtraction assigment.

◆ operator*=()

template<typename Type >
void PIMathMatrix< Type >::operator*= ( const Type &  v)
inline

Multiplication assignment with value v.

Parameters
vvalue for the multiplication assigment.

◆ operator/=()

template<typename Type >
void PIMathMatrix< Type >::operator/= ( const Type &  v)
inline

Division assignment with value v.

Parameters
vvalue for the division assigment.

◆ operator-() [1/2]

template<typename Type >
PIMathMatrix< Type > PIMathMatrix< Type >::operator- ( ) const
inline

Negation operation.

Returns
copy of the negative matrix

◆ operator+()

template<typename Type >
PIMathMatrix< Type > PIMathMatrix< Type >::operator+ ( const PIMathMatrix< Type > &  sm) const
inline

Matrix addition.

Parameters
smis matrix term.
Returns
the result of matrix addition.

◆ operator-() [2/2]

template<typename Type >
PIMathMatrix< Type > PIMathMatrix< Type >::operator- ( const PIMathMatrix< Type > &  sm) const
inline

Matrix substraction.

Parameters
smis matrix subtrahend.
Returns
the result of matrix substraction.

◆ operator*()

template<typename Type >
PIMathMatrix< Type > PIMathMatrix< Type >::operator* ( const Type &  v) const
inline

Matrix multiplication.

Parameters
vis value factor.
Returns
the result of matrix multiplication.

◆ operator/()

template<typename Type >
PIMathMatrix< Type > PIMathMatrix< Type >::operator/ ( const Type &  v) const
inline

Matrix division.

Parameters
vis value divider.
Returns
the result of matrix division.

◆ determinant()

template<typename Type >
Type PIMathMatrix< Type >::determinant ( bool *  ok = 0) const
inline

​Calculate Determinant of the matrix.

Works only with square matrix, nonzero matrices and invertible matrix.

Parameters
okis a parameter with which we can find out if the method worked correctly.
Returns
matrix determinant.

◆ trace()

template<typename Type >
Type PIMathMatrix< Type >::trace ( ) const
inline

Calculate the trace of a matrix.

Works only with square matrix matrix.

Returns
matrix trace.

◆ toUpperTriangular()

template<typename Type >
PIMathMatrix< Type > & PIMathMatrix< Type >::toUpperTriangular ( bool *  ok = 0)
inline

Transforming matrix to upper triangular.

Works only with square matrix, nonzero matrices and invertible matrix.

Parameters
okis a parameter with which we can find out if the method worked correctly.
Returns
copy of transformed upper triangular matrix.

◆ invert()

template<typename Type >
PIMathMatrix< Type > & PIMathMatrix< Type >::invert ( bool *  ok = 0,
PIMathVector< Type > *  sv = 0 
)
inline

Matrix inversion operation.

Works only with square matrix, nonzero matrices and invertible matrix.

Parameters
okis a parameter with which we can find out if the method worked correctly.
Returns
inverted matrix.

◆ inverted()

template<typename Type >
PIMathMatrix< Type > PIMathMatrix< Type >::inverted ( bool *  ok = 0) const
inline

Matrix inversion operation.

Works only with square matrix, nonzero matrices and invertible matrix.

Parameters
okis a parameter with which we can find out if the method worked correctly.
Returns
copy of inverted matrix.

◆ transposed()

template<typename Type >
PIMathMatrix< Type > PIMathMatrix< Type >::transposed ( ) const
inline

Matrix transposition operation.

Works only with square matrix matrix.

Returns
copy of transposed matrix