eigen 中的matrix

目录

Storage orders

         Constructors

固定尺寸和动态尺寸

块操作


Storage orders

eigen矩阵存储顺序两种,column-major 和 row-major,默认列排列

A = \begin{bmatrix} 8 & 2 & 2 & 9 \\ 9 & 1 & 4 & 4 \\ 3 & 5 & 4 & 5 \end{bmatrix}.

example:

Matrix<int, 3, 4, ColMajor> Acolmajor;
Acolmajor << 8, 2, 2, 9,
             9, 1, 4, 4,
             3, 5, 4, 5;
cout << "The matrix A:" << endl;
cout << Acolmajor << endl << endl; 
 
cout << "In memory (column-major):" << endl;
for (int i = 0; i < Acolmajor.size(); i++)
  cout << *(Acolmajor.data() + i) << "  ";
cout << endl << endl;
 
Matrix<int, 3, 4, RowMajor> Arowmajor = Acolmajor;
cout << "In memory (row-major):" << endl;
for (int i = 0; i < Arowmajor.size(); i++)
  cout << *(Arowmajor.data() + i) << "  ";
cout << endl;

 Output:

The matrix A:
8 2 2 9
9 1 4 4
3 5 4 5

In memory (column-major):
8  9  3  2  1  5  2  4  4  9  4  5  

In memory (row-major):
8  2  2  9  9  1  4  4  3  5  4  5 

Constructors

默认构造函数不执行任何动态内存分配,

Matrix3f a;
MatrixXf b;

a是一个3x3矩阵,未初始化的数组float[9]。

b是动态大小矩阵,大小为0x0。

支持c++11,可以按如下初始化任意大小的矩阵,

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}

也可以逗号初始化comma-initializer syntax

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

固定尺寸和动态尺寸

什么时候应该使用固定尺寸(例如Matrix4f),什么时候应该使用动态尺寸(例如MatrixXf)?简单的答案是:尽可能对非常小的尺寸使用固定尺寸,并在较大尺寸或必须使用的地方使用动态尺寸。对于小尺寸,尤其是小于(大约)16 的尺寸,使用固定尺寸对性能非常有利,因为它允许Eigen避免动态内存分配和展开循环。在内部,固定大小的Eigen矩阵只是一个普通数组,即

Matrix4f mymatrix; 
//really amounts to just doing
float mymatrix[16];

所以这真的有零运行成本。相比之下,动态大小矩阵的数组总是在堆heap上分配,所以这样做

MatrixXf mymatrix(rows,columns);
//amounts to doing
float *mymatrix = new float[rows*columns];

除此之外,该MatrixXf对象将其行数和列数存储为成员变量。

当然,使用固定大小的限制是只有在编译时知道大小时才有可能。此外,对于足够大的尺寸,例如大于(大约)32 的尺寸,使用固定尺寸的性能优势变得可以忽略不计。更糟糕的是,尝试在函数内使用固定大小创建一个非常大的矩阵可能会导致堆栈溢出stack overflow,因为Eigen会尝试将数组自动分配为局部变量,而这通常在堆栈上完成。最后,根据情况,当使用动态大小时, Eigen也可以更积极地尝试向量化(使用 SIMD 指令),请参阅Vectorization

块操作

块是矩阵或数组的矩形部分。块表达式既可以用作右值,也可以用作左值。与通常的特征表达式一样,只要让编译器进行优化。

两种表示形式:

 

 Example:

#include <Eigen/Dense>
#include <iostream>
 
int main()
{
  Eigen::Array22f m;
  m << 1,2,
       3,4;
  Eigen::Array44f a = Eigen::Array44f::Constant(0.6);
  std::cout << "Here is the array a:\n" << a << "\n\n";
  a.block<2,2>(1,1) = m;
  std::cout << "Here is now a with m copied into its central 2x2 block:\n" << a << "\n\n";
  a.block(0,0,2,3) = a.block(2,1,2,3);
  std::cout << "Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:\n" << a << "\n\n";
}

Output: 

Here is the array a:
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6

Here is now a with m copied into its central 2x2 block:
0.6 0.6 0.6 0.6
0.6   1   2 0.6
0.6   3   4 0.6
0.6 0.6 0.6 0.6

Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:
  3   4 0.6 0.6
0.6 0.6 0.6 0.6
0.6   3   4 0.6
0.6 0.6 0.6 0.6

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值