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.

249 lines
7.2 KiB

  1. namespace Eigen {
  2. /** \eigenManualPage SparseQuickRefPage Quick reference guide for sparse matrices
  3. \eigenAutoToc
  4. <hr>
  5. 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 :
  6. 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.
  7. \section SparseMatrixInit Sparse Matrix Initialization
  8. <table class="manual">
  9. <tr><th> Category </th> <th> Operations</th> <th>Notes</th></tr>
  10. <tr><td>Constructor</td>
  11. <td>
  12. \code
  13. SparseMatrix<double> sm1(1000,1000);
  14. SparseMatrix<std::complex<double>,RowMajor> sm2;
  15. \endcode
  16. </td> <td> Default is ColMajor</td> </tr>
  17. <tr class="alt">
  18. <td> Resize/Reserve</td>
  19. <td>
  20. \code
  21. sm1.resize(m,n); //Change sm1 to a m x n matrix.
  22. sm1.reserve(nnz); // Allocate room for nnz nonzeros elements.
  23. \endcode
  24. </td>
  25. <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>
  26. </tr>
  27. <tr>
  28. <td> Assignment </td>
  29. <td>
  30. \code
  31. SparseMatrix<double,Colmajor> sm1;
  32. // Initialize sm2 with sm1.
  33. SparseMatrix<double,Rowmajor> sm2(sm1), sm3;
  34. // Assignment and evaluations modify the storage order.
  35. sm3 = sm1;
  36. \endcode
  37. </td>
  38. <td> The copy constructor can be used to convert from a storage order to another</td>
  39. </tr>
  40. <tr class="alt">
  41. <td> Element-wise Insertion</td>
  42. <td>
  43. \code
  44. // Insert a new element;
  45. sm1.insert(i, j) = v_ij;
  46. // Update the value v_ij
  47. sm1.coeffRef(i,j) = v_ij;
  48. sm1.coeffRef(i,j) += v_ij;
  49. sm1.coeffRef(i,j) -= v_ij;
  50. \endcode
  51. </td>
  52. <td> insert() assumes that the element does not already exist; otherwise, use coeffRef()</td>
  53. </tr>
  54. <tr>
  55. <td> Batch insertion</td>
  56. <td>
  57. \code
  58. std::vector< Eigen::Triplet<double> > tripletList;
  59. tripletList.reserve(estimation_of_entries);
  60. // -- Fill tripletList with nonzero elements...
  61. sm1.setFromTriplets(TripletList.begin(), TripletList.end());
  62. \endcode
  63. </td>
  64. <td>A complete example is available at \link TutorialSparseFilling Triplet Insertion \endlink.</td>
  65. </tr>
  66. <tr class="alt">
  67. <td> Constant or Random Insertion</td>
  68. <td>
  69. \code
  70. sm1.setZero(); // Set the matrix with zero elements
  71. sm1.setConstant(val); //Replace all the nonzero values with val
  72. \endcode
  73. </td>
  74. <td> The matrix sm1 should have been created before ???</td>
  75. </tr>
  76. </table>
  77. \section SparseBasicInfos Matrix properties
  78. Beyond the basic functions rows() and cols(), there are some useful functions that are available to easily get some informations from the matrix.
  79. <table class="manual">
  80. <tr>
  81. <td> \code
  82. sm1.rows(); // Number of rows
  83. sm1.cols(); // Number of columns
  84. sm1.nonZeros(); // Number of non zero values
  85. sm1.outerSize(); // Number of columns (resp. rows) for a column major (resp. row major )
  86. sm1.innerSize(); // Number of rows (resp. columns) for a row major (resp. column major)
  87. sm1.norm(); // Euclidian norm of the matrix
  88. sm1.squaredNorm(); // Squared norm of the matrix
  89. sm1.blueNorm();
  90. sm1.isVector(); // Check if sm1 is a sparse vector or a sparse matrix
  91. sm1.isCompressed(); // Check if sm1 is in compressed form
  92. ...
  93. \endcode </td>
  94. </tr>
  95. </table>
  96. \section SparseBasicOps Arithmetic operations
  97. 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.
  98. <table class="manual">
  99. <tr><th> Operations </th> <th> Code </th> <th> Notes </th></tr>
  100. <tr>
  101. <td> add subtract </td>
  102. <td> \code
  103. sm3 = sm1 + sm2;
  104. sm3 = sm1 - sm2;
  105. sm2 += sm1;
  106. sm2 -= sm1; \endcode
  107. </td>
  108. <td>
  109. sm1 and sm2 should have the same storage order
  110. </td>
  111. </tr>
  112. <tr class="alt"><td>
  113. scalar product</td><td>\code
  114. sm3 = sm1 * s1; sm3 *= s1;
  115. sm3 = s1 * sm1 + s2 * sm2; sm3 /= s1;\endcode
  116. </td>
  117. <td>
  118. Many combinations are possible if the dimensions and the storage order agree.
  119. </tr>
  120. <tr>
  121. <td> %Sparse %Product </td>
  122. <td> \code
  123. sm3 = sm1 * sm2;
  124. dm2 = sm1 * dm1;
  125. dv2 = sm1 * dv1;
  126. \endcode </td>
  127. <td>
  128. </td>
  129. </tr>
  130. <tr class='alt'>
  131. <td> transposition, adjoint</td>
  132. <td> \code
  133. sm2 = sm1.transpose();
  134. sm2 = sm1.adjoint();
  135. \endcode </td>
  136. <td>
  137. Note that the transposition change the storage order. There is no support for transposeInPlace().
  138. </td>
  139. </tr>
  140. <tr>
  141. <td> Permutation </td>
  142. <td>
  143. \code
  144. perm.indices(); // Reference to the vector of indices
  145. sm1.twistedBy(perm); // Permute rows and columns
  146. sm2 = sm1 * perm; //Permute the columns
  147. sm2 = perm * sm1; // Permute the columns
  148. \endcode
  149. </td>
  150. <td>
  151. </td>
  152. </tr>
  153. <tr>
  154. <td>
  155. Component-wise ops
  156. </td>
  157. <td>\code
  158. sm1.cwiseProduct(sm2);
  159. sm1.cwiseQuotient(sm2);
  160. sm1.cwiseMin(sm2);
  161. sm1.cwiseMax(sm2);
  162. sm1.cwiseAbs();
  163. sm1.cwiseSqrt();
  164. \endcode</td>
  165. <td>
  166. sm1 and sm2 should have the same storage order
  167. </td>
  168. </tr>
  169. </table>
  170. \section sparseotherops Other supported operations
  171. <table class="manual">
  172. <tr><th>Operations</th> <th> Code </th> <th> Notes</th> </tr>
  173. <tr>
  174. <td>Sub-matrices</td>
  175. <td>
  176. \code
  177. sm1.block(startRow, startCol, rows, cols);
  178. sm1.block(startRow, startCol);
  179. sm1.topLeftCorner(rows, cols);
  180. sm1.topRightCorner(rows, cols);
  181. sm1.bottomLeftCorner( rows, cols);
  182. sm1.bottomRightCorner( rows, cols);
  183. \endcode
  184. </td> <td> </td>
  185. </tr>
  186. <tr>
  187. <td> Range </td>
  188. <td>
  189. \code
  190. sm1.innerVector(outer);
  191. sm1.innerVectors(start, size);
  192. sm1.leftCols(size);
  193. sm2.rightCols(size);
  194. sm1.middleRows(start, numRows);
  195. sm1.middleCols(start, numCols);
  196. sm1.col(j);
  197. \endcode
  198. </td>
  199. <td>A inner vector is either a row (for row-major) or a column (for column-major). As stated earlier, the evaluation can be done in a matrix with different storage order </td>
  200. </tr>
  201. <tr>
  202. <td> Triangular and selfadjoint views</td>
  203. <td>
  204. \code
  205. sm2 = sm1.triangularview<Lower>();
  206. sm2 = sm1.selfadjointview<Lower>();
  207. \endcode
  208. </td>
  209. <td> Several combination between triangular views and blocks views are possible
  210. \code
  211. \endcode </td>
  212. </tr>
  213. <tr>
  214. <td>Triangular solve </td>
  215. <td>
  216. \code
  217. dv2 = sm1.triangularView<Upper>().solve(dv1);
  218. dv2 = sm1.topLeftCorner(size, size).triangularView<Lower>().solve(dv1);
  219. \endcode
  220. </td>
  221. <td> For general sparse solve, Use any suitable module described at \ref TopicSparseSystems </td>
  222. </tr>
  223. <tr>
  224. <td> Low-level API</td>
  225. <td>
  226. \code
  227. sm1.valuePtr(); // Pointer to the values
  228. sm1.innerIndextr(); // Pointer to the indices.
  229. sm1.outerIndexPtr(); //Pointer to the beginning of each inner vector
  230. \endcode
  231. </td>
  232. <td> If the matrix is not in compressed form, makeCompressed() should be called before. Note that these functions are mostly provided for interoperability purposes with external libraries. 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>
  233. </tr>
  234. </table>
  235. */
  236. }