only_rows case, columns in the only_cols.
#include <Matrix.h> #include <SparseMatrix.h> #include <ListMatrix.h> using namespace polymake;
The Matrix class family implement matrices in the usual algebraic notion. It contains three persistent matrix types with different data storage organization:
ElementType(), which is 0 for most numerical types) are not stored, but implicitly encoded by the gaps in the
key set. Each row and column is organized as a balanced binary search (AVL) tree.
std::list. Can be parameterized with any
persistent vector type.
The following brief analysis might help you when choosing the most efficient representation for a concrete case. r and c are number of rows and columns respectively, and n is the average number of non-zero elements in a row/column.
SparseMatrix deploys an adaptive reallocation strategy similar to std::vector,
reserving additional stock memory by every reallocation. This way, the amortized reallocation costs are
proportional to the logarithm of the number of appended rows/columns.
| Operation | Matrix | SparseMatrix | ListMatrix |
|---|---|---|---|
| rowwise iteration | O(r) with approx. equal overhead | ||
| columnwise iteration | O(c) | O(c) | O(c), but with rather high overhead |
| random row/column access | O(1) | O(1) | — |
| random element access | O(1) | O(log(n)) | — |
| append a row | O(r c) + reallocation costs | O(r) + reallocation costs + n O(log(n)) | O(1) |
| append a column | O(r c) + reallocation costs | O(c) + reallocation costs + n O(log(n))
See also the special class RestrictedSparseMatrix.
|
c*(appending costs for a VectorType) |
Matrix and SparseMatrix keep the data attached to a smart pointer
with reference counting.
element_type |
ElementType, the persistent element type |
top_type |
Top, the concrete matrix object behind the generic reference |
generic_type |
GenericMatrix<Top> itself |
persistent_type |
Matrix<ElementType> for dense matrices,SparseMatrix<ElementType> for sparse matrices. |
row_typecol_type |
Alias classes representing single
matrix rows and columns respectively. They always belong to the
GenericVector family.
|
bool is_sparse |
true if the element sequence in an arbitrary row or column
can contain gaps expressing implicit zero elements. |
GenericMatrix directly are scattered over the entire page.
They are discernible on the prefix GenericMatrix:: or parameters of type GenericMatrix&.
To use other methods via a generic reference (or pointer), you must first move to the concrete object using the
top() method.
Matrix, SparseMatrix, or ListMatrix.
Matrix, or ListMatrix built with dense row vectors.
Matrix, SparseMatrix, ListMatrix,
or an alias object referring to a mutable (non-const) martix.
SparseMatrix, ListMatrix<SparseVector>,
or an alias object referring to a mutable sparse matrix.
SparseMatrix deploys an adaptive reallocation strategy similar to std::vector,
reserving additional stock memory by every reallocation. A special case, looking at the first glance like a "no operation":
M.resize(M.rows(), M.cols()), gets rid of this extra allocated storage.
The following methods are defined for SparseMatrix objects only.
back_inserter of a std::list.)
They will get the old indices of non-empty rows (columns) assigned in the ascending order.
select function.
In the inverted variant (inv_perm), it is an inverse permutation:
the first element specifies the new position of the 0-th row (column) etc.
The following methods are defined for ListMatrix objects only.
GenericMatrix.
The matrix classes as such do not implement any STL container interfaces: they are simply not defined for multi-dimensional containers. However, a matrix can be disguised in three different ways, yielding STL-conforming containers:
| pseudo-container class | producing function | description |
|---|---|---|
const Rows<Top> |
rows(GenericMatrix&);rows(const GenericMatrix&); |
A sequence of row vectors; each element is a GenericVector.
|
const Cols<Top> |
cols(GenericMatrix&);cols(const GenericMatrix&); |
A sequence of column vectors; each element is a GenericVector
|
const Serialized<Top> |
serialize(GenericMatrix&);serialize(const GenericMatrix&); |
A sequence of elements in rowwise order (the column index changes first).
The Serialized object itself is a GenericVector
of dimension rows()*cols()
|
SparseMatrix specialitiesSparseMatrix are sparse containers, so the iterators over them visit only the (presumably non-zero)
elements physically stored in the matrix.
All these methods are, indeed, defined for the corresponding const_iterator classes too.
Matrix, SparseMatrix,
block matrix built of random-row-access matrices,
or a minor of a random-row-access matrix with a random-access row index set
(sequence or series).
Matrix, SparseMatrix,
block matrix built of random-column-access matrices,
or a minor of a random-column-access matrix with a random-access column index set
(sequence or series).
Matrix,
block matrix built of random-access matrices,
or a minor of a random-access matrix with random-access row and column index sets
(sequence or series).
pm::VectorSlice.
It inherits the const-ness from the original matrix.
const variants of these methods create immutable slice objects.
The index range check can be activated.
SparseMatrix offer the binary tree
data access methods additionally to the GenericVector interface.
operator[] is a mere synonym for row(int), and is defined
with the sole purpose to allow the C-style expressions M[i][j].
Please be aware that M(i,j) is a much more efficient way to access a single matrix
element, as it doesn't involve creation of a temporary vector row object.
GenericMatrix argument to a template function where it has a chance to be evaluated multiple
times. See also prevent_lazy class.
ElementType of matrix and vector operands,
and Scalar type can be different, provided the arithmetic operator with corresponding arguments exists.
The dimension check can be activated.
Transposed&,
referring to the originator matrix.
It inherits the const-ness from the latter.
pm::MatrixMinor.
It inherits the const-ness from the originator matrix.
pm::RowChain
or pm::ColChain.
It is mutable only if both operands are mutable too.
pm::SingleRow&
or SingleCol&, pointing to the source vector.
pm::DiagMatrix.
pm::RepeatedRow or pm::RepeatedCol.
It contains only a single copy of a vector, or just a reference to it,
but makes it to appear as a sequence of identical row (column) vectors.
pm::SameElementSparseMatrix.
It contains only a single copy of an element, or just a reference to an element,
but makes it to appear as a matrix of identical elements.
GenericSet,
Complement,
and a special identifier All (choosing all rows/columns) is allowed here.
const variant of this method creates an immutable minor object.
The index range check can be activated.
| i=0 | the main diagonal (anti-diagonal) |
| i>0 | the i-th diagonal below the main |
| i<0 | the (-i)-th diagonal above the main |
const variants of these methods create immutable slice objects.
Rows and Cols get swapped.
ElementType of the operands must be identical, otherwise you will
get a rather cryptic message from the compiler.
ElementType of the operands must be identical, otherwise you will
get a rather cryptic message from the compiler.
ElementType, where
it cannot be deduced from the function arguments.
true cells
and 0 in false cells.
If omitted, x is taken equal to 1; note the obligatory explicit template parameter specification in this case.
std::ostream::width is not 0 before the operator is called,
it is repeatedly restored for each row, thus yielding a column-aligned table.
std::iostream::failure exception in the constructor,
or via std::iostream::fail() flag in the input operator.
only_rows case, columns in the only_cols.
only_rows case, rows in the only_cols.
A special class allowing for efficient incremental building of sparse matrices.
If you have to fill a sparse matrix element for element, but do not know one of its dimensions in advance,
you would have to resize it repeatedly, which would severely impact the performance. This special class
maintains the internal data only for one direction, and thus can be filled cheaper than SparseMatrix.
On the other hand, almost all matrix operations described above need information about both directions (rows and columns.)
Therefore, RestrictedSparseMatrix is deliberately not included in the GenericMatrix
class family. Its only puspose is to gather elements and finally hand them over to a SparseMatrix object
(without copying!)
only_cols case),
all elements are implicit zeroes.
only_cols case),
initialize the rows (columns) from the input sequence.
Each item supplied by src should be a GenericVector.
std::iostream::failure
if the input data can't be parsed as items of ElementType.
Currently this constructor is defined only for only_rows case.
only_rows and only_cols modi.
only_rows and only_cols modi.
RestrictedSparseMatrix object
is to hand over its internal data to a new created SparseMatrix.
Any further action on the RestrictedSparseMatrix object (except destruction, obviously)
is prohibited: it would lead to the immediate program crash.
template <typename ElementType> struct SparseMatrix2x2 {
int i,j;
ElementType a_ii, a_ij, a_ji, a_jj;
};
A compact encoding of matrices of special structure, which occur as companion matrices
in elimination, triangularization, etc. algorithms. Note that this class is not derived from
GenericMatrix.
For two given integer indices i and j, it encodes a sparse square matrix A of arbitrary dimensions with 4 designated elements Ai,i, Ai,j, Aj,i, and Aj,j. Other elements are implicit zeroes except for the rest of the main diagonal, which is filled with ones.
const Transposed<SparseMatrix2x2>&.
SparseMatrix2x2(int _i, int _j,
const ElementType& _a_ii, const ElementType& _a_ij,
const ElementType& _a_ji, const ElementType& _a_jj);
(*this) with U from the left, that is, apply U to the rows.
(*this) with U from the right, that is, apply U to the columns.