blob: 2b3fdc61c5c165db44e833e02f129e75e5b686b7 [file] [log] [blame]
Gael Guennebaud86a19262009-01-16 17:20:12 +00001namespace Eigen {
2
3/** \page TutorialSparse Tutorial - Getting started with the sparse module
4 \ingroup Tutorial
5
6<div class="eimainmenu">\ref index "Overview"
7 | \ref TutorialCore "Core features"
8 | \ref TutorialGeometry "Geometry"
9 | \ref TutorialAdvancedLinearAlgebra "Advanced linear algebra"
10 | \b Sparse \b matrix
11</div>
12
13\b Table \b of \b contents \n
14 - \ref TutorialSparseIntro
15 - \ref TutorialSparseFilling
16 - \ref TutorialSparseFeatureSet
17 - \ref TutorialSparseDirectSolvers
18<hr>
19
20\section TutorialSparseIntro Introduction
21
22In 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.
23
24In order to get the best of the Eigen's sparse matrix, it is important to have a rough idea of the way they are internally stored. In Eigen we chose to use the common and generic Compressed Column/Row Storage scheme. Let m be a column-major sparse matrix. Then its nonzero coefficients are sequentially stored in memory in a col-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).
25
26Here is an example, with the matrix:
27<table>
28<tr><td>0</td><td>3</td><td>0</td><td>0</td><td>0</td></tr>
29<tr><td>22</td><td>0</td><td>0</td><td>0</td><td>17</td></tr>
30<tr><td>7</td><td>5</td><td>0</td><td>1</td><td>0</td></tr>
31<tr><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
32<tr><td>0</td><td>0</td><td>14</td><td>0</td><td>8</td></tr>
33</table>
34
35and its internal representation using the Compressed Column Storage format:
36<table>
37<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>
38<tr><td>Inner indices:</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>
39</table>
40Outer indices:<table><tr><td>0</td><td>2</td><td>4</td><td>5</td><td>6</td><td>\em 7 </td></tr></table>
41
42As 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 dimension and the \em outer dimension. For instance, it is easy to loop over the coefficients of an \em inner \em vector (e.g., a column of a col-major matrix), but very inefficient to do the same for an \em outer \em vector (e.g., a row of a col-major matrix).
43
44
45\b Eigen's \b sparse \b matrix \b and \b vector \b class \n
46Before using any sparse feature you must include the Sparse header file:
47\code
48#include <Eigen/Sparse>
49\endcode
50In Eigen, sparse matrices are handled by the SparseMatrix class:
51\code
52SparseMatrix<std::complex<float> > m1(1000,2000); // declare a 1000x2000 col-major sparse matrix of complex<float>
53SparseMatrix<double,RowMajor> m2(1000,2000); // declare a 1000x2000 row-major sparse matrix of double
54\endcode
55where \c Scalar is the type of the coefficient and \c Options is a set of bit flags allowing to control the shape and storage of the matrix. A typical choice for \c Options is \c RowMajor to get a row-major matrix (the default is col-major).
56
57Unlike the dense path, the sparse module provides a special class to declare a sparse vector:
58\code
59SparseVector<std::complex<float> > v1(1000); // declare a column sparse vector of complex<float> of size 1000
60SparseVector<double,RowMajor> v2(1000); // declare a row sparse vector of double of size 1000
61\endcode
62Note that here the size of a vector denotes its dimension and not the number of nonzero coefficients which is initially zero.
63
64\b Matrix \b and \b vector \b properties \n
65
66<table>
67<tr><td>Standard \n dimensions</td><td>\code
68mat.rows()
69mat.cols()\endcode</td>
70<td>\code
71vec.size() \endcode</td>
72</tr>
73<tr><td>Sizes along the \n inner/outer dimensions</td><td>\code
74mat.innerSize()
75mat.outerSize()\endcode</td>
76<td></td>
77</tr>
78<tr><td>Number of non \n zero coefficiens</td><td>\code
79mat.nonZeros() \endcode</td>
80<td>\code
81vec.nonZeros() \endcode</td></tr>
82</table>
83
84\b Iterating \b over \b the \b nonzero \b coefficients \n
85
86Iterating over the coefficients of a sparse matrix can be done only in the same order than the storage order. Here is an example:
87<table>
88<tr><td>
89\code
90SparseMatrix<double> m1(rows,cols);
91for (int k=0; k<m1.outerSize(); ++k)
92 for (SparseMatrix<double>::InnerIterator it(m1,k); it; ++it)
93 {
94 it.value();
95 it.row(); // row index
96 it.col(); // col index (here it is equal to k)
97 it.index(); // inner index, here it is equal to it.row()
98 }
99\endcode
100</td><td>
101\code
102SparseVector<double> v1(size);
103for (SparseVector<double>::InnerIterator it(v1); it; ++it)
104{
105 it.value(); // == v1[ it.index() ]
106 it.index();
107}
108\endcode
109</td></tr>
110</table>
111
112
113\section TutorialSparseFilling Filling a sparse matrix
114
115Unlike dense matrix, filling a sparse matrix efficiently is a though problem which requires some special care from the user. Indeed, directly filling in a random order a matrix in a compressed storage format would requires a lot of searches and memory copies, and some trade of between the efficiency and the ease of use have to be found. In Eigen we currently provide three ways to set the elements of a sparse matrix:
116
1171 - 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:
118\code
119SparseMatrix<double,RowMajor> m(rows,cols);
120m.startFill(rows*cols*percent_of_non_zero); // estimate of the number of nonzeros (optional)
121for (int i=0; i\<rows; ++i)
122 for (int j=0; j\<cols; ++j)
123 if (rand()\<percent_of_non_zero)
124 m.fill(i,j) = rand();
125m.endFill();
126\endcode
127
1282 - If you can set each outer vector in a consistent order, but do not have sorted data for each inner vector, then you can use fillrand() instead of fill():
129\code
130SparseMatrix<double,RowMajor> m(rows,cols);
131m.startFill(rows*cols*percent_of_non_zero); // estimate of the number of nonzeros (optional)
132for (int i=0; i\<rows; ++i)
133 for (int k=0; k\<cols*percent_of_non_zero; ++k)
134 m.fillrand(i,rand(0,cols)) = rand();
135m.endFill();
136\endcode
137The fillrand() function performs a sorted insertion into an array sequentially stored in memory and requires a copy of all coefficients stored after its target position. This method is therefore reserved for matrices having only a few elements per row/column (up to 50) and works better if the insertion are almost sorted.
138
1393 - Eventually, if none of the above solution is practicable for you, then you have to use a RandomSetter which temporarily wraps the matrix into a more flexible hash map allowing complete random accesses:
140\code
141SparseMatrix<double,RowMajor> m(rows,cols);
142{
143 RandomSetter<SparseMatrix<double,RowMajor> > setter(m);
144 for (int k=0; k\<cols*rows*percent_of_non_zero; ++k)
145 setter(rand(0,rows), rand(0,cols)) = rand();
146}
147\endcode
148The matrix \c m is set at the destruction of the setter, hence the use of a nested block. This imposed syntax has the advantage to emphasize the critical section where m is not valid and cannot be used.
149
150
151\section TutorialSparseFeatureSet Supported operators and functions
152
153In the following \em sm denote a sparse matrix, \em sv a sparse vector, \em dm a dense matrix, and \em dv a dense vector.
154In Eigen's sparse module we chose to expose only the subset of the dense matrix API which can be efficiently implemented. Moreover, all combinations are not always possible. For instance, it is not possible to add two sparse matrices having two different storage order. On the other hand it is perfectly fine to evaluate a sparse matrix/expression to a matrix having a different storage order:
155\code
156SparseMatrixType sm1, sm2, sm3;
157sm3 = sm1.transpose() + sm2; // invalid
158sm3 = SparseMatrixType(sm1.transpose()) + sm2; // correct
159\endcode
160
161Here are some examples of the supported operations:
162\code
163s_1 *= 0.5;
164sm4 = sm1 + sm2 + sm3; // only if s_1, s_2 and s_3 have the same storage order
165sm3 = sm1 * sm2;
166dv3 = sm1 * dv2;
167dm3 = sm1 * dm2;
168dm3 = dm2 * sm1;
169sm3 = sm1.cwise() * sm2; // only if s_1 and s_2 have the same storage order
170dv2 = sm1.marked<UpperTriangular>().solveTriangular(dv2);
171\endcode
172
173The product of a sparse matrix A time a dense matrix/vector dv with A symmetric can be optimized by telling that to Eigen:
174\code
175res = A.marked<SeflAdjoint>() * dv; // if all coefficients of A are stored
176res = A.marked<SeflAdjoint|UpperTriangular>() * dv; // if only the upper part of A is stored
177res = A.marked<SeflAdjoint|LowerTriangular>() * dv; // if only the lower part of A is stored
178\endcode
179
180
181\section TutorialSparseDirectSolvers Using the direct solvers
182
183TODO
184
185\subsection TutorialSparseDirectSolvers_LLT LLT
186Cholmod, Taucs.
187
188\subsection TutorialSparseDirectSolvers_LDLT LDLT
189
190
191\subsection TutorialSparseDirectSolvers_LU LU
192SuperLU, UmfPack.
193
194*/
195
196}