MathToolKit  1.1.1
C++ maths framework
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
NPMatrix< T > Class Template Reference

Representation of dense matrices of arbitrary size in a template field T. More...

Inheritance diagram for NPMatrix< T >:
NVector< T >

Public Member Functions

 NPMatrix (size_t n=0, size_t p=0)
 Construct a \( n \times p \) matrix initialized with NVector(size_t dim) constructor. More...
 
 NPMatrix (const vector< vector< T > > &data)
 Construct a matrix using a bi-dimensional std::vector. \( Aij \) represents data[i][j]. More...
 
 NPMatrix (const NPMatrix< T > &m)
 Construct a matrix by using copy() method. More...
 
 NPMatrix (initializer_list< initializer_list< T >> list)
 Construct a \( n \times p \) matrix using a bi-dimensional initializer list {{}}. More...
 
 NPMatrix (const NVector< T > &u, size_t n=1)
 Construct a \( n \) rows matrix using a std::vector. More...
 
 NPMatrix (const vector< NVector< T > > &vectors)
 Construct a \( n \times p \) matrix using a vector<NVector<T>>.` All the vectors must have the same dimension. More...
 
 ~NPMatrix ()
 
string str () const override
 Create a string representing the matrix. More...
 
bool isSquare () const
 
bool isUpper () const
 
bool isLower () const
 
bool isDiagonal () const
 
NPMatrix< T > & operator= (const NPMatrix< T > &m)
 
Getters
size_t n () const
 Number of rows \( n \). More...
 
size_t p () const
 Number of columns \( p \). More...
 
NVector< T > row (size_t i) const
 \( i^{th} \) row of the matrix as a NVector<T>. More...
 
NVector< T > col (size_t j) const
 \( j^{th} \) column the matrix as a NVector<T>. More...
 
vector< NVector< T > > rows (size_t i1=0, size_t i2=MAX_SIZE) const
 Create an array containing the rows of the matrix. More...
 
vector< NVector< T > > cols (size_t j1=0, size_t j2=MAX_SIZE) const
 Create an array containing the columns of the matrix in the form of std::vector<NVector<T>>. More...
 
NPMatrix< T > upper () const
 Create a new matrix containing upper part of this matrix. More...
 
NPMatrix< T > lower () const
 Create a new matrix containing lower part of this matrix. More...
 
NPMatrix< T > lupL () const
 \( L \) matrix of \( LU \) decomposition of the matrix. More...
 
NPMatrix< T > lupU () const
 \( U \) matrix of \( LU \) decomposition of the matrix. More...
 
Setters

Setters of a NPMatrix.

All the setters return a reference to *this in order to allow cascade calls eg setRow().setCol(). Setters cannot be used with operator()(size_t, size_t, size_t, size_t).

Returns
Setters returns a reference to *this.
NPMatrix< T > & setRow (const NVector< T > &u, size_t i1)
 Set row with given vector. More...
 
NPMatrix< T > & setCol (const NVector< T > &u, size_t j1)
 Set column with given vector. More...
 
NPMatrix< T > & setRows (const vector< NVector< T >> &vectors, size_t i1=0)
 Replace the components of the matrix with the array of vectors. More...
 
NPMatrix< T > & setCols (const vector< NVector< T >> &vectors, size_t j1=0)
 Replace the components of the matrix with the array of vectors. More...
 
Manipulators

Manipulators such as swap, shift...

Manipulators allow cascade calls. The behavior is the same as Setters.

NPMatrix< T > & swap (size_t i1, size_t j1, size_t i2, size_t j2)
 Swap \( A_{i_1j_1} \) and \( A_{i_2j_2} \). More...
 
NPMatrix< T > & swapRow (size_t i1, size_t i2)
 Swap \( R_{i_1} \) and \( R_{i_2} \). More...
 
NPMatrix< T > & swapCol (size_t j1, size_t j2)
 Swap \( C_{j_1} \) and \( C_{j_2} \). More...
 
NPMatrix< T > & shiftRow (size_t i, long iterations=1)
 shift the \( i^{th} \) row iterations times. More...
 
NPMatrix< T > & shiftCol (size_t j, long iterations=1)
 shift the \( j^{th} \) column iterations times. More...
 
Algebra

Matrix algebraical operations.

Algebraical operations consists on method specific to matrices algebra such as inversion, determinant...

NPMatrix< T > transposed () const
 Transposed matrix. More...
 
trace () const
 Trace of this matrix \( A_{00} + A_{11} + ... + A_{(n-1)(n-1)} \). More...
 
NPMatrix< T > shifted (const NPMatrix< T > &m) const
 Concatenate matrix m to this matrix. More...
 
NPMatrix< T > & reduce ()
 Apply Gauss Jordan elimination on matrix to calculate inverse without using \( LU \) decomposition. More...
 
det () const
 determinant of this matrix \( det(A) \). Using the \( LU \) decomposition \( O(n) \). More...
 
Function Operator

Access to components and sub-matrix

C++ function operator are here used to give intuitive access to the matrix.

T & operator() (size_t i, size_t j)
 Bi-dimensional access operator. More...
 
operator() (size_t i, size_t j) const
 
NPMatrix< T > operator() (size_t i1, size_t j1, size_t i2, size_t j2) const
 Manipulate sub-matrix. More...
 
NPMatrix< T > & operator() (size_t i1, size_t j1, size_t i2, size_t j2)
 Manipulate sub-matrix. More...
 
- Public Member Functions inherited from NVector< T >
 NVector (const std::vector< T > &data)
 Construct a vector using a std::vector. \( x_k \) represents data[k]. More...
 
 NVector (std::initializer_list< T > list)
 Construct a vector using an initializer list {}. More...
 
 NVector (size_t dim=0)
 Construct a vector of a given size. More...
 
 NVector (const NVector< T > &u)
 Construct a vector by using copy() method. More...
 
virtual ~NVector ()=default
 
size_t dim () const
 Dimension of the vector. More...
 
std::vector< T > array () const
 Creates an array with this vector. More...
 
NVector< T > & operator+= (const NVector< T > &u)
 
NVector< T > & operator-= (const NVector< T > &u)
 
NVector< T > & operator= (const NVector< T > &u)
 Copy source object on this object using copy(). More...
 
max () const
 Maximum of the coordinates of the vector \( (x_0, x_1, .. x_{(n-1)}) \). More...
 
min () const
 Minimum of the coordinates of the vector \( (x_0, x_1, .. x_{(n-1)}) \). More...
 
size_t maxIndex () const
 Index of maximum of the coordinates of vector \( (x_0, x_1, ..., x_{(n-1)}) \). More...
 
size_t minIndex () const
 Index of minimum of the coordinates of vector \( (x_0, x_1, ..., x_{(n-1)}) \). More...
 
maxAbs () const
 Maximum of the coordinates of the vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \). More...
 
minAbs () const
 Minimum of the coordinates of the vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \). More...
 
size_t maxAbsIndex () const
 Index of maximum of the coordinates of vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \). More...
 
size_t minAbsIndex () const
 Index of minimum of the coordinates of vector \( (|x_0|, |x_1|, ..., |x_{(n-1)}|) \). More...
 
NVector< T > & swap (size_t k1, size_t k2)
 Permutation of two elements. More...
 
NVector< T > & shift (long iterations)
 Shifts this vector iterations times. More...
 
NVector< T > & fill (T s)
 Fill vector with a scalar. For example fill(3) will set \( (3, 3, 3, ..., 3) \). More...
 
T & operator() (long k)
 Simple access operator. More...
 
operator() (long k) const
 
NVector< T > operator() (size_t k1, size_t k2) const
 Manipulate sub-vector. More...
 
NVector< T > & operator() (size_t k1, size_t k2)
 Manipulate sub-vector. More...
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 

Static Public Member Functions

static NPMatrix< T > zeros (size_t n, size_t p=0)
 \( n \times p \) matrix filled with 0. More...
 
static NPMatrix< T > ones (size_t n, size_t p=0)
 \( n \times p \) matrix filled with 1. More...
 
static NPMatrix< T > cano (size_t i, size_t j, size_t n, size_t p=0)
 \( n \times p \) canonical matrix More...
 
static NPMatrix< T > eye (size_t n)
 \( n^{th} \) order identity matrix More...
 
static NPMatrix< T > diag (const vector< T > &data, size_t n)
 diagonal \( n^{th} \) order diagonal matrix filled with data array. More...
 
static NPMatrix< T > scalar (T s, size_t n)
 a scalar \( n^{th} \) order matrix with s value More...
 
static NPMatrix< T > ndiag (const vector< NVector< T > > &data)
 Fills a square matrix matrix by giving diagonal data. More...
 
static NPMatrix< T > nscalar (const vector< T > &scalars, size_t n)
 Generalization of scalar matrix with multiple diagonal. More...
 
- Static Public Member Functions inherited from NVector< T >
static NVector< T > zeros (size_t dim)
 
static NVector< T > ones (size_t dim)
 
static NVector< T > scalar (T s, size_t dim)
 
static NVector< T > cano (size_t k, size_t dim)
 
static NVector< T > sum (const std::vector< NVector > &vectors)
 Sum of multiples vectors. More...
 
static NVector< T > sumProd (const std::vector< T > &scalars, const std::vector< NVector > &vectors)
 Linear combination of scalars and vectors. More...
 

Protected Member Functions

 NPMatrix (const NVector< T > &u, size_t n, size_t p, size_t i1=0, size_t j1=0, size_t i2=0, size_t j2=0)
 
NPMatrix< T > & swap (Parts element, size_t k1, size_t k2)
 
NPMatrix< T > & shift (Parts element, size_t k, long iterations)
 
size_t maxAbsIndex (Parts element, size_t k, size_t r) const
 
size_t maxAbsIndexRow (size_t i, size_t r=0) const
 
size_t maxAbsIndexCol (size_t j, size_t r=0) const
 
NVector< T > & vectorProduct (NVector< T > &u) const
 
NPMatrix< T > & matrixProduct (const NPMatrix< T > &m)
 
NPMatrix< T > & add (const NPMatrix< T > &m)
 
NPMatrix< T > & sub (const NPMatrix< T > &m)
 
NPMatrix< T > & opp () override
 
NPMatrix< T > & prod (T s) override
 
NPMatrix< T > & div (T s) override
 
NPMatrix< T > & pow (long exp)
 
void rPow (long exp)
 
NPMatrix< T > & inv ()
 
NVector< T > & solve (NVector< T > &u) const
 
void lupClear () const
 
void lupReset () const
 
void lupCopy (const NPMatrix &m) const
 
void lupSelfCopy () const
 
void lupUpdate () const
 
NPMatrix< T > & clean () const
 
NPMatrix< T > & cleanBoth (const NPMatrix< T > &m) const
 
bool isValidRowIndex (size_t i) const
 
bool isValidColIndex (size_t j) const
 
bool isValidIndex (size_t i, size_t j) const
 
bool isBetweenI12 (size_t i) const
 
bool isBetweenJ12 (size_t j) const
 
bool matchSizeForProduct (const NVector< T > &u) const
 
bool matchSizeForProduct (const NPMatrix< T > &m) const
 
bool hasSameSize (const NPMatrix< T > &m) const
 
bool hasDefaultBrowseIndices () const override
 
void setDefaultBrowseIndices () const override
 
NPMatrix< T > & copy (const NPMatrix< T > &m)
 
NPMatrix< T > & copy (const vector< vector< T >> &data)
 
size_t vectorIndex (size_t i, size_t j) const
 
size_t getRowFromVectorIndex (size_t k) const
 
size_t getColFromVectorIndex (size_t k) const
 
NPMatrix< T > subMatrix (size_t i1=0, size_t j1=MAX_SIZE, size_t i2=0, size_t j2=MAX_SIZE) const
 
NPMatrix< T > & setSubMatrix (const NPMatrix< T > &m)
 
NPMatrix< T > & forEach (const NPMatrix< T > &m, const function< void(T &, const T &)> &binary_op)
 
NPMatrix< T > & forEach (T s, const function< void(T &, T)> &binary_op) override
 
- Protected Member Functions inherited from NVector< T >
 NVector (const std::vector< T > &data, size_t k1, size_t k2)
 Vector with regularly spaced components. More...
 
NVector< T > & add (const NVector< T > &u)
 
NVector< T > & sub (const NVector< T > &u)
 
dotProduct (const NVector< T > &u) const
 
norm () const
 
distance (const NVector< T > &u) const
 
bool isValidIndex (size_t k) const
 
bool isBetweenK12 (size_t k) const
 
bool isNull () const
 
bool isEqual (const NVector< T > &u) const
 
bool hasSameSize (const NVector< T > &u) const
 
NVector< T > & forEach (const NVector< T > &u, const std::function< void(T &, const T &)> &binary_op)
 
virtual NVector< T > & forEach (T s, const std::function< void(T &, T)> &binary_op)
 
NVector< T > & copy (const NVector< T > &u)
 
NVector< T > subVector (size_t k1, size_t k2) const
 
void setSubVector (const NVector< T > &u)
 

Static Protected Member Functions

static size_t pIfNotNull (size_t n, size_t p)
 

Protected Attributes

size_t _n {}
 
size_t _p {}
 
size_t _i1 {}
 
size_t _j1 {}
 
size_t _i2 {}
 
size_t _j2 {}
 
unique_ptr< NPMatrix< T > > _a {}
 Matrix \( A = LU \) where \( PA = LU \) = this. More...
 
unique_ptr< vector< size_t > > _perm {}
 permutation vector \( P \) such as \( PA = LU \). More...
 
- Protected Attributes inherited from NVector< T >
size_t _k1 {}
 
size_t _k2 {}
 

Friends

bool operator== (const NPMatrix< T > &a, const NPMatrix< T > &b)
 
bool operator!= (const NPMatrix< T > &a, const NPMatrix< T > &b)
 

Algebraical Operators

Commons algebraical operations for matrix

New algebraical operations are documented here. the operations that already exist in NVector are implemented the same way as they are in NVector.

NPMatrix< T > & operator+= (const NPMatrix< T > &m)
 
NPMatrix< T > & operator-= (const NPMatrix< T > &m)
 
NPMatrix< T > & operator*= (const NPMatrix< T > &m)
 
NPMatrix< T > & operator*= (T s) override
 
NPMatrix< T > & operator/= (T s) override
 
NPMatrix< T > & operator^= (long exp)
 
NPMatrix< T > operator+ (NPMatrix< T > a, const NPMatrix< T > &b)
 
NPMatrix< T > operator- (NPMatrix< T > a, const NPMatrix< T > &b)
 
NPMatrix< T > operator- (NPMatrix< T > m)
 
NPMatrix< T > operator* (T s, NPMatrix< T > m)
 
NPMatrix< T > operator* (NPMatrix< T > m, T s)
 
NPMatrix< T > operator* (NPMatrix< T > a, const NPMatrix< T > &b)
 Usual matrix multiplication. More...
 
NVector< T > operator* (const NPMatrix< T > &m, NVector< T > v)
 Usual matrix vector product (linear mapping). More...
 
NPMatrix< T > operator/ (NPMatrix< T > m, T s)
 
NPMatrix< T > operator^ (NPMatrix< T > m, long exp)
 Exponantiate matrix \( M \) using given exponent. More...
 
NVector< T > operator% (const NPMatrix< T > &m, NVector< T > v)
 Solve the linear system formed by \( M \) and \( v \). More...
 
operator| (const NPMatrix< T > &a, const NPMatrix< T > &b)
 
operator! (const NPMatrix< T > &m)
 
operator/ (const NPMatrix< T > &a, const NPMatrix< T > &b)
 
NVector< T > & operator*= (NVector< T > &u, const NPMatrix< T > &m)
 
NVector< T > & operator%= (NVector< T > &u, const NPMatrix< T > &m)
 

Detailed Description

template<typename T>
class NPMatrix< T >

Representation of dense matrices of arbitrary size in a template field T.

Date
04/05/2018
Author
samiBendou

The matrix components are stored in a linear form using the index transformation \( k = p i + j \). The underlying std::vector<T> is represented as t[p * i + j]. The underlying NVector<T> is \( (A_{00}, A_{01}, ..., A_{0(P - 1)}, A_{10}, ..., A_{1(P - 1)}, ..., A_{(N-1)0}, ...) \).

Features

     Featuring algebraical operations such as matrix product, linear map, Gauss Jordan elimination.
     setters & getters, swappers and classic matrix generators such as ones, zeros...

LU Decomposition

     The \form#3 decomposition is stored as a property if the matrix is inversible. It is auto-updated only when needed.
     It allow to compute inverse, determinant and other inversion related operations more efficiently.

Sub-range operators

     The `NPMatrix` class provides a function operator similar to @ref FuncOpVec

Definitions

     All along this page we will use the following definitions :
        - `a`/`b`/`m`: A given `NPMatrix`. The components are noted are noted \form#4.
                       By default, \form#5 denote `this` matrix.
        - `R`        : Row of a matrix
        - `C`        : Col of a matrix
        - `n `       : Number of rows
        - `p `       : Number of Columns
        - `i `       : Row Index between \form#6
        - `j `       : Column Index \form#7
        - `k`, `l`   : either compound index or underlying `std::vector` array index.

Constructor & Destructor Documentation

template<typename T>
NPMatrix< T >::NPMatrix ( size_t  n = 0,
size_t  p = 0 
)
inlineexplicit

Construct a \( n \times p \) matrix initialized with NVector(size_t dim) constructor.

template<typename T>
NPMatrix< T >::NPMatrix ( const vector< vector< T > > &  data)
inline

Construct a matrix using a bi-dimensional std::vector. \( Aij \) represents data[i][j].

Parameters
databi-dimensional std::vector source.

All the data[i] must have the same length. They are seen as the rows of the matrix. data is copied into this matrix using copy().

template<typename T>
NPMatrix< T >::NPMatrix ( const NPMatrix< T > &  m)
inline

Construct a matrix by using copy() method.

Parameters
mNPMatrix source.
template<typename T>
NPMatrix< T >::NPMatrix ( initializer_list< initializer_list< T >>  list)
inline

Construct a \( n \times p \) matrix using a bi-dimensional initializer list {{}}.

Parameters
listbi-dimensional std::initializer_list source.

Convert list and uses bi-dimensional array constructor.

template<typename T>
NPMatrix< T >::NPMatrix ( const NVector< T > &  u,
size_t  n = 1 
)
inlineexplicit

Construct a \( n \) rows matrix using a std::vector.

Parameters
uNVector source of size \( q \)
nnumber of rows formed by u

The matrix is of size \( n \times p = q \) with \( p = q / n \). Resulting p is computed using integer division so if reminder is not null a part of u will be truncated.

template<typename T>
NPMatrix< T >::NPMatrix ( const vector< NVector< T > > &  vectors)
inlineexplicit

Construct a \( n \times p \) matrix using a vector<NVector<T>>.` All the vectors must have the same dimension.

