1// A simple quickref for Eigen. Add anything that's missing. 2// Main author: Keir Mierle 3 4#include <Eigen/Core> 5#include <Eigen/Array> 6 7Matrix<double, 3, 3> A; // Fixed rows and cols. Same as Matrix3d. 8Matrix<double, 3, Dynamic> B; // Fixed rows, dynamic cols. 9Matrix<double, Dynamic, Dynamic> C; // Full dynamic. Same as MatrixXd. 10Matrix<double, 3, 3, RowMajor> E; // Row major; default is column-major. 11Matrix3f P, Q, R; // 3x3 float matrix. 12Vector3f x, y, z; // 3x1 float matrix. 13RowVector3f a, b, c; // 1x3 float matrix. 14double s; 15 16// Basic usage 17// Eigen // Matlab // comments 18x.size() // length(x) // vector size 19C.rows() // size(C)(1) // number of rows 20C.cols() // size(C)(2) // number of columns 21x(i) // x(i+1) // Matlab is 1-based 22C(i,j) // C(i+1,j+1) // 23 24A.resize(4, 4); // Runtime error if assertions are on. 25B.resize(4, 9); // Runtime error if assertions are on. 26A.resize(3, 3); // Ok; size didn't change. 27B.resize(3, 9); // Ok; only dynamic cols changed. 28 29A << 1, 2, 3, // Initialize A. The elements can also be 30 4, 5, 6, // matrices, which are stacked along cols 31 7, 8, 9; // and then the rows are stacked. 32B << A, A, A; // B is three horizontally stacked A's. 33A.fill(10); // Fill A with all 10's. 34A.setRandom(); // Fill A with uniform random numbers in (-1, 1). 35 // Requires #include <Eigen/Array>. 36A.setIdentity(); // Fill A with the identity. 37 38// Matrix slicing and blocks. All expressions listed here are read/write. 39// Templated size versions are faster. Note that Matlab is 1-based (a size N 40// vector is x(1)...x(N)). 41// Eigen // Matlab 42x.head(n) // x(1:n) 43x.head<n>() // x(1:n) 44x.tail(n) // N = rows(x); x(N - n: N) 45x.tail<n>() // N = rows(x); x(N - n: N) 46x.segment(i, n) // x(i+1 : i+n) 47x.segment<n>(i) // x(i+1 : i+n) 48P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols) 49P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols) 50P.topLeftCorner(rows, cols) // P(1:rows, 1:cols) 51P.topRightCorner(rows, cols) // [m n]=size(P); P(1:rows, n-cols+1:n) 52P.bottomLeftCorner(rows, cols) // [m n]=size(P); P(m-rows+1:m, 1:cols) 53P.bottomRightCorner(rows, cols) // [m n]=size(P); P(m-rows+1:m, n-cols+1:n) 54P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols) 55P.topRightCorner<rows,cols>() // [m n]=size(P); P(1:rows, n-cols+1:n) 56P.bottomLeftCorner<rows,cols>() // [m n]=size(P); P(m-rows+1:m, 1:cols) 57P.bottomRightCorner<rows,cols>() // [m n]=size(P); P(m-rows+1:m, n-cols+1:n) 58 59// Of particular note is Eigen's swap function which is highly optimized. 60// Eigen // Matlab 61R.row(i) = P.col(j); // R(i, :) = P(:, i) 62R.col(j1).swap(mat1.col(j2)); // R(:, [j1 j2]) = R(:, [j2, j1]) 63 64// Views, transpose, etc; all read-write except for .adjoint(). 65// Eigen // Matlab 66R.adjoint() // R' 67R.transpose() // R.' or conj(R') 68R.diagonal() // diag(R) 69x.asDiagonal() // diag(x) 70 71// All the same as Matlab, but matlab doesn't have *= style operators. 72// Matrix-vector. Matrix-matrix. Matrix-scalar. 73y = M*x; R = P*Q; R = P*s; 74a = b*M; R = P - Q; R = s*P; 75a *= M; R = P + Q; R = P/s; 76 R *= Q; R = s*P; 77 R += Q; R *= s; 78 R -= Q; R /= s; 79 80 // Vectorized operations on each element independently 81 // (most require #include <Eigen/Array>) 82// Eigen // Matlab 83R = P.cwiseProduct(Q); // R = P .* Q 84R = P.array() * s.array();// R = P .* s 85R = P.cwiseQuotient(Q); // R = P ./ Q 86R = P.array() / Q.array();// R = P ./ Q 87R = P.array() + s.array();// R = P + s 88R = P.array() - s.array();// R = P - s 89R.array() += s; // R = R + s 90R.array() -= s; // R = R - s 91R.array() < Q.array(); // R < Q 92R.array() <= Q.array(); // R <= Q 93R.cwiseInverse(); // 1 ./ P 94R.array().inverse(); // 1 ./ P 95R.array().sin() // sin(P) 96R.array().cos() // cos(P) 97R.array().pow(s) // P .^ s 98R.array().square() // P .^ 2 99R.array().cube() // P .^ 3 100R.cwiseSqrt() // sqrt(P) 101R.array().sqrt() // sqrt(P) 102R.array().exp() // exp(P) 103R.array().log() // log(P) 104R.cwiseMax(P) // max(R, P) 105R.array().max(P.array()) // max(R, P) 106R.cwiseMin(P) // min(R, P) 107R.array().min(P.array()) // min(R, P) 108R.cwiseAbs() // abs(P) 109R.array().abs() // abs(P) 110R.cwiseAbs2() // abs(P.^2) 111R.array().abs2() // abs(P.^2) 112(R.array() < s).select(P,Q); // (R < s ? P : Q) 113 114// Reductions. 115int r, c; 116// Eigen // Matlab 117R.minCoeff() // min(R(:)) 118R.maxCoeff() // max(R(:)) 119s = R.minCoeff(&r, &c) // [aa, bb] = min(R); [cc, dd] = min(aa); 120 // r = bb(dd); c = dd; s = cc 121s = R.maxCoeff(&r, &c) // [aa, bb] = max(R); [cc, dd] = max(aa); 122 // row = bb(dd); col = dd; s = cc 123R.sum() // sum(R(:)) 124R.colwise.sum() // sum(R) 125R.rowwise.sum() // sum(R, 2) or sum(R')' 126R.prod() // prod(R(:)) 127R.colwise.prod() // prod(R) 128R.rowwise.prod() // prod(R, 2) or prod(R')' 129R.trace() // trace(R) 130R.all() // all(R(:)) 131R.colwise().all() // all(R) 132R.rowwise().all() // all(R, 2) 133R.any() // any(R(:)) 134R.colwise().any() // any(R) 135R.rowwise().any() // any(R, 2) 136 137// Dot products, norms, etc. 138// Eigen // Matlab 139x.norm() // norm(x). Note that norm(R) doesn't work in Eigen. 140x.squaredNorm() // dot(x, x) Note the equivalence is not true for complex 141x.dot(y) // dot(x, y) 142x.cross(y) // cross(x, y) Requires #include <Eigen/Geometry> 143 144// Eigen can map existing memory into Eigen matrices. 145float array[3]; 146Map<Vector3f>(array, 3).fill(10); 147int data[4] = 1, 2, 3, 4; 148Matrix2i mat2x2(data); 149MatrixXi mat2x2 = Map<Matrix2i>(data); 150MatrixXi mat2x2 = Map<MatrixXi>(data, 2, 2); 151 152// Solve Ax = b. Result stored in x. Matlab: x = A \ b. 153bool solved; 154solved = A.ldlt().solve(b, &x)); // A sym. p.s.d. #include <Eigen/Cholesky> 155solved = A.llt() .solve(b, &x)); // A sym. p.d. #include <Eigen/Cholesky> 156solved = A.lu() .solve(b, &x)); // Stable and fast. #include <Eigen/LU> 157solved = A.qr() .solve(b, &x)); // No pivoting. #include <Eigen/QR> 158solved = A.svd() .solve(b, &x)); // Stable, slowest. #include <Eigen/SVD> 159// .ldlt() -> .matrixL() and .matrixD() 160// .llt() -> .matrixL() 161// .lu() -> .matrixL() and .matrixU() 162// .qr() -> .matrixQ() and .matrixR() 163// .svd() -> .matrixU(), .singularValues(), and .matrixV() 164 165// Eigenvalue problems 166// Eigen // Matlab 167A.eigenvalues(); // eig(A); 168EigenSolver<Matrix3d> eig(A); // [vec val] = eig(A) 169eig.eigenvalues(); // diag(val) 170eig.eigenvectors(); // vec 171