Class SparseMatrix. This class is designed to present an interface nearly identical to class Matrix, but more efficiently handle sparse matrices, in which most of the elements are zero. The class stores only non-zero elements; using a map of SparseVectors, with key = row index. it also stores a nominal dimension - number of rows and columns. The class uses a proxy class, SMatProxy, to access elements; this allows rvalues and lvalues to be treated separately. Notes on speed. The most expensive parts are the Proxy::operator(), then find() and lower_bound(). Never use the Proxy stuff within the class, always use iterators and assign values to the maps directly. Never assign zeros to the maps. See timing and test results in the test program smtest.cpp Matrix multiply is orders of magnitude faster. Transpose() is much faster than the Matrix version, which is something of a surprise. Most time consuming is looping over columns; this is expected since the design stores by row. The trick is to re-write the algorithm in terms of the transpose of the column-loop matrix, and then apply a transpose(), which is cheap, either before starting (when col-loop matrix is input) or after returning (output). Then the loops become loops over rows. NB. never store zeros in the map, particularly when you are creating the matrix and using it at the same time, as in inverseLT().
Definition at line 53 of file SparseMatrix.hpp.
#include <SparseMatrix.hpp>
Public Member Functions | |
void | clear () |
clear - set all data to 0 (i.e. remove all data); leave dimensions alone More... | |
SparseVector< T > | colCopy (const unsigned int j) const |
return col j of this SparseMatrix as a SparseVector More... | |
unsigned int | cols () const |
get number of columns - of the real Matrix, not the data array More... | |
unsigned int | datasize () const |
datasize - number of non-zero data More... | |
double | density () const |
density - ratio of number of non-zero element to size() More... | |
SparseVector< T > | diagCopy () const |
return diagonal of this SparseMatrix as a SparseVector More... | |
std::string | dump (const int p=3, bool dosci=false) const |
Dump only non-zero values, with indexes (i,value) More... | |
void | flatten (std::vector< unsigned int > &rows, std::vector< unsigned int > &cols, std::vector< T > &values) const |
Convert to "dump-able" form : 3 parallel vectors; rows, cols, values. More... | |
bool | isEmpty () const |
is this SM empty? NB may have to call zeroize() to get a yes. More... | |
bool | isFilled (const unsigned int i, const unsigned int j) |
true if the element (i,j) is non-zero More... | |
operator Matrix< T > () const | |
cast to Matrix or implicit conversion to Matrix<T> More... | |
SMatProxy< T > | operator() (unsigned int i, unsigned int j) |
operator() for non-const, but SMatProxy does all the work More... | |
const SMatProxy< T > | operator() (unsigned int i, unsigned int j) const |
operator() for const, but SMatProxy does all the work More... | |
SparseMatrix< T > & | operator*= (const T &value) |
Multiply all elements by a scalar T constant. More... | |
SparseMatrix< T > & | operator+= (const Matrix< T > &SM) |
Matrix addition: SparseMatrix += Matrix. More... | |
SparseMatrix< T > & | operator+= (const SparseMatrix< T > &SM) |
Matrix addition: SparseMatrix += SparseMatrix. More... | |
SparseMatrix< T > | operator- () const |
SparseMatrix< T > & | operator-= (const Matrix< T > &SM) |
Matrix subtraction: SparseMatrix -= Matrix. More... | |
SparseMatrix< T > & | operator-= (const SparseMatrix< T > &SM) |
Minimum element - return 0 if empty. More... | |
SparseMatrix< T > & | operator/= (const T &value) |
void | resize (const unsigned int newrows, const unsigned int newcols) |
resize - only changes len and removes elements if necessary More... | |
SparseVector< T > | rowCopy (const unsigned int i) const |
return row i of this SparseMatrix as a SparseVector More... | |
unsigned int | rows () const |
get number of rows - of the real Matrix, not the data array More... | |
unsigned int | size () const |
size of matrix = rows()*cols() More... | |
SparseMatrix () | |
empty constructor More... | |
SparseMatrix (const Matrix< T > &M) | |
constructor from regular Matrix<T> More... | |
SparseMatrix (const SparseMatrix< T > &SM, const unsigned int &rind, const unsigned int &cind, const unsigned int &rnum, const unsigned int &cnum) | |
SparseMatrix (unsigned int r, unsigned int c) | |
constructor with dimensions More... | |
void | swapCols (const unsigned int ii, const unsigned int jj) |
swap columns of this SparseMatrix More... | |
void | swapRows (const unsigned int &ii, const unsigned int &jj) |
swap rows of this SparseMatrix More... | |
void | zeroize (const T tol=static_cast< T >(SparseVector< T >::zeroTolerance)) |
zeroize - remove elements that are less than tolerance in abs value More... | |
Private Member Functions | |
std::map< unsigned int, std::vector< unsigned int > > | columnMap () const |
Private Attributes | |
unsigned int | ncols |
unsigned int | nrows |
dimensions of the "real" matrix (not the number of data stored) More... | |
std::map< unsigned int, SparseVector< T > > | rowsMap |
map of row index, row SparseVector More... | |
|
inline |
empty constructor
Definition at line 436 of file SparseMatrix.hpp.
|
inline |
constructor with dimensions
Definition at line 439 of file SparseMatrix.hpp.
gnsstk::SparseMatrix< T >::SparseMatrix | ( | const SparseMatrix< T > & | SM, |
const unsigned int & | rind, | ||
const unsigned int & | cind, | ||
const unsigned int & | rnum, | ||
const unsigned int & | cnum | ||
) |
sub-matrix constructor
SV | SparseVector to copy |
ind | starting index for the copy |
n | length of new SparseVector |
Definition at line 726 of file SparseMatrix.hpp.
gnsstk::SparseMatrix< T >::SparseMatrix | ( | const Matrix< T > & | M | ) |
constructor from regular Matrix<T>
Definition at line 760 of file SparseMatrix.hpp.
|
inline |
clear - set all data to 0 (i.e. remove all data); leave dimensions alone
Definition at line 514 of file SparseMatrix.hpp.
SparseVector< T > gnsstk::SparseMatrix< T >::colCopy | ( | const unsigned int | j | ) | const |
return col j of this SparseMatrix as a SparseVector
Definition at line 1676 of file SparseMatrix.hpp.
|
inline |
get number of columns - of the real Matrix, not the data array
Definition at line 463 of file SparseMatrix.hpp.
|
inlineprivate |
private function to build a "column map" for this matrix, containing vectors (of row-indexes) for each column. colMap[column index] = vector of all row indexes, in ascending order
Definition at line 676 of file SparseMatrix.hpp.
|
inline |
datasize - number of non-zero data
Definition at line 469 of file SparseMatrix.hpp.
|
inline |
density - ratio of number of non-zero element to size()
Definition at line 486 of file SparseMatrix.hpp.
SparseVector< T > gnsstk::SparseMatrix< T >::diagCopy |
return diagonal of this SparseMatrix as a SparseVector
Definition at line 1694 of file SparseMatrix.hpp.
|
inline |
Dump only non-zero values, with indexes (i,value)
Definition at line 570 of file SparseMatrix.hpp.
|
inline |
Convert to "dump-able" form : 3 parallel vectors; rows, cols, values.
Definition at line 596 of file SparseMatrix.hpp.
|
inline |
is this SM empty? NB may have to call zeroize() to get a yes.
Definition at line 480 of file SparseMatrix.hpp.
|
inline |
true if the element (i,j) is non-zero
Definition at line 528 of file SparseMatrix.hpp.
gnsstk::SparseMatrix< T >::operator Matrix< T > |
cast to Matrix or implicit conversion to Matrix<T>
cast to Matrix<T>
Definition at line 788 of file SparseMatrix.hpp.
|
inline |
operator() for non-const, but SMatProxy does all the work
Definition at line 553 of file SparseMatrix.hpp.
|
inline |
operator() for const, but SMatProxy does all the work
Definition at line 537 of file SparseMatrix.hpp.
SparseMatrix< T > & gnsstk::SparseMatrix< T >::operator*= | ( | const T & | value | ) |
Multiply all elements by a scalar T constant.
Definition at line 1560 of file SparseMatrix.hpp.
SparseMatrix< T > & gnsstk::SparseMatrix< T >::operator+= | ( | const Matrix< T > & | SM | ) |
Matrix addition: SparseMatrix += Matrix.
Definition at line 1530 of file SparseMatrix.hpp.
SparseMatrix< T > & gnsstk::SparseMatrix< T >::operator+= | ( | const SparseMatrix< T > & | SM | ) |
Matrix addition: SparseMatrix += SparseMatrix.
Definition at line 1501 of file SparseMatrix.hpp.
|
inline |
Definition at line 633 of file SparseMatrix.hpp.
SparseMatrix< T > & gnsstk::SparseMatrix< T >::operator-= | ( | const Matrix< T > & | SM | ) |
Matrix subtraction: SparseMatrix -= Matrix.
Definition at line 1422 of file SparseMatrix.hpp.
SparseMatrix< T > & gnsstk::SparseMatrix< T >::operator-= | ( | const SparseMatrix< T > & | SM | ) |
Minimum element - return 0 if empty.
Matrix subtraction: SparseMatrix -= SparseMatrix.
Definition at line 1393 of file SparseMatrix.hpp.
SparseMatrix< T > & gnsstk::SparseMatrix< T >::operator/= | ( | const T & | value | ) |
Definition at line 1586 of file SparseMatrix.hpp.
|
inline |
resize - only changes len and removes elements if necessary
Definition at line 492 of file SparseMatrix.hpp.
SparseVector< T > gnsstk::SparseMatrix< T >::rowCopy | ( | const unsigned int | i | ) | const |
return row i of this SparseMatrix as a SparseVector
Definition at line 1662 of file SparseMatrix.hpp.
|
inline |
get number of rows - of the real Matrix, not the data array
Definition at line 460 of file SparseMatrix.hpp.
|
inline |
size of matrix = rows()*cols()
Definition at line 466 of file SparseMatrix.hpp.
void gnsstk::SparseMatrix< T >::swapCols | ( | const unsigned int | ii, |
const unsigned int | jj | ||
) |
swap columns of this SparseMatrix
Definition at line 1754 of file SparseMatrix.hpp.
void gnsstk::SparseMatrix< T >::swapRows | ( | const unsigned int & | ii, |
const unsigned int & | jj | ||
) |
swap rows of this SparseMatrix
Definition at line 1713 of file SparseMatrix.hpp.
void gnsstk::SparseMatrix< T >::zeroize | ( | const T | tol = static_cast<T>(SparseVector<T>::zeroTolerance) | ) |
zeroize - remove elements that are less than tolerance in abs value
zeroize - remove elements that are less than tolerance in abs value NB. This routine is called only by the user - routines defined here do not zeroize, as there is no way to appropriately choose a tolerance. The default tolerance for this routine is SparseVector<T>::zeroTolerance. The caller may want to consider a tolerance related to SM.maxabs().
Definition at line 806 of file SparseMatrix.hpp.
|
friend |
Compute the identity matrix of dimension dim x dim
dim | dimension of desired identity matrix (dim x dim) |
Definition at line 1776 of file SparseMatrix.hpp.
|
friend |
inverse (via Gauss-Jordan)
Exception | inverse via Gauss-Jordan; NB GJ involves only row operations. NB not the best numerically; for high condition number, use inverseViaCholesky, or cast to Matrix, use either LUD or SVD, then cast back to SparseMatrix. |
Exception |
Definition at line 1890 of file SparseMatrix.hpp.
|
friend |
Compute inverse of lower-triangular SparseMatrix.
inverseLT
Definition at line 2154 of file SparseMatrix.hpp.
|
friend |
Exception | Compute inverse of a symmetric positive definite matrix using Cholesky decomposition. |
A | SparseMatrix to be inverted; symmetric and positive definite, const |
Exception | if input SparseMatrix is not square, not positive definite, or singular |
Definition at line 2277 of file SparseMatrix.hpp.
|
friend |
Exception | Compute lower triangular square root of a symmetric positive definite matrix (Cholesky decomposition) Crout algorithm. |
A | SparseMatrix to be decomposed; symmetric and positive definite, const |
if | input SparseMatrix is not square |
if | input SparseMatrix is not positive definite |
Exception |
Definition at line 2065 of file SparseMatrix.hpp.
|
friend |
|
friend |
Maximum element - return 0 if empty.
Definition at line 881 of file SparseMatrix.hpp.
|
friend |
Maximum absolute value - return 0 if empty.
Definition at line 927 of file SparseMatrix.hpp.
|
friend |
Maximum element - return 0 if empty.
Definition at line 858 of file SparseMatrix.hpp.
|
friend |
Minimum absolute value - return 0 if empty.
Definition at line 904 of file SparseMatrix.hpp.
|
friend |
|
friend |
Matrix multiply: SparseMatrix = Matrix * SparseMatrix.
Definition at line 1270 of file SparseMatrix.hpp.
|
friend |
Matrix,Vector multiply: SparseVector = Matrix * SparseVector.
Definition at line 1023 of file SparseMatrix.hpp.
|
friend |
Matrix multiply: SparseMatrix = SparseMatrix * Matrix.
Definition at line 1225 of file SparseMatrix.hpp.
|
friend |
Matrix multiply: SparseMatrix = SparseMatrix * SparseMatrix.
Definition at line 1186 of file SparseMatrix.hpp.
|
friend |
Matrix multiply: SparseMatrix = SparseMatrix * SparseMatrix.
Definition at line 1186 of file SparseMatrix.hpp.
|
friend |
Matrix,Vector multiply: SparseVector = SparseMatrix * SparseVector.
Definition at line 996 of file SparseMatrix.hpp.
|
friend |
Matrix,Vector multiply: SparseVector = SparseMatrix * Vector.
Definition at line 1056 of file SparseMatrix.hpp.
|
friend |
Vector,Matrix multiply: SparseVector = SparseVector * Matrix.
Definition at line 1119 of file SparseMatrix.hpp.
|
friend |
Vector,Matrix multiply: SparseVector = SparseVector * SparseMatrix.
Definition at line 1083 of file SparseMatrix.hpp.
|
friend |
Vector,Matrix multiply: SparseVector = Vector * SparseMatrix.
Definition at line 1153 of file SparseMatrix.hpp.
|
friend |
Matrix addition: SparseMatrix = Matrix + SparseMatrix : copy, += M in rev order
Definition at line 1643 of file SparseMatrix.hpp.
|
friend |
Matrix addition: SparseMatrix = SparseMatrix + Matrix : copy, += M.
Definition at line 1624 of file SparseMatrix.hpp.
|
friend |
Matrix addition: SparseMatrix = SparseMatrix + SparseMatrix : copy, += SM.
Definition at line 1608 of file SparseMatrix.hpp.
|
friend |
Matrix subtraction: SparseMatrix = Matrix - SparseMatrix.
Definition at line 1483 of file SparseMatrix.hpp.
|
friend |
Matrix subtraction: SparseMatrix = SparseMatrix - Matrix.
Definition at line 1467 of file SparseMatrix.hpp.
|
friend |
Matrix subtraction: SparseMatrix = SparseMatrix - SparseMatrix.
Definition at line 1451 of file SparseMatrix.hpp.
|
friend |
Definition at line 1343 of file SparseMatrix.hpp.
|
friend |
Concatenation SparseMatrix || Vector TD the rest of them...
Definition at line 1313 of file SparseMatrix.hpp.
|
friend |
Proxy needs access to rowsMap.
Definition at line 361 of file SparseMatrix.hpp.
|
friend |
Householder transformation of a matrix.
Exception |
Definition at line 2297 of file SparseMatrix.hpp.
|
friend |
Exception | Square root information measurement update, with new data in the form of a single SparseMatrix concatenation of H and D: A = H || D. See doc for the overloaded SrifMU(). |
Definition at line 2467 of file SparseMatrix.hpp.
|
friend |
Exception |
Definition at line 2627 of file SparseMatrix.hpp.
|
friend |
Compute diagonal of P*C*P^T, ie diagonal of transform of square Matrix C.
diag of P * C * PT
Exception |
Definition at line 1836 of file SparseMatrix.hpp.
|
friend |
transpose
Definition at line 829 of file SparseMatrix.hpp.
|
friend |
products MT*M, M*MT, M*C*MT etc MT * M
Exception |
|
friend |
Exception | Compute upper triangular square root of a symmetric positive definite matrix (Cholesky decomposition) Crout algorithm; that is A = transpose(U)*U. Note that this result will be equal to transpose(lowerCholesky(A)) == transpose(Ch.L from class Cholesky), NOT Ch.U; class Cholesky computes L,U where A = L*LT = U*UT [while A=UT*U here]. |
A | SparseMatrix to be decomposed; symmetric and positive definite, const |
Exception | if input SparseMatrix is not square |
Exception | if input SparseMatrix is not positive definite |
Definition at line 2262 of file SparseMatrix.hpp.
|
private |
Definition at line 664 of file SparseMatrix.hpp.
|
private |
dimensions of the "real" matrix (not the number of data stored)
Definition at line 664 of file SparseMatrix.hpp.
|
private |
map of row index, row SparseVector
Definition at line 667 of file SparseMatrix.hpp.