首页 > 其他 > 详细

Eigen教程(11)

时间:2017-01-25 21:16:06      阅读:376      评论:0      收藏:0      [点我收藏+]

整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html

存储顺序

对于矩阵和二维数组有两种存储方式,列优先和行优先。

假设矩阵:

技术分享

按行优先存储,内存中形式如下:

8 2 2 9 9 1 4 4 3 5 4 5

列优先,内存格式:

8 9 3 2 1 5 2 4 4 9 4 5

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;

输出

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 

PlainObjectBase::data()函数可以返回矩阵中第一个元素的内存位置。

存储顺序及选择

Matrix类模板中可以设定存储的方向,RowMajor表示行优先,ColMajor表示列优先。默认是列优先。

如何选择存储方式呢?

  1. 如果要和其他库合作开发,为了转化方便,可以选择同样的存储方式。
  2. 应用中涉及大量行遍历操作,应该选择行优先,寻址更快。反之亦然。
  3. 默认是列优先,而且大多库都是按照这个顺序的,默认的不失为较好的。

Eigen教程(11)

原文:http://www.cnblogs.com/houkai/p/6349991.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!