Eigen库学习(1)

Eigen库学习(1)-Matrix类

基础

typedef Matrix <float4,4> Matrix4f ;
typedef Matrix <float3,1> Vector3f ;
typedef Matrix <double,Dynamic,Dynamic> MatrixXd ;
//生疏的表达式
typedef Matrix <int1,2> RowVector2i ;
typedef Matrix <int,Dynamic,1> VectorXi ;
//a 是一个3乘3的矩阵,具有未初始化系数的普通浮点数[9]
Matrix3f a;
//b 是一个动态大小的矩阵,其大小目前是0乘0,并且其系数数组尚未分配
MatrixXf b;
//a 是一个10x15动态大小的矩阵,具有已分配但当前未初始化的系数
MatrixXf a(10,15;
//b 是一个大小为30的动态大小向量,具有已分配但当前未初始化的系数
VectorXf b(30;
//一些构造函数来初始化大小为4的小型固定大小向量的系数
Vector2d a(5.0,6.0;
Vector3d b(5.0,6.0,7.0;
Vector4d c(5.0,6.0,7.0,8.0;

系数访问器

#include <iostream>
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
  MatrixXd m(2,2);
  m(0,0) = 3;
  m(1,0) = 2.5;
  m(0,1) = -1;
  m(1,1) = m(1,0) + m(0,1);
  std::cout << "Here is the matrix m:\n" << m << std::endl;
  VectorXd v(2);
  v(0) = 4;
  v(1) = v(0) - 1;
  std::cout << "Here is the vector v:\n" << v << std::endl;
}
}

逗号初始化

Matrix3f m;
m << 1, 2, 3,
     4, 5, 6,
     7, 8, 9;
std::cout << m;

调整

rows(),cols()和size()
行数, 列数 和系数数

#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
  MatrixXd m(2,5);
  m.resize(4,3);
  std::cout << "The matrix m is of size "
            << m.rows() << "x" << m.cols() << std::endl;
  std::cout << "It has " << m.size() << " coefficients" << std::endl;
  VectorXd v(2);
  v.resize(5);
  std::cout << "The vector v is of size " << v.size() << std::endl;
  std::cout << "As a matrix, v is of size "
            << v.rows() << "x" << v.cols() << std::endl;
}

固定与动态尺寸

什么时候应该使用固定尺寸(例如Matrix4f),何时应该更喜欢动态尺寸(例如MatrixXf)?简单的答案是:尽可能使用固定尺寸的小尺寸,并在较大尺寸或您需要的地方使用动态尺寸。

Matrix4f mymatrix;
上式等价于
float mymatrix[16]; 
MatrixXf mymatrix(rows,columns);
上式等价于
float *mymatrix = new float[rows*columns]; 

可选的模板参数

Matrix <float3,3,RowMajor>
//此类型表示行主要3x3矩阵,默认情况下,存储顺序是列专用

猜你喜欢

转载自blog.csdn.net/lxn9492878lbl/article/details/83511235