npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
npstat::Matrix< Numeric, Len > Class Template Reference

#include <Matrix.hh>

Public Types

typedef Numeric value_type
 

Public Member Functions

 Matrix ()
 
 Matrix (unsigned nrows, unsigned ncols)
 
 Matrix (unsigned nrows, unsigned ncols, int initCode)
 
 Matrix (unsigned nrows, unsigned ncols, const Numeric *data)
 
 Matrix (const Matrix &)
 
 Matrix (Matrix &&)
 
template<typename Num2 , unsigned Len2>
 Matrix (const Matrix< Num2, Len2 > &)
 
template<typename Num2 , unsigned Len2>
 Matrix (const Matrix< Num2, Len2 > &, unsigned rowMin, unsigned rowMax, unsigned columnMin, unsigned columnMax)
 
Matrixoperator= (const Matrix &)
 
Matrixoperator= (Matrix &&)
 
template<typename Num2 , unsigned Len2>
Matrixoperator= (const Matrix< Num2, Len2 > &)
 
template<typename Iterator >
MatrixsetFromTriplets (Iterator first, Iterator last)
 
template<typename Num2 >
MatrixsetData (const Num2 *data, unsigned dataLength)
 
template<typename Num2 >
MatrixsetRow (unsigned row, const Num2 *data, unsigned dataLength)
 
template<typename Num2 >
MatrixsetColumn (unsigned col, const Num2 *data, unsigned dataLength)
 
MatrixtagAsDiagonal (bool b=true)
 
template<typename Num2 >
MatrixdiagFill (const Num2 *data, unsigned dataLen)
 
unsigned nRows () const
 
unsigned nColumns () const
 
unsigned long length () const
 
Numeric * data () const
 
bool isSquare () const
 
bool isSymmetric () const
 
bool isAntiSymmetric () const
 
bool isDiagonal () const
 
bool isMainDiagonal () const
 
bool isZero () const
 
Matrixuninitialize ()
 
bool isCompatible (const Matrix &other) const
 
Matrixresize (unsigned nrows, unsigned ncols)
 
MatrixzeroOut ()
 
MatrixclearMainDiagonal ()
 
MatrixmakeDiagonal ()
 
MatrixconstFill (Numeric c)
 
MatrixTthis ()
 
bool operator== (const Matrix &) const
 
bool operator!= (const Matrix &) const
 
Numeric * operator[] (unsigned)
 
const Numeric * operator[] (unsigned) const
 
Matrixset (unsigned row, unsigned column, Numeric value)
 
Numeric operator() (unsigned row, unsigned column) const
 
Numeric at (unsigned row, unsigned column) const
 
Numeric rowSum (unsigned row) const
 
Numeric columnSum (unsigned column) const
 
Matrix removeRowAndColumn (unsigned row, unsigned column) const
 
unsigned nonZeros () const
 
void coarseSum (unsigned n, unsigned m, Matrix *result) const
 
void coarseAverage (unsigned n, unsigned m, Matrix *result) const
 
Matrix operator+ () const
 
Matrix operator- () const
 
Matrix operator* (const Matrix &r) const
 
Matrix operator* (Numeric r) const
 
Matrix operator/ (Numeric r) const
 
Matrix operator+ (const Matrix &r) const
 
Matrix operator- (const Matrix &r) const
 
Matrix hadamardProduct (const Matrix &r) const
 
Matrix hadamardRatio (const Matrix &denominator) const
 
void times (const Matrix &r, Matrix *result) const
 
void times (Numeric r, Matrix *result) const
 
void over (Numeric r, Matrix *result) const
 
void plus (const Matrix &r, Matrix *result) const
 
void minus (const Matrix &r, Matrix *result) const
 
Matrixoperator*= (Numeric r)
 
Matrixoperator/= (Numeric r)
 
Matrixoperator+= (const Matrix &r)
 
Matrixoperator-= (const Matrix &r)
 
template<typename Num2 >
Matrix timesVector (const Num2 *data, unsigned dataLen) const
 
template<typename Num2 , typename Num3 >
void timesVector (const Num2 *data, unsigned dataLen, Num3 *result, unsigned resultLen) const
 
template<typename Num2 >
Numeric timesVector (unsigned rowNumber, const Num2 *data, unsigned dataLen) const
 
template<typename Num2 >
Matrix rowMultiply (const Num2 *data, unsigned dataLen) const
 
