1 // A simple quickref for Eigen. Add anything that's missing. 2 // Main author: Keir Mierle 3 4 #include <Eigen/Dense> 5 6 Matrix<double, 3, 3> A; // Fixed rows and cols. Same as Matrix3d. 7 Matrix<double, 3, Dynamic> B; // Fixed rows, dynamic cols. 8 Matrix<double, Dynamic, Dynamic> C; // Full dynamic. Same as MatrixXd. 9 Matrix<double, 3, 3, RowMajor> E; // Row major; default is column-major. 10 Matrix3f P, Q, R; // 3x3 float matrix. 11 Vector3f x, y, z; // 3x1 float matrix. 12 RowVector3f a, b, c; // 1x3 float matrix. 13 VectorXd v; // Dynamic column vector of doubles 14 double s; 15 16 // Basic usage 17 // Eigen // Matlab // comments 18 x.size() // length(x) // vector size 19 C.rows() // size(C,1) // number of rows 20 C.cols() // size(C,2) // number of columns 21 x(i) // x(i+1) // Matlab is 1-based 22 C(i,j) // C(i+1,j+1) // 23 24 A.resize(4, 4); // Runtime error if assertions are on. 25 B.resize(4, 9); // Runtime error if assertions are on. 26 A.resize(3, 3); // Ok; size didn't change. 27 B.resize(3, 9); // Ok; only dynamic cols changed. 28 29 A << 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. 32 B << A, A, A; // B is three horizontally stacked A's. 33 A.fill(10); // Fill A with all 10's. 34 35 // Eigen // Matlab 36 MatrixXd::Identity(rows,cols) // eye(rows,cols) 37 C.setIdentity(rows,cols) // C = eye(rows,cols) 38 MatrixXd::Zero(rows,cols) // zeros(rows,cols) 39 C.setZero(rows,cols) // C = ones(rows,cols) 40 MatrixXd::Ones(rows,cols) // ones(rows,cols) 41 C.setOnes(rows,cols) // C = ones(rows,cols) 42 MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1 // MatrixXd::Random returns uniform random numbers in (-1, 1). 43 C.setRandom(rows,cols) // C = rand(rows,cols)*2-1 44 VectorXd::LinSpaced(size,low,high) // linspace(low,high,size)' 45 v.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 52 x.head(n) // x(1:n) 53 x.head<n>() // x(1:n) 54 x.tail(n) // x(end - n + 1: end) 55 x.tail<n>() // x(end - n + 1: end) 56 x.segment(i, n) // x(i+1 : i+n) 57 x.segment<n>(i) // x(i+1 : i+n) 58 P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols) 59 P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols) 60 P.row(i) // P(i+1, :) 61 P.col(j) // P(:, j+1) 62 P.leftCols<cols>() // P(:, 1:cols) 63 P.leftCols(cols) // P(:, 1:cols) 64 P.middleCols<cols>(j) // P(:, j+1:j+cols) 65 P.middleCols(j, cols) // P(:, j+1:j+cols) 66 P.rightCols<cols>() // P(:, end-cols+1:end) 67 P.rightCols(cols) // P(:, end-cols+1:end) 68 P.topRows<rows>() // P(1:rows, :) 69 P.topRows(rows) // P(1:rows, :) 70 P.middleRows<rows>(i) // P(:, i+1:i+rows) 71 P.middleRows(i, rows) // P(:, i+1:i+rows) 72 P.bottomRows<rows>() // P(:, end-rows+1:end) 73 P.bottomRows(rows) // P(:, end-rows+1:end) 74 P.topLeftCorner(rows, cols) // P(1:rows, 1:cols) 75 P.topRightCorner(rows, cols) // P(1:rows, end-cols+1:end) 76 P.bottomLeftCorner(rows, cols) // P(end-rows+1:end, 1:cols) 77 P.bottomRightCorner(rows, cols) // P(end-rows+1:end, end-cols+1:end) 78 P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols) 79 P.topRightCorner<rows,cols>() // P(1:rows, end-cols+1:end) 80 P.bottomLeftCorner<rows,cols>() // P(end-rows+1:end, 1:cols) 81 P.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 85 R.row(i) = P.col(j); // R(i, :) = P(:, i) 86 R.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 90 R.adjoint() // R' 91 R.transpose() // R.' or conj(R') 92 R.diagonal() // diag(R) 93 x.asDiagonal() // diag(x) 94 R.transpose().colwise().reverse(); // rot90(R) 95 R.conjugate() // conj(R) 96 97 // All the same as Matlab, but matlab doesn't have *= style operators. 98 // Matrix-vector. Matrix-matrix. Matrix-scalar. 99 y = M*x; R = P*Q; R = P*s; 100 a = b*M; R = P - Q; R = s*P; 101 a *= 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 108 R = P.cwiseProduct(Q); // R = P .* Q 109 R = P.array() * s.array();// R = P .* s 110 R = P.cwiseQuotient(Q); // R = P ./ Q 111 R = P.array() / Q.array();// R = P ./ Q 112 R = P.array() + s.array();// R = P + s 113 R = P.array() - s.array();// R = P - s 114 R.array() += s; // R = R + s 115 R.array() -= s; // R = R - s 116 R.array() < Q.array(); // R < Q 117 R.array() <= Q.array(); // R <= Q 118 R.cwiseInverse(); // 1 ./ P 119 R.array().inverse(); // 1 ./ P 120 R.array().sin() // sin(P) 121 R.array().cos() // cos(P) 122 R.array().pow(s) // P .^ s 123 R.array().square() // P .^ 2 124 R.array().cube() // P .^ 3 125 R.cwiseSqrt() // sqrt(P) 126 R.array().sqrt() // sqrt(P) 127 R.array().exp() // exp(P) 128 R.array().log() // log(P) 129 R.cwiseMax(P) // max(R, P) 130 R.array().max(P.array()) // max(R, P) 131 R.cwiseMin(P) // min(R, P) 132 R.array().min(P.array()) // min(R, P) 133 R.cwiseAbs() // abs(P) 134 R.array().abs() // abs(P) 135 R.cwiseAbs2() // abs(P.^2) 136 R.array().abs2() // abs(P.^2) 137 (R.array() < s).select(P,Q); // (R < s ? P : Q) 138 139 // Reductions. 140 int r, c; 141 // Eigen // Matlab 142 R.minCoeff() // min(R(:)) 143 R.maxCoeff() // max(R(:)) 144 s = R.minCoeff(&r, &c) // [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i); 145 s = R.maxCoeff(&r, &c) // [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i); 146 R.sum() // sum(R(:)) 147 R.colwise().sum() // sum(R) 148 R.rowwise().sum() // sum(R, 2) or sum(R')' 149 R.prod() // prod(R(:)) 150 R.colwise().prod() // prod(R) 151 R.rowwise().prod() // prod(R, 2) or prod(R')' 152 R.trace() // trace(R) 153 R.all() // all(R(:)) 154 R.colwise().all() // all(R) 155 R.rowwise().all() // all(R, 2) 156 R.any() // any(R(:)) 157 R.colwise().any() // any(R) 158 R.rowwise().any() // any(R, 2) 159 160 // Dot products, norms, etc. 161 // Eigen // Matlab 162 x.norm() // norm(x). Note that norm(R) doesn't work in Eigen. 163 x.squaredNorm() // dot(x, x) Note the equivalence is not true for complex 164 x.dot(y) // dot(x, y) 165 x.cross(y) // cross(x, y) Requires #include <Eigen/Geometry> 166 167 //// Type conversion 168 // Eigen // Matlab 169 A.cast<double>(); // double(A) 170 A.cast<float>(); // single(A) 171 A.cast<int>(); // int32(A) 172 A.real(); // real(A) 173 A.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: 177 MatrixXf F = MatrixXf::Zero(3,3); 178 A += F; // illegal in Eigen. In Matlab A = A+F is allowed 179 A += 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. 182 float array[3]; 183 Vector3f::Map(array).fill(10); // create a temporary Map over array and sets entries to 10 184 int data[4] = {1, 2, 3, 4}; 185 Matrix2i mat2x2(data); // copies data into mat2x2 186 Matrix2i::Map(data) = 2*mat2x2; // overwrite elements of data with 2*mat2x2 187 MatrixXi::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. 190 x = A.ldlt().solve(b)); // A sym. p.s.d. #include <Eigen/Cholesky> 191 x = A.llt() .solve(b)); // A sym. p.d. #include <Eigen/Cholesky> 192 x = A.lu() .solve(b)); // Stable and fast. #include <Eigen/LU> 193 x = A.qr() .solve(b)); // No pivoting. #include <Eigen/QR> 194 x = 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 203 A.eigenvalues(); // eig(A); 204 EigenSolver<Matrix3d> eig(A); // [vec val] = eig(A) 205 eig.eigenvalues(); // diag(val) 206 eig.eigenvectors(); // vec 207 // For self-adjoint matrices use SelfAdjointEigenSolver<> 208