1、下载Eigen解压得到如下文件结构,其中的Eigen文件夹中包含了所有的头文件和源代码。头文件在Eigen目录下,源代码在Eigen/src目录下.

2、使用时,只需在VC的附加目录中添加Eigen的路径,并在程序开头使用#include <Eigen/Dense>等即可。其中Eigen中不同的头文件可实现的功能如下所示。

3、模板库说明:
- Eigen是一个类模板库,其中的类和使用方法如下(下标由0开始):
- 在Eigen,所有的矩阵和向量都是Matrix模板类的对象,Vector只是一种特殊的矩阵(一行或者一列)。Matrix有6个模板参数。
Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime, int Options = 0, int MaxRowsAtCompileTime = RowsAtCompileTime, int MaxColsAtCompileTime = ColsAtCompileTime>
讯享网
Scalar是表示元素的类型,RowsAtCompileTime为矩阵的行,ColsAtCompileTime为矩阵的列,Options表示存储主序(默认是按列存储,可使用RowMajor改为按行存储),MaxRowsAtCompileTime和MaxColsAtCompileTime表示在编译阶段矩阵的上限。
以下所有类型的矩阵都是由这个泛型实例化出的。
- Eigen不只限于已知大小(编译阶段)的矩阵,有些矩阵的尺寸是运行时确定的,于是引入了一个特殊的标识符:Dynamic表示大小在运行时确定,如下所示
讯享网typedef Matrix<double, Dynamic, Dynamic> MatrixXd;
- 对于矩阵使用(行,列)获取元素;
- 对于向量,使用(索引)或[索引]获取元素,以0为起始。
- matrix的大小可以通过rows()、cols()、size()获取,resize()可以重新调整动态matrix的大小,元素的值会被改变,如果不想改变执行 conservativeResize()。
- 使用一个矩阵对另一个矩阵赋值时,如果两者尺寸不同,左阵尺寸会自动变为与右阵相同。
- 对于大矩阵,应使用动态数组,否则会由于固定大小的矩阵分配内存过多而崩溃;
4、使用方法
矩阵声明
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
矩阵属性
讯享网x.size() // vector size C.rows() // number of rows C.cols() // number of columns x(i) // Matlab is 1-based C(i,j) //
矩阵生成

