最近一直在做工程上的事情,比较多的使用了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; // Fixed rows and cols. Same as Matrix3d.
Matrix<double, 3, Dynamic> B; // Fixed rows, dynamic cols.
Matrix<double, Dynamic, Dynamic> C; // Full dynamic. Same as MatrixXd.
Matrix<double, 3, 3, RowMajor> E; // Row major; default is column-major.
Matrix3f P, Q, R; // 3x3 float matrix.
Vector3f x, y, z; // 3x1 float matrix.
RowVector3f a, b, c; // 1x3 float matrix.
VectorXd v; // Dynamic column vector of doubles
基本使用方法
// Basic usage
// Eigen // Matlab // comments
x.size() // length(x) // vector size
C.rows() // size(C,1) // number of rows
C.cols() // size(C,2) // number of columns
x(i) // x(i+1) // Matlab is 1-based
C(i,j) // C(i+1,j+1) //
A.resize(4, 4); // Runtime error if assertions are on.
B.resize(4, 9); // Runtime error if assertions are on.
A.resize(3, 3); // Ok; size didn't change.
B.resize(3, 9); // Ok; only dynamic cols changed.
A << 1, 2, 3, // Initialize A. The elements can also be
4, 5, 6, // matrices, which are stacked along cols
7, 8, 9; // and then the rows are stacked.
B << A, A, A; // B is three horizontally stacked A's.
A.fill(10); // Fill A with all 10's.
特殊矩阵生成
// Eigen // Matlab
MatrixXd::Identity(rows,cols) // eye(rows,cols)
C.setIdentity(rows,cols) // C = eye(rows,cols)
MatrixXd::Zero(rows,cols) // zeros(rows,cols)
C.setZero(rows,cols) // C = ones(rows,cols)
MatrixXd::Ones(rows,cols) // ones(rows,cols)
C.setOnes(rows,cols) // C = ones(rows,cols)
MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1 //