Eigen::Map的用法及其底层逻辑

在这里插入图片描述

class Stride

template<int _OuterStrideAtCompileTime, int _InnerStrideAtCompileTime>
class Stride
{
  public:
    typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
    enum {
      InnerStrideAtCompileTime = _InnerStrideAtCompileTime,
      OuterStrideAtCompileTime = _OuterStrideAtCompileTime
    };
    ...
}

其中: The inner stride is the pointer increment between two consecutive entries within a given row of a row-major matrix or within a given column of a column-major matrix.(内部跨距是行主矩阵的给定行内或列主矩阵的给定列内的两个连续条目之间的指针增量)
The outer stride is the pointer increment between two consecutive rows of a row-major matrix or between two consecutive columns of a column-major matrix.(外部跨距是行主矩阵的两个连续行之间或列主矩阵的两个连续列之间的指针增量)

测试

输入1

    int array[8];
    for(int i = 0; i < 8; ++i) array[i] = i;
    cout << "Column-major:\n" << Map<Matrix<int,2,4> >(array)          << endl;
    cout << "Row-major:\n"    << Map<Matrix<int,2,4,RowMajor> >(array) << endl;
    cout << "Row-major using stride:\n" <<
                  Map<Matrix<int,2,4>, Unaligned, Stride<1,4> >(array) << endl;

输出1

Column-major:
0 2 4 6
1 3 5 7
Row-major:
0 1 2 3
4 5 6 7
Row-major using stride:
0 1 2 3
4 5 6 7

说明1

Column-major(默认的就是列主元)和Row-major(强行制定为行主元)的结果很容易理解
Row-major using stride中没有强行指定行主元,因此这里为默认的列主元,Stride<1,4>中:1表示_OuterStrideAtCompileTime,即列主矩阵的两个连续列之间的指针增量(在array[8]中连续取值),4表示_InnerStrideAtCompileTime,即列主矩阵的给定列内的两个连续条目之间的指针增量(在array[8]中每间隔4个指针增量取1个值),因此结果就是那样子的


输入2

int array[8];
    for(int i = 0; i < 8; ++i) array[i] = i;
    cout << "Column-major:\n" << Map<Matrix<int,2,4> >(array)          << endl;
    cout << "Row-major:\n"    << Map<Matrix<int,2,4,RowMajor> >(array) << endl;
    cout << "Row-major using stride:\n" <<
                  Map<Matrix<int,2,4>, Unaligned, Stride<4,1> >(array) << endl;//换成了Stride<4,1>

输出2

Column-major:
0 2 4 6
1 3 5 7
Row-major:
0 1 2 3
4 5 6 7
Row-major using stride:
        0         4 110842600         0
        1         5     32585         0

说明2

Stride<4,1>的解释说明类似,Row-major using stride:的后两列不正常的原因是指针越界,超过了array[8]的最后一个数值。换成array[16],就会正常,见输入3。

输入3

    int array[16];
    for(int i = 0; i < 16; ++i) array[i] = i;
    cout << "Column-major:\n" << Map<Matrix<int,2,4> >(array)          << endl;
    cout << "Row-major:\n"    << Map<Matrix<int,2,4,RowMajor> >(array) << endl;
    cout << "Row-major using stride:\n" <<
                  Map<Matrix<int,2,4>, Unaligned, Stride<4,1> >(array) << endl;

输出3

Column-major:
0 2 4 6
1 3 5 7
Row-major:
0 1 2 3
4 5 6 7
Row-major using stride:
 0  4  8 12
 1  5  9 13
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值