xref: /aosp_15_r20/external/eigen/doc/TutorialSparse.dox (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Linamespace Eigen {
2*bf2c3715SXin Li
3*bf2c3715SXin Li/** \eigenManualPage TutorialSparse Sparse matrix manipulations
4*bf2c3715SXin Li
5*bf2c3715SXin Li\eigenAutoToc
6*bf2c3715SXin Li
7*bf2c3715SXin LiManipulating and solving sparse problems involves various modules which are summarized below:
8*bf2c3715SXin Li
9*bf2c3715SXin Li<table class="manual">
10*bf2c3715SXin Li<tr><th>Module</th><th>Header file</th><th>Contents</th></tr>
11*bf2c3715SXin Li<tr><td>\link SparseCore_Module SparseCore \endlink</td><td>\code#include <Eigen/SparseCore>\endcode</td><td>SparseMatrix and SparseVector classes, matrix assembly, basic sparse linear algebra (including sparse triangular solvers)</td></tr>
12*bf2c3715SXin Li<tr><td>\link SparseCholesky_Module SparseCholesky \endlink</td><td>\code#include <Eigen/SparseCholesky>\endcode</td><td>Direct sparse LLT and LDLT Cholesky factorization to solve sparse self-adjoint positive definite problems</td></tr>
13*bf2c3715SXin Li<tr><td>\link SparseLU_Module SparseLU \endlink</td><td>\code #include<Eigen/SparseLU> \endcode</td>
14*bf2c3715SXin Li<td>%Sparse LU factorization to solve general square sparse systems</td></tr>
15*bf2c3715SXin Li<tr><td>\link SparseQR_Module SparseQR \endlink</td><td>\code #include<Eigen/SparseQR>\endcode </td><td>%Sparse QR factorization for solving sparse linear least-squares problems</td></tr>
16*bf2c3715SXin Li<tr><td>\link IterativeLinearSolvers_Module IterativeLinearSolvers \endlink</td><td>\code#include <Eigen/IterativeLinearSolvers>\endcode</td><td>Iterative solvers to solve large general linear square problems (including self-adjoint positive definite problems)</td></tr>
17*bf2c3715SXin Li<tr><td>\link Sparse_Module Sparse \endlink</td><td>\code#include <Eigen/Sparse>\endcode</td><td>Includes all the above modules</td></tr>
18*bf2c3715SXin Li</table>
19*bf2c3715SXin Li
20*bf2c3715SXin Li\section TutorialSparseIntro Sparse matrix format
21*bf2c3715SXin Li
22*bf2c3715SXin LiIn many applications (e.g., finite element methods) it is common to deal with very large matrices where only a few coefficients are different from zero.  In such cases, memory consumption can be reduced and performance increased by using a specialized representation storing only the nonzero coefficients. Such a matrix is called a sparse matrix.
23*bf2c3715SXin Li
24*bf2c3715SXin Li\b The \b %SparseMatrix \b class
25*bf2c3715SXin Li
26*bf2c3715SXin LiThe class SparseMatrix is the main sparse matrix representation of Eigen's sparse module; it offers high performance and low memory usage.
27*bf2c3715SXin LiIt implements a more versatile variant of the widely-used Compressed Column (or Row) Storage scheme.
28*bf2c3715SXin LiIt consists of four compact arrays:
29*bf2c3715SXin Li - \c Values: stores the coefficient values of the non-zeros.
30*bf2c3715SXin Li - \c InnerIndices: stores the row (resp. column) indices of the non-zeros.
31*bf2c3715SXin Li - \c OuterStarts: stores for each column (resp. row) the index of the first non-zero in the previous two arrays.
32*bf2c3715SXin Li - \c InnerNNZs: stores the number of non-zeros of each column (resp. row).
33*bf2c3715SXin LiThe word \c inner refers to an \em inner \em vector that is a column for a column-major matrix, or a row for a row-major matrix.
34*bf2c3715SXin LiThe word \c outer refers to the other direction.
35*bf2c3715SXin Li
36*bf2c3715SXin LiThis storage scheme is better explained on an example. The following matrix
37*bf2c3715SXin Li<table class="manual">
38*bf2c3715SXin Li<tr><td> 0</td><td>3</td><td> 0</td><td>0</td><td> 0</td></tr>
39*bf2c3715SXin Li<tr><td>22</td><td>0</td><td> 0</td><td>0</td><td>17</td></tr>
40*bf2c3715SXin Li<tr><td> 7</td><td>5</td><td> 0</td><td>1</td><td> 0</td></tr>
41*bf2c3715SXin Li<tr><td> 0</td><td>0</td><td> 0</td><td>0</td><td> 0</td></tr>
42*bf2c3715SXin Li<tr><td> 0</td><td>0</td><td>14</td><td>0</td><td> 8</td></tr>
43*bf2c3715SXin Li</table>
44*bf2c3715SXin Li
45*bf2c3715SXin Liand one of its possible sparse, \b column \b major representation:
46*bf2c3715SXin Li<table class="manual">
47*bf2c3715SXin Li<tr><td>Values:</td>        <td>22</td><td>7</td><td>_</td><td>3</td><td>5</td><td>14</td><td>_</td><td>_</td><td>1</td><td>_</td><td>17</td><td>8</td></tr>
48*bf2c3715SXin Li<tr><td>InnerIndices:</td>  <td> 1</td><td>2</td><td>_</td><td>0</td><td>2</td><td> 4</td><td>_</td><td>_</td><td>2</td><td>_</td><td> 1</td><td>4</td></tr>
49*bf2c3715SXin Li</table>
50*bf2c3715SXin Li<table class="manual">
51*bf2c3715SXin Li<tr><td>OuterStarts:</td><td>0</td><td>3</td><td>5</td><td>8</td><td>10</td><td>\em 12 </td></tr>
52*bf2c3715SXin Li<tr><td>InnerNNZs:</td>    <td>2</td><td>2</td><td>1</td><td>1</td><td> 2</td><td></td></tr>
53*bf2c3715SXin Li</table>
54*bf2c3715SXin Li
55*bf2c3715SXin LiCurrently the elements of a given inner vector are guaranteed to be always sorted by increasing inner indices.
56*bf2c3715SXin LiThe \c "_" indicates available free space to quickly insert new elements.
57*bf2c3715SXin LiAssuming no reallocation is needed, the insertion of a random element is therefore in O(nnz_j) where nnz_j is the number of nonzeros of the respective inner vector.
58*bf2c3715SXin LiOn the other hand, inserting elements with increasing inner indices in a given inner vector is much more efficient since this only requires to increase the respective \c InnerNNZs entry that is a O(1) operation.
59*bf2c3715SXin Li
60*bf2c3715SXin LiThe case where no empty space is available is a special case, and is referred as the \em compressed mode.
61*bf2c3715SXin LiIt corresponds to the widely used Compressed Column (or Row) Storage schemes (CCS or CRS).
62*bf2c3715SXin LiAny SparseMatrix can be turned to this form by calling the SparseMatrix::makeCompressed() function.
63*bf2c3715SXin LiIn this case, one can remark that the \c InnerNNZs array is redundant with \c OuterStarts because we have the equality: \c InnerNNZs[j] = \c OuterStarts[j+1]-\c OuterStarts[j].
64*bf2c3715SXin LiTherefore, in practice a call to SparseMatrix::makeCompressed() frees this buffer.
65*bf2c3715SXin Li
66*bf2c3715SXin LiIt is worth noting that most of our wrappers to external libraries requires compressed matrices as inputs.
67*bf2c3715SXin Li
68*bf2c3715SXin LiThe results of %Eigen's operations always produces \b compressed sparse matrices.
69*bf2c3715SXin LiOn the other hand, the insertion of a new element into a SparseMatrix converts this later to the \b uncompressed mode.
70*bf2c3715SXin Li
71*bf2c3715SXin LiHere is the previous matrix represented in compressed mode:
72*bf2c3715SXin Li<table class="manual">
73*bf2c3715SXin Li<tr><td>Values:</td>        <td>22</td><td>7</td><td>3</td><td>5</td><td>14</td><td>1</td><td>17</td><td>8</td></tr>
74*bf2c3715SXin Li<tr><td>InnerIndices:</td>  <td> 1</td><td>2</td><td>0</td><td>2</td><td> 4</td><td>2</td><td> 1</td><td>4</td></tr>
75*bf2c3715SXin Li</table>
76*bf2c3715SXin Li<table class="manual">
77*bf2c3715SXin Li<tr><td>OuterStarts:</td><td>0</td><td>2</td><td>4</td><td>5</td><td>6</td><td>\em 8 </td></tr>
78*bf2c3715SXin Li</table>
79*bf2c3715SXin Li
80*bf2c3715SXin LiA SparseVector is a special case of a SparseMatrix where only the \c Values and \c InnerIndices arrays are stored.
81*bf2c3715SXin LiThere is no notion of compressed/uncompressed mode for a SparseVector.
82*bf2c3715SXin Li
83*bf2c3715SXin Li
84*bf2c3715SXin Li\section TutorialSparseExample First example
85*bf2c3715SXin Li
86*bf2c3715SXin LiBefore describing each individual class, let's start with the following typical example: solving the Laplace equation \f$ \Delta u = 0 \f$ on a regular 2D grid using a finite difference scheme and Dirichlet boundary conditions.
87*bf2c3715SXin LiSuch problem can be mathematically expressed as a linear problem of the form \f$ Ax=b \f$ where \f$ x \f$ is the vector of \c m unknowns (in our case, the values of the pixels), \f$ b \f$ is the right hand side vector resulting from the boundary conditions, and \f$ A \f$ is an \f$ m \times m \f$ matrix containing only a few non-zero elements resulting from the discretization of the Laplacian operator.
88*bf2c3715SXin Li
89*bf2c3715SXin Li<table class="manual">
90*bf2c3715SXin Li<tr><td>
91*bf2c3715SXin Li\include Tutorial_sparse_example.cpp
92*bf2c3715SXin Li</td>
93*bf2c3715SXin Li<td>
94*bf2c3715SXin Li\image html Tutorial_sparse_example.jpeg
95*bf2c3715SXin Li</td></tr></table>
96*bf2c3715SXin Li
97*bf2c3715SXin LiIn this example, we start by defining a column-major sparse matrix type of double \c SparseMatrix<double>, and a triplet list of the same scalar type \c  Triplet<double>. A triplet is a simple object representing a non-zero entry as the triplet: \c row index, \c column index, \c value.
98*bf2c3715SXin Li
99*bf2c3715SXin LiIn the main function, we declare a list \c coefficients of triplets (as a std vector) and the right hand side vector \f$ b \f$ which are filled by the \a buildProblem function.
100*bf2c3715SXin LiThe raw and flat list of non-zero entries is then converted to a true SparseMatrix object \c A.
101*bf2c3715SXin LiNote that the elements of the list do not have to be sorted, and possible duplicate entries will be summed up.
102*bf2c3715SXin Li
103*bf2c3715SXin LiThe last step consists of effectively solving the assembled problem.
104*bf2c3715SXin LiSince the resulting matrix \c A is symmetric by construction, we can perform a direct Cholesky factorization via the SimplicialLDLT class which behaves like its LDLT counterpart for dense objects.
105*bf2c3715SXin Li
106*bf2c3715SXin LiThe resulting vector \c x contains the pixel values as a 1D array which is saved to a jpeg file shown on the right of the code above.
107*bf2c3715SXin Li
108*bf2c3715SXin LiDescribing the \a buildProblem and \a save functions is out of the scope of this tutorial. They are given \ref TutorialSparse_example_details "here" for the curious and reproducibility purpose.
109*bf2c3715SXin Li
110*bf2c3715SXin Li
111*bf2c3715SXin Li
112*bf2c3715SXin Li
113*bf2c3715SXin Li\section TutorialSparseSparseMatrix The SparseMatrix class
114*bf2c3715SXin Li
115*bf2c3715SXin Li\b %Matrix \b and \b vector \b properties \n
116*bf2c3715SXin Li
117*bf2c3715SXin LiThe SparseMatrix and SparseVector classes take three template arguments:
118*bf2c3715SXin Li * the scalar type (e.g., double)
119*bf2c3715SXin Li * the storage order (ColMajor or RowMajor, the default is ColMajor)
120*bf2c3715SXin Li * the inner index type (default is \c int).
121*bf2c3715SXin Li
122*bf2c3715SXin LiAs for dense Matrix objects, constructors takes the size of the object.
123*bf2c3715SXin LiHere are some examples:
124*bf2c3715SXin Li
125*bf2c3715SXin Li\code
126*bf2c3715SXin LiSparseMatrix<std::complex<float> > mat(1000,2000);         // declares a 1000x2000 column-major compressed sparse matrix of complex<float>
127*bf2c3715SXin LiSparseMatrix<double,RowMajor> mat(1000,2000);              // declares a 1000x2000 row-major compressed sparse matrix of double
128*bf2c3715SXin LiSparseVector<std::complex<float> > vec(1000);              // declares a column sparse vector of complex<float> of size 1000
129*bf2c3715SXin LiSparseVector<double,RowMajor> vec(1000);                   // declares a row sparse vector of double of size 1000
130*bf2c3715SXin Li\endcode
131*bf2c3715SXin Li
132*bf2c3715SXin LiIn the rest of the tutorial, \c mat and \c vec represent any sparse-matrix and sparse-vector objects, respectively.
133*bf2c3715SXin Li
134*bf2c3715SXin LiThe dimensions of a matrix can be queried using the following functions:
135*bf2c3715SXin Li<table class="manual">
136*bf2c3715SXin Li<tr><td>Standard \n dimensions</td><td>\code
137*bf2c3715SXin Limat.rows()
138*bf2c3715SXin Limat.cols()\endcode</td>
139*bf2c3715SXin Li<td>\code
140*bf2c3715SXin Livec.size() \endcode</td>
141*bf2c3715SXin Li</tr>
142*bf2c3715SXin Li<tr><td>Sizes along the \n inner/outer dimensions</td><td>\code
143*bf2c3715SXin Limat.innerSize()
144*bf2c3715SXin Limat.outerSize()\endcode</td>
145*bf2c3715SXin Li<td></td>
146*bf2c3715SXin Li</tr>
147*bf2c3715SXin Li<tr><td>Number of non \n zero coefficients</td><td>\code
148*bf2c3715SXin Limat.nonZeros() \endcode</td>
149*bf2c3715SXin Li<td>\code
150*bf2c3715SXin Livec.nonZeros() \endcode</td></tr>
151*bf2c3715SXin Li</table>
152*bf2c3715SXin Li
153*bf2c3715SXin Li
154*bf2c3715SXin Li\b Iterating \b over \b the \b nonzero \b coefficients \n
155*bf2c3715SXin Li
156*bf2c3715SXin LiRandom access to the elements of a sparse object can be done through the \c coeffRef(i,j) function.
157*bf2c3715SXin LiHowever, this function involves a quite expensive binary search.
158*bf2c3715SXin LiIn most cases, one only wants to iterate over the non-zeros elements. This is achieved by a standard loop over the outer dimension, and then by iterating over the non-zeros of the current inner vector via an InnerIterator. Thus, the non-zero entries have to be visited in the same order than the storage order.
159*bf2c3715SXin LiHere is an example:
160*bf2c3715SXin Li<table class="manual">
161*bf2c3715SXin Li<tr><td>
162*bf2c3715SXin Li\code
163*bf2c3715SXin LiSparseMatrix<double> mat(rows,cols);
164*bf2c3715SXin Lifor (int k=0; k<mat.outerSize(); ++k)
165*bf2c3715SXin Li  for (SparseMatrix<double>::InnerIterator it(mat,k); it; ++it)
166*bf2c3715SXin Li  {
167*bf2c3715SXin Li    it.value();
168*bf2c3715SXin Li    it.row();   // row index
169*bf2c3715SXin Li    it.col();   // col index (here it is equal to k)
170*bf2c3715SXin Li    it.index(); // inner index, here it is equal to it.row()
171*bf2c3715SXin Li  }
172*bf2c3715SXin Li\endcode
173*bf2c3715SXin Li</td><td>
174*bf2c3715SXin Li\code
175*bf2c3715SXin LiSparseVector<double> vec(size);
176*bf2c3715SXin Lifor (SparseVector<double>::InnerIterator it(vec); it; ++it)
177*bf2c3715SXin Li{
178*bf2c3715SXin Li  it.value(); // == vec[ it.index() ]
179*bf2c3715SXin Li  it.index();
180*bf2c3715SXin Li}
181*bf2c3715SXin Li\endcode
182*bf2c3715SXin Li</td></tr>
183*bf2c3715SXin Li</table>
184*bf2c3715SXin LiFor a writable expression, the referenced value can be modified using the valueRef() function.
185*bf2c3715SXin LiIf the type of the sparse matrix or vector depends on a template parameter, then the \c typename keyword is
186*bf2c3715SXin Lirequired to indicate that \c InnerIterator denotes a type; see \ref TopicTemplateKeyword for details.
187*bf2c3715SXin Li
188*bf2c3715SXin Li
189*bf2c3715SXin Li\section TutorialSparseFilling Filling a sparse matrix
190*bf2c3715SXin Li
191*bf2c3715SXin LiBecause of the special storage scheme of a SparseMatrix, special care has to be taken when adding new nonzero entries.
192*bf2c3715SXin LiFor instance, the cost of a single purely random insertion into a SparseMatrix is \c O(nnz), where \c nnz is the current number of non-zero coefficients.
193*bf2c3715SXin Li
194*bf2c3715SXin LiThe simplest way to create a sparse matrix while guaranteeing good performance is thus to first build a list of so-called \em triplets, and then convert it to a SparseMatrix.
195*bf2c3715SXin Li
196*bf2c3715SXin LiHere is a typical usage example:
197*bf2c3715SXin Li\code
198*bf2c3715SXin Litypedef Eigen::Triplet<double> T;
199*bf2c3715SXin Listd::vector<T> tripletList;
200*bf2c3715SXin LitripletList.reserve(estimation_of_entries);
201*bf2c3715SXin Lifor(...)
202*bf2c3715SXin Li{
203*bf2c3715SXin Li  // ...
204*bf2c3715SXin Li  tripletList.push_back(T(i,j,v_ij));
205*bf2c3715SXin Li}
206*bf2c3715SXin LiSparseMatrixType mat(rows,cols);
207*bf2c3715SXin Limat.setFromTriplets(tripletList.begin(), tripletList.end());
208*bf2c3715SXin Li// mat is ready to go!
209*bf2c3715SXin Li\endcode
210*bf2c3715SXin LiThe \c std::vector of triplets might contain the elements in arbitrary order, and might even contain duplicated elements that will be summed up by setFromTriplets().
211*bf2c3715SXin LiSee the SparseMatrix::setFromTriplets() function and class Triplet for more details.
212*bf2c3715SXin Li
213*bf2c3715SXin Li
214*bf2c3715SXin LiIn some cases, however, slightly higher performance, and lower memory consumption can be reached by directly inserting the non-zeros into the destination matrix.
215*bf2c3715SXin LiA typical scenario of this approach is illustrated below:
216*bf2c3715SXin Li\code
217*bf2c3715SXin Li1: SparseMatrix<double> mat(rows,cols);         // default is column major
218*bf2c3715SXin Li2: mat.reserve(VectorXi::Constant(cols,6));
219*bf2c3715SXin Li3: for each i,j such that v_ij != 0
220*bf2c3715SXin Li4:   mat.insert(i,j) = v_ij;                    // alternative: mat.coeffRef(i,j) += v_ij;
221*bf2c3715SXin Li5: mat.makeCompressed();                        // optional
222*bf2c3715SXin Li\endcode
223*bf2c3715SXin Li
224*bf2c3715SXin Li- 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-zeros per column or row can easily be 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 providing a vector object with an operator[](int j) returning the reserve size of the \c j-th inner vector (e.g., via a VectorXi or std::vector<int>). 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. If this line is omitted, then the first insertion of a new element will reserve room for 2 elements per inner vector.
225*bf2c3715SXin Li- The line 4 performs a sorted insertion. In this example, the ideal case is when the \c j-th column is not full and contains non-zeros whose inner-indices are smaller than \c i. In this case, this operation boils down to trivial O(1) operation.
226*bf2c3715SXin Li- When calling insert(i,j) the element \c i \c ,j must not already exists, otherwise use the coeffRef(i,j) method that will allow to, e.g., accumulate values. This method first performs a binary search and finally calls insert(i,j) if the element does not already exist. It is more flexible than insert() but also more costly.
227*bf2c3715SXin Li- The line 5 suppresses the remaining empty space and transforms the matrix into a compressed column storage.
228*bf2c3715SXin Li
229*bf2c3715SXin Li
230*bf2c3715SXin Li
231*bf2c3715SXin Li\section TutorialSparseFeatureSet Supported operators and functions
232*bf2c3715SXin Li
233*bf2c3715SXin LiBecause of their special storage format, sparse matrices cannot offer the same level of flexibility than dense matrices.
234*bf2c3715SXin LiIn Eigen's sparse module we chose to expose only the subset of the dense matrix API which can be efficiently implemented.
235*bf2c3715SXin LiIn the following \em sm denotes a sparse matrix, \em sv a sparse vector, \em dm a dense matrix, and \em dv a dense vector.
236*bf2c3715SXin Li
237*bf2c3715SXin Li\subsection TutorialSparse_BasicOps Basic operations
238*bf2c3715SXin Li
239*bf2c3715SXin Li%Sparse expressions support most of the unary and binary coefficient wise operations:
240*bf2c3715SXin Li\code
241*bf2c3715SXin Lism1.real()   sm1.imag()   -sm1                    0.5*sm1
242*bf2c3715SXin Lism1+sm2      sm1-sm2      sm1.cwiseProduct(sm2)
243*bf2c3715SXin Li\endcode
244*bf2c3715SXin LiHowever, <strong>a strong restriction is that the storage orders must match</strong>. For instance, in the following example:
245*bf2c3715SXin Li\code
246*bf2c3715SXin Lism4 = sm1 + sm2 + sm3;
247*bf2c3715SXin Li\endcode
248*bf2c3715SXin Lism1, sm2, and sm3 must all be row-major or all column-major.
249*bf2c3715SXin LiOn the other hand, there is no restriction on the target matrix sm4.
250*bf2c3715SXin LiFor 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:
251*bf2c3715SXin Li\code
252*bf2c3715SXin LiSparseMatrix<double> A, B;
253*bf2c3715SXin LiB = SparseMatrix<double>(A.transpose()) + A;
254*bf2c3715SXin Li\endcode
255*bf2c3715SXin Li
256*bf2c3715SXin LiBinary coefficient wise operators can also mix sparse and dense expressions:
257*bf2c3715SXin Li\code
258*bf2c3715SXin Lism2 = sm1.cwiseProduct(dm1);
259*bf2c3715SXin Lidm2 = sm1 + dm1;
260*bf2c3715SXin Lidm2 = dm1 - sm1;
261*bf2c3715SXin Li\endcode
262*bf2c3715SXin LiPerformance-wise, the adding/subtracting sparse and dense matrices is better performed in two steps. For instance, instead of doing <tt>dm2 = sm1 + dm1</tt>, better write:
263*bf2c3715SXin Li\code
264*bf2c3715SXin Lidm2 = dm1;
265*bf2c3715SXin Lidm2 += sm1;
266*bf2c3715SXin Li\endcode
267*bf2c3715SXin LiThis version has the advantage to fully exploit the higher performance of dense storage (no indirection, SIMD, etc.), and to pay the cost of slow sparse evaluation on the few non-zeros of the sparse matrix only.
268*bf2c3715SXin Li
269*bf2c3715SXin Li
270*bf2c3715SXin Li%Sparse expressions also support transposition:
271*bf2c3715SXin Li\code
272*bf2c3715SXin Lism1 = sm2.transpose();
273*bf2c3715SXin Lism1 = sm2.adjoint();
274*bf2c3715SXin Li\endcode
275*bf2c3715SXin LiHowever, there is no transposeInPlace() method.
276*bf2c3715SXin Li
277*bf2c3715SXin Li
278*bf2c3715SXin Li\subsection TutorialSparse_Products Matrix products
279*bf2c3715SXin Li
280*bf2c3715SXin Li%Eigen supports various kind of sparse matrix products which are summarize below:
281*bf2c3715SXin Li  - \b sparse-dense:
282*bf2c3715SXin Li    \code
283*bf2c3715SXin Lidv2 = sm1 * dv1;
284*bf2c3715SXin Lidm2 = dm1 * sm1.adjoint();
285*bf2c3715SXin Lidm2 = 2. * sm1 * dm1;
286*bf2c3715SXin Li    \endcode
287*bf2c3715SXin Li  - \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():
288*bf2c3715SXin Li    \code
289*bf2c3715SXin Lidm2 = sm1.selfadjointView<>() * dm1;        // if all coefficients of A are stored
290*bf2c3715SXin Lidm2 = A.selfadjointView<Upper>() * dm1;     // if only the upper part of A is stored
291*bf2c3715SXin Lidm2 = A.selfadjointView<Lower>() * dm1;     // if only the lower part of A is stored
292*bf2c3715SXin Li    \endcode
293*bf2c3715SXin Li  - \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:
294*bf2c3715SXin Li    \code
295*bf2c3715SXin Lism3 = sm1 * sm2;
296*bf2c3715SXin Lism3 = 4 * sm1.adjoint() * sm2;
297*bf2c3715SXin Li    \endcode
298*bf2c3715SXin Li    The second algorithm prunes on the fly the explicit zeros, or the values smaller than a given threshold. It is enabled and controlled through the prune() functions:
299*bf2c3715SXin Li    \code
300*bf2c3715SXin Lism3 = (sm1 * sm2).pruned();                  // removes numerical zeros
301*bf2c3715SXin Lism3 = (sm1 * sm2).pruned(ref);               // removes elements much smaller than ref
302*bf2c3715SXin Lism3 = (sm1 * sm2).pruned(ref,epsilon);       // removes elements smaller than ref*epsilon
303*bf2c3715SXin Li    \endcode
304*bf2c3715SXin Li
305*bf2c3715SXin Li  - \b permutations. Finally, permutations can be applied to sparse matrices too:
306*bf2c3715SXin Li    \code
307*bf2c3715SXin LiPermutationMatrix<Dynamic,Dynamic> P = ...;
308*bf2c3715SXin Lism2 = P * sm1;
309*bf2c3715SXin Lism2 = sm1 * P.inverse();
310*bf2c3715SXin Lism2 = sm1.transpose() * P;
311*bf2c3715SXin Li    \endcode
312*bf2c3715SXin Li
313*bf2c3715SXin Li
314*bf2c3715SXin Li\subsection TutorialSparse_SubMatrices Block operations
315*bf2c3715SXin Li
316*bf2c3715SXin LiRegarding read-access, sparse matrices expose the same API than for dense matrices to access to sub-matrices such as blocks, columns, and rows. See \ref TutorialBlockOperations for a detailed introduction.
317*bf2c3715SXin LiHowever, for performance reasons, writing to a sub-sparse-matrix is much more limited, and currently only contiguous sets of columns (resp. rows) of a column-major (resp. row-major) SparseMatrix are writable. Moreover, this information has to be known at compile-time, leaving out methods such as <tt>block(...)</tt> and <tt>corner*(...)</tt>. The available API for write-access to a SparseMatrix are summarized below:
318*bf2c3715SXin Li\code
319*bf2c3715SXin LiSparseMatrix<double,ColMajor> sm1;
320*bf2c3715SXin Lism1.col(j) = ...;
321*bf2c3715SXin Lism1.leftCols(ncols) = ...;
322*bf2c3715SXin Lism1.middleCols(j,ncols) = ...;
323*bf2c3715SXin Lism1.rightCols(ncols) = ...;
324*bf2c3715SXin Li
325*bf2c3715SXin LiSparseMatrix<double,RowMajor> sm2;
326*bf2c3715SXin Lism2.row(i) = ...;
327*bf2c3715SXin Lism2.topRows(nrows) = ...;
328*bf2c3715SXin Lism2.middleRows(i,nrows) = ...;
329*bf2c3715SXin Lism2.bottomRows(nrows) = ...;
330*bf2c3715SXin Li\endcode
331*bf2c3715SXin Li
332*bf2c3715SXin LiIn addition, sparse matrices expose the SparseMatrixBase::innerVector() and SparseMatrixBase::innerVectors() methods, which are aliases to the col/middleCols methods for a column-major storage, and to the row/middleRows methods for a row-major storage.
333*bf2c3715SXin Li
334*bf2c3715SXin Li\subsection TutorialSparse_TriangularSelfadjoint Triangular and selfadjoint views
335*bf2c3715SXin Li
336*bf2c3715SXin LiJust as with dense matrices, the triangularView() function can be used to address a triangular part of the matrix, and perform triangular solves with a dense right hand side:
337*bf2c3715SXin Li\code
338*bf2c3715SXin Lidm2 = sm1.triangularView<Lower>(dm1);
339*bf2c3715SXin Lidv2 = sm1.transpose().triangularView<Upper>(dv1);
340*bf2c3715SXin Li\endcode
341*bf2c3715SXin Li
342*bf2c3715SXin LiThe selfadjointView() function permits various operations:
343*bf2c3715SXin Li - optimized sparse-dense matrix products:
344*bf2c3715SXin Li    \code
345*bf2c3715SXin Lidm2 = sm1.selfadjointView<>() * dm1;        // if all coefficients of A are stored
346*bf2c3715SXin Lidm2 = A.selfadjointView<Upper>() * dm1;     // if only the upper part of A is stored
347*bf2c3715SXin Lidm2 = A.selfadjointView<Lower>() * dm1;     // if only the lower part of A is stored
348*bf2c3715SXin Li    \endcode
349*bf2c3715SXin Li - copy of triangular parts:
350*bf2c3715SXin Li    \code
351*bf2c3715SXin Lism2 = sm1.selfadjointView<Upper>();                               // makes a full selfadjoint matrix from the upper triangular part
352*bf2c3715SXin Lism2.selfadjointView<Lower>() = sm1.selfadjointView<Upper>();      // copies the upper triangular part to the lower triangular part
353*bf2c3715SXin Li    \endcode
354*bf2c3715SXin Li - application of symmetric permutations:
355*bf2c3715SXin Li \code
356*bf2c3715SXin LiPermutationMatrix<Dynamic,Dynamic> P = ...;
357*bf2c3715SXin Lism2 = A.selfadjointView<Upper>().twistedBy(P);                                // compute P S P' from the upper triangular part of A, and make it a full matrix
358*bf2c3715SXin Lism2.selfadjointView<Lower>() = A.selfadjointView<Lower>().twistedBy(P);       // compute P S P' from the lower triangular part of A, and then only compute the lower part
359*bf2c3715SXin Li \endcode
360*bf2c3715SXin Li
361*bf2c3715SXin LiPlease, refer to the \link SparseQuickRefPage Quick Reference \endlink  guide for the list of supported operations. The list of linear solvers available is \link TopicSparseSystems here. \endlink
362*bf2c3715SXin Li
363*bf2c3715SXin Li*/
364*bf2c3715SXin Li
365*bf2c3715SXin Li}
366