Table of contents
This part of the tutorial focuses on solving linear problem of the form , where both and are known, and is the unknown. Moreover, assumed to be a squared matrix. Of course, the methods described here can be used whenever an expression involve the product of an inverse matrix with a vector or another matrix: . Eigen offers various algorithms to this problem, and its choice mainly depends on the nature of the matrix , such as its shape, size and numerical properties.
If the matrix is triangular (upper or lower) and invertible (the coefficients of the diagonal are all not zero), then the problem can be solved directly using MatrixBase::solveTriangular(), or better, MatrixBase::solveTriangularInPlace(). Here is an example:
cout << "Here is the matrix m:" << endl << m << endl;
n.part<Eigen::LowerTriangular>() *= 2;
cout << "Here is the matrix n:" << endl << n << endl;
cout << "And now here is m.inverse()*n, taking advantage of the fact that"
" m is upper-triangular:" << endl
Here is the matrix m:
1 1 1
0 1 1
0 0 1
Here is the matrix n:
2 1 1
2 2 1
2 2 2
And now here is m.inverse()*n, taking advantage of the fact that m is upper-triangular:
0 -1 0
0 0 -1
2 2 2
See MatrixBase::solveTriangular() for more details.
If the matrix is small ( ) and invertible, then a good approach is to directly compute the inverse of the matrix , and then obtain the solution by . With Eigen, this can be implemented like this:
If the matrix is positive definite, then the best method is to use a Cholesky decomposition. Such positive definite matrices often arise when solving overdetermined problems in a least square sense (see below). Eigen offers two different Cholesky decompositions: a decomposition where L is a lower triangular matrix, and a decomposition where L is lower triangular with unit diagonal and D is a diagonal matrix. The latter avoids square roots and is therefore slightly more stable than the former one.
when the value of the right hand side is not needed anymore, then it is faster to use the in place API, e.g.:
In that case the value of is replaced by the result .
If the linear problem has to solved for various right hand side , but same matrix , then it is highly recommended to perform the decomposition of $ A $ only once, e.g.:
If the matrix does not fit in any of the previous categories, or if you are unsure about the numerical stability of your problem, then you can use the LU solver based on a decomposition of the same name : see the section LU below. Actually, Eigen's LU module does not implement a standard LU decomposition, but rather a so-called LU decomposition with full pivoting and rank update which has much better numerical stability. The API of the LU solver is the same than the Cholesky one, except that there is no in place variant:
Again, the LU decomposition can be stored to be reused or to perform other kernel operations:
See the section LU below.
Eigen provides a rank-revealing LU decomposition with full pivoting, which has very good numerical stability.
Alternatively, you can construct a named LU decomposition, which allows you to reuse it for more than one operation: