c++ Eigen库使用教程

官方说明链接
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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值