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.

269 lines
10 KiB

  1. namespace Eigen {
  2. /** \page TutorialLinearAlgebra Tutorial page 6 - Linear algebra and decompositions
  3. \ingroup Tutorial
  4. \li \b Previous: \ref TutorialAdvancedInitialization
  5. \li \b Next: \ref TutorialReductionsVisitorsBroadcasting
  6. This tutorial explains how to solve linear systems, compute various decompositions such as LU,
  7. QR, %SVD, eigendecompositions... for more advanced topics, don't miss our special page on
  8. \ref TopicLinearAlgebraDecompositions "this topic".
  9. \b Table \b of \b contents
  10. - \ref TutorialLinAlgBasicSolve
  11. - \ref TutorialLinAlgSolutionExists
  12. - \ref TutorialLinAlgEigensolving
  13. - \ref TutorialLinAlgInverse
  14. - \ref TutorialLinAlgLeastsquares
  15. - \ref TutorialLinAlgSeparateComputation
  16. - \ref TutorialLinAlgRankRevealing
  17. \section TutorialLinAlgBasicSolve Basic linear solving
  18. \b The \b problem: You have a system of equations, that you have written as a single matrix equation
  19. \f[ Ax \: = \: b \f]
  20. Where \a A and \a b are matrices (\a b could be a vector, as a special case). You want to find a solution \a x.
  21. \b The \b solution: You can choose between various decompositions, depending on what your matrix \a A looks like,
  22. and depending on whether you favor speed or accuracy. However, let's start with an example that works in all cases,
  23. and is a good compromise:
  24. <table class="example">
  25. <tr><th>Example:</th><th>Output:</th></tr>
  26. <tr>
  27. <td>\include TutorialLinAlgExSolveColPivHouseholderQR.cpp </td>
  28. <td>\verbinclude TutorialLinAlgExSolveColPivHouseholderQR.out </td>
  29. </tr>
  30. </table>
  31. In this example, the colPivHouseholderQr() method returns an object of class ColPivHouseholderQR. Since here the
  32. matrix is of type Matrix3f, this line could have been replaced by:
  33. \code
  34. ColPivHouseholderQR<Matrix3f> dec(A);
  35. Vector3f x = dec.solve(b);
  36. \endcode
  37. Here, ColPivHouseholderQR is a QR decomposition with column pivoting. It's a good compromise for this tutorial, as it
  38. works for all matrices while being quite fast. Here is a table of some other decompositions that you can choose from,
  39. depending on your matrix and the trade-off you want to make:
  40. <table class="manual">
  41. <tr>
  42. <th>Decomposition</th>
  43. <th>Method</th>
  44. <th>Requirements on the matrix</th>
  45. <th>Speed</th>
  46. <th>Accuracy</th>
  47. </tr>
  48. <tr>
  49. <td>PartialPivLU</td>
  50. <td>partialPivLu()</td>
  51. <td>Invertible</td>
  52. <td>++</td>
  53. <td>+</td>
  54. </tr>
  55. <tr class="alt">
  56. <td>FullPivLU</td>
  57. <td>fullPivLu()</td>
  58. <td>None</td>
  59. <td>-</td>
  60. <td>+++</td>
  61. </tr>
  62. <tr>
  63. <td>HouseholderQR</td>
  64. <td>householderQr()</td>
  65. <td>None</td>
  66. <td>++</td>
  67. <td>+</td>
  68. </tr>
  69. <tr class="alt">
  70. <td>ColPivHouseholderQR</td>
  71. <td>colPivHouseholderQr()</td>
  72. <td>None</td>
  73. <td>+</td>
  74. <td>++</td>
  75. </tr>
  76. <tr>
  77. <td>FullPivHouseholderQR</td>
  78. <td>fullPivHouseholderQr()</td>
  79. <td>None</td>
  80. <td>-</td>
  81. <td>+++</td>
  82. </tr>
  83. <tr class="alt">
  84. <td>LLT</td>
  85. <td>llt()</td>
  86. <td>Positive definite</td>
  87. <td>+++</td>
  88. <td>+</td>
  89. </tr>
  90. <tr>
  91. <td>LDLT</td>
  92. <td>ldlt()</td>
  93. <td>Positive or negative semidefinite</td>
  94. <td>+++</td>
  95. <td>++</td>
  96. </tr>
  97. </table>
  98. All of these decompositions offer a solve() method that works as in the above example.
  99. For example, if your matrix is positive definite, the above table says that a very good
  100. choice is then the LDLT decomposition. Here's an example, also demonstrating that using a general
  101. matrix (not a vector) as right hand side is possible.
  102. <table class="example">
  103. <tr><th>Example:</th><th>Output:</th></tr>
  104. <tr>
  105. <td>\include TutorialLinAlgExSolveLDLT.cpp </td>
  106. <td>\verbinclude TutorialLinAlgExSolveLDLT.out </td>
  107. </tr>
  108. </table>
  109. For a \ref TopicLinearAlgebraDecompositions "much more complete table" comparing all decompositions supported by Eigen (notice that Eigen
  110. supports many other decompositions), see our special page on
  111. \ref TopicLinearAlgebraDecompositions "this topic".
  112. \section TutorialLinAlgSolutionExists Checking if a solution really exists
  113. Only you know what error margin you want to allow for a solution to be considered valid.
  114. So Eigen lets you do this computation for yourself, if you want to, as in this example:
  115. <table class="example">
  116. <tr><th>Example:</th><th>Output:</th></tr>
  117. <tr>
  118. <td>\include TutorialLinAlgExComputeSolveError.cpp </td>
  119. <td>\verbinclude TutorialLinAlgExComputeSolveError.out </td>
  120. </tr>
  121. </table>
  122. \section TutorialLinAlgEigensolving Computing eigenvalues and eigenvectors
  123. You need an eigendecomposition here, see available such decompositions on \ref TopicLinearAlgebraDecompositions "this page".
  124. Make sure to check if your matrix is self-adjoint, as is often the case in these problems. Here's an example using
  125. SelfAdjointEigenSolver, it could easily be adapted to general matrices using EigenSolver or ComplexEigenSolver.
  126. The computation of eigenvalues and eigenvectors does not necessarily converge, but such failure to converge is
  127. very rare. The call to info() is to check for this possibility.
  128. <table class="example">
  129. <tr><th>Example:</th><th>Output:</th></tr>
  130. <tr>
  131. <td>\include TutorialLinAlgSelfAdjointEigenSolver.cpp </td>
  132. <td>\verbinclude TutorialLinAlgSelfAdjointEigenSolver.out </td>
  133. </tr>
  134. </table>
  135. \section TutorialLinAlgInverse Computing inverse and determinant
  136. First of all, make sure that you really want this. While inverse and determinant are fundamental mathematical concepts,
  137. in \em numerical linear algebra they are not as popular as in pure mathematics. Inverse computations are often
  138. advantageously replaced by solve() operations, and the determinant is often \em not a good way of checking if a matrix
  139. is invertible.
  140. However, for \em very \em small matrices, the above is not true, and inverse and determinant can be very useful.
  141. While certain decompositions, such as PartialPivLU and FullPivLU, offer inverse() and determinant() methods, you can also
  142. call inverse() and determinant() directly on a matrix. If your matrix is of a very small fixed size (at most 4x4) this
  143. allows Eigen to avoid performing a LU decomposition, and instead use formulas that are more efficient on such small matrices.
  144. Here is an example:
  145. <table class="example">
  146. <tr><th>Example:</th><th>Output:</th></tr>
  147. <tr>
  148. <td>\include TutorialLinAlgInverseDeterminant.cpp </td>
  149. <td>\verbinclude TutorialLinAlgInverseDeterminant.out </td>
  150. </tr>
  151. </table>
  152. \section TutorialLinAlgLeastsquares Least squares solving
  153. The best way to do least squares solving is with a SVD decomposition. Eigen provides one as the JacobiSVD class, and its solve()
  154. is doing least-squares solving.
  155. Here is an example:
  156. <table class="example">
  157. <tr><th>Example:</th><th>Output:</th></tr>
  158. <tr>
  159. <td>\include TutorialLinAlgSVDSolve.cpp </td>
  160. <td>\verbinclude TutorialLinAlgSVDSolve.out </td>
  161. </tr>
  162. </table>
  163. Another way, potentially faster but less reliable, is to use a LDLT decomposition
  164. of the normal matrix. In any case, just read any reference text on least squares, and it will be very easy for you
  165. to implement any linear least squares computation on top of Eigen.
  166. \section TutorialLinAlgSeparateComputation Separating the computation from the construction
  167. In the above examples, the decomposition was computed at the same time that the decomposition object was constructed.
  168. There are however situations where you might want to separate these two things, for example if you don't know,
  169. at the time of the construction, the matrix that you will want to decompose; or if you want to reuse an existing
  170. decomposition object.
  171. What makes this possible is that:
  172. \li all decompositions have a default constructor,
  173. \li all decompositions have a compute(matrix) method that does the computation, and that may be called again
  174. on an already-computed decomposition, reinitializing it.
  175. For example:
  176. <table class="example">
  177. <tr><th>Example:</th><th>Output:</th></tr>
  178. <tr>
  179. <td>\include TutorialLinAlgComputeTwice.cpp </td>
  180. <td>\verbinclude TutorialLinAlgComputeTwice.out </td>
  181. </tr>
  182. </table>
  183. Finally, you can tell the decomposition constructor to preallocate storage for decomposing matrices of a given size,
  184. so that when you subsequently decompose such matrices, no dynamic memory allocation is performed (of course, if you
  185. are using fixed-size matrices, no dynamic memory allocation happens at all). This is done by just
  186. passing the size to the decomposition constructor, as in this example:
  187. \code
  188. HouseholderQR<MatrixXf> qr(50,50);
  189. MatrixXf A = MatrixXf::Random(50,50);
  190. qr.compute(A); // no dynamic memory allocation
  191. \endcode
  192. \section TutorialLinAlgRankRevealing Rank-revealing decompositions
  193. Certain decompositions are rank-revealing, i.e. are able to compute the rank of a matrix. These are typically
  194. also the decompositions that behave best in the face of a non-full-rank matrix (which in the square case means a
  195. singular matrix). On \ref TopicLinearAlgebraDecompositions "this table" you can see for all our decompositions
  196. whether they are rank-revealing or not.
  197. Rank-revealing decompositions offer at least a rank() method. They can also offer convenience methods such as isInvertible(),
  198. and some are also providing methods to compute the kernel (null-space) and image (column-space) of the matrix, as is the
  199. case with FullPivLU:
  200. <table class="example">
  201. <tr><th>Example:</th><th>Output:</th></tr>
  202. <tr>
  203. <td>\include TutorialLinAlgRankRevealing.cpp </td>
  204. <td>\verbinclude TutorialLinAlgRankRevealing.out </td>
  205. </tr>
  206. </table>
  207. Of course, any rank computation depends on the choice of an arbitrary threshold, since practically no
  208. floating-point matrix is \em exactly rank-deficient. Eigen picks a sensible default threshold, which depends
  209. on the decomposition but is typically the diagonal size times machine epsilon. While this is the best default we
  210. could pick, only you know what is the right threshold for your application. You can set this by calling setThreshold()
  211. on your decomposition object before calling rank() or any other method that needs to use such a threshold.
  212. The decomposition itself, i.e. the compute() method, is independent of the threshold. You don't need to recompute the
  213. decomposition after you've changed the threshold.
  214. <table class="example">
  215. <tr><th>Example:</th><th>Output:</th></tr>
  216. <tr>
  217. <td>\include TutorialLinAlgSetThreshold.cpp </td>
  218. <td>\verbinclude TutorialLinAlgSetThreshold.out </td>
  219. </tr>
  220. </table>
  221. \li \b Next: \ref TutorialReductionsVisitorsBroadcasting
  222. */
  223. }