官方说明链接
html
The Matrix class
矩阵 向量定义
- 常用的三个参数
Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
分别是数据类型,行数,列数。 - 完整参数
Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime, int Options = 0, int MaxRowsAtCompileTime = RowsAtCompileTime, int MaxColsAtCompileTime = ColsAtCompileTime>
typedef Matrix<float, 4, 4> Matrix4f; // 可以简写矩阵类型
// 列向量和行向量 默认是列向量
typedef Matrix<float, 3, 1> Vector3f;
typedef Matrix<int, 1, 2> RowVector2i
// 不确定矩阵的行数或者列数时候
typedef Matrix<double, Dynamic, Dynamic> MatrixXd;
typedef Matrix<int, Dynamic, 1> VectorXi;
//定义举例
MatrixXf a(10,15);
VectorXf b(30);
// 初始化举例
Vector2i a(1, 2); // A column vector containing the elements {1, 2}
Matrix<int, 5, 1> b {1, 2, 3, 4, 5}; // A row-vector containing the elements {1, 2, 3, 4, 5}
Matrix<int, 1, 5> c = {1, 2, 3, 4, 5}; // A column vector containing the elements {1, 2, 3, 4, 5}
MatrixXi a { // construct a 2x2 matrix
{1, 2}, // first row
{3, 4} // second row
};
Matrix<double, 2, 3> b {
{2, 3, 4},
{5, 6, 7},
};
// comma initialization
Matrix3f m;
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;
- 方便的定义名称
MatrixNt for Matrix<type, N, N>. For example, MatrixXi for Matrix<int, Dynamic, Dynamic>.
MatrixXNt for Matrix<type, Dynamic, N>. For example, MatrixX3i for Matrix<int, Dynamic, 3>.
MatrixNXt for Matrix<type, N, Dynamic>. For example, Matrix4Xd for Matrix<d, 4, Dynamic>.
VectorNt for Matrix<type, N, 1>. For example, Vector2f for Matrix<float, 2, 1>.
RowVectorNt for Matrix<type, 1, N>. For example, RowVector3d for Matrix<double, 1, 3>.
元素读取 行列数目获取
// 圆括号来进行下标引用
Eigen::MatrixXd m(2,2);
m(0,0) = 3;
// 获取行和列的数目
m.rows()
m.cols()
use fixed sizes for very small sizes where you can, and use dynamic sizes for larger sizes or where you have to.
运算操作
Eigen::Matrix2d a;
a << 1, 2,
3, 4;
Eigen::Vector3d v(1,2,3);
std::cout << "Now v =\n" << v << std::endl; // 和标量之间的除法
MatrixXcf a = MatrixXcf::Random(2,2);
cout << "Here is the matrix a^T\n" << a.transpose() << endl; // 转置
cout << "Here is the conjugate of a\n" << a.conjugate() << endl; // 共轭
cout << "Here is the matrix a^*\n" << a.adjoint() << endl; // 转置 + 共轭
- 原地转置!!!
a = a.transpose(); // !!! do NOT do this !!!
MatrixXf a(2,3); a << 1, 2, 3, 4, 5, 6;
cout << "Here is the initial matrix a:\n" << a << endl;
a.transposeInPlace();
cout << "and after being transposed:\n" << a << endl;
- 乘法
Eigen::Vector3d v(1,2,3);
Eigen::Vector3d w(0,1,2);
std::cout << "Dot product: " << v.dot(w) << std::endl;
double dp = v.adjoint()*w; // automatic conversion of the inner product to a scalar
std::cout << "Dot product via a matrix product: " << dp << std::endl; // 点乘
std::cout << "Cross product:\n" << v.cross(w) << std::endl; // 叉乘
Eigen::Matrix2d mat;
mat << 1, 2,
3, 4;
cout << "Here is mat.sum(): " << mat.sum() << endl;
cout << "Here is mat.prod(): " << mat.prod() << endl;
cout << "Here is mat.mean(): " << mat.mean() << endl;
cout << "Here is mat.minCoeff(): " << mat.minCoeff() << endl;
cout << "Here is mat.maxCoeff(): " << mat.maxCoeff() << endl; //可以传俩个指针进去 获取极值点位置
cout << "Here is mat.trace(): " << mat.trace() << endl;