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