2 /** \eigenManualPage SparseQuickRefPage Quick reference guide for sparse matrices
7 In this page, we give a quick summary of the main operations available for sparse matrices in the class SparseMatrix. First, it is recommended to read the introductory tutorial at \ref TutorialSparse. The important point to have in mind when working on sparse matrices is how they are stored :
8 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.
10 \section SparseMatrixInit Sparse Matrix Initialization
11 <table class="manual">
12 <tr><th> Category </th> <th> Operations</th> <th>Notes</th></tr>
13 <tr><td>Constructor</td>
16 SparseMatrix<double> sm1(1000,1000);
17 SparseMatrix<std::complex<double>,RowMajor> sm2;
19 </td> <td> Default is ColMajor</td> </tr>
21 <td> Resize/Reserve</td>
24 sm1.resize(m,n); // Change sm1 to a m x n matrix.
25 sm1.reserve(nnz); // Allocate room for nnz nonzeros elements.
28 <td> Note that when calling reserve(), it is not required that nnz is the exact number of nonzero elements in the final matrix. However, an exact estimation will avoid multiple reallocations during the insertion phase. </td>
34 SparseMatrix<double,Colmajor> sm1;
35 // Initialize sm2 with sm1.
36 SparseMatrix<double,Rowmajor> sm2(sm1), sm3;
37 // Assignment and evaluations modify the storage order.
41 <td> The copy constructor can be used to convert from a storage order to another</td>
44 <td> Element-wise Insertion</td>
47 // Insert a new element;
48 sm1.insert(i, j) = v_ij;
50 // Update the value v_ij
51 sm1.coeffRef(i,j) = v_ij;
52 sm1.coeffRef(i,j) += v_ij;
53 sm1.coeffRef(i,j) -= v_ij;
56 <td> insert() assumes that the element does not already exist; otherwise, use coeffRef()</td>
59 <td> Batch insertion</td>
62 std::vector< Eigen::Triplet<double> > tripletList;
63 tripletList.reserve(estimation_of_entries);
64 // -- Fill tripletList with nonzero elements...
65 sm1.setFromTriplets(TripletList.begin(), TripletList.end());
68 <td>A complete example is available at \link TutorialSparseFilling Triplet Insertion \endlink.</td>
71 <td> Constant or Random Insertion</td>
77 <td>Remove all non-zero coefficients</td>
82 \section SparseBasicInfos Matrix properties
83 Beyond the basic functions rows() and cols(), there are some useful functions that are available to easily get some informations from the matrix.
84 <table class="manual">
87 sm1.rows(); // Number of rows
88 sm1.cols(); // Number of columns
89 sm1.nonZeros(); // Number of non zero values
90 sm1.outerSize(); // Number of columns (resp. rows) for a column major (resp. row major )
91 sm1.innerSize(); // Number of rows (resp. columns) for a row major (resp. column major)
92 sm1.norm(); // Euclidian norm of the matrix
93 sm1.squaredNorm(); // Squared norm of the matrix
95 sm1.isVector(); // Check if sm1 is a sparse vector or a sparse matrix
96 sm1.isCompressed(); // Check if sm1 is in compressed form
102 \section SparseBasicOps Arithmetic operations
103 It is easy to perform arithmetic operations on sparse matrices provided that the dimensions are adequate and that the matrices have the same storage order. Note that the evaluation can always be done in a matrix with a different storage order. In the following, \b sm denotes a sparse matrix, \b dm a dense matrix and \b dv a dense vector.
104 <table class="manual">
105 <tr><th> Operations </th> <th> Code </th> <th> Notes </th></tr>
108 <td> add subtract </td>
116 sm1 and sm2 should have the same storage order
121 scalar product</td><td>\code
122 sm3 = sm1 * s1; sm3 *= s1;
123 sm3 = s1 * sm1 + s2 * sm2; sm3 /= s1;\endcode
126 Many combinations are possible if the dimensions and the storage order agree.
130 <td> %Sparse %Product </td>
141 <td> transposition, adjoint</td>
143 sm2 = sm1.transpose();
147 Note that the transposition change the storage order. There is no support for transposeInPlace().
151 <td> Permutation </td>
154 perm.indices(); // Reference to the vector of indices
155 sm1.twistedBy(perm); // Permute rows and columns
156 sm2 = sm1 * perm; // Permute the columns
157 sm2 = perm * sm1; // Permute the columns
169 sm1.cwiseProduct(sm2);
170 sm1.cwiseQuotient(sm2);
177 sm1 and sm2 should have the same storage order
182 \section sparseotherops Other supported operations
183 <table class="manual">
184 <tr><th style="min-width:initial"> Code </th> <th> Notes</th> </tr>
185 <tr><td colspan="2">Sub-matrices</td></tr>
189 sm1.block(startRow, startCol, rows, cols);
190 sm1.block(startRow, startCol);
191 sm1.topLeftCorner(rows, cols);
192 sm1.topRightCorner(rows, cols);
193 sm1.bottomLeftCorner( rows, cols);
194 sm1.bottomRightCorner( rows, cols);
197 Contrary to dense matrices, here <strong>all these methods are read-only</strong>.\n
198 See \ref TutorialSparse_SubMatrices and below for read-write sub-matrices.
201 <tr class="alt"><td colspan="2"> Range </td></tr>
205 sm1.innerVector(outer); // RW
206 sm1.innerVectors(start, size); // RW
207 sm1.leftCols(size); // RW
208 sm2.rightCols(size); // RO because sm2 is row-major
209 sm1.middleRows(start, numRows); // RO because sm1 is column-major
210 sm1.middleCols(start, numCols); // RW
215 A inner vector is either a row (for row-major) or a column (for column-major).\n
216 As stated earlier, for a read-write sub-matrix (RW), the evaluation can be done in a matrix with different storage order.
219 <tr><td colspan="2"> Triangular and selfadjoint views</td></tr>
223 sm2 = sm1.triangularview<Lower>();
224 sm2 = sm1.selfadjointview<Lower>();
227 <td> Several combination between triangular views and blocks views are possible
231 <tr class="alt"><td colspan="2">Triangular solve </td></tr>
235 dv2 = sm1.triangularView<Upper>().solve(dv1);
236 dv2 = sm1.topLeftCorner(size, size)
237 .triangularView<Lower>().solve(dv1);
240 <td> For general sparse solve, Use any suitable module described at \ref TopicSparseSystems </td>
242 <tr><td colspan="2"> Low-level API</td></tr>
246 sm1.valuePtr(); // Pointer to the values
247 sm1.innerIndextr(); // Pointer to the indices.
248 sm1.outerIndexPtr(); // Pointer to the beginning of each inner vector
252 If the matrix is not in compressed form, makeCompressed() should be called before.\n
253 Note that these functions are mostly provided for interoperability purposes with external libraries.\n
254 A better access to the values of the matrix is done by using the InnerIterator class as described in \link TutorialSparse the Tutorial Sparse \endlink section</td>
256 <tr class="alt"><td colspan="2">Mapping external buffers</td></tr>
260 int outerIndexPtr[cols+1];
261 int innerIndices[nnz];
263 Map<SparseMatrix<double> > sm1(rows,cols,nnz,outerIndexPtr, // read-write
264 innerIndices,values);
265 Map<const SparseMatrix<double> > sm2(...); // read-only
268 <td>As for dense matrices, class Map<SparseMatrixType> can be used to see external buffers as an %Eigen's SparseMatrix object. </td>