Parameters
vectorsbi-dimensional std::vector source.

Convert vectors and uses bi-dimensional array constructor.

template<typename T>
NPMatrix< T >::~NPMatrix ( )
inline
template<typename T>
NPMatrix< T >::NPMatrix ( const NVector< T > &  u,
size_t  n,
size_t  p,
size_t  i1 = 0,
size_t  j1 = 0,
size_t  i2 = 0,
size_t  j2 = 0 
)
explicitprotected

Member Function Documentation

template<typename T >
string NPMatrix< T >::str ( ) const
overridevirtual

Create a string representing the matrix.

The returned string has the following form :

```commandline "(m(0,0), m(0,1), ..., m(0,(P-1))) (m(1,0), m(1,1), ..., m(1,(P-1))) (m(i,0), ..., a(i,j, Ai(P-1))) (m((N-1),0), ..., ..., ...)" `` Wheremisthismatrix. Each row ofmis formatted usingNVector::str()`.

Returns
Returns a string representing the matrix.

Reimplemented from NVector< T >.

template<typename T>
bool NPMatrix< T >::isSquare ( ) const
inline
template<typename T >
bool NPMatrix< T >::isUpper ( ) const
template<typename T >
bool NPMatrix< T >::isLower ( ) const
template<typename T >
bool NPMatrix< T >::isDiagonal ( ) const
template<typename T>
size_t NPMatrix< T >::n ( ) const
inline

