You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
8.0 KiB

  1. namespace Eigen {
  2. /** \page SparseQuickRefPage Quick reference guide for sparse matrices
  3. \b Table \b of \b contents
  4. - \ref Constructors
  5. - \ref SparseMatrixInsertion
  6. - \ref SparseBasicInfos
  7. - \ref SparseBasicOps
  8. - \ref SparseInterops
  9. - \ref sparsepermutation
  10. - \ref sparsesubmatrices
  11. - \ref sparseselfadjointview
  12. \n
  13. <hr>
  14. 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 first the introductory tutorial at \ref TutorialSparse. The important point to have in mind when working on sparse matrices is how they are stored :
  15. 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.
  16. \section Constructors Constructors and assignments
  17. 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.
  18. \code
  19. SparseMatrix<double> sm1(1000,1000); // 1000x1000 compressed sparse matrix of double.
  20. SparseMatrix<std::complex<double>,RowMajor> sm2; // Compressed row major matrix of complex double.
  21. \endcode
  22. The copy constructor and assignment can be used to convert matrices from a storage order to another
  23. \code
  24. SparseMatrix<double,Colmajor> sm1;
  25. // Eventually fill the matrix sm1 ...
  26. SparseMatrix<double,Rowmajor> sm2(sm1), sm3; // Initialize sm2 with sm1.
  27. sm3 = sm1; // Assignment and evaluations modify the storage order.
  28. \endcode
  29. \section SparseMatrixInsertion Allocating and inserting values
  30. resize() and reserve() are used to set the size and allocate space for nonzero elements
  31. \code
  32. sm1.resize(m,n); //Change sm to a mxn matrix.
  33. sm1.reserve(nnz); // Allocate room for nnz nonzeros elements.
  34. \endcode
  35. 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.
  36. Insertions of values in the sparse matrix can be done directly by looping over nonzero elements and use the insert() function
  37. \code
  38. // Direct insertion of the value v_ij;
  39. sm1.insert(i, j) = v_ij; // It is assumed that v_ij does not already exist in the matrix.
  40. \endcode
  41. After insertion, a value at (i,j) can be modified using coeffRef()
  42. \code
  43. // Update the value v_ij
  44. sm1.coeffRef(i,j) = v_ij;
  45. sm1.coeffRef(i,j) += v_ij;
  46. sm1.coeffRef(i,j) -= v_ij;
  47. ...
  48. \endcode
  49. The recommended way to insert values is to build a list of triplets (row, col, val) and then call setFromTriplets().
  50. \code
  51. sm1.setFromTriplets(TripletList.begin(), TripletList.end());
  52. \endcode
  53. A complete example is available at \ref TutorialSparseFilling.
  54. The following functions can be used to set constant or random values in the matrix.
  55. \code
  56. sm1.setZero(); // Reset the matrix with zero elements
  57. ...
  58. \endcode
  59. \section SparseBasicInfos Matrix properties
  60. Beyond the functions rows() and cols() that are used to get the number of rows and columns, there are some useful functions that are available to easily get some informations from the matrix.
  61. <table class="manual">
  62. <tr>
  63. <td> \code
  64. sm1.rows(); // Number of rows
  65. sm1.cols(); // Number of columns
  66. sm1.nonZeros(); // Number of non zero values
  67. sm1.outerSize(); // Number of columns (resp. rows) for a column major (resp. row major )
  68. sm1.innerSize(); // Number of rows (resp. columns) for a row major (resp. column major)
  69. sm1.norm(); // (Euclidian ??) norm of the matrix
  70. sm1.squaredNorm(); //
  71. sm1.isVector(); // Check if sm1 is a sparse vector or a sparse matrix
  72. ...
  73. \endcode </td>
  74. </tr>
  75. </table>
  76. \section SparseBasicOps Arithmetic operations
  77. 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.
  78. <table class="manual">
  79. <tr><th> Operations </th> <th> Code </th> <th> Notes </th></tr>
  80. <tr>
  81. <td> add subtract </td>
  82. <td> \code
  83. sm3 = sm1 + sm2;
  84. sm3 = sm1 - sm2;
  85. sm2 += sm1;
  86. sm2 -= sm1; \endcode
  87. </td>
  88. <td>
  89. sm1 and sm2 should have the same storage order
  90. </td>
  91. </tr>
  92. <tr class="alt"><td>
  93. scalar product</td><td>\code
  94. sm3 = sm1 * s1; sm3 *= s1;
  95. sm3 = s1 * sm1 + s2 * sm2; sm3 /= s1;\endcode
  96. </td>
  97. <td>
  98. Many combinations are possible if the dimensions and the storage order agree.
  99. </tr>
  100. <tr>
  101. <td> Product </td>
  102. <td> \code
  103. sm3 = sm1 * sm2;
  104. dm2 = sm1 * dm1;
  105. dv2 = sm1 * dv1;
  106. \endcode </td>
  107. <td>
  108. </td>
  109. </tr>
  110. <tr class='alt'>
  111. <td> transposition, adjoint</td>
  112. <td> \code
  113. sm2 = sm1.transpose();
  114. sm2 = sm1.adjoint();
  115. \endcode </td>
  116. <td>
  117. Note that the transposition change the storage order. There is no support for transposeInPlace().
  118. </td>
  119. </tr>
  120. <tr>
  121. <td>
  122. Component-wise ops
  123. </td>
  124. <td>\code
  125. sm1.cwiseProduct(sm2);
  126. sm1.cwiseQuotient(sm2);
  127. sm1.cwiseMin(sm2);
  128. sm1.cwiseMax(sm2);
  129. sm1.cwiseAbs();
  130. sm1.cwiseSqrt();
  131. \endcode</td>
  132. <td>
  133. sm1 and sm2 should have the same storage order
  134. </td>
  135. </tr>
  136. </table>
  137. \section SparseInterops Low-level storage
  138. There are a set of low-levels functions to get the standard compressed storage pointers. The matrix should be in compressed mode which can be checked by calling isCompressed(); makeCompressed() should do the job otherwise.
  139. \code
  140. // Scalar pointer to the values of the matrix, size nnz
  141. sm1.valuePtr();
  142. // Index pointer to get the row indices (resp. column indices) for column major (resp. row major) matrix, size nnz
  143. sm1.innerIndexPtr();
  144. // Index pointer to the beginning of each row (resp. column) in valuePtr() and innerIndexPtr() for column major (row major). The size is outersize()+1;
  145. sm1.outerIndexPtr();
  146. \endcode
  147. These pointers can therefore be easily used to send the matrix to some external libraries/solvers that are not yet supported by Eigen.
  148. \section sparsepermutation Permutations, submatrices and Selfadjoint Views
  149. In many cases, it is necessary to reorder the rows and/or the columns of the sparse matrix for several purposes : fill-in reducing during matrix decomposition, better data locality for sparse matrix-vector products... The class PermutationMatrix is available to this end.
  150. \code
  151. PermutationMatrix<Dynamic, Dynamic, int> perm;
  152. // Reserve and fill the values of perm;
  153. perm.inverse(n); // Compute eventually the inverse permutation
  154. sm1.twistedBy(perm) //Apply the permutation on rows and columns
  155. sm2 = sm1 * perm; // ??? Apply the permutation on columns ???;
  156. sm2 = perm * sm1; // ??? Apply the permutation on rows ???;
  157. \endcode
  158. \section sparsesubmatrices Sub-matrices
  159. The following functions are useful to extract a block of rows (resp. columns) from a row-major (resp. column major) sparse matrix. Note that because of the particular storage, it is not ?? efficient ?? to extract a submatrix comprising a certain number of subrows and subcolumns.
  160. \code
  161. sm1.innerVector(outer); // Returns the outer -th column (resp. row) of the matrix if sm is col-major (resp. row-major)
  162. sm1.innerVectors(outer); // Returns the outer -th column (resp. row) of the matrix if mat is col-major (resp. row-major)
  163. sm1.middleRows(start, numRows); // For row major matrices, get a range of numRows rows
  164. sm1.middleCols(start, numCols); // For column major matrices, get a range of numCols cols
  165. \endcode
  166. Examples :
  167. \section sparseselfadjointview Sparse triangular and selfadjoint Views
  168. \code
  169. sm2 = sm1.triangularview<Lower>(); // Get the lower triangular part of the matrix.
  170. dv2 = sm1.triangularView<Upper>().solve(dv1); // Solve the linear system with the uppper triangular part.
  171. sm2 = sm1.selfadjointview<Lower>(); // Build a selfadjoint matrix from the lower part of sm1.
  172. \endcode
  173. */
  174. }