LeastSquares.dox 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. namespace Eigen {
  2. /** \eigenManualPage LeastSquares Solving linear least squares systems
  3. This page describes how to solve linear least squares systems using %Eigen. An overdetermined system
  4. of equations, say \a Ax = \a b, has no solutions. In this case, it makes sense to search for the
  5. vector \a x which is closest to being a solution, in the sense that the difference \a Ax - \a b is
  6. as small as possible. This \a x is called the least square solution (if the Euclidean norm is used).
  7. The three methods discussed on this page are the SVD decomposition, the QR decomposition and normal
  8. equations. Of these, the SVD decomposition is generally the most accurate but the slowest, normal
  9. equations is the fastest but least accurate, and the QR decomposition is in between.
  10. \eigenAutoToc
  11. \section LeastSquaresSVD Using the SVD decomposition
  12. The \link BDCSVD::solve() solve() \endlink method in the BDCSVD class can be directly used to
  13. solve linear squares systems. It is not enough to compute only the singular values (the default for
  14. this class); you also need the singular vectors but the thin SVD decomposition suffices for
  15. computing least squares solutions:
  16. <table class="example">
  17. <tr><th>Example:</th><th>Output:</th></tr>
  18. <tr>
  19. <td>\include TutorialLinAlgSVDSolve.cpp </td>
  20. <td>\verbinclude TutorialLinAlgSVDSolve.out </td>
  21. </tr>
  22. </table>
  23. This is example from the page \link TutorialLinearAlgebra Linear algebra and decompositions \endlink.
  24. \section LeastSquaresQR Using the QR decomposition
  25. The solve() method in QR decomposition classes also computes the least squares solution. There are
  26. three QR decomposition classes: HouseholderQR (no pivoting, so fast but unstable),
  27. ColPivHouseholderQR (column pivoting, thus a bit slower but more accurate) and FullPivHouseholderQR
  28. (full pivoting, so slowest and most stable). Here is an example with column pivoting:
  29. <table class="example">
  30. <tr><th>Example:</th><th>Output:</th></tr>
  31. <tr>
  32. <td>\include LeastSquaresQR.cpp </td>
  33. <td>\verbinclude LeastSquaresQR.out </td>
  34. </tr>
  35. </table>
  36. \section LeastSquaresNormalEquations Using normal equations
  37. Finding the least squares solution of \a Ax = \a b is equivalent to solving the normal equation
  38. <i>A</i><sup>T</sup><i>Ax</i> = <i>A</i><sup>T</sup><i>b</i>. This leads to the following code
  39. <table class="example">
  40. <tr><th>Example:</th><th>Output:</th></tr>
  41. <tr>
  42. <td>\include LeastSquaresNormalEquations.cpp </td>
  43. <td>\verbinclude LeastSquaresNormalEquations.out </td>
  44. </tr>
  45. </table>
  46. If the matrix \a A is ill-conditioned, then this is not a good method, because the condition number
  47. of <i>A</i><sup>T</sup><i>A</i> is the square of the condition number of \a A. This means that you
  48. lose twice as many digits using normal equation than if you use the other methods.
  49. */
  50. }