http://blog.csdn.net/shuzfan/article/details/52367329
最近一直在做工程上的事情,比较多的使用了Eigen矩阵运算库。
简单说一下Eigen的特点:
(1) 使用方便、无需预编译,调用开销小
(2) 函数丰富,风格有点近似MATLAB,易上手;
(3) 速度中规中矩,比OpenCV快,比MKL、openBLAS慢;
Eigen3.3版本链接 http://eigen.tuxfamily.org/index.php?title=Main_Page
注:绝大部分使用说明和示例都可以在官网上找到,所以有时候不需要纠结百度到的与实际不符,可以直接官网or谷歌
———————————————————————————————————————————————
使用方法很简单:下载Eigen后解压,然后包含解压路径,最后只需要在程序里引用头文件
#include <Eigen/Dense>
———————————————————————————————————————————————
基本使用方法如下:
原址链接为http://eigen.tuxfamily.org/dox/AsciiQuickReference.txt
矩阵定义
#include <Eigen/Dense>Matrix<double, 3, 3> A; Matrix<double, 3, Dynamic> B; Matrix<double, Dynamic, Dynamic> C; Matrix<double, 3, 3, RowMajor> E; Matrix3f P, Q, R; Vector3f x, y, z; RowVector3f a, b, c; VectorXd v;
基本使用方法
x.size() C.rows() C.cols() x(i) C(i,j) A.resize(4, 4); B.resize(4, 9); A.resize(3, 3); B.resize(3, 9); A << 1, 2, 3, 4, 5, 6, 7, 8, 9; B << A, A, A; A.fill(10);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
特殊矩阵生成
MatrixXd::Identity(rows,cols) C.setIdentity(rows,cols) MatrixXd::Zero(rows,cols) C.setZero(rows,cols) MatrixXd::Ones(rows,cols) C.setOnes(rows,cols) MatrixXd::Random(rows,cols) C.setRandom(rows,cols) VectorXd::LinSpaced(size,low,high) v.setLinSpaced(size,low,high)
矩阵块操作
x.head(n) x.head<n>() x.tail(n) x.tail<n>() x.segment(i, n) x.segment<n>(i) P.block(i, j, rows, cols) P.block<rows, cols>(i, j) P.row(i) P.col(j) P.leftCols<cols>() P.leftCols(cols) P.middleCols<cols>(j) P.middleCols(j, cols) P.rightCols<cols>() P.rightCols(cols) P.topRows<rows>() P.topRows(rows) P.middleRows<rows>(i) P.middleRows(i, rows) P.bottomRows<rows>() P.bottomRows(rows) P.topLeftCorner(rows, cols) P.topRightCorner(rows, cols) P.bottomLeftCorner(rows, cols) P.bottomRightCorner(rows, cols) P.topLeftCorner<rows,cols>() P.topRightCorner<rows,cols>() P.bottomLeftCorner<rows,cols>() P.bottomRightCorner<rows,cols>()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
矩阵元素交换以及转置等
R.row(i) = P.col(j); R.col(j1).swap(mat1.col(j2)); R.adjoint() R.transpose() R.diagonal() x.asDiagonal() R.transpose().colwise().reverse(); R.conjugate()
矩阵四则运算
// All the same as Matlab, but matlab doesn't have *= style operators.// Matrix-vector. Matrix-matrix. Matrix-scalar.y = M*x; R = P*Q; R = P*s;a = b*M; R = P - Q; R = s*P;a *= M; R = P + Q; R = P/s; R *= Q; R = s*P; R += Q; R *= s; R -= Q; R /= s;
单个元素操作
R = P.cwiseProduct(Q); R = P.array() * s.array();R = P.cwiseQuotient(Q); R = P.array() / Q.array();R = P.array() + s.array();R = P.array() - s.array();R.array() += s; R.array() -= s; R.array() < Q.array(); R.array() <= Q.array(); R.cwiseInverse(); R.array().inverse(); R.array().sin() R.array().cos() R.array().pow(s) R.array().square() R.array().cube() R.cwiseSqrt() R.array().sqrt() R.array().exp() R.array().log() R.cwiseMax(P) R.array().max(P.array()) R.cwiseMin(P) R.array().min(P.array()) R.cwiseAbs() R.array().abs() R.cwiseAbs2() R.array().abs2() (R.array() < s).select(P,Q);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
矩阵缩减
// Reductions.int r, c// Eigen // MatlabR.minCoeff() // min(R(:))R.maxCoeff() // max(R(:))s = R.minCoeff(&r, &c) // [s, i] = min(R(:))s = R.maxCoeff(&r, &c) // [s, i] = max(R(:))R.sum() // sum(R(:))R.colwise().sum() // sum(R)R.rowwise().sum() // sum(R, 2) or sum(R')'R.prod() // prod(R(:))R.colwise().prod() // prod(R)R.rowwise().prod() // prod(R, 2) or prod(R')'R.trace() // trace(R)R.all() // all(R(:))R.colwise().all() // all(R)R.rowwise().all() // all(R, 2)R.any() // any(R(:))R.colwise().any() // any(R)R.rowwise().any() // any(R, 2)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
矩阵点乘及归一化
x.norm() x.squaredNorm() x.dot(y) x.cross(y)
矩阵类型转换
A.cast<double>(); A.cast<float>(); A.cast<int>(); A.real(); A.imag(); MatrixXf F = MatrixXf::Zero(3,3);A += F; A += F.cast<double>();
内存映射创建矩阵
// Eigen can map existing memory into Eigen matrices.float array[3];Vector3f::Map(array).fill(10); // create a temporary Map over array and sets entries to 10int data[4] = {1, 2, 3, 4};Matrix2i mat2x2(data); // copies data into mat2x2Matrix2i::Map(data) = 2*mat2x2; // overwrite elements of data with 2*mat2x2MatrixXi::Map(data, 2, 2) += mat2x2; // adds mat2x2 to elements of data (alternative syntax if size is not know at compile time)
解方程
// Solve Ax = b. Result stored in x. Matlab: x = A \ b.x = A.ldlt().solve(b))x = A.llt() .solve(b))x = A.lu() .solve(b))x = A.qr() .solve(b))x = A.svd() .solve(b))// .ldlt() -> .matrixL() and .matrixD()// .llt() -> .matrixL()// .lu() -> .matrixL() and .matrixU()// .qr() -> .matrixQ() and .matrixR()// .svd() -> .matrixU(), .singularValues(), and .matrixV()
特征值
A.eigenvalues(); EigenSolver<Matrix3d> eig(A); eig.eigenvalues(); eig.eigenvectors();
下面是我实际遇到的补充一下:
求广义逆矩阵
using Eigen::Dynamic;using Eigen::Matrix;using Eigen::RowMajor;typedef Matrix<float, Dynamic, Dynamic, RowMajor> MatXf;MatXf pinv(MatXf x){ JacobiSVD<MatXf> svd(x,ComputeFullU | ComputeFullV); float pinvtoler=1.e-8; MatXf singularValues_inv = svd.singularValues(); for ( long i=0; i<x.cols(); ++i) { if ( singularValues_inv(i) > pinvtoler ) singularValues_inv(i)=1.0/singularValues_inv(i); else singularValues_inv(i)=0; } return svd.matrixV()*singularValues_inv.asDiagonal()*svd.matrixU().transpose();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
—————————————————————————————————————————————————
常用的和MATLAB类似的函数实现如下:
原址链接http://igl.ethz.ch/projects/libigl/matlab-to-eigen.html