Number of rows \( n \).

template<typename T>
size_t NPMatrix< T >::p ( ) const
inline

Number of columns \( p \).

template<typename T >
NVector< T > NPMatrix< T >::row ( size_t  i) const

\( i^{th} \) row of the matrix as a NVector<T>.

template<typename T >
NVector< T > NPMatrix< T >::col ( size_t  j) const

\( j^{th} \) column the matrix as a NVector<T>.

template<typename T >
vector< NVector< T > > NPMatrix< T >::rows ( size_t  i1 = 0,
size_t  i2 = MAX_SIZE 
) const

Create an array containing the rows of the matrix.

Parameters
i1First row to be taken.
i2Last row to be taken \( i_1 \leq i_2 \).

The behavior of rows() is the following :

  • rows() returns all the rows.
  • rows(i1) returns the rows \( [R_{i1}, R_{(i1+1)},..., R_{(n-1)}] \)
  • rows(i1, i2) returns the rows \( [R_{i1}, R_{(i1+1)},..., R_{i2}] \)
Returns
Returns an array containing the rows of the matrix as NVector.
template<typename T >
vector< NVector< T > > NPMatrix< T >::cols ( size_t  j1 = 0,
size_t  j2 = MAX_SIZE 
) const

Create an array containing the columns of the matrix in the form of std::vector<NVector<T>>.

