npstat::Matrix< Numeric, Len > Class Template Reference
Detailed Descriptiontemplate<typename Numeric, unsigned Len = 16U>
|
npstat::Matrix< Numeric, Len >::Matrix | ( | ) |
Default constructor creates an unitialized matrix which can be assigned from other matrices
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
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.
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
npstat::Matrix< Numeric, Len >::Matrix | ( | const Matrix< Numeric, Len > & | ) |
Copy constructor
npstat::Matrix< Numeric, Len >::Matrix | ( | Matrix< Numeric, Len > && | ) |
Move constructor
npstat::Matrix< Numeric, Len >::Matrix | ( | const Matrix< Num2, Len2 > & | ) |
Converting copy constructor
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.
Matrix npstat::Matrix< Numeric, Len >::antiSymmetrize | ( | ) | const |
Construct an antisymmetric matrix from this one (antisymmetrize)
std::pair<unsigned,unsigned> npstat::Matrix< Numeric, Len >::argmax | ( | ) | const |
Row and column of the largest matrix element
std::pair<unsigned,unsigned> npstat::Matrix< Numeric, Len >::argmaxAbs | ( | ) | const |
Row and column of the matrix element with the largest absolute value
std::pair<unsigned,unsigned> npstat::Matrix< Numeric, Len >::argmin | ( | ) | const |
Row and column of the smallest matrix element
Numeric npstat::Matrix< Numeric, Len >::at | ( | unsigned | row, |
unsigned | column | ||
) | const |
Access by value with bounds checking
Matrix npstat::Matrix< Numeric, Len >::bilinear | ( | const Matrix< Numeric, Len > & | r | ) | const |
Bilinear form with a matrix (r^T M r)
Numeric npstat::Matrix< Numeric, Len >::bilinear | ( | const Num2 * | data, |
unsigned | dataLen | ||
) | const |
Bilinear form with a vector (v^T M v)
Matrix npstat::Matrix< Numeric, Len >::bilinearT | ( | const Matrix< Numeric, Len > & | r | ) | const |
Bilinear form with a transposed matrix (r M r^T)
|
inline |
Method related to "geners" I/O
Matrix& npstat::Matrix< Numeric, Len >::clearMainDiagonal | ( | ) |
This method sets all diagonal elements to 0. It can only be used with square matrices.
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.
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.
Numeric npstat::Matrix< Numeric, Len >::columnSum | ( | unsigned | column | ) | const |
Sum of the values in the given column
Matrix& npstat::Matrix< Numeric, Len >::constFill | ( | Numeric | c | ) |
This method sets all elements to the given value
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.
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.
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.
Numeric npstat::Matrix< Numeric, Len >::det | ( | ) | const |
Calculate the determinant (matrix must be square)
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.
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.
double npstat::Matrix< Numeric, Len >::frobeniusNorm | ( | ) | const |
Frobenius norm of this matrix
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.
Matrix npstat::Matrix< Numeric, Len >::hadamardProduct | ( | const Matrix< Numeric, Len > & | r | ) | const |
Hadamard product (a.k.a. Schur product)
Matrix npstat::Matrix< Numeric, Len >::hadamardRatio | ( | const Matrix< Numeric, Len > & | denominator | ) | const |
Hadamard ratio. All elements of the denominator must be non-zero
Matrix npstat::Matrix< Numeric, Len >::inv | ( | ) | const |
Inverse of a general matrix (which must be square and non-singular)
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.
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.
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.
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.
Numeric npstat::Matrix< Numeric, Len >::maxAbsValue | ( | ) | const |
Maximum absolute value for any row/column
Numeric npstat::Matrix< Numeric, Len >::maxValue | ( | ) | const |
Maximum value for any row/column
Numeric npstat::Matrix< Numeric, Len >::minValue | ( | ) | const |
Minimum value for any row/column
unsigned npstat::Matrix< Numeric, Len >::nonZeros | ( | ) | const |
Number of non-zero elements
|
inline |
A simple inspection of matrix properties
Numeric npstat::Matrix< Numeric, Len >::operator() | ( | unsigned | row, |
unsigned | column | ||
) | const |
Access by value without bounds checking
Matrix npstat::Matrix< Numeric, Len >::operator* | ( | const Matrix< Numeric, Len > & | r | ) | const |
Binary algebraic operation with matrix or scalar
Matrix& npstat::Matrix< Numeric, Len >::operator*= | ( | Numeric | r | ) |
In-place algebraic operations with matrices and scalars
Matrix npstat::Matrix< Numeric, Len >::operator+ | ( | ) | const |
Unary plus
Matrix npstat::Matrix< Numeric, Len >::operator- | ( | ) | const |
Unary minus
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.
Matrix& npstat::Matrix< Numeric, Len >::operator= | ( | const Matrix< Num2, Len2 > & | ) |
Converting assignment operator
Matrix& npstat::Matrix< Numeric, Len >::operator= | ( | Matrix< Numeric, Len > && | ) |
Move assignment operator
bool npstat::Matrix< Numeric, Len >::operator== | ( | const Matrix< Numeric, Len > & | ) | const |
Compare two matrices for equality
Numeric* npstat::Matrix< Numeric, Len >::operator[] | ( | unsigned | ) |
Non-const access to the data (works like 2-d array in C). No bounds checking.
const Numeric* npstat::Matrix< Numeric, Len >::operator[] | ( | unsigned | ) | const |
Const access to the data (works like 2-d array in C). No bounds checking.
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.
Matrix npstat::Matrix< Numeric, Len >::pow | ( | unsigned | degree | ) | const |
Power of a matrix. Matrix must be square.
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.
Matrix npstat::Matrix< Numeric, Len >::removeRowAndColumn | ( | unsigned | row, |
unsigned | column | ||
) | const |
Remove row and/or column. Indices out of range are ignored
Matrix& npstat::Matrix< Numeric, Len >::resize | ( | unsigned | nrows, |
unsigned | ncols | ||
) |
This method changes the object dimensions. All data is lost in the process.
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.
Matrix npstat::Matrix< Numeric, Len >::rowMultiply | ( | const Num2 * | data, |
unsigned | dataLen | ||
) | const |
Multiplication by a row on the left
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).
Numeric npstat::Matrix< Numeric, Len >::rowSum | ( | unsigned | row | ) | const |
Sum of the values in the given row
Matrix& npstat::Matrix< Numeric, Len >::set | ( | unsigned | row, |
unsigned | column, | ||
Numeric | value | ||
) |
Data modification method with bounds checking
Matrix& npstat::Matrix< Numeric, Len >::setColumn | ( | unsigned | col, |
const Num2 * | data, | ||
unsigned | dataLength | ||
) |
Set a column from an external array
Matrix& npstat::Matrix< Numeric, Len >::setData | ( | const Num2 * | data, |
unsigned | dataLength | ||
) |
Set all data from an external array
Matrix& npstat::Matrix< Numeric, Len >::setFromTriplets | ( | Iterator | first, |
Iterator | last | ||
) |
Set from triplets (for compatibility with Eigen)
Matrix& npstat::Matrix< Numeric, Len >::setRow | ( | unsigned | row, |
const Num2 * | data, | ||
unsigned | dataLength | ||
) |
Set a row from an external array
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.
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.
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
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.
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.
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.
Matrix npstat::Matrix< Numeric, Len >::symmetrize | ( | ) | const |
Construct a symmetric matrix from this one (symmetrize)
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.
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.
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.
Matrix npstat::Matrix< Numeric, Len >::T | ( | ) | const |
Return transposed matrix
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.
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.
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
Matrix npstat::Matrix< Numeric, Len >::timesT | ( | ) | const |
Return the product of this matrix with its transpose, M*M^T
Matrix npstat::Matrix< Numeric, Len >::timesT | ( | const Matrix< Numeric, Len > & | r | ) | const |
Return the product of this matrix with the transposed argument
Matrix npstat::Matrix< Numeric, Len >::timesVector | ( | const Num2 * | data, |
unsigned | dataLen | ||
) | const |
Multiplication by a vector represented by a pointer and array size
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).
Numeric npstat::Matrix< Numeric, Len >::tr | ( | ) | const |
Calculate the trace (matrix must be square)
Matrix npstat::Matrix< Numeric, Len >::triDiagInv | ( | ) | const |
Inverse of a square tridiagonal matrix
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.
Matrix npstat::Matrix< Numeric, Len >::Ttimes | ( | const Matrix< Numeric, Len > & | r | ) | const |
Return the product of this matrix transposed with the argument
Matrix npstat::Matrix< Numeric, Len >::TtimesThis | ( | ) | const |
Return the product of this matrix transposed with this, M^T*M
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.
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.
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.
Matrix& npstat::Matrix< Numeric, Len >::zeroOut | ( | ) |
This method sets all elements to 0