-Class | Module | Solver kind | Matrix kind | Features related to performance |
- Dependencies,License | Notes |
-SimplicialLLt | \link SparseCholesky_Module SparseCholesky \endlink | Direct LLt factorization | SPD | Fill-in reducing |
+
Class | Module | Solver kind | Matrix kind | Features related to performance |
+ Dependencies,License | Notes |
+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 |
+ 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 |
@@ -251,7 +264,7 @@ They are summarized in the following table:
-
CholmodDecomposition | \link CholmodSupport_Module CholmodSupport \endlink | Direct LLT factorization | SPD | Fill-in reducing, Leverage fast dense algebra |
+
CholmodSupernodalLLT | \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 |
@@ -318,6 +331,121 @@ The compute() method is equivalent to calling both analyzePattern() and factoriz
Finally, each solver provides some specific features, such as determinant, access to the factors, controls of the iterations, and so on.
More details are availble in the documentations of the respective classes.
+
+\section TutorialSparseFeatureSet Supported operators and functions
+
+Because of their special storage format, sparse matrices cannot offer the same level of flexbility than dense matrices.
+In Eigen's sparse module we chose to expose only the subset of the dense matrix API which can be efficiently implemented.
+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.
+
+\subsection TutorialSparse_BasicOps Basic operations
+
+%Sparse expressions support most of the unary and binary coefficient wise operations:
+\code
+sm1.real() sm1.imag() -sm1 0.5*sm1
+sm1+sm2 sm1-sm2 sm1.cwiseProduct(sm2)
+\endcode
+However, a strong restriction is that the storage orders must match. For instance, in the following example:
+\code
+sm4 = sm1 + sm2 + sm3;
+\endcode
+sm1, sm2, and sm3 must all be row-major or all column major.
+On the other hand, there is no restriction on the target matrix sm4.
+For instance, this means that for computing \f$ A^T + A \f$, the matrix \f$ A^T \f$ must be evaluated into a temporary matrix of compatible storage order:
+\code
+SparseMatrix A, B;
+B = SparseMatrix(A.transpose()) + A;
+\endcode
+
+Binary coefficient wise operators can also mix sparse and dense expressions:
+\code
+sm2 = sm1.cwiseProduct(dm1);
+dm2 = sm1 + dm1;
+\endcode
+
+
+%Sparse expressions also support transposition:
+\code
+sm1 = sm2.transpose();
+sm1 = sm2.adjoint();
+\endcode
+However, there is no transposeInPlace() method.
+
+
+\subsection TutorialSparse_Products Matrix products
+
+%Eigen supports various kind of sparse matrix products which are summarize below:
+ - \b sparse-dense:
+ \code
+dv2 = sm1 * dv1;
+dm2 = dm1 * sm1.adjoint();
+dm2 = 2. * sm1 * dm1;
+ \endcode
+ - \b symmetric \b sparse-dense. The product of a sparse symmetric matrix with a dense matrix (or vector) can also be optimized by specifying the symmetry with selfadjointView():
+ \code
+dm2 = sm1.selfadjointView<>() * dm1; // if all coefficients of A are stored
+dm2 = A.selfadjointView() * dm1; // if only the upper part of A is stored
+dm2 = A.selfadjointView() * dm1; // if only the lower part of A is stored
+ \endcode
+ - \b sparse-sparse. For sparse-sparse products, two different algorithms are available. The default one is conservative and preserve the explicit zeros that might appear:
+ \code
+sm3 = sm1 * sm2;
+sm3 = 4 * sm1.adjoint() * sm2;
+ \endcode
+ The second algorithm punes on the fly the explicit zeros, or the values smaller than a given threshold. It is enabled and controlled through the prune() functions:
+ \code
+sm3 = (sm1 * sm2).prune(); // removes numerical zeros
+sm3 = (sm1 * sm2).prune(ref); // removes elements much smaller than ref
+sm3 = (sm1 * sm2).prune(ref,epsilon); // removes elements much smaller than ref*epsilon
+ \endcode
+
+ - \b permutations. Finally, permutations can be applied to sparse matrices too:
+ \code
+PermutationMatrix P = ...;
+sm2 = P * sm1;
+sm2 = sm1 * P.inverse();
+sm2 = sm1.transpose() * P;
+ \endcode
+
+
+\subsection TutorialSparse_TriangularSelfadjoint Triangular and selfadjoint views
+
+Just as dense matrices, the triangularView() function can be used to address a triangular part of the matrix, and perform triangular solves with a dense right and side:
+\code
+dm2 = sm1.triangularView(dm1);
+dv2 = sm1.transpose().triangularView(dv1);
+\endcode
+
+The selfadjointView() function permits various operations:
+ - optimized sparse-dense matrix products:
+ \code
+dm2 = sm1.selfadjointView<>() * dm1; // if all coefficients of A are stored
+dm2 = A.selfadjointView() * dm1; // if only the upper part of A is stored
+dm2 = A.selfadjointView() * dm1; // if only the lower part of A is stored
+ \endcode
+ - copy of triangular parts:
+ \code
+sm2 = sm1.selfadjointView(); // makes a full selfadjoint matrix from the upper triangular part
+sm2.selfadjointView() = sm1.selfadjointView(); // copie the upper triangular part to the lower triangular part
+ \endcode
+ - application of symmetric permutations:
+ \code
+PermutationMatrix P = ...;
+sm2 = A.selfadjointView().twistedBy(P); // compute P S P' from the upper triangular part of A, and make it a full matrix
+sm2.selfadjointView() = A.selfadjointView().twistedBy(P); // compute P S P' from the lower triangular part of A, and then only compute the lower part
+ \endcode
+
+\subsection TutorialSparse_Submat Sub-matrices
+
+%Sparse matrices does not support yet the addressing of arbitrary sub matrices. Currently, one can only reference a set of contiguous \em inner vectors, i.e., a set of contiguous rows for a row-major matrix, or a set of contiguous columns for a column major matrix:
+\code
+ sm1.innerVector(j); // returns an expression of the j-th column (resp. row) of the matrix if sm1 is col-major (resp. row-major)
+ sm1.innerVectors(j, nb); // returns an expression of the nb columns (resp. row) starting from the j-th column (resp. row)
+ // of the matrix if sm1 is col-major (resp. row-major)
+ sm1.middleRows(j, nb); // for row major matrices only, get a range of nb rows
+ sm1.middleCols(j, nb); // for column major matrices only, get a range of nb columns
+\endcode
+
\li \b Next: \ref TutorialMapClass
*/
diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt
index 50ce7ee0c..96bff41bf 100644
--- a/doc/CMakeLists.txt
+++ b/doc/CMakeLists.txt
@@ -36,6 +36,7 @@ set(snippets_targets "")
add_definitions("-DEIGEN_MAKING_DOCS")
add_subdirectory(examples)
+add_subdirectory(special_examples)
add_subdirectory(snippets)
add_custom_target(
diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in
index e87c7c2ba..e9e89d486 100644
--- a/doc/Doxyfile.in
+++ b/doc/Doxyfile.in
@@ -592,6 +592,7 @@ RECURSIVE = YES
EXCLUDE = "${Eigen_SOURCE_DIR}/Eigen/Eigen2Support" \
"${Eigen_SOURCE_DIR}/Eigen/src/Eigen2Support" \
"${Eigen_SOURCE_DIR}/doc/examples" \
+ "${Eigen_SOURCE_DIR}/doc/special_examples" \
"${Eigen_SOURCE_DIR}/doc/snippets"
# The EXCLUDE_SYMLINKS tag can be used select whether or not files or
@@ -638,7 +639,9 @@ EXCLUDE_SYMBOLS = internal::* Flagged* *InnerIterator* DenseStorage<*
EXAMPLE_PATH = "${Eigen_SOURCE_DIR}/doc/snippets" \
"${Eigen_BINARY_DIR}/doc/snippets" \
"${Eigen_SOURCE_DIR}/doc/examples" \
- "${Eigen_BINARY_DIR}/doc/examples"
+ "${Eigen_BINARY_DIR}/doc/examples" \
+ "${Eigen_SOURCE_DIR}/doc/special_examples" \
+ "${Eigen_BINARY_DIR}/doc/special_examples"
# If the value of the EXAMPLE_PATH tag contains directories, you can use the
# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp
diff --git a/doc/SparseQuickReference.dox b/doc/SparseQuickReference.dox
index 7b86e50d3..7d6eb0fa9 100644
--- a/doc/SparseQuickReference.dox
+++ b/doc/SparseQuickReference.dox
@@ -18,7 +18,7 @@ In this page, we give a quick summary of the main operations available for spars
i.e either row major or column major. The default is column major. Most arithmetic operations on sparse matrices will assert that they have the same storage order. Moreover, when interacting with external libraries that are not yet supported by Eigen, it is important to know how to send the required matrix pointers.
\section Constructors Constructors and assignments
-SparseMatrix is the core class to build and manipulate sparse matrices in Eigen. It takes as template parameters the Scalar type and the storage order, either RowMajor or ColumnMajor. The default is ColumnMajor. ??? It is possible to modify the default storage order at compile-time with the cmake variable \b EIGEN_DEFAULT_ROW_MAJOR ???
+SparseMatrix is the core class to build and manipulate sparse matrices in Eigen. It takes as template parameters the Scalar type and the storage order, either RowMajor or ColumnMajor. The default is ColumnMajor.
\code
SparseMatrix sm1(1000,1000); // 1000x1000 compressed sparse matrix of double.
@@ -195,4 +195,4 @@ The following functions are useful to extract a block of rows (resp. columns) fr
*/
-}
\ No newline at end of file
+}
diff --git a/doc/TutorialSparse_example_details.dox b/doc/TutorialSparse_example_details.dox
new file mode 100644
index 000000000..0438da8bb
--- /dev/null
+++ b/doc/TutorialSparse_example_details.dox
@@ -0,0 +1,4 @@
+/**
+\page TutorialSparse_example_details
+\include Tutorial_sparse_example_details.cpp
+*/
diff --git a/doc/special_examples/CMakeLists.txt b/doc/special_examples/CMakeLists.txt
new file mode 100644
index 000000000..eeeae1d2a
--- /dev/null
+++ b/doc/special_examples/CMakeLists.txt
@@ -0,0 +1,20 @@
+
+if(NOT EIGEN_TEST_NOQT)
+ find_package(Qt4)
+ if(QT4_FOUND)
+ include(${QT_USE_FILE})
+ endif()
+endif(NOT EIGEN_TEST_NOQT)
+
+
+if(QT4_FOUND)
+ add_executable(Tutorial_sparse_example Tutorial_sparse_example.cpp Tutorial_sparse_example_details.cpp)
+ target_link_libraries(Tutorial_sparse_example ${EIGEN_STANDARD_LIBRARIES_TO_LINK_TO} ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY})
+
+ add_custom_command(
+ TARGET Tutorial_sparse_example
+ POST_BUILD
+ COMMAND Tutorial_sparse_example
+ ARGS ${CMAKE_CURRENT_BINARY_DIR}/../html/Tutorial_sparse_example.jpeg
+ )
+endif(QT4_FOUND)
diff --git a/doc/special_examples/Tutorial_sparse_example.cpp b/doc/special_examples/Tutorial_sparse_example.cpp
new file mode 100644
index 000000000..002f19f01
--- /dev/null
+++ b/doc/special_examples/Tutorial_sparse_example.cpp
@@ -0,0 +1,32 @@
+#include
+#include
+
+typedef Eigen::SparseMatrix SpMat; // declares a column-major sparse matrix type of double
+typedef Eigen::Triplet T;
+
+void buildProblem(std::vector& coefficients, Eigen::VectorXd& b, int n);
+void saveAsBitmap(const Eigen::VectorXd& x, int n, const char* filename);
+
+int main(int argc, char** argv)
+{
+ int n = 300; // size of the image
+ int m = n*n; // number of unknows (=number of pixels)
+
+ // Assembly:
+ std::vector coefficients; // list of non-zeros coefficients
+ Eigen::VectorXd b(m); // the right hand side-vector resulting from the constraints
+ buildProblem(coefficients, b, n);
+
+ SpMat A(m,m);
+ A.setFromTriplets(coefficients.begin(), coefficients.end());
+
+ // Solving:
+ Eigen::SimplicialCholesky chol(A); // performs a Cholesky factorization of A
+ Eigen::VectorXd x = chol.solve(b); // use the factorization to solve for the given right hand side
+
+ // Export the result to a file:
+ saveAsBitmap(x, n, argv[1]);
+
+ return 0;
+}
+
diff --git a/doc/special_examples/Tutorial_sparse_example_details.cpp b/doc/special_examples/Tutorial_sparse_example_details.cpp
new file mode 100644
index 000000000..8c3020b63
--- /dev/null
+++ b/doc/special_examples/Tutorial_sparse_example_details.cpp
@@ -0,0 +1,44 @@
+#include
+#include
+#include
+
+typedef Eigen::SparseMatrix SpMat; // declares a column-major sparse matrix type of double
+typedef Eigen::Triplet T;
+
+void insertCoefficient(int id, int i, int j, double w, std::vector& coeffs,
+ Eigen::VectorXd& b, const Eigen::VectorXd& boundary)
+{
+ int n = boundary.size();
+ int id1 = i+j*n;
+
+ if(i==-1 || i==n) b(id) -= w * boundary(j); // constrained coeffcieint
+ else if(j==-1 || j==n) b(id) -= w * boundary(i); // constrained coeffcieint
+ else coeffs.push_back(T(id,id1,w)); // unknown coefficient
+}
+
+void buildProblem(std::vector& coefficients, Eigen::VectorXd& b, int n)
+{
+ b.setZero();
+ Eigen::ArrayXd boundary = Eigen::ArrayXd::LinSpaced(n, 0,M_PI).sin().pow(2);
+ for(int j=0; j bits = (x*255).cast();
+ QImage img(bits.data(), n,n,QImage::Format_Indexed8);
+ img.setColorCount(256);
+ for(int i=0;i<256;i++) img.setColor(i,qRgb(i,i,i));
+ img.save(filename);
+}