Parameters
j1First column to be taken.
j2Last column to be taken \( j_1 \leq j_2 \).

The behavior of cols() is the analog to rows().

Returns
Returns an array containing the column of the matrix as NVector.
template<typename T >
NPMatrix< T > NPMatrix< T >::upper ( ) const

Create a new matrix containing upper part of this matrix.

Returns
Returns upper part of this matrix. The lower part contains 0.
template<typename T >
NPMatrix< T > NPMatrix< T >::lower ( ) const

Create a new matrix containing lower part of this matrix.

Returns
Returns lower part of this matrix. The upper part contains 0.
template<typename T >
NPMatrix< T > NPMatrix< T >::lupL ( ) const

\( L \) matrix of \( LU \) decomposition of the matrix.

template<typename T >
NPMatrix< T > NPMatrix< T >::lupU ( ) const

\( U \) matrix of \( LU \) decomposition of the matrix.

template<typename T>
NPMatrix< T > & NPMatrix< T >::setRow ( const NVector< T > &  u,
size_t  i1 
)

Set row with given vector.

Parameters
usource NVector.
i1index of row to set.

The dimension of the vector must be inferior or equal to the number of columns.

template<typename T>
NPMatrix< T > & NPMatrix< T >::setCol ( const NVector< T > &  u,
size_t  j1 
)

