You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
9.4 KiB

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