Eigen 使用参考

from:http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt

Eigen 矩阵定义

 

  1. #include <Eigen/Dense>  
  2.   
  3. Matrix<double, 3, 3> A;               // Fixed rows and cols. Same as Matrix3d.  
  4. Matrix<double, 3, Dynamic> B;         // Fixed rows, dynamic cols.  
  5. Matrix<double, Dynamic, Dynamic> C;   // Full dynamic. Same as MatrixXd.  
  6. Matrix<double, 3, 3, RowMajor> E;     // Row major; default is column-major.  
  7. Matrix3f P, Q, R;                     // 3x3 float matrix.  
  8. Vector3f x, y, z;                     // 3x1 float matrix.  
  9. RowVector3f a, b, c;                  // 1x3 float matrix.  
  10. VectorXd v;                           // Dynamic column vector of doubles  
  11. double s;                              

Eigen 基础使用

 

  1. // Basic usage  
  2. // Eigen          // Matlab           // comments  
  3. x.size()          // length(x)        // vector size  
  4. C.rows()          // size(C,1)        // number of rows  
  5. C.cols()          // size(C,2)        // number of columns  
  6. x(i)              // x(i+1)           // Matlab is 1-based  
  7. C(i,j)            // C(i+1,j+1)       //  
  8.   
  9. A.resize(4, 4);   // Runtime error if assertions are on.  
  10. B.resize(4, 9);   // Runtime error if assertions are on.  
  11. A.resize(3, 3);   // Ok; size didn't change.  
  12. B.resize(3, 9);   // Ok; only dynamic cols changed.  
  13.                     
  14. A << 1, 2, 3,     // Initialize A. The elements can also be  
  15.      4, 5, 6,     // matrices, which are stacked along cols  
  16.      7, 8, 9;     // and then the rows are stacked.  
  17. B << A, A, A;     // B is three horizontally stacked A's.  
  18. A.fill(10);       // Fill A with all 10's.  

Eigen 特殊矩阵生成

 

  1. // Eigen                            // Matlab  
  2. MatrixXd::Identity(rows,cols)       // eye(rows,cols)  
  3. C.setIdentity(rows,cols)            // C = eye(rows,cols)  
  4. MatrixXd::Zero(rows,cols)           // zeros(rows,cols)  
  5. C.setZero(rows,cols)                // C = ones(rows,cols)  
  6. MatrixXd::Ones(rows,cols)           // ones(rows,cols)  
  7. C.setOnes(rows,cols)                // C = ones(rows,cols)  
  8. MatrixXd::Random(rows,cols)         // rand(rows,cols)*2-1        // MatrixXd::Random returns uniform random numbers in (-1, 1).  
  9. C.setRandom(rows,cols)              // C = rand(rows,cols)*2-1  
  10. VectorXd::LinSpaced(size,low,high)   // linspace(low,high,size)'  
  11. v.setLinSpaced(size,low,high)        // v = linspace(low,high,size)'  

Eigen 矩阵分块

 

  1. // Matrix slicing and blocks. All expressions listed here are read/write.  
  2. // Templated size versions are faster. Note that Matlab is 1-based (a size N  
  3. // vector is x(1)...x(N)).  
  4. // Eigen                           // Matlab  
  5. x.head(n)                          // x(1:n)  
  6. x.head<n>()                        // x(1:n)  
  7. x.tail(n)                          // x(end - n + 1: end)  
  8. x.tail<n>()                        // x(end - n + 1: end)  
  9. x.segment(i, n)                    // x(i+1 : i+n)  
  10. x.segment<n>(i)                    // x(i+1 : i+n)  
  11. P.block(i, j, rows, cols)          // P(i+1 : i+rows, j+1 : j+cols)  
  12. P.block<rows, cols>(i, j)          // P(i+1 : i+rows, j+1 : j+cols)  
  13. P.row(i)                           // P(i+1, :)  
  14. P.col(j)                           // P(:, j+1)  
  15. P.leftCols<cols>()                 // P(:, 1:cols)  
  16. P.leftCols(cols)                   // P(:, 1:cols)  
  17. P.middleCols<cols>(j)              // P(:, j+1:j+cols)  
  18. P.middleCols(j, cols)              // P(:, j+1:j+cols)  
  19. P.rightCols<cols>()                // P(:, end-cols+1:end)  
  20. P.rightCols(cols)                  // P(:, end-cols+1:end)  
  21. P.topRows<rows>()                  // P(1:rows, :)  
  22. P.topRows(rows)                    // P(1:rows, :)  
  23. P.middleRows<rows>(i)              // P(i+1:i+rows, :)  
  24. P.middleRows(i, rows)              // P(i+1:i+rows, :)  
  25. P.bottomRows<rows>()               // P(end-rows+1:end, :)  
  26. P.bottomRows(rows)                 // P(end-rows+1:end, :)  
  27. P.topLeftCorner(rows, cols)        // P(1:rows, 1:cols)  
  28. P.topRightCorner(rows, cols)       // P(1:rows, end-cols+1:end)  
  29. P.bottomLeftCorner(rows, cols)     // P(end-rows+1:end, 1:cols)  
  30. P.bottomRightCorner(rows, cols)    // P(end-rows+1:end, end-cols+1:end)  
  31. P.topLeftCorner<rows,cols>()       // P(1:rows, 1:cols)  
  32. P.topRightCorner<rows,cols>()      // P(1:rows, end-cols+1:end)  
  33. P.bottomLeftCorner<rows,cols>()    // P(end-rows+1:end, 1:cols)  
  34. P.bottomRightCorner<rows,cols>()   // P(end-rows+1:end, end-cols+1:end)  