Set column with given vector.

Parameters
usource NVector.
j1index of column to set.

Same behavior as setRow().

template<typename T>
NPMatrix< T > & NPMatrix< T >::setRows ( const vector< NVector< T >> &  vectors,
size_t  i1 = 0 
)

Replace the components of the matrix with the array of vectors.

Parameters
vectorsRows to set on the matrix. std::vector of NVector<T>.
i1First row to set.

The input vectors must verify the following conditions :

  • The length of each NVector<T> must be inferior or equal to the number of columns.
  • The total size of vectors must be inferior or equal to the number of rows.

If i1 + vectors.size() is greater than n Then the algorithm truncate the array of NVector<T>.

If the size of vectors is \( n \times q \) than the setRows(vectors) will return :

\[ \begin{bmatrix} v_{00} & ... & ... & ... & v_{0(p-1)} \\ v_{10} & ... & ... & ... & v_{1(p-1)} \\ ... & ... & ... & ... & ... \\ v_{(q-1)0} & ... & ... & ... & v_{(q-1)(p-1)} \\ A_{q0} & ... & ... & ... & A_{q(p-1)} \\ ... & ... & ... & ... & ... \\ A_{(n-1)0} & ... & ... & ... & A_{(n-1)(j1 - 1)} \\ \end{bmatrix} \]

Where \( v_{ij} \) represents vectors[i](j).

template<typename T>
NPMatrix< T > & NPMatrix< T >::setCols ( const vector< NVector< T >> &  vectors,
size_t  j1 = 0 
)

Replace the components of the matrix with the array of vectors.

Parameters
vectorsRows to set on the matrix. std::vector of NVector<T>.
j1Start index to set row.

The behavior of setCols() is analog to setRows().

template<typename T>
NPMatrix<T>& NPMatrix< T >::swap ( size_t  i1,
size_t  j1,
size_t  i2,
size_t  j2 
)
inline

Swap \( A_{i_1j_1} \) and \( A_{i_2j_2} \).

Parameters
i1first row indices to swap
j1first col indices to swap
i2second row indices to swap
j2second col indices to swap
template<typename T>
NPMatrix<T>& NPMatrix< T >::swapRow ( size_t  i1,
size_t  i2 
)
inline

Swap \( R_{i_1} \) and \( R_{i_2} \).

Parameters
i1first row indices to swap
i2second row indices to swap
template<typename T>
NPMatrix<T>& NPMatrix< T >::swapCol ( size_t  j1,
size_t  j2 
)
inline

Swap \( C_{j_1} \) and \( C_{j_2} \).

Parameters
j1first col indices to swap
j2second col indices to swap
template<typename T>
NPMatrix<T>& NPMatrix< T >::shiftRow ( size_t  i,
long  iterations = 1 
)
inline

shift the \( i^{th} \) row iterations times.

Parameters
iindex of row to shift
iterationsnumber of times to shift.

If iterations is positive, shift is powered to the left, else to the right. For example shiftRow(0, 2) will set the matrix to :

\[ \begin{bmatrix} A_{20} & A_{30} & ... & A_{(n-1)0} & A_{00} & A_{10} \\ ... & ... & ... & ... & ... & ... \\ ... & ... & ... & ... & ... & ... \\ A_{(n-1)0} & A_{(n-1)1} & ... & ... & ... & A_{(n-1)(p-1)}\\ \end{bmatrix} \]

template<typename T>
NPMatrix<T>& NPMatrix< T >::shiftCol ( size_t  j,
long  iterations = 1 
)
inline

shift the \( j^{th} \) column iterations times.

Parameters
jindex of column to shift
iterationsnumber of times to shift.

The behavior is analog to shiftRow(). If iterations is positive, shift is powered to the up, else to the bottom.

template<typename T >
NPMatrix< T > NPMatrix< T >::transposed ( ) const

Transposed matrix.

Returns
Value of transposed \( A^\top \).
template<typename T >
T NPMatrix< T >::trace ( ) const

Trace of this matrix \( A_{00} + A_{11} + ... + A_{(n-1)(n-1)} \).

Returns
Value of the trace.
template<typename T>
NPMatrix< T > NPMatrix< T >::shifted ( const NPMatrix< T > &  m) const

Concatenate matrix m to this matrix.

Parameters
mMatrix to concatenate.

The shifted matrix is the matrix obtained after concatenation of \( A \) and \( M \) columns. \( A \) and \( M \) must have the same number of rows.

Returns
Returns the value of shifted matrix \( [ A | M ] \).
template<typename T >
NPMatrix< T > & NPMatrix< T >::reduce ( )

Apply Gauss Jordan elimination on matrix to calculate inverse without using \( LU \) decomposition.

To perform inverse computation, shift the matrix you want to invert than apply this function. If the matrix is inversible, than the inverse of the matrix is on the right part of the matrix. \( O(n^3) \).

Returns
Reference to *this the shifted matrix.
template<typename T >
T NPMatrix< T >::det ( ) const

determinant of this matrix \( det(A) \). Using the \( LU \) decomposition \( O(n) \).

Returns
Value of determinant.
template<typename T>
NPMatrix<T>& NPMatrix< T >::operator+= ( const NPMatrix< T > &  m)
inline
template<typename T>
NPMatrix<T>& NPMatrix< T >::operator-= ( const NPMatrix< T > &  m)
inline
template<typename T>
NPMatrix<T>& NPMatrix< T >::operator*= ( const NPMatrix< T > &  m)
inline
template<typename T>
NPMatrix<T>& NPMatrix< T >::operator*= ( s)
inlineoverridevirtual

Reimplemented from NVector< T >.

template<typename T>
NPMatrix<T>& NPMatrix< T >::operator/= ( s)
inlineoverridevirtual

Reimplemented from NVector< T >.

template<typename T>
NPMatrix<T>& NPMatrix< T >::operator^= ( long  exp)
inline
template<typename T>
T& NPMatrix< T >::operator() ( size_t  i,
size_t  j 
)
inline

Bi-dimensional access operator.

Access operator equivalent to [i][j] on bi-dimensional arrays. The behavior is analog to NVector::operator()(long) except that negatives values are disallowed. For example, A(0, 0) return the element located at first row and first column.

Returns
component \( A_{ij} \) of the matrix.
template<typename T>
T NPMatrix< T >::operator() ( size_t  i,
size_t  j 
) const
inline
template<typename T>
NPMatrix<T> NPMatrix< T >::operator() ( size_t  i1,
size_t  j1,
size_t  i2,
size_t  j2 
) const
inline

Manipulate sub-matrix.

Parameters
i1first row to take
j1first col to take
i2last row to take \( n \gt i_2 \geq i_1 \geq 0 \)
j2last row to take \( p \gt j_2 \geq j_1 \geq 0 \) of columns

This operator is similar to NVector<T>::operator()(size_t, size_t) const "vector sub-range operator". It allows operations on a restricted range of the matrix :

\[ \begin{bmatrix} A_{i_1j_1} & ... & A_{i_2j_1} \\ ... & ... & ... \\ A_{i_2j_1} & ... & A_{i_2j_2} \\ \end{bmatrix} \]

For example, a(0, 0, 1, 1) will return the value of the \( 2 \times 2 \) upper-left matrix :

\[ \begin{bmatrix} A_{00} & A_{01} \\ A_{10} & A_{11} \\ \end{bmatrix} \]

Returns
value of the sub matrix. @
template<typename T >
NPMatrix< T > & NPMatrix< T >::operator() ( size_t  i1,
size_t  j1,
size_t  i2,
size_t  j2 
)

Manipulate sub-matrix.

This operator is similar to previous operator()(size_t, size_t, size_t, size_t) const "operator" except that it sets browse indices. See NVector operator for more details.

Returns
reference to *this.
template<typename T>
NPMatrix<T>& NPMatrix< T >::operator= ( const NPMatrix< T > &  m)
inline
template<typename T>
static NPMatrix<T> NPMatrix< T >::zeros ( size_t  n,
size_t  p = 0 
)
inlinestatic

\( n \times p \) matrix filled with 0.

template<typename T>
static NPMatrix<T> NPMatrix< T >::ones ( size_t  n,
size_t  p = 0 
)
inlinestatic

\( n \times p \) matrix filled with 1.

template<typename T>
static NPMatrix<T> NPMatrix< T >::cano ( size_t  i,
size_t  j,
size_t  n,
size_t  p = 0 
)
inlinestatic

\( n \times p \) canonical matrix

Parameters
iRow where to put 1.
jCol where to put 1.

canonical matrices \( E_{ij} \) of \( M_{np}(K) \) which contains 1 in position \( ij \) and 0 elsewhere. This matrix is eviqualent to \( \delta_{ij} \) Kronecker's delta symbol.

template<typename T >
NPMatrix< T > NPMatrix< T >::eye ( size_t  n)
static

\( n^{th} \) order identity matrix

Parameters
nSize of the matrix
Returns
\( Id \) identity matrix.
template<typename T>
NPMatrix< T > NPMatrix< T >::diag ( const vector< T > &  data,
size_t  n 
)
static

diagonal \( n^{th} \) order diagonal matrix filled with data array.

Parameters
datavalues of diagonal [ \( d_0, d_1, ..., d_{(n-1)} \)].
nsize of the matrix.
template<typename T>
static NPMatrix<T> NPMatrix< T >::scalar ( s,
size_t  n 
)
inlinestatic

a scalar \( n^{th} \) order matrix with s value

Parameters
nsize of the matrix
sscalar value

Scalar matrices are a diagonal matrix filled a unique value.

Returns
\( n^{th} \) order matrix equal to \( s \cdot Id \).
template<typename T>
NPMatrix< T > NPMatrix< T >::ndiag ( const vector< NVector< T > > &  data)
static

Fills a square matrix matrix by giving diagonal data.

Parameters
datavalues of diagonals as std::vector of NVector.

This method is a generalization of diag() method. The resulting matrix looks like :

