Table of contents
Matrix3d m = Matrix3d::Zero(); m.part<Eigen::UpperTriangular>().setOnes(); cout << "Here is the matrix m:" << endl << m << endl; Matrix3d n = Matrix3d::Ones(); 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 << m.marked<Eigen::UpperTriangular>().solveTriangular(n); | output: 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.
#include <Eigen/LU> Matrix4f A = Matrix4f::Random(); Vector4f b = Vector4f::Random(); Vector4f x = A.inverse() * b;
Note that the function inverse() is defined in the LU module. See MatrixBase::inverse() for more details.
#include <Eigen/Cholesky> MatrixXf D = MatrixXf::Random(8,4); MatrixXf A = D.transpose() * D; VectorXf b = D.transpose() * VectorXf::Random(4); VectorXf x; A.llt().solve(b,&x); // using a LLT factorization A.ldlt().solve(b,&x); // using a LDLT factorization
A.llt().solveInPlace(b);
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.:
// ... LLT<MatrixXf> lltOfA(A); lltOfA.solveInPlace(b0); lltOfA.solveInPlace(b1); // ...
#include <Eigen/LU> MatrixXf A = MatrixXf::Random(20,20); VectorXf b = VectorXf::Random(20); VectorXf x; A.lu().solve(b, &x);
Again, the LU decomposition can be stored to be reused or to perform other kernel operations:
// ... LU<MatrixXf> luOfA(A); luOfA.solve(b, &x); // ...
See the section LU below.
#include <Eigen/SVD> MatrixXf A = MatrixXf::Random(20,20); VectorXf b = VectorXf::Random(20); VectorXf x; A.svd().solve(b, &x); SVD<MatrixXf> svdOfA(A); svdOfA.solve(b, &x);
You can obtain the LU decomposition of a matrix by calling lu() , which is the easiest way if you're going to use the LU decomposition only once, as in
#include <Eigen/LU> MatrixXf A = MatrixXf::Random(20,20); VectorXf b = VectorXf::Random(20); VectorXf x; A.lu().solve(b, &x);
Alternatively, you can construct a named LU decomposition, which allows you to reuse it for more than one operation:
#include <Eigen/LU> MatrixXf A = MatrixXf::Random(20,20); Eigen::LUDecomposition<MatrixXf> lu(A); cout << "The rank of A is" << lu.rank() << endl; if(lu.isInvertible()) { cout << "A is invertible, its inverse is:" << endl << lu.inverse() << endl; } else { cout << "Here's a matrix whose columns form a basis of the kernel a.k.a. nullspace of A:" << endl << lu.kernel() << endl; }
top