Standard \n dimensions | \code
mat.rows()
@@ -134,56 +146,25 @@ for (SparseVector::InnerIterator it(vec); it; ++it)
If the type of the sparse matrix or vector depends on a template parameter, then the \c typename keyword is
required to indicate that \c InnerIterator denotes a type; see \ref TopicTemplateKeyword for details.
-
\section TutorialSparseFilling Filling a sparse matrix
-Because of the special storage scheme of a SparseMatrix, adding new nonzero entries can have consequences for performance. For instance, the cost of a purely random insertion into a SparseMatrix is O(nnz), where nnz is the current number of nonzero coefficients. In order to cover all use cases with best efficiency, Eigen provides various mechanisms, from the easiest but slowest, to the fastest but most restrictive.
+Because of the special storage scheme of a SparseMatrix, special care has to be taken when adding new nonzero entries.
+For instance, the cost of inserting nnz non zeros in a a single purely random insertion into a SparseMatrix is O(nnz), where nnz is the current number of nonzero coefficients.
-If you don't have any prior knowledge about the order your matrix will be filled, then the best choice is to use a DynamicSparseMatrix. With a DynamicSparseMatrix, you can add or modify any coefficients at any time using the coeffRef(row,col) method. Here is an example:
+A typical scenario to insert nonzeros is illustrated bellow:
\code
-DynamicSparseMatrix aux(1000,1000);
-aux.reserve(estimated_number_of_non_zero); // optional
-for (...)
- for each j // the j can be random
- for each i interacting with j // the i can be random
- aux.coeffRef(i,j) += foo(i,j);
-\endcode
-Then the DynamicSparseMatrix object can be converted to a compact SparseMatrix to be used, e.g., by one of our supported solvers:
-\code
-SparseMatrix mat(aux);
+1: SparseMatrix m(rows,cols); // default is column major
+2: aux.reserve(VectorXi::Constant(rows,6));
+3: for each i,j such that v_ij != 0
+4: mat.insert(i,j) = v_ij;
+5: mat.makeCompressed(); // optional
\endcode
-In order to optimize this process, instead of the generic coeffRef(i,j) method one can also use:
- - \code m.insert(i,j) = value; \endcode which assumes the coefficient of coordinate (i,j) does not already exist (otherwise this is a programming error and your program will stop).
- - \code m.insertBack(i,j) = value; \endcode which, in addition to the requirements of insert(), also assumes that the coefficient of coordinate (i,j) will be inserted at the end of the target inner-vector. More precisely, if the matrix m is column major, then the row index of the last non zero coefficient of the j-th column must be smaller than i.
+- The key ingredient here is the line 2 where we reserve room for 6 non zeros per column. In many cases, the number of non zero per column or row can be easily known in advance. If it varies significantly for each inner vector, then it is possible to specify a reserve size for each inner vector by provifing a VectorXi or std::vector compatible object. If only a rought estimate of the number of nonzeros per inner-vector can be obtained, it is highly recommended to overestimate it rather than the opposite.
+- The line 4 performs a sorted insertion. In this example, the ideal case is when the j-th column is not full and contains non-zeros whose inner-indices are smaller than i. In this case, this operation boils down to trivial O(1) operation.
+- The line 5 suppresses the left empty room and transform the matrix to a compressed column storage.
-The SparseMatrix class also supports random insertion via the insert() method. However, it should only be used when the inserted coefficient is nearly the last one of the compact storage array. In practice, this means it should be used only to perform random (or sorted) insertion into the current inner-vector while filling the inner-vectors in increasing order. Moreover, with a SparseMatrix an insertion session must be closed by a call to finalize() before any use of the matrix. Here is an example for a column major matrix:
-
-\code
-SparseMatrix mat(1000,1000);
-mat.reserve(estimated_number_of_non_zero); // optional
-for each j // should be in increasing order for performance reasons
- for each i interacting with j // the i can be random
- mat.insert(i,j) = foo(i,j); // optional for a DynamicSparseMatrix
-mat.finalize();
-\endcode
-
-Finally, the fastest way to fill a SparseMatrix object is to insert the elements in purely increasing order (increasing inner index per outer index, and increasing outer index) using the insertBack() function:
-
-\code
-SparseMatrix mat(1000,1000);
-mat.reserve(estimated_number_of_non_zero); // optional
-for(int j=0; j<1000; ++j)
-{
- mat.startVec(j); // optional for a DynamicSparseMatrix
- for each i interacting with j // with increasing i
- mat.insertBack(i,j) = foo(i,j);
-}
-mat.finalize(); // optional for a DynamicSparseMatrix
-\endcode
-Note that there is also an insertBackByOuterInner(Index outer, Index inner) function which allows one to write code agnostic to the storage order.
-
\section TutorialSparseFeatureSet Supported operators and functions
In the following \em sm denotes a sparse matrix, \em sv a sparse vector, \em dm a dense matrix, and \em dv a dense vector.
@@ -214,48 +195,97 @@ res = A.selfadjointView() * d; // if only the lower part of A is stored
\endcode
-\section TutorialSparseDirectSolvers Using the direct solvers
+\section TutorialSparseDirectSolvers Solving linear problems
-To solve a sparse problem you currently have to use one or several of the following "unsupported" modules:
-- \ref SparseExtra_Module
- - \b solvers: SparseLLT, SparseLDLT (\#include )
- - \b notes: built-in basic LLT and LDLT solvers
-- \ref CholmodSupport_Module
- - \b solver: SparseLLT (\#include )
- - \b notes: LLT solving using Cholmod, requires a SparseMatrix object. (recommended for symmetric/selfadjoint problems)
-- \ref UmfPackSupport_Module
- - \b solver: SparseLU (\#include )
- - \b notes: LU solving using UmfPack, requires a SparseMatrix object (recommended for squared matrices)
-- \ref SuperLUSupport_Module
- - \b solver: SparseLU (\#include )
- - \b notes: (LU solving using SuperLU, requires a SparseMatrix object, recommended for squared matrices)
-- \ref TaucsSupport_Module
- - \b solver: SparseLLT (\#include )
- - \b notes: LLT solving using Taucs, requires a SparseMatrix object (not recommended)
+Eigen currently provides a limited set of built-in solvers as well as wrappers to external solver libraries.
+They are summarized in the following table:
-\warning Those modules are currently considered to be unsupported because 1) they are not documented, and 2) their API is likely to change in the future.
+
+Class | Module | Solver kind | Matrix kind | Features related to performance |
+ Dependencies,License |
+ Notes |
-Here is a typical example:
+SimplicialLLt | \link SparseCholesky_Module SparseCholesky \endlink | Direct LLt factorization | SPD | Fill-in reducing |
+ built-in, LGPL |
+ SimplicialLDLt is often preferable |
+SimplicialLDLt | \link SparseCholesky_Module SparseCholesky \endlink | Direct LDLt factorization | SPD | Fill-in reducing |
+ built-in, LGPL |
+ Recommended for very sparse and not too large problems (e.g., 2D Poisson eq.) |
+ConjugateGradient | \link IterativeLinearSolvers_Module IterativeLinearSolvers \endlink | Classic iterative CG | SPD | Preconditionning |
+ built-in, LGPL |
+ Recommended for large symmetric problems (e.g., 3D Poisson eq.) |
+BiCGSTAB | \link IterativeLinearSolvers_Module IterativeLinearSolvers \endlink | Iterative stabilized bi-conjugate gradient | Square | Preconditionning |
+ built-in, LGPL |
+ Might not always converge |
+
+
+
+CholmodDecomposition | \link CholmodSupport_Module CholmodSupport \endlink | Direct LLT factorization | SPD | Fill-in reducing, Leverage fast dense algebra |
+ Requires the SuiteSparse package, \b GPL |
+ |
+UmfPackLU | \link UmfPackSupport_Module UmfPackSupport \endlink | Direct LU factorization | Square | Fill-in reducing, Leverage fast dense algebra |
+ Requires the SuiteSparse package, \b GPL |
+ |
+SuperLU | \link SuperLUSupport_Module SuperLUSupport \endlink | Direct LU factorization | Square | Fill-in reducing, Leverage fast dense algebra |
+ Requires the SuperLU library, custom (BSD-like) |
+ |
+
+
+Here \c SPD means symmetric positive definite.
+
+All these solvers follow the same general concept.
+Here is a typical and general example:
\code
-#include
+#include
// ...
SparseMatrix A;
// fill A
VectorXd b, x;
// fill b
-// solve Ax = b using UmfPack:
-SparseLU,UmfPack> lu_of_A(A);
-if(!lu_of_A.succeeded()) {
+// solve Ax = b
+SolverClassName > solver;
+solver.compute(A);
+if(solver.info()!=Succeeded) {
// decomposition failed
return;
}
-if(!lu_of_A.solve(b,&x)) {
+x = solver.solve(b);
+if(solver.info()!=Succeeded) {
// solving failed
return;
}
+// solve for another right hand side:
+x1 = solver.solve(b1);
\endcode
-See also the class SparseLLT, class SparseLU, and class SparseLDLT.
+For \c SPD solvers, a second optional template argument allows to specify which triangular part have to be used, e.g.:
+
+\code
+#include
+
+ConjugateGradient, Eigen::Upper> solver;
+x = solver.compute(A).solve(b);
+\endcode
+In the above example, only the upper triangular part of the input matrix A is considered for solving. The opposite triangle might either be empty or contain arbitrary values.
+
+In the case where multiple problems with the same sparcity pattern have to be solved, then the "compute" step can be decomposed as follow:
+\code
+SolverClassName > solver;
+solver.analyzePattern(A); // for this step the numerical values of A are not used
+solver.factorize(A);
+x1 = solver.solve(b1);
+x2 = solver.solve(b2);
+...
+A = ...; // modify the values of the nonzeros of A, the nonzeros pattern must stay unchanged
+solver.factorize(A);
+x1 = solver.solve(b1);
+x2 = solver.solve(b2);
+...
+\endcode
+The compute() methode is equivalent to calling both analyzePattern() and factorize().
+
+Finally, each solver provides some specific features, such as determinant, access to the factors, controls of the iterations, etc.
+More details are availble on the documentation of the respective classes.
\li \b Next: TODO
|