1// A simple quickref for Eigen. Add anything that's missing. 2// Main author: Keir Mierle 3 4#include <Eigen/Dense> 5 6Matrix<double, 3, 3> A; // Fixed rows and cols. Same as Matrix3d. 7Matrix<double, 3, Dynamic> B; // Fixed rows, dynamic cols. 8Matrix<double, Dynamic, Dynamic> C; // Full dynamic. Same as MatrixXd. 9Matrix<double, 3, 3, RowMajor> E; // Row major; default is column-major. 10Matrix3f P, Q, R; // 3x3 float matrix. 11Vector3f x, y, z; // 3x1 float matrix. 12RowVector3f a, b, c; // 1x3 float matrix. 13VectorXd v; // Dynamic column vector of doubles 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. 34 35// Eigen // Matlab 36MatrixXd::Identity(rows,cols) // eye(rows,cols) 37C.setIdentity(rows,cols) // C = eye(rows,cols) 38MatrixXd::Zero(rows,cols) // zeros(rows,cols) 39C.setZero(rows,cols) // C = ones(rows,cols) 40MatrixXd::Ones(rows,cols) // ones(rows,cols) 41C.setOnes(rows,cols) // C = ones(rows,cols) 42MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1 // MatrixXd::Random returns uniform random numbers in (-1, 1). 43C.setRandom(rows,cols) // C = rand(rows,cols)*2-1 44VectorXd::LinSpaced(size,low,high) // linspace(low,high,size)' 45v.setLinSpaced(size,low,high) // v = linspace(low,high,size)' 46 47 48// Matrix slicing and blocks. All expressions listed here are read/write. 49// Templated size versions are faster. Note that Matlab is 1-based (a size N 50// vector is x(1)...x(N)). 51// Eigen // Matlab 52x.head(n) // x(1:n) 53x.head<n>() // x(1:n) 54x.tail(n) // x(end - n + 1: end) 55x.tail<n>() // x(end - n + 1: end) 56x.segment(i, n) // x(i+1 : i+n) 57x.segment<n>(i) // x(i+1 : i+n) 58P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols) 59P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols) 60P.row(i) // P(i+1, :) 61P.col(j) // P(:, j+1) 62P.leftCols<cols>() // P(:, 1:cols) 63P.leftCols(cols) // P(:, 1:cols) 64P.middleCols<cols>(j) // P(:, j+1:j+cols) 65P.middleCols(j, cols) // P(:, j+1:j+cols) 66P.rightCols<cols>() // P(:, end-cols+1:end) 67P.rightCols(cols) // P(:, end-cols+1:end) 68P.topRows<rows>() // P(1:rows, :) 69P.topRows(rows) // P(1:rows, :) 70P.middleRows<rows>(i) // P(i+1:i+rows, :) 71P.middleRows(i, rows) // P(i+1:i+rows, :) 72P.bottomRows<rows>() // P(end-rows+1:end, :) 73P.bottomRows(rows) // P(end-rows+1:end, :) 74P.topLeftCorner(rows, cols) // P(1:rows, 1:cols) 75P.topRightCorner(rows, cols) // P(1:rows, end-cols+1:end) 76P.bottomLeftCorner(rows, cols) // P(end-rows+1:end, 1:cols) 77P.bottomRightCorner(rows, cols) // P(end-rows+1:end, end-cols+1:end) 78P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols) 79P.topRightCorner<rows,cols>() // P(1:rows, end-cols+1:end) 80P.bottomLeftCorner<rows,cols>() // P(end-rows+1:end, 1:cols) 81P.bottomRightCorner<rows,cols>() // P(end-rows+1:end, end-cols+1:end) 82 83// Of particular note is Eigen's swap function which is highly optimized. 84// Eigen // Matlab 85R.row(i) = P.col(j); // R(i, :) = P(:, i) 86R.col(j1).swap(mat1.col(j2)); // R(:, [j1 j2]) = R(:, [j2, j1]) 87 88// Views, transpose, etc; all read-write except for .adjoint(). 89// Eigen // Matlab 90R.adjoint() // R' 91R.transpose() // R.' or conj(R') 92R.diagonal() // diag(R) 93x.asDiagonal() // diag(x) 94R.transpose().colwise().reverse(); // rot90(R) 95R.conjugate() // conj(R) 96 97// All the same as Matlab, but matlab doesn't have *= style operators. 98// Matrix-vector. Matrix-matrix. Matrix-scalar. 99y = M*x; R = P*Q; R = P*s; 100a = b*M; R = P - Q; R = s*P; 101a *= M; R = P + Q; R = P/s; 102 R *= Q; R = s*P; 103 R += Q; R *= s; 104 R -= Q; R /= s; 105 106// Vectorized operations on each element independently 107// Eigen // Matlab 108R = P.cwiseProduct(Q); // R = P .* Q 109R = P.array() * s.array();// R = P .* s 110R = P.cwiseQuotient(Q); // R = P ./ Q 111R = P.array() / Q.array();// R = P ./ Q 112R = P.array() + s.array();// R = P + s 113R = P.array() - s.array();// R = P - s 114R.array() += s; // R = R + s 115R.array() -= s; // R = R - s 116R.array() < Q.array(); // R < Q 117R.array() <= Q.array(); // R <= Q 118R.cwiseInverse(); // 1 ./ P 119R.array().inverse(); // 1 ./ P 120R.array().sin() // sin(P) 121R.array().cos() // cos(P) 122R.array().pow(s) // P .^ s 123R.array().square() // P .^ 2 124R.array().cube() // P .^ 3 125R.cwiseSqrt() // sqrt(P) 126R.array().sqrt() // sqrt(P) 127R.array().exp() // exp(P) 128R.array().log() // log(P) 129R.cwiseMax(P) // max(R, P) 130R.array().max(P.array()) // max(R, P) 131R.cwiseMin(P) // min(R, P) 132R.array().min(P.array()) // min(R, P) 133R.cwiseAbs() // abs(P) 134R.array().abs() // abs(P) 135R.cwiseAbs2() // abs(P.^2) 136R.array().abs2() // abs(P.^2) 137(R.array() < s).select(P,Q); // (R < s ? P : Q) 138 139// Reductions. 140int r, c; 141// Eigen // Matlab 142R.minCoeff() // min(R(:)) 143R.maxCoeff() // max(R(:)) 144s = R.minCoeff(&r, &c) // [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i); 145s = R.maxCoeff(&r, &c) // [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i); 146R.sum() // sum(R(:)) 147R.colwise().sum() // sum(R) 148R.rowwise().sum() // sum(R, 2) or sum(R')' 149R.prod() // prod(R(:)) 150R.colwise().prod() // prod(R) 151R.rowwise().prod() // prod(R, 2) or prod(R')' 152R.trace() // trace(R) 153R.all() // all(R(:)) 154R.colwise().all() // all(R) 155R.rowwise().all() // all(R, 2) 156R.any() // any(R(:)) 157R.colwise().any() // any(R) 158R.rowwise().any() // any(R, 2) 159 160// Dot products, norms, etc. 161// Eigen // Matlab 162x.norm() // norm(x). Note that norm(R) doesn't work in Eigen. 163x.squaredNorm() // dot(x, x) Note the equivalence is not true for complex 164x.dot(y) // dot(x, y) 165x.cross(y) // cross(x, y) Requires #include <Eigen/Geometry> 166 167//// Type conversion 168// Eigen // Matlab 169A.cast<double>(); // double(A) 170A.cast<float>(); // single(A) 171A.cast<int>(); // int32(A) 172A.real(); // real(A) 173A.imag(); // imag(A) 174// if the original type equals destination type, no work is done 175 176// Note that for most operations Eigen requires all operands to have the same type: 177MatrixXf F = MatrixXf::Zero(3,3); 178A += F; // illegal in Eigen. In Matlab A = A+F is allowed 179A += F.cast<double>(); // F converted to double and then added (generally, conversion happens on-the-fly) 180 181// Eigen can map existing memory into Eigen matrices. 182float array[3]; 183Vector3f::Map(array).fill(10); // create a temporary Map over array and sets entries to 10 184int data[4] = {1, 2, 3, 4}; 185Matrix2i mat2x2(data); // copies data into mat2x2 186Matrix2i::Map(data) = 2*mat2x2; // overwrite elements of data with 2*mat2x2 187MatrixXi::Map(data, 2, 2) += mat2x2; // adds mat2x2 to elements of data (alternative syntax if size is not know at compile time) 188 189// Solve Ax = b. Result stored in x. Matlab: x = A \ b. 190x = A.ldlt().solve(b)); // A sym. p.s.d. #include <Eigen/Cholesky> 191x = A.llt() .solve(b)); // A sym. p.d. #include <Eigen/Cholesky> 192x = A.lu() .solve(b)); // Stable and fast. #include <Eigen/LU> 193x = A.qr() .solve(b)); // No pivoting. #include <Eigen/QR> 194x = A.svd() .solve(b)); // Stable, slowest. #include <Eigen/SVD> 195// .ldlt() -> .matrixL() and .matrixD() 196// .llt() -> .matrixL() 197// .lu() -> .matrixL() and .matrixU() 198// .qr() -> .matrixQ() and .matrixR() 199// .svd() -> .matrixU(), .singularValues(), and .matrixV() 200 201// Eigenvalue problems 202// Eigen // Matlab 203A.eigenvalues(); // eig(A); 204EigenSolver<Matrix3d> eig(A); // [vec val] = eig(A) 205eig.eigenvalues(); // diag(val) 206eig.eigenvectors(); // vec 207// For self-adjoint matrices use SelfAdjointEigenSolver<> 208