Eigen
Install
wget https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.bz2
Build
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
target_link_libraries (example Eigen3::Eigen)
Use
Matrix storage order is column-major.
- Import
#include <Eigen/Dense>
- Static size matrix
Matrix3f matrix;
// equivalent to:
Matrix <float 3, 3> matrix;
Vector3f vector;
// equivalent to:
Matrix <float 3, 1> vector;
RowVector2i row_vector;
Matrix <int 1, 2> vector;
- Dynamic size matrix
Matrix <float, Dynamic, Dynamic> matrix;
// or short:
MatrixXf matrix;
// <rows, cols>
MatrixXd matrix(10, 10)
- Initialize matrix
MatrixXd matrix;
matrix = MatrixXf::Zero(2, 2);
matrix = MatrixXf::Ones(2, 2);
matrix = MatrixXf::Constant(2, 2, 1.343); // <row, col, value>
matrix = MatrixXf::Identity(2, 2);
matrix = MatrixXf::Random(2, 2);
matrix.setZero(2, 2);
matrix.setOnes(2, 2);
matrix.setConstant(2, 2, 1,343);
matrix.setIdentity(2, 2);
Vector3f vector {{1, 2, 3}};
MatrixXd diagonalMatrix;
diagonalMatrix = vector.asDiagonal();
- Value Assignment
// <row, col>
matrix(0, 0) = 1;
matrix(0, 1) = 2;
matrix(1, 0) = 3;
matrix(1, 1) = 4;
// or
matrix << 1, 2,
3, 4;
// or
MatrixXi matrix { // construct a 2x2 matrix
{1, 2}, // first row
{3, 4} // second row
};
- Value Access
// <start row index, start col index, height, width
matrix.block(1, 1, 2, 2);
matrix.row(0);
matrix.col(0);
matrix.topRows(1);
matrix.bottomRows(1);
matrix.leftCols(1);
matrix.rightCols(1);
- Basic Operations
Matrix3d matrix, matrixA, matrixB;
// Elementwise add
matrixA + matrixB;
// Matrix multiplication
matrixA * matrixB;
// Coefficient wise multiplication
matrixA.cwiseProduct(matrixB);
matrix.transpose();
matrix.transposeInPlace();
matrix.inverse();
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
mat.sum();
mat.prod();
mat.mean();
mat.minCoeff();
mat.maxCoeff();
mat.trace();
- Sizing
MatrixXf matrix(10, 10);
matrix.rows();
matrix.cols();
matrix.size();
matrix.resize(20, 20);