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.

170 lines
7.4 KiB

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