\[ \begin{bmatrix} d_{(mid)0} & d_{(mid+1)0} & ... & d_{(2 mid-1)0} \\ d_{(mid-1)0} & d_{(mid)1} & ... & d_{(2 mid-2)1} \\ ... & ... & ... & ...\\ d_{(0)0} & d_{(1)1} & ... & d_{(mid)(n-1)} \\ \end{bmatrix} \]

   Where \form#62 is the representation of `data[l](k)`.

   The `data` array should be as following :
    - `data[l]` is the values of coefficients of the \form#63 sub-diagonal from the left/up.
    - `data[middle]` is the values of coefficients on the diagonal.

   The input data must be ordered such as `d[0]` has size equal to `1`, `d[1]` to `2`, ...,
   `d[middle]` to `n`, `d[middle+1]` to `n-1`, ..., `d[2 * middle - 1]` to `1`.
Returns
a n-diagonal matrix filled with data.
template<typename T>
NPMatrix< T > NPMatrix< T >::nscalar ( const vector< T > &  scalars,
size_t  n 
)
static

Generalization of scalar matrix with multiple diagonal.

Parameters
scalarsarray of scalars values to fill diagonals [ \( s_0, s_1, ..., s_{q-1} \) ].
nsize of the matrix.

The behavior is a restriction of ndiag() method, the matrix is filled using diagonals containing the same value all along.

For example if nscalar({2, 1}, 3) will return :

\[ \begin{bmatrix} 1 & 2 & 0 \\ 2 & 1 & 2 \\ 0 & 2 & 1 \\ \end{bmatrix} \]

Center diagonal is filled with s1 and the other diagonal are filled with s0.

Returns
a n-scalar Matrix filled with given scalars.
template<typename T >
NPMatrix< T > & NPMatrix< T >::swap ( Parts  element,
size_t  k1,
size_t  k2 
)
protected
template<typename T >
NPMatrix< T > & NPMatrix< T >::shift ( Parts  element,
size_t  k,
long  iterations 
)
protected
template<typename T >
size_t NPMatrix< T >::maxAbsIndex ( Parts  element,
size_t  k,
size_t  r 
) const
protected
template<typename T>
size_t NPMatrix< T >::maxAbsIndexRow ( size_t  i,
size_t  r = 0 
) const
inlineprotected
template<typename T>
size_t NPMatrix< T >::maxAbsIndexCol ( size_t  j,
size_t  r = 0 
) const
inlineprotected
template<typename T>
NVector< T > & NPMatrix< T >::vectorProduct ( NVector< T > &  u) const
protected
template<typename T>
NPMatrix< T > & NPMatrix< T >::matrixProduct ( const NPMatrix< T > &  m)
protected
template<typename T>
NPMatrix<T>& NPMatrix< T >::add ( const NPMatrix< T > &  m)
inlineprotected
template<typename T>
NPMatrix<T>& NPMatrix< T >::sub ( const NPMatrix< T > &  m)
inlineprotected
template<typename T>
NPMatrix<T>& NPMatrix< T >::opp ( )
inlineoverrideprotectedvirtual

Reimplemented from NVector< T >.

template<typename T>
NPMatrix<T>& NPMatrix< T >::prod ( s)
inlineoverrideprotectedvirtual

Reimplemented from NVector< T >.

template<typename T>
NPMatrix<T>& NPMatrix< T >::div ( s)
inlineoverrideprotectedvirtual

Reimplemented from NVector< T >.

template<typename T >
NPMatrix< T > & NPMatrix< T >::pow ( long  exp)
protected
template<typename T >
void NPMatrix< T >::rPow ( long  exp)
protected
template<typename T >
NPMatrix< T > & NPMatrix< T >::inv ( )
protected
template<typename T>
NVector< T > & NPMatrix< T >::solve ( NVector< T > &  u) const
protected
template<typename T >
void NPMatrix< T >::lupClear ( ) const
protected
template<typename T >
void NPMatrix< T >::lupReset ( ) const
protected
template<typename T >
void NPMatrix< T >::lupCopy ( const NPMatrix< T > &  m) const
protected
template<typename T>
void NPMatrix< T >::lupSelfCopy ( ) const
inlineprotected
template<typename T >
void NPMatrix< T >::lupUpdate ( ) const
protected
template<typename T>
NPMatrix<T>& NPMatrix< T >::clean ( ) const
inlineprotected
template<typename T>
NPMatrix<T>& NPMatrix< T >::cleanBoth ( const NPMatrix< T > &  m) const
inlineprotected
template<typename T>
bool NPMatrix< T >::isValidRowIndex ( size_t  i) const
inlineprotected
template<typename T>
bool NPMatrix< T >::isValidColIndex ( size_t  j) const
inlineprotected
template<typename T>
bool NPMatrix< T >::isValidIndex ( size_t  i,
size_t  j 
) const
inlineprotected
template<typename T>
bool NPMatrix< T >::isBetweenI12 ( size_t  i) const
inlineprotected
template<typename T>
bool NPMatrix< T >::isBetweenJ12 ( size_t  j) const
inlineprotected
template<typename T>
static size_t NPMatrix< T >::pIfNotNull ( size_t  n,
size_t  p 
)
inlinestaticprotected
template<typename T>
bool NPMatrix< T >::matchSizeForProduct ( const NVector< T > &  u) const
inlineprotected
template<typename T>
bool NPMatrix< T >::matchSizeForProduct ( const NPMatrix< T > &  m) const
inlineprotected
template<typename T>
bool NPMatrix< T >::hasSameSize ( const NPMatrix< T > &  m) const
inlineprotected
template<typename T >
bool NPMatrix< T >::hasDefaultBrowseIndices ( ) const
overrideprotectedvirtual

