Is it possible to flatten an Eigen matrix without copying?
Asked Answered
G

4

9

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

Gastight answered 10/10, 2016 at 5:3 Comment(1)
what's wrong with using a mapping function (row, col)->vector_ix (or the inverse vector_ix->(row,col) and for-cycle-ing in a inline void flatten(const MatrixXd& src, VectorXd& dest, direct_mapper_func& f=line_major_direct) (iterating over src row/col) or inline void flatten(const MatrixXd& src, VectorXd& dest, inverse_mapper_func& f=line_major_inverse) (and iterating over the dest ix)? You can even flatten using a 'triangular' rule like the one used to demonstrate rational numbers are countableKliman
D
9

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.

Delafuente answered 11/10, 2016 at 13:8 Comment(0)
C
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.

Colonnade answered 10/10, 2016 at 5:41 Comment(2)
The resize() method is a no-operation if the actual matrix size doesn't change; otherwise it is destructive: the values of the coefficients may change.. Is this a special case here because the total number of elements indeed stays the same, or were you just lucky and size actually means the shape?Mashburn
@Mashburn It’s kinda one and he same thing. Reshaping the matrix would as far as I know mean that the number of elements doesn’t change. Therefore, reshape it is a special case of resize kinda by definition. Lucky for me.Colonnade
M
2

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.

Mashburn answered 21/4, 2022 at 16:12 Comment(0)
E
0

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 
Emergent answered 18/1, 2021 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.