Eigen 矩阵元素交换

 

  1. // Of particular note is Eigen's swap function which is highly optimized.  
  2. // Eigen                           // Matlab  
  3. R.row(i) = P.col(j);               // R(i, :) = P(:, i)  
  4. R.col(j1).swap(mat1.col(j2));      // R(:, [j1 j2]) = R(:, [j2, j1])  

Eigen 矩阵转置

 

  1. // Views, transpose, etc; all read-write except for .adjoint().  
  2. // Eigen                           // Matlab  
  3. R.adjoint()                        // R'  
  4. R.transpose()                      // R.' or conj(R')  
  5. R.diagonal()                       // diag(R)  
  6. x.asDiagonal()                     // diag(x)  
  7. R.transpose().colwise().reverse(); // rot90(R)  
  8. R.conjugate()                      // conj(R)  

Eigen 矩阵乘积

 

  1. // All the same as Matlab, but matlab doesn't have *= style operators.  
  2. // Matrix-vector.  Matrix-matrix.   Matrix-scalar.  
  3. y  = M*x;          R  = P*Q;        R  = P*s;  
  4. a  = b*M;          R  = P - Q;      R  = s*P;  
  5. a *= M;            R  = P + Q;      R  = P/s;  
  6.                    R *= Q;          R  = s*P;  
  7.                    R += Q;          R *= s;  
  8.                    R -= Q;          R /= s;  

Eigen 矩阵单个元素操作

 

  1. // Vectorized operations on each element independently  
  2. // Eigen                  // Matlab  
  3. R = P.cwiseProduct(Q);    // R = P .* Q  
  4. R = P.array() * s.array();// R = P .* s  
  5. R = P.cwiseQuotient(Q);   // R = P ./ Q  
  6. R = P.array() / Q.array();// R = P ./ Q  
  7. R = P.array() + s.array();// R = P + s  
  8. R = P.array() - s.array();// R = P - s  
  9. R.array() += s;           // R = R + s  
  10. R.array() -= s;           // R = R - s  
  11. R.array() < Q.array();    // R < Q  
  12. R.array() <= Q.array();   // R <= Q  
  13. R.cwiseInverse();         // 1 ./ P  
  14. R.array().inverse();      // 1 ./ P  
  15. R.array().sin()           // sin(P)  
  16. R.array().cos()           // cos(P)  
  17. R.array().pow(s)          // P .^ s  
  18. R.array().square()        // P .^ 2  
  19. R.array().cube()          // P .^ 3  
  20. R.cwiseSqrt()             // sqrt(P)  
  21. R.array().sqrt()          // sqrt(P)  
  22. R.array().exp()           // exp(P)  
  23. R.array().log()           // log(P)  
  24. R.cwiseMax(P)             // max(R, P)  
  25. R.array().max(P.array())  // max(R, P)  
  26. R.cwiseMin(P)             // min(R, P)  
  27. R.array().min(P.array())  // min(R, P)  
  28. R.cwiseAbs()              // abs(P)  
  29. R.array().abs()           // abs(P)  
  30. R.cwiseAbs2()             // abs(P.^2)  
  31. R.array().abs2()          // abs(P.^2)  
  32. (R.array() < s).select(P,Q);  // (R < s ? P : Q)  