template<typename Num2 , typename Num3 >
void rowMultiply (const Num2 *data, unsigned dataLen, Num3 *result, unsigned resultLen) const
 
template<typename Num2 >
Numeric rowMultiply (unsigned columnNumber, const Num2 *data, unsigned dataLen) const
 
template<typename Num2 >
Numeric bilinear (const Num2 *data, unsigned dataLen) const
 
Matrix bilinear (const Matrix &r) const
 
Matrix bilinearT (const Matrix &r) const
 
bool solveLinearSystem (const Numeric *rhs, unsigned lenRhs, Numeric *solution) const
 
bool solveLinearSystems (const Matrix &RHS, Matrix *X) const
 
bool underdeterminedLinearSystem (const Numeric *rhs, unsigned lenRhs, const Matrix &V, Numeric *solution, unsigned lenSolution, Numeric *resultNormSquared=0, Matrix *A=0) const
 
bool linearLeastSquares (const Numeric *rhs, unsigned lenRhs, Numeric *solution, unsigned lenSolution) const
 
bool constrainedLeastSquares (const Numeric *rhs1, unsigned lenRhs1, const Matrix &B, const Numeric *rhs2, unsigned lenRhs2, Numeric *solution, unsigned lenSol, Numeric *resultChiSquare=0, Matrix *resultCovarianceMatrix=0, Numeric *unconstrainedSolution=0, Matrix *unconstrainedCovmat=0, Matrix *projectionMatrix=0, Matrix *A=0) const
 
bool weightedLeastSquares (const Numeric *rhs, unsigned lenRhs, const Matrix &inverseCovarianceMatrix, Numeric *solution, unsigned lenSolution, Numeric *resultChiSquare=0, Matrix *resultCovarianceMatrix=0) const
 
Matrix T () const
 
Matrix TtimesThis () const
 
Matrix timesT () const
 
Matrix timesT (const Matrix &r) const
 
Matrix Ttimes (const Matrix &r) const
 
Matrix directSum (const Matrix &added) const
 
Matrix symmetrize () const
 
Matrix antiSymmetrize () const
 
Matrix outer (const Matrix &r) const
 
Numeric minValue () const
 
Numeric maxValue () const
 
Numeric maxAbsValue () const
 
std::pair< unsigned, unsigned > argmin () const
 
std::pair< unsigned, unsigned > argmax () const
 
std::pair< unsigned, unsigned > argmaxAbs () const
 
double frobeniusNorm () const
 
Numeric tr () const
 
Numeric sp () const
 
Numeric productTr (const Matrix &r) const
 
Numeric productSp (const Matrix &r) const
 
Numeric det () const
 
Matrix symPDInv () const
 
Matrix symPDEigenInv (double tol, bool extend=true, EigenMethod m=EIGEN_SIMPLE) const
 
Matrix symInv () const
 
Matrix triDiagInv () const
 
Matrix inv () const
 
Matrix leftInv () const
 
Matrix rightInv () const
 
void tdSymEigen (Numeric *eigenvalues, unsigned lenEigenvalues, Matrix *eigenvectors=0, EigenMethod m=EIGEN_RRR) const
 
void symEigen (Numeric *eigenvalues, unsigned lenEigenvalues, Matrix *eigenvectors=0, EigenMethod m=EIGEN_SIMPLE) const
 
void genEigen (typename GeneralizedComplex< Numeric >::type *eigenvalues, unsigned lenEigenvalues, Matrix *rightEigenvectors=0, Matrix *leftEigenvectors=0) const
 
void svd (Numeric *singularValues, unsigned lenSingularValues, Matrix *U, Matrix *V, SvdMethod m=SVD_D_AND_C) const
 
double symPSDefEffectiveRank (double tol=0.0, EigenMethod m=EIGEN_D_AND_C, double *eigenSum=0) const
 
template<class Functor >
Matrix symFcn (const Functor &fcn) const
 
Matrix pow (unsigned degree) const
 
Matrix covarToCorr () const
 
Matrix corrToCovar (const double *variances, unsigned lenVariances) const
 
gs::ClassId classId () const
 
bool write (std::ostream &of) const
 

Static Public Member Functions

static const char * classname ()
 
static unsigned version ()
 
static void restore (const gs::ClassId &id, std::istream &in, Matrix *m)
 

Detailed Description

template<typename Numeric, unsigned Len = 16U>
class npstat::Matrix< Numeric, Len >

