Eigen
Header-only linear algebra with expression templates that eliminate temporaries.
What it is
Eigen is a high-performance C++ template library for linear algebra, including matrices, vectors, numerical solvers, and related algorithms. It is widely used in scientific computing, machine learning, robotics, and computer graphics.
Eigen allows defining fixed-size and dynamic-size matrices and vectors, performing arithmetic, solving linear systems, computing eigenvalues, performing decompositions (LU, QR, SVD), and supporting advanced operations like tensor computations.
- Licence
- MPL 2.0
- Best known for
- Expression templates — `a + b + c` compiles to one loop with no temporaries
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Matrix and vector mathematics in C++ — robotics, graphics, physics, optimisation
- You want BLAS-level performance without linking a Fortran library
Look elsewhere when
- You need GPU acceleration or automatic differentiation
Installation
sudo apt install libeigen3-devGetting started
The smallest useful thing you can do with it, and what each part means.
#include <Eigen/Dense>
#include <iostream>
int main() {
Eigen::Matrix3d mat;
mat << 1, 2, 3,
4, 5, 6,
7, 8, 9;
Eigen::Vector3d vec(1, 2, 3);
std::cout << mat << std::endl;
std::cout << vec << std::endl;
return 0;
}Eigen::Matrix2d A;
A << 1, 2, 3, 4;
Eigen::Matrix2d B;
B << 5, 6, 7, 8;
Eigen::Matrix2d C = A + B;
std::cout << C << std::endl;Advanced usage
Where the library earns its place over a simpler alternative.
Eigen::Matrix2d A;
A << 3, 1, 1, 2;
Eigen::Vector2d b(9, 8);
Eigen::Vector2d x = A.colPivHouseholderQr().solve(b);
std::cout << x << std::endl;Eigen::Matrix2d A;
A << 1, 2, 2, 3;
Eigen::EigenSolver<Eigen::Matrix2d> solver(A);
std::cout << 'Eigenvalues: ' << solver.eigenvalues() << std::endl;
std::cout << 'Eigenvectors: ' << solver.eigenvectors() << std::endl;Eigen::Matrix3d A;
A << 1, 2, 3, 0, 1, 4, 5, 6, 0;
Eigen::FullPivLU<Eigen::Matrix3d> lu(A);
std::cout << 'Rank: ' << lu.rank() << std::endl;Eigen::MatrixXd mat(4,4);
mat.setRandom();
std::cout << mat << std::endl;Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Assertion failed
- Occurs when matrix dimensions are incompatible. Check that operations are dimensionally correct.
- Eigen decomposition fails
- Ensure matrices are square when required and check that numerical stability conditions are met.
Best practices
- Use fixed-size matrices for small, performance-critical computations.
- Use `.noalias()` when performing chained operations to avoid unnecessary temporaries.
- Leverage built-in decompositions (LU, QR, SVD) instead of implementing your own.
- Use Eigen’s expression templates for efficient vectorized operations.
- Include only necessary headers (Dense, Sparse, LU, etc.) to reduce compilation times.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Eigen was created by Gael Guennebaud and Benoit Jacob to provide a fast, flexible, and easy-to-use library for linear algebra in C++. Its template-based design allows for efficient computations at compile-time and runtime. Eigen is highly optimized, supports arbitrary-sized matrices, and integrates seamlessly with other C++ libraries.