Eigen 矩阵化简

 

  1. // Reductions.  
  2. int r, c;  
  3. // Eigen                  // Matlab  
  4. R.minCoeff()              // min(R(:))  
  5. R.maxCoeff()              // max(R(:))  
  6. s = R.minCoeff(&r, &c)    // [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i);  
  7. s = R.maxCoeff(&r, &c)    // [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i);  
  8. R.sum()                   // sum(R(:))  
  9. R.colwise().sum()         // sum(R)  
  10. R.rowwise().sum()         // sum(R, 2) or sum(R')'  
  11. R.prod()                  // prod(R(:))  
  12. R.colwise().prod()        // prod(R)  
  13. R.rowwise().prod()        // prod(R, 2) or prod(R')'  
  14. R.trace()                 // trace(R)  
  15. R.all()                   // all(R(:))  
  16. R.colwise().all()         // all(R)  
  17. R.rowwise().all()         // all(R, 2)  
  18. R.any()                   // any(R(:))  
  19. R.colwise().any()         // any(R)  
  20. R.rowwise().any()         // any(R, 2)  

Eigen 矩阵点乘

 

  1. // Dot products, norms, etc.  
  2. // Eigen                  // Matlab  
  3. x.norm()                  // norm(x).    Note that norm(R) doesn't work in Eigen.  
  4. x.squaredNorm()           // dot(x, x)   Note the equivalence is not true for complex  
  5. x.dot(y)                  // dot(x, y)  
  6. x.cross(y)                // cross(x, y) Requires #include <Eigen/Geometry>  

Eigen 矩阵类型转换

 

  1.  Type conversion  
  2. // Eigen                           // Matlab  
  3. A.cast<double>();                  // double(A)  
  4. A.cast<float>();                   // single(A)  
  5. A.cast<int>();                     // int32(A)  
  6. A.real();                          // real(A)  
  7. A.imag();                          // imag(A)  
  8. // if the original type equals destination type, no work is done  

 

  1. // Note that for most operations Eigen requires all operands to have the same type:  
  2. MatrixXf F = MatrixXf::Zero(3,3);  
  3. A += F;                // illegal in Eigen. In Matlab A = A+F is allowed  
  4. A += F.cast<double>(); // F converted to double and then added (generally, conversion happens on-the-fly)  
  5.   
  6. // Eigen can map existing memory into Eigen matrices.  
  7. float array[3];  
  8. Vector3f::Map(array).fill(10);            // create a temporary Map over array and sets entries to 10  
  9. int data[4] = {1, 2, 3, 4};  
  10. Matrix2i mat2x2(data);                    // copies data into mat2x2  
  11. Matrix2i::Map(data) = 2*mat2x2;           // overwrite elements of data with 2*mat2x2  
  12. MatrixXi::Map(data, 2, 2) += mat2x2;      // adds mat2x2 to elements of data (alternative syntax if size is not know at compile time)  

Eigen 求解线性方程组 Ax = b

 

  1. // Solve Ax = b. Result stored in x. Matlab: x = A \ b.  
  2. x = A.ldlt().solve(b));  // A sym. p.s.d.    #include <Eigen/Cholesky>  
  3. x = A.llt() .solve(b));  // A sym. p.d.      #include <Eigen/Cholesky>  
  4. x = A.lu()  .solve(b));  // Stable and fast. #include <Eigen/LU>  
  5. x = A.qr()  .solve(b));  // No pivoting.     #include <Eigen/QR>  
  6. x = A.svd() .solve(b));  // Stable, slowest. #include <Eigen/SVD>  
  7. // .ldlt() -> .matrixL() and .matrixD()  
  8. // .llt()  -> .matrixL()  
  9. // .lu()   -> .matrixL() and .matrixU()  
  10. // .qr()   -> .matrixQ() and .matrixR()  
  11. // .svd()  -> .matrixU(), .singularValues(), and .matrixV()  

Eigen 矩阵特征值

 

  1. // Eigenvalue problems  
  2. // Eigen                          // Matlab  
  3. A.eigenvalues();                  // eig(A);  
  4. EigenSolver<Matrix3d> eig(A);     // [vec val] = eig(A)  
  5. eig.eigenvalues();                // diag(val)  
  6. eig.eigenvectors();               // vec  
  7. // For self-adjoint matrices use SelfAdjointEigenSolver<> 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值