'Is it possible to flatten an Eigen matrix without copying?

Suppose you have a matrix A:

1 2
3 4

There are two flattenings:

1
2
3
4

and

1
3
2
4

If the default (ColMajor) storage type is used, we can get the latter as

VectorXd v = Map<const VectorXd>(A.data(), A.size())

This only copies the data once.

But to get the former, the best I can think of is

MatrixXd At = A.transpose()
VectorXd v  = Map<const VectorXd>(At.data(), At.size())

This copies the data twice, unfortunately.

Somewhat confusingly (to me at least)

VectorXd v  = Map<const VectorXd>(A.transpose().data(), A.size())

compiles, but produces completely identical results as not having the transpose there.

See also: Eigen Convert Matrix to Vector



Solution 1:[1]

Note that you can name a Map object:

Map<const VectorXd> v1(A.data(), A.size());

and use v1 like a VectorXd. Of course, modifying v1 will also modify A. To pass it to functions accepting a const VectorXd& object without copy, either make your function template or make take a Ref<const VectorXd>.

Then the first case requires zero copy, and the second 1 transposed copy.

Solution 2:[2]

If you're willing to use a Matrix instead of a Vector you could use the following:

Eigen::MatrixXi m(2, 2);
m << 1, 2, 3, 4;
std::cout << m << "\n\n";

// Option 1
Eigen::MatrixXi v1;
v1 = m.transpose();  // Copy #1
v1.resize(1, 4);  // No copy
std::cout << v1 << "\n\n";

// Option 2
v1 = m;  // Copy #1
v1.resize(1, 4);  // No copy
std::cout << v1 << "\n\n";

Note that in some cases you may get a performance hit when using the 1D matrix.

Solution 3:[3]

Just use the Map API, here is an example creating a 1D linear view of a matrix as you want:

Eigen::MatrixXf M1(2,2); // Column-major storage    
M1 << 1, 2, 3, 4;

Eigen::Map<Eigen::RowVectorXf> v1(M1.data(), M1.size());
cout << "v1:" << endl << v1 << endl;

Eigen::Matrix<float,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> M2(M1);
Eigen::Map<Eigen::RowVectorXf> v2(M2.data(), M2.size());
std::cout << "v2:" << std::endl << v2 << std::endl;

Output:

v1:
1 3 2 4
v2:
1 2 3 4 

Solution 4:[4]

Since the version 3.4, Eigen exposes convenient methods to reshape a matrix to another matrix of different sizes or vector. All cases are handled via the DenseBase::reshaped(NRowsType,NColsType) and DenseBase::reshaped() functions. Those functions do not perform in-place reshaping, but instead return a view on the input expression. docs source

Sadly I do not yet have version 3.4 to test it, but the docs have some good examples too:

A very common usage of reshaping is to create a 1D linear view over a given 2D matrix or expression. In this case, sizes can be deduced and thus omitted as in the following example:

Matrix4i m = Matrix4i::Random();
cout << "Here is the matrix m:" << endl << m << endl;
cout << "Here is m.reshaped().transpose():" << endl << m.reshaped().transpose() << endl;
cout << "Here is m.reshaped<RowMajor>().transpose():  " << endl << m.reshaped<RowMajor>().transpose() << endl;
Here is the matrix m:
-10   1   4   7
 -8  -6   9 -10
  5 -10  -2  -9
 -1   4   0   1
Here is m.reshaped().transpose():
-10  -8   5  -1   1  -6 -10   4   4   9  -2   0   7 -10  -9   1
Here is m.reshaped<RowMajor>().transpose():  
-10   1   4   7  -8  -6   9 -10   5 -10  -2  -9  -1   4   0   1

Beware though, that assigning a reshaped matrix to itself is currently not supported.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ggael
Solution 2 Avi Ginsburg
Solution 3 GPrathap
Solution 4 lucidbrot