Reimplemented from NVector< T >.

template<typename T >
void NPMatrix< T >::setDefaultBrowseIndices ( ) const
overrideprotectedvirtual

Reimplemented from NVector< T >.

template<typename T>
NPMatrix< T > & NPMatrix< T >::copy ( const NPMatrix< T > &  m)
protected
template<typename T>
NPMatrix< T > & NPMatrix< T >::copy ( const vector< vector< T >> &  data)
protected
template<typename T>
size_t NPMatrix< T >::vectorIndex ( size_t  i,
size_t  j 
) const
inlineprotected
template<typename T>
size_t NPMatrix< T >::getRowFromVectorIndex ( size_t  k) const
inlineprotected
template<typename T>
size_t NPMatrix< T >::getColFromVectorIndex ( size_t  k) const
inlineprotected
template<typename T >
NPMatrix< T > NPMatrix< T >::subMatrix ( size_t  i1 = 0,
size_t  j1 = MAX_SIZE,
size_t  i2 = 0,
size_t  j2 = MAX_SIZE 
) const
protected
template<typename T>
NPMatrix< T > & NPMatrix< T >::setSubMatrix ( const NPMatrix< T > &  m)
protected
template<typename T>
NPMatrix< T > & NPMatrix< T >::forEach ( const NPMatrix< T > &  m,
const function< void(T &, const T &)> &  binary_op 
)
protected
template<typename T>
NPMatrix< T > & NPMatrix< T >::forEach ( s,
const function< void(T &, T)> &  binary_op 
)
overrideprotected

Friends And Related Function Documentation

template<typename T>
NPMatrix<T> operator+ ( NPMatrix< T >  a,
const NPMatrix< T > &  b 
)
friend
template<typename T>
NPMatrix<T> operator- ( NPMatrix< T >  a,
const NPMatrix< T > &  b 
)
friend
template<typename T>
NPMatrix<T> operator- ( NPMatrix< T >  m)
friend
template<typename T>
NPMatrix<T> operator* ( s,
NPMatrix< T >  m 
)
friend
template<typename T>
NPMatrix<T> operator* ( NPMatrix< T >  m,
s 
)
friend
template<typename T>
NPMatrix<T> operator* ( NPMatrix< T >  a,
const NPMatrix< T > &  b 
)
friend

Usual matrix multiplication.

The matrices must have the length. Natural \( O(n^3) \) matrix product is used.

Returns
value of \( A B \).
template<typename T>
NVector<T> operator* ( const NPMatrix< T > &  m,
NVector< T >  v 
)
friend

Usual matrix vector product (linear mapping).

The number of rows of m must be equal to the dimension of \( v \). Natural \( O(n^2) \) linear mapping is used.

Returns
value of \( M v \).
template<typename T>
NPMatrix<T> operator/ ( NPMatrix< T >  m,
s 
)
friend
template<typename T>
NPMatrix<T> operator^ ( NPMatrix< T >  m,
long  exp 
)
friend

Exponantiate matrix \( M \) using given exponent.

Parameters
mmatrix to exponentiate.
expexponent.

Fast exponentiation implementation. If \( exp < 0 \) we calculate the power of the inverse matrix using inv() method.

Returns
value of \( M^{exp} \) exponentiated matrix.
template<typename T>
NVector<T> operator% ( const NPMatrix< T > &  m,
NVector< T >  v 
)
friend

Solve the linear system formed by \( M \) and \( v \).

Parameters
mmatrix of the equation system.
vsecond member of the equation system.

The linear system is \( MX = v \) where \( X \) is unknown. This algorithm uses \( LU \) decomposition.

Returns
Value of the solution of the system \( X \).
template<typename T>
T operator| ( const NPMatrix< T > &  a,
const NPMatrix< T > &  b 
)
friend
template<typename T>
T operator! ( const NPMatrix< T > &  m)
friend
template<typename T>
T operator/ ( const NPMatrix< T > &  a,
const NPMatrix< T > &  b 
)
friend
template<typename T>
NVector<T>& operator*= ( NVector< T > &  u,
const NPMatrix< T > &  m 
)
friend
template<typename T>
NVector<T>& operator%= ( NVector< T > &  u,
const NPMatrix< T > &  m 
)
friend
template<typename T>
bool operator== ( const NPMatrix< T > &  a,
const NPMatrix< T > &  b 
)
friend
template<typename T>
bool operator!= ( const NPMatrix< T > &  a,
const NPMatrix< T > &  b 
)
friend

Member Data Documentation

template<typename T>
size_t NPMatrix< T >::_n {}
protected
template<typename T>
size_t NPMatrix< T >::_p {}
protected
template<typename T>
size_t NPMatrix< T >::_i1 {}
mutableprotected
template<typename T>
size_t NPMatrix< T >::_j1 {}
mutableprotected
template<typename T>
size_t NPMatrix< T >::_i2 {}
mutableprotected
template<typename T>
size_t NPMatrix< T >::_j2 {}
mutableprotected
template<typename T>
unique_ptr<NPMatrix<T> > NPMatrix< T >::_a {}
mutableprotected

Matrix \( A = LU \) where \( PA = LU \) = this.

_a points to the \( A \) NMatrix or to nullptr if the matrix don't have \( LU \) decomposition.

template<typename T>
unique_ptr<vector<size_t> > NPMatrix< T >::_perm {}
mutableprotected

permutation vector \( P \) such as \( PA = LU \).

Represented as unsigned long array.