A simple helper class for matrix manipulations. Depending on how much space is provided with the "Len" parameter, the data will be placed either on the stack or on the heap. Do not set "Len" to 0, the code assumes that the stack space is available for at least one element.

C storage convention is used for internal data. In the element access operations, array bounds are not checked.

A number of operations (solving linear systems, calculating eigenvalues and eigenvectors, singular value decomposition, etc) are performed by calling appropriate routines from LAPACK. This usually limits the Numeric template parameter types for which these operations are available to float and double. If the operation is unavailable for the template parameter used, std::invalid_argument exception is raised.

Note that for simple matrix operations this class is likely to be slower than matrix classes based on expression templates (such as those in boost uBLAS or in Blitz++). If speed is really important for your calculations, consider using a dedicated matrix library.

Constructor & Destructor Documentation

◆ Matrix() [1/8]

template<typename Numeric , unsigned Len = 16U>
npstat::Matrix< Numeric, Len >::Matrix ( )

Default constructor creates an unitialized matrix which can be assigned from other matrices

◆ Matrix() [2/8]

template<typename Numeric , unsigned Len = 16U>
npstat::Matrix< Numeric, Len >::Matrix ( unsigned  nrows,
unsigned  ncols 
)

This constructor creates an unitialized matrix which can be assigned element-by-element or from another matrix with the same dimensions

◆ Matrix() [3/8]

template<typename Numeric , unsigned Len = 16U>
npstat::Matrix< Numeric, Len >::Matrix ( unsigned  nrows,
unsigned  ncols,
int  initCode 
)

This constructor initializes the matrix as follows:

initCode = 0: all elements are initialized to 0.

initCode = 1: matrix must be square; diagonal elements are initialized to 1, all other elements to 0.

◆ Matrix() [4/8]

template<typename Numeric , unsigned Len = 16U>
npstat::Matrix< Numeric, Len >::Matrix ( unsigned  nrows,
unsigned  ncols,
const Numeric *  data 
)

This constructor initializes the matrix from the given 1-d array using C storage conventions

◆ Matrix() [5/8]

template<typename Numeric , unsigned Len = 16U>
npstat::Matrix< Numeric, Len >::Matrix ( const Matrix< Numeric, Len > &  )

Copy constructor

◆ Matrix() [6/8]

template<typename Numeric , unsigned Len = 16U>
npstat::Matrix< Numeric, Len >::Matrix ( Matrix< Numeric, Len > &&  )

Move constructor

◆ Matrix() [7/8]

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 , unsigned Len2>
npstat::Matrix< Numeric, Len >::Matrix ( const Matrix< Num2, Len2 > &  )

Converting copy constructor

◆ Matrix() [8/8]

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 , unsigned Len2>
npstat::Matrix< Numeric, Len >::Matrix ( const Matrix< Num2, Len2 > &  ,
unsigned  rowMin,
unsigned  rowMax,
unsigned  columnMin,
unsigned  columnMax 
)

Constructor from a subrange of another matrix. The minimum row and column numbers are included, maximum numbers are excluded.

Member Function Documentation

◆ antiSymmetrize()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::antiSymmetrize ( ) const

Construct an antisymmetric matrix from this one (antisymmetrize)

◆ argmax()

template<typename Numeric , unsigned Len = 16U>
std::pair<unsigned,unsigned> npstat::Matrix< Numeric, Len >::argmax ( ) const

Row and column of the largest matrix element

◆ argmaxAbs()

template<typename Numeric , unsigned Len = 16U>
std::pair<unsigned,unsigned> npstat::Matrix< Numeric, Len >::argmaxAbs ( ) const

Row and column of the matrix element with the largest absolute value

◆ argmin()

template<typename Numeric , unsigned Len = 16U>
std::pair<unsigned,unsigned> npstat::Matrix< Numeric, Len >::argmin ( ) const

Row and column of the smallest matrix element

◆ at()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::at ( unsigned  row,
unsigned  column 
) const

Access by value with bounds checking

◆ bilinear() [1/2]

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::bilinear ( const Matrix< Numeric, Len > &  r) const

Bilinear form with a matrix (r^T M r)

◆ bilinear() [2/2]

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Numeric npstat::Matrix< Numeric, Len >::bilinear ( const Num2 *  data,
unsigned  dataLen 
) const

Bilinear form with a vector (v^T M v)

◆ bilinearT()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::bilinearT ( const Matrix< Numeric, Len > &  r) const