MatrixXd::Identity(rows,cols) // 单位阵 C.setIdentity(rows,cols) // C = 单位阵 MatrixXd::Zero(rows,cols) // 0阵 C.setZero(rows,cols) // C = 0阵 MatrixXd::Ones(rows,cols) // ones(rows,cols) C.setOnes(rows,cols) // C = ones(rows,cols) MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1 C.setRandom(rows,cols) // C = rand(rows,cols)*2-1 VectorXd::LinSpaced(size,low,high) // linspace(low,high,size)' v.setLinSpaced(size,low,high) // v = linspace(low,high,size)'
矩阵操作
讯享网x.head(n) // x(1:n) x.head<n>() // x(1:n) x.tail(n) // x(end - n + 1: end) x.tail<n>() // x(end - n + 1: end) x.segment(i, n) // x(i+1 : i+n) x.segment<n>(i) // x(i+1 : i+n) P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols) P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols) P.row(i) // P(i+1, :) P.col(j) // P(:, j+1) P.leftCols<cols>() // P(:, 1:cols) P.leftCols(cols) // P(:, 1:cols) P.middleCols<cols>(j) // P(:, j+1:j+cols) P.middleCols(j, cols) // P(:, j+1:j+cols) P.rightCols<cols>() // P(:, end-cols+1:end) P.rightCols(cols) // P(:, end-cols+1:end) P.topRows<rows>() // P(1:rows, :) P.topRows(rows) // P(1:rows, :) P.middleRows<rows>(i) // P(i+1:i+rows, :) P.middleRows(i, rows) // P(i+1:i+rows, :) P.bottomRows<rows>() // P(end-rows+1:end, :) P.bottomRows(rows) // P(end-rows+1:end, :) P.topLeftCorner(rows, cols) // P(1:rows, 1:cols) P.topRightCorner(rows, cols) // P(1:rows, end-cols+1:end) P.bottomLeftCorner(rows, cols) // P(end-rows+1:end, 1:cols) P.bottomRightCorner(rows, cols) // P(end-rows+1:end, end-cols+1:end) P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols) P.topRightCorner<rows,cols>() // P(1:rows, end-cols+1:end) P.bottomLeftCorner<rows,cols>() // P(end-rows+1:end, 1:cols) P.bottomRightCorner<rows,cols>() // P(end-rows+1:end, end-cols+1:end) R.row(i) = P.col(j); // R(i, :) = P(:, i) R.col(j1).swap(mat1.col(j2)); // R(:, [j1 j2]) = R(:, [j2, j1]) R.adjoint() // R' R.transpose() // R.' or conj(R') R.diagonal() // diag(R) x.asDiagonal() // diag(x) R.transpose().colwise().reverse(); // rot90(R) R.conjugate() // conj(R)
矩阵与向量运算
y = M*x; a = b*M; a *= M;
矩阵与矩阵运算
讯享网R = P*Q; R = P - Q; R = P + Q; R *= Q; R += Q; R -= Q;
矩阵与标量运算
R = P*s; R = s*P; R = P/s; R = s*P; R *= s; R /= s;
矩阵单个元素分别操作
讯享网R = P.cwiseProduct(Q); // R = P .* Q R = P.array() * s.array();// R = P .* s R = P.cwiseQuotient(Q); // R = P ./ Q R = P.array() / Q.array(); // R = P ./ Q R = P.array() + s.array();// R = P + s R = P.array() - s.array();// R = P - s R.array() += s; // R = R + s R.array() -= s; // R = R - s R.array() < Q.array(); // R < Q R.array() <= Q.array(); // R <= Q R.cwiseInverse(); // 1 ./ P R.array().inverse(); // 1 ./ P R.array().sin() // sin(P) R.array().cos() // cos(P) R.array().pow(s) // P .^ s R.array().square() // P .^ 2 R.array().cube() // P .^ 3 R.cwiseSqrt() // sqrt(P) R.array().sqrt() // sqrt(P) R.array().exp() // exp(P) R.array().log() // log(P) R.cwiseMax(P) // max(R, P) R.array().max(P.array()) // max(R, P) R.cwiseMin(P) // min(R, P) R.array().min(P.array()) // min(R, P) R.cwiseAbs() // abs(P) R.array().abs() // abs(P) R.cwiseAbs2() // abs(P.^2) R.array().abs2() // abs(P.^2) (R.array() < s).select(P,Q); // (R < s ? P : Q)
矩阵缩减
R.minCoeff() // min(R(:)) R.maxCoeff() // max(R(:)) s = R.minCoeff(&r, &c) // [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i); s = R.maxCoeff(&r, &c) // [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i); 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)
向量归一及内外积
讯享网x.norm() // norm(x). Note that norm(R) doesn't work in Eigen. x.squaredNorm() // dot(x, x) Note the equivalence is not true for complex x.dot(y) // dot(x, y) x.cross(y) // cross(x, y) Requires #include <Eigen/Geometry>
矩阵类型变换
A.cast<double>(); // double(A) A.cast<float>(); // single(A) A.cast<int>(); // int32(A) A.real(); // real(A) A.imag(); // imag(A)
数组填充矩阵
讯享网float array[3]; Vector3f::Map(array).fill(10); // create a temporary Map over array and sets entries to 10 int data[4] = {1, 2, 3, 4}; Matrix2i mat2x2(data); // copies data into mat2x2 Matrix2i::Map(data) = 2*mat2x2; // overwrite elements of data with 2*mat2x2 MatrixXi::Map(data, 2, 2) += mat2x2; // adds mat2x2 to elements of data (alternative syntax if size is not know at compile time)
解方程
x = A.ldlt().solve(b)); // A sym. p.s.d. #include <Eigen/Cholesky> x = A.llt() .solve(b)); // A sym. p.d. #include <Eigen/Cholesky> x = A.lu() .solve(b)); // Stable and fast. #include <Eigen/LU> x = A.qr() .solve(b)); // No pivoting. #include <Eigen/QR> x = A.svd() .solve(b)); // Stable, slowest. #include <Eigen/SVD>
求特征值
讯享网A.eigenvalues(); // eig(A); EigenSolver<Matrix3d> eig(A); // [vec val] = eig(A) eig.eigenvalues(); // diag(val) eig.eigenvectors(); // vec
(本文中的示例代码均来自官方示例)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/133967.html