namespace Eigen { /** \page TutorialSparse Tutorial - Getting started with the sparse module \ingroup Tutorial
\ref index "Overview" | \ref TutorialCore "Core features" | \ref TutorialGeometry "Geometry" | \ref TutorialAdvancedLinearAlgebra "Advanced linear algebra" | \b Sparse \b matrix
\b Table \b of \b contents \n - \ref TutorialSparseIntro - \ref TutorialSparseFilling - \ref TutorialSparseFeatureSet - \ref TutorialSparseDirectSolvers
\section TutorialSparseIntro Sparse matrix representations In many applications (e.g., finite element methods) it is common to deal with very large matrices where only a few coefficients are different than zero. Both in term of memory consumption and performance, it is fundamental to use an adequate representation storing only nonzero coefficients. Such a matrix is called a sparse matrix. \b Declaring \b sparse \b matrices \b and \b vectors \n The SparseMatrix class is the main sparse matrix representation of the Eigen's sparse module which offers high performance, low memory usage, and compatibility with most of sparse linear algebra packages. Because of its limited flexibility, we also provide a DynamicSparseMatrix variante taillored for low-level sparse matrix assembly. Both of them can be either row major or column major: \code #include SparseMatrix > m1(1000,2000); // declare a 1000x2000 col-major compressed sparse matrix of complex SparseMatrix m2(1000,2000); // declare a 1000x2000 row-major compressed sparse matrix of double DynamicSparseMatrix > m1(1000,2000); // declare a 1000x2000 col-major dynamic sparse matrix of complex DynamicSparseMatrix m2(1000,2000); // declare a 1000x2000 row-major dynamic sparse matrix of double \endcode Although a sparse matrix could also be used to represent a sparse vector, for that purpose it is better to use the specialized SparseVector class: \code SparseVector > v1(1000); // declare a column sparse vector of complex of size 1000 SparseVector v2(1000); // declare a row sparse vector of double of size 1000 \endcode Note that here the size of a vector denotes its dimension and not the number of nonzero coefficients which is initially zero (like sparse matrices). \b Overview \b of \b the \b internal \b sparse \b storage \n In order to get the best of the Eigen's sparse objects, it is important to have a rough idea of the way they are internally stored. The SparseMatrix class implements the common and generic Compressed Column/Row Storage scheme. It consists of three compact arrays storing the values with their respective inner coordinates, and pointer indices to the begining of each outer vector. For instance, let \c m be a column-major sparse matrix. Then its nonzero coefficients are sequentially stored in memory in a column-major order (\em values). A second array of integer stores the respective row index of each coefficient (\em inner \em indices). Finally, a third array of integer, having the same length than the number of columns, stores the index in the previous arrays of the first element of each column (\em outer \em indices). Here is an example, with the matrix:
03000
2200017
75010
00000
001408
and its internal representation using the Compressed Column Storage format:
Values: 22735141178
Inner indices: 1202 42 14
Outer indices:
02456\em 7
As you can guess, here the storage order is even more important than with dense matrix. We will therefore often make a clear difference between the \em inner and \em outer dimensions. For instance, it is easy to loop over the coefficients of an \em inner \em vector (e.g., a column of a column-major matrix), but completely inefficient to do the same for an \em outer \em vector (e.g., a row of a col-major matrix). The SparseVector class implements the same compressed storage scheme but, of course, without any outer index buffer. Since all nonzero coefficients of such a matrix are sequentially stored in memory, random insertion of new nonzeros can be extremely costly. To overcome this limitation, Eigen's sparse module provides a DynamicSparseMatrix class which is basically implemented as an array of SparseVector. In other words, a DynamicSparseMatrix is a SparseMatrix where the values and inner-indices arrays have been splitted into multiple small and resizable arrays. Assuming the number of nonzeros per inner vector is relatively low, this slight modification allow for very fast random insertion at the cost of a slight memory overhead and a lost of compatibility with other sparse libraries used by some of our highlevel solvers. Note that the major memory overhead comes from the extra memory preallocated by each inner vector to avoid an expensive memory reallocation at every insertion. To summarize, it is recommanded to use a SparseMatrix whenever this is possible, and reserve the use of DynamicSparseMatrix for matrix assembly purpose when a SparseMatrix is not flexible enough. The respective pro/cons of both representations are summarized in the following table:
SparseMatrixDynamicSparseMatrix
memory usage*****
sorted insertion******
random insertion \n in sorted inner vector****
sorted insertion \n in random inner vector-***
random insertion-**
coeff wise unary operators******
coeff wise binary operators******
matrix products*****(*)
transpose*****
redux*****
*= scalar*****
Compatibility with highlevel solvers \n (TAUCS, Cholmod, SuperLU, UmfPack)***-
\b Matrix \b and \b vector \b properties \n Here mat and vec represents any sparse-matrix and sparse-vector types respectively.
Standard \n dimensions\code mat.rows() mat.cols()\endcode \code vec.size() \endcode
Sizes along the \n inner/outer dimensions\code mat.innerSize() mat.outerSize()\endcode
Number of non \n zero coefficiens\code mat.nonZeros() \endcode \code vec.nonZeros() \endcode
\b Iterating \b over \b the \b nonzero \b coefficients \n Iterating over the coefficients of a sparse matrix can be done only in the same order than the storage order. Here is an example:
\code SparseMatrixType mat(rows,cols); for (int k=0; k\ \code SparseVector vec(size); for (SparseVector::InnerIterator it(vec); it; ++it) { it.value(); // == vec[ it.index() ] it.index(); } \endcode
\section TutorialSparseFilling Filling a sparse matrix A DynamicSparseMatrix object can be set and updated just like any dense matrix using the coeffRef(row,col) method. If the coefficient is not stored yet, then it will be inserted in the matrix. Here is an example: \code DynamicSparseMatrix aux(1000,1000); for (...) for each i for each j interacting with i aux.coeffRef(i,j) += foo(o1,o2); SparseMatrix mat(aux); // convert the DynamicSparseMatrix to a SparseMatrix \endcode Sometimes, however, we simply want to set all the coefficients of a matrix before using it through standard matrix operations (addition, product, etc.). In that case it faster to use the low-level startFill()/fill()/fillrand()/endFill() interface. Even though this interface is availabe for both sparse matrix types, their respective restrictions slightly differ from one representation to the other. In all case, a call to startFill() set the matrix to zero, and the fill*() functions will fail if the coefficient already exist. As a first difference, for SparseMatrix, the fill*() functions can only be called inside a startFill()/endFill() pair, and no other member functions are allowed during the filling process, i.e., until endFill() has been called. On the other hand, a DynamicSparseMatrix is always in a stable state, and the startFill()/endFill() functions are only for compatibility purpose. Another difference is that the fill*() functions must be called with increasing outer indices for a SparseMatrix, while they can be random for a DynamicSparseMatrix. Finally, the fill() function assumes the coefficient are inserted in a sorted order per inner vector, while the fillrand() variante allows random insertions (the outer indices must still be sorted for SparseMatrix). Some examples: 1 - If you can set the coefficients in exactly the same order that the storage order, then the matrix can be filled directly and very efficiently. Here is an example initializing a random, row-major sparse matrix: \code SparseMatrix m(rows,cols); m.startFill(rows*cols*percent_of_non_zero); // estimate of the number of nonzeros (optional) for (int i=0; i\ m(rows,cols); m.startFill(rows*cols*percent_of_non_zero); // estimate of the number of nonzeros (optional) for (int i=0; i\ m(rows,cols); { RandomSetter > setter(m); for (int k=0; k\().solveTriangular(dv2); \endcode The product of a sparse matrix A time a dense matrix/vector dv with A symmetric can be optimized by telling that to Eigen: \code res = A.marked() * dv; // if all coefficients of A are stored res = A.marked() * dv; // if only the upper part of A is stored res = A.marked() * dv; // if only the lower part of A is stored \endcode \section TutorialSparseDirectSolvers Using the direct solvers TODO \subsection TutorialSparseDirectSolvers_LLT LLT Cholmod, Taucs. \subsection TutorialSparseDirectSolvers_LDLT LDLT \subsection TutorialSparseDirectSolvers_LU LU SuperLU, UmfPack. */ }