Search code examples
eigenstride

Strange Eigen map behavior with zero stride


From Eigen::Stride docs:

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.

Let say I want to create a matrix that consists repeated vector as a row. In python numpy I can use zero length stride to do this. Eigen docs say nothing about zero strides but behavior looks very strange:

typedef Matrix<float, Dynamic, Dynamic, RowMajor> MatrixType;

MatrixType M1(3, 3);
M1 << 1, 2, 3,
      4, 5, 6,
      7, 8, 9;

Map<MatrixType, 0, Stride<0, 0>> zeroMap(M1.data(), 2, 2);  
Map<MatrixType, 0, Stride<2, 0>> oneMap(M1.data(), 2, 2);  

cout << "Row stride = 0:" << "\n";
cout << zeroMap << "\n" << "Row stride = 2:" << "\n";
cout << oneMap;
cout << "\n";

Returns the same result in both cases:

Row stride = 0:
1 2
3 4
Row stride = 2:
1 2
3 4

Why are results the same with stride 0 and stride 2?


Solution

  • A stride of 0 at compile time means "natural stride" in Eigen. If you want to repeat a vector multiple times, you should use the .replicate() function:

    M1.row(0).replicate<3,1>();
    

    Also have a look at .rowwise().replicate(), .colwise().replicate(), each with template arguments or runtime arguments (depending on what you actually need).