AsciiQuickReference.txt 10 KB

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