Bilinear form with a transposed matrix (r M r^T)

◆ classId()

template<typename Numeric , unsigned Len = 16U>
gs::ClassId npstat::Matrix< Numeric, Len >::classId ( ) const
inline

Method related to "geners" I/O

◆ clearMainDiagonal()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::clearMainDiagonal ( )

This method sets all diagonal elements to 0. It can only be used with square matrices.

◆ coarseAverage()

template<typename Numeric , unsigned Len = 16U>
void npstat::Matrix< Numeric, Len >::coarseAverage ( unsigned  n,
unsigned  m,
Matrix< Numeric, Len > *  result 
) const

Replace every n x m square block in the matrix by its average The number of rows must be divisible by n. The number of columns must be divisible by m.

◆ coarseSum()

template<typename Numeric , unsigned Len = 16U>
void npstat::Matrix< Numeric, Len >::coarseSum ( unsigned  n,
unsigned  m,
Matrix< Numeric, Len > *  result 
) const

Replace every n x m square block in the matrix by its sum. The number of rows must be divisible by n. The number of columns must be divisible by m.

◆ columnSum()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::columnSum ( unsigned  column) const

Sum of the values in the given column

◆ constFill()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::constFill ( Numeric  c)

This method sets all elements to the given value

◆ constrainedLeastSquares()

template<typename Numeric , unsigned Len = 16U>
bool npstat::Matrix< Numeric, Len >::constrainedLeastSquares ( const Numeric *  rhs1,
unsigned  lenRhs1,
const Matrix< Numeric, Len > &  B,
const Numeric *  rhs2,
unsigned  lenRhs2,
Numeric *  solution,
unsigned  lenSol,
Numeric *  resultChiSquare = 0,
Matrix< Numeric, Len > *  resultCovarianceMatrix = 0,
Numeric *  unconstrainedSolution = 0,
Matrix< Numeric, Len > *  unconstrainedCovmat = 0,
Matrix< Numeric, Len > *  projectionMatrix = 0,
Matrix< Numeric, Len > *  A = 0 
) const

Solution of a linear system of equations M*x = rhs1 in the least squares sense, subject to the constraint B*x = rhs2. Number of equations (i.e., dimensionality of rhs1) plus the number of constraints (i.e., dimensionality of rhs2) should exceed the dimensionality of x. "false" is returned in case of failure. If the chi-square of the result is desired, make the "resultChiSquare" argument point to some valid location. Same goes for the result covariance matrix and all subsequent arguments which can be used to extract various intermediate results:

unconstrainedSolution – Array to store the unconstrained solution. Must have at least "lenSol" elements.

unconstrainedCovmat – Covariance matrix of the unconstrained solution.

projectionMatrix – Projection matrix used to obtain a part of the constrained solution from the unconstrained one.

A – Matrix multiplied by rhs2 to get the second part of the constrained solution.

Note that requesting the result covariance matrix or any other subsequent output switches the code to a different and slower "textbook" algorithm instead of an optimized algorithm from Lapack.

◆ corrToCovar()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::corrToCovar ( const double *  variances,
unsigned  lenVariances 
) const

The following function derives a covariance matrix out of a correlation matrix and of the covariances.

◆ covarToCorr()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::covarToCorr ( ) const

The following function derives a correlation matrix out of a covariance matrix. In the returned matrix all diagonal elements will be set to 1.

◆ det()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::det ( ) const

Calculate the determinant (matrix must be square)

◆ diagFill()

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Matrix& npstat::Matrix< Numeric, Len >::diagFill ( const Num2 *  data,
unsigned  dataLen 
)

Fill the main diagonal and set all other elements to zero. This operation tags the matrix as diagonal.

◆ directSum()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::directSum ( const Matrix< Numeric, Len > &  added) const

"directSum" method constructs a block-diagonal matrix with this matrix and the argument matrix inside the diagonal blocks. Block which contains this matrix is the top left one.

◆ frobeniusNorm()

template<typename Numeric , unsigned Len = 16U>
double npstat::Matrix< Numeric, Len >::frobeniusNorm ( ) const

Frobenius norm of this matrix

◆ genEigen()

template<typename Numeric , unsigned Len = 16U>
void npstat::Matrix< Numeric, Len >::genEigen ( typename GeneralizedComplex< Numeric >::type *  eigenvalues,
unsigned  lenEigenvalues,
Matrix< Numeric, Len > *  rightEigenvectors = 0,
Matrix< Numeric, Len > *  leftEigenvectors = 0 
) const

