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