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
(row, col)->vector_ix
(or the inversevector_ix->(row,col)
and for-cycle-ing in ainline void flatten(const MatrixXd& src, VectorXd& dest, direct_mapper_func& f=line_major_direct)
(iterating over src row/col) orinline 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 countable – Kliman