Eigenvalues and eigenvectors of a real general matrix. Either "rightEigenvectors" or "leftEigenvectors" or both can be NULL if they are not needed.

If an eigenvalue is real, the corresponding row of the output eigenvector matrix is set to the computed eigenvector. If an eigenvalue is complex, all such eigenvalues come in complex conjugate pairs. Corresponding eigenvectors make up such a pair as well. The matrix row which corresponds to the first eigenvalue in the pair is set to the real part of the pair and the matrix row which corresponds to the second eigenvalue in the pair is set to the imaginary part of the pair. For the right eigenvectors, the first complex eigenvector in the pair needs to be constructed by adding the imaginary part. The second eigenvector has the imaginary part subtracted. For the left eigenvectors the order is reversed: the first eigenvector needs to be constructed by subtracting the imaginary part, and the second by adding it.

◆ hadamardProduct()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::hadamardProduct ( const Matrix< Numeric, Len > &  r) const

Hadamard product (a.k.a. Schur product)

◆ hadamardRatio()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::hadamardRatio ( const Matrix< Numeric, Len > &  denominator) const

Hadamard ratio. All elements of the denominator must be non-zero

◆ inv()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::inv ( ) const

Inverse of a general matrix (which must be square and non-singular)

◆ isCompatible()

template<typename Numeric , unsigned Len = 16U>
bool npstat::Matrix< Numeric, Len >::isCompatible ( const Matrix< Numeric, Len > &  other) const

Check if this matrix has the same number of rows and columns as the other one. "false" will be returned in case any one of the two matrices (or both) is not initialized.

◆ leftInv()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::leftInv ( ) const

Left pseudoinverse of a real, full rank matrix. This pseudoinverse can be calculated when the number of columns does not exceed the number of rows.

◆ linearLeastSquares()

template<typename Numeric , unsigned Len = 16U>
bool npstat::Matrix< Numeric, Len >::linearLeastSquares ( const Numeric *  rhs,
unsigned  lenRhs,
Numeric *  solution,
unsigned  lenSolution 
) const

Solution of a linear overdetermined system of equations M*x = rhs in the least squares sense. The "lenRhs" parameter should be equal to the number of matrix rows, and it should exceed "lenSolution" ("lenSolution" should be equal to the number of columns). "false" is returned in case of failure.

◆ makeDiagonal()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::makeDiagonal ( )

This method sets all non-diagonal elements to 0. This operation is only valid for square matrices. It tags the matrix as diagonal.

◆ maxAbsValue()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::maxAbsValue ( ) const

Maximum absolute value for any row/column

◆ maxValue()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::maxValue ( ) const

Maximum value for any row/column

◆ minValue()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::minValue ( ) const

Minimum value for any row/column

◆ nonZeros()

template<typename Numeric , unsigned Len = 16U>
unsigned npstat::Matrix< Numeric, Len >::nonZeros ( ) const

Number of non-zero elements

◆ nRows()

template<typename Numeric , unsigned Len = 16U>
unsigned npstat::Matrix< Numeric, Len >::nRows ( ) const
inline

A simple inspection of matrix properties

◆ operator()()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::operator() ( unsigned  row,
unsigned  column 
) const

Access by value without bounds checking

◆ operator*()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::operator* ( const Matrix< Numeric, Len > &  r) const

Binary algebraic operation with matrix or scalar

◆ operator*=()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::operator*= ( Numeric  r)

In-place algebraic operations with matrices and scalars

◆ operator+()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::operator+ ( ) const

Unary plus

◆ operator-()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::operator- ( ) const

Unary minus

◆ operator=() [1/3]

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::operator= ( const Matrix< Numeric, Len > &  )

Assignment operator. The matrix on the left must either be in an uninitialized state or have compatible dimensions with the matrix on the right.

◆ operator=() [2/3]

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 , unsigned Len2>
Matrix& npstat::Matrix< Numeric, Len >::operator= ( const Matrix< Num2, Len2 > &  )

Converting assignment operator

◆ operator=() [3/3]

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::operator= ( Matrix< Numeric, Len > &&  )

Move assignment operator

◆ operator==()

template<typename Numeric , unsigned Len = 16U>
bool npstat::Matrix< Numeric, Len >::operator== ( const Matrix< Numeric, Len > &  ) const

Compare two matrices for equality

◆ operator[]() [1/2]

template<typename Numeric , unsigned Len = 16U>
Numeric* npstat::Matrix< Numeric, Len >::operator[] ( unsigned  )

Non-const access to the data (works like 2-d array in C). No bounds checking.

◆ operator[]() [2/2]

template<typename Numeric , unsigned Len = 16U>
const Numeric* npstat::Matrix< Numeric, Len >::operator[] ( unsigned  ) const

Const access to the data (works like 2-d array in C). No bounds checking.

◆ outer()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::outer ( const Matrix< Numeric, Len > &  r) const

Matrix outer product. The number of rows of the result equals the product of the numbers of rows of this matrix and the argument matrix. The number of columns of the result is the product of the numbers of columns.

◆ pow()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::pow ( unsigned  degree) const

Power of a matrix. Matrix must be square.

◆ productTr()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::productTr ( const Matrix< Numeric, Len > &  r) const

Calculate the trace of a product of this matrix with another. This works faster if only the trace is of interest but not the product itself.

◆ removeRowAndColumn()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::removeRowAndColumn ( unsigned  row,
unsigned  column 
) const

Remove row and/or column. Indices out of range are ignored

◆ resize()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::resize ( unsigned  nrows,
unsigned  ncols 
)

This method changes the object dimensions. All data is lost in the process.

◆ rightInv()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::rightInv ( ) const

Right pseudoinverse of a real, full rank matrix. This pseudoinverse can be calculated when the number of rows does not exceed the number of columns.

◆ rowMultiply() [1/2]

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Matrix npstat::Matrix< Numeric, Len >::rowMultiply ( const Num2 *  data,
unsigned  dataLen 
) const

Multiplication by a row on the left

◆ rowMultiply() [2/2]

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Numeric npstat::Matrix< Numeric, Len >::rowMultiply ( unsigned  columnNumber,
const Num2 *  data,
unsigned  dataLen 
) const

Multiplication by a row on the left. This function is useful when only one element of the result is needed (or elements are needed one at a time).

◆ rowSum()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::rowSum ( unsigned  row) const

Sum of the values in the given row

◆ set()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::set ( unsigned  row,
unsigned  column,
Numeric  value 
)

Data modification method with bounds checking

◆ setColumn()

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Matrix& npstat::Matrix< Numeric, Len >::setColumn ( unsigned  col,
const Num2 *  data,
unsigned  dataLength 
)

Set a column from an external array

◆ setData()

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Matrix& npstat::Matrix< Numeric, Len >::setData ( const Num2 *  data,
unsigned  dataLength 
)

Set all data from an external array

◆ setFromTriplets()

template<typename Numeric , unsigned Len = 16U>
template<typename Iterator >
Matrix& npstat::Matrix< Numeric, Len >::setFromTriplets ( Iterator  first,
Iterator  last 
)

Set from triplets (for compatibility with Eigen)

◆ setRow()

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Matrix& npstat::Matrix< Numeric, Len >::setRow ( unsigned  row,
const Num2 *  data,
unsigned  dataLength 
)

Set a row from an external array

◆ solveLinearSystem()

template<typename Numeric , unsigned Len = 16U>
bool npstat::Matrix< Numeric, Len >::solveLinearSystem ( const Numeric *  rhs,
unsigned  lenRhs,
Numeric *  solution 
) const

Solution of a linear system of equations M*x = rhs. The matrix must be square. "false" is returned in case the matrix is degenerate.

◆ solveLinearSystems()

template<typename Numeric , unsigned Len = 16U>
bool npstat::Matrix< Numeric, Len >::solveLinearSystems ( const Matrix< Numeric, Len > &  RHS,
Matrix< Numeric, Len > *  X 
) const

Solution of linear systems of equations M*X = RHS. The matrix M (this one) must be square. "false" is returned in case this matrix is degenerate.

◆ svd()

template<typename Numeric , unsigned Len = 16U>
void npstat::Matrix< Numeric, Len >::svd ( Numeric *  singularValues,
unsigned  lenSingularValues,
Matrix< Numeric, Len > *  U,
Matrix< Numeric, Len > *  V,
SvdMethod  m = SVD_D_AND_C 
) const

Singular value decomposition of a matrix. The length of the singular values buffer must be at least min(nrows, ncolumns).

On exit, matrices U and V will contain singular vectors of the current matrix, row-by-row. The decomposition is then

*this = U^T * diag(singularValues, nrows, ncolumns) * V

◆ symEigen()

template<typename Numeric , unsigned Len = 16U>
void npstat::Matrix< Numeric, Len >::symEigen ( Numeric *  eigenvalues,
unsigned  lenEigenvalues,
Matrix< Numeric, Len > *  eigenvectors = 0,
EigenMethod  m = EIGEN_SIMPLE 
) const

Eigenvalues and eigenvectors of a real symmetric matrix. The "eigenvectors" pointer can be NULL if only eigenvalues are needed. If it is not NULL, the rows of that matrix will be set to the computed eigenvectors.

The "m" parameter specifies the LAPACK method to use for calculating the eigendecomposition.

◆ symFcn()

template<typename Numeric , unsigned Len = 16U>
template<class Functor >
Matrix npstat::Matrix< Numeric, Len >::symFcn ( const Functor &  fcn) const

Calculate a function of this matrix. It is assumed that the matrix is symmetric and real. The calculation is performed by calculating the eignenbasis, evaluating the function of the eigenvalues, and then transforming back to the original basis.

◆ symInv()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::symInv ( ) const

Inverse of a real symmetric matrix. If the matrix is not symmetric, its symmetrized version will be used internally. Use this function (it has better numerical precision than inv()) if you know that your matrix is symmetric.

◆ symmetrize()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::symmetrize ( ) const

Construct a symmetric matrix from this one (symmetrize)

◆ symPDEigenInv()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::symPDEigenInv ( double  tol,
bool  extend = true,
EigenMethod  m = EIGEN_SIMPLE 
) const

Invert real symmetric positive-definite matrix using eigendecomposition. Use this function if you know that your matrix is supposed to be symmetric and positive-definite but might not be due to numerical round-offs. The arguments of this method are as folows:

tol – Eigenvalues whose ratios to the largest eigenvalue are below this parameter will be either truncated or extended (per value of the next argument). This parameter must lie in (0, 1).

extend – If "true", the inverse of small eigenvalues will be replaced by the inverse of the largest eigenvalue divided by the tolerance parameter. If "false", inverse of such eigenvalues will be set to 0.

m – LAPACK method to use for calculating the eigendecomposition.

◆ symPDInv()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::symPDInv ( ) const

Inverse of a real symmetric positive-definite matrix. Use this function (it has better numerical precision than symInv()) if you know that your matrix is symmetric and positive definite. For example, a non-singular covariance matrix calculated for some dataset has these properties.

◆ symPSDefEffectiveRank()

template<typename Numeric , unsigned Len = 16U>
double npstat::Matrix< Numeric, Len >::symPSDefEffectiveRank ( double  tol = 0.0,
EigenMethod  m = EIGEN_D_AND_C,
double *  eigenSum = 0 
) const

Calculate entropy-based effective rank. It is assumed that the matrix is symmetric positive semidefinite.

The "tol" parameter specifies the eigenvalue cutoff: all eigenvalues equal to tol*maxEigenvalue or smaller will be ignored. This parameter must be non-negative.

The "m" parameter specifies the LAPACK method to use for calculating eigenvalues.

If "eigenSum" pointer is not NULL, *eigenSum will be filled on exit with the sum of eigenvalues above the cutoff divided by the largest eigenvalue.

◆ T()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::T ( ) const

Return transposed matrix

◆ tagAsDiagonal()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::tagAsDiagonal ( bool  b = true)

Tag this matrix as diagonal. This may improve the speed of certain operations. Of course, this should be done only if you know for sure that this matrix is, indeed, diagonal. Works for square matrices only.

This method can also be used to tag matrices as non-diagonal.

◆ tdSymEigen()

template<typename Numeric , unsigned Len = 16U>
void npstat::Matrix< Numeric, Len >::tdSymEigen ( Numeric *  eigenvalues,
unsigned  lenEigenvalues,
Matrix< Numeric, Len > *  eigenvectors = 0,
EigenMethod  m = EIGEN_RRR 
) const

Eigenvalues and eigenvectors of a real tridiagonal symmetric matrix. The "eigenvectors" pointer can be NULL if only eigenvalues are needed. If it is not NULL, the rows of that matrix will be set to the computed eigenvectors.

The "m" parameter specifies the LAPACK method to use for calculating the eigendecomposition.

◆ times()

template<typename Numeric , unsigned Len = 16U>
void npstat::Matrix< Numeric, Len >::times ( const Matrix< Numeric, Len > &  r,
Matrix< Numeric, Len > *  result 
) const

Binary algebraic operation which writes its result into an existing matrix potentially avoiding memory allocation

◆ timesT() [1/2]

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::timesT ( ) const

Return the product of this matrix with its transpose, M*M^T

◆ timesT() [2/2]

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::timesT ( const Matrix< Numeric, Len > &  r) const

Return the product of this matrix with the transposed argument

◆ timesVector() [1/2]

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Matrix npstat::Matrix< Numeric, Len >::timesVector ( const Num2 *  data,
unsigned  dataLen 
) const

Multiplication by a vector represented by a pointer and array size

◆ timesVector() [2/2]

template<typename Numeric , unsigned Len = 16U>
template<typename Num2 >
Numeric npstat::Matrix< Numeric, Len >::timesVector ( unsigned  rowNumber,
const Num2 *  data,
unsigned  dataLen 
) const

Multiplication by a vector represented by a pointer and array size. This function is useful when only one element of the result is needed (or elements are needed one at a time).

◆ tr()

template<typename Numeric , unsigned Len = 16U>
Numeric npstat::Matrix< Numeric, Len >::tr ( ) const

Calculate the trace (matrix must be square)

◆ triDiagInv()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::triDiagInv ( ) const

Inverse of a square tridiagonal matrix

◆ Tthis()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::Tthis ( )

Method for transposing this matrix. Uses std::swap for square matrices but might allocate memory from the heap if the matrix is not square.

◆ Ttimes()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::Ttimes ( const Matrix< Numeric, Len > &  r) const

Return the product of this matrix transposed with the argument

◆ TtimesThis()

template<typename Numeric , unsigned Len = 16U>
Matrix npstat::Matrix< Numeric, Len >::TtimesThis ( ) const

Return the product of this matrix transposed with this, M^T*M

◆ underdeterminedLinearSystem()

template<typename Numeric , unsigned Len = 16U>
bool npstat::Matrix< Numeric, Len >::underdeterminedLinearSystem ( const Numeric *  rhs,
unsigned  lenRhs,
const Matrix< Numeric, Len > &  V,
Numeric *  solution,
unsigned  lenSolution,
Numeric *  resultNormSquared = 0,
Matrix< Numeric, Len > *  A = 0 
) const

Solution of an underdetermined linear system of equations M*x = rhs in the minimum norm sense (M is this matrix). The norm is defined by sqrt(x^T*V^(-1)*x). The result is given by A*rhs, where A is the right inverse of M (i.e., M*A = I) minimizing the norm. V plays the role of the inverse Gram matrix. It must be symmetric and positive-definite.

This method can also be used for solving constrained least squares problems. For example, minimization of (y - y0)^T*V^(-1)*(y - y0) under the condition M*y = rhs can be reduced to solving an underdetermined linear system using x = y - y0 and M*x = rhs' = rhs - M*y0. The application code can call this function assuming that V is the covariance matrix of y and using rhs' as the "rhs" argument. Subsequently, y can be calculated as x + y0. The covariance matrix of y can be subsequently calculated by V_y = (I - P)*V*(I - P)^T, where P = A*M. Operator (I - P) actually projects the x (i.e., y - y0) space onto the space of solutions of the equation M*x = 0.

"false" is returned in case of failure.

◆ uninitialize()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::uninitialize ( )

This method resets the object to an unintialized state Can be applied in order to force the assignment operators to work.

◆ weightedLeastSquares()

template<typename Numeric , unsigned Len = 16U>
bool npstat::Matrix< Numeric, Len >::weightedLeastSquares ( const Numeric *  rhs,
unsigned  lenRhs,
const Matrix< Numeric, Len > &  inverseCovarianceMatrix,
Numeric *  solution,
unsigned  lenSolution,
Numeric *  resultChiSquare = 0,
Matrix< Numeric, Len > *  resultCovarianceMatrix = 0 
) const

Weighted least squares problem. The inverse covariance matrix must be symmetric and positive definite. If the chi-square of the result is desired, make the "resultChiSquare" argument point to some valid location. Same goes for the result covariance matrix. "false" is returned in case of failure.

◆ zeroOut()

template<typename Numeric , unsigned Len = 16U>
Matrix& npstat::Matrix< Numeric, Len >::zeroOut ( )

This method sets all elements to 0


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