Reputation: 2347
How can I reshape data
of size 1×2N
to a complex form in Eigen to a form a P×Q
complex matrix, with N
complex numbers, P×Q=N
? In data
, the real and imaginary parts are right next to each other. I would like to dynamically reshape data
as the data can have different sizes. I am trying to prevent copying and just map the data to complex type.
int N = 9;
int P = 3;
int Q = 6;
float *data = new float[2*N];
for(int i = 0; i < 2*N; i++)
data[i] = i + 1; // data = {1, 2, 3, 4, ..., 17, 18};
Eigen::Map<Eigen::MatrixXcf> A(data, P, Q); // trying to have something like this.
// Desired reshaping:
// A = [
// 1 + 2i 7 + 8i 13 + 14i
// 3 + 4i 9 + 10i 15 + 16i
// 5 + 6i 11 + 12i 17 + 18i
// ]
I tried to first convert data
to a complex Eigen array (to ultimately convert to MatrixXcf
), which does not work either:
Eigen::Map<Eigen::ArrayXf> Arr(data, N); // this works
Eigen::Map<Eigen::ArrayXcf> Arrc(A.data(), N); // trying to map data to an Eigen complex array.
Could stride
in Eigen::Map
be helpful?
The simplest solution is to loop through all the elements and convert data
to an array of std::complex<float> *datac = new std::complex<float>[N];
. I was wondering if Eigen can map data
to datac
. Thanks in advance.
Upvotes: 0
Views: 783
Reputation: 10596
Here is the MCVE answer (online example) with some extra examples of how you can use the stride to get different outcomes:
#include "Eigen/Core"
#include <iostream>
#include <complex>
int main()
{
int N = 9;
int P = 3;
int Q = 6;
float *data = new float[20*N];
for(int i = 0; i < 20*N; i++)
data[i] = i + 1; // data = {1, 2, 3, 4, ..., 170, 180};
// Produces the output of the "Desired reshaping"
Eigen::Map<Eigen::MatrixXcf>
A((std::complex<float>*)(data), P, P);
std::cout << A << "\n\n";
// Produces what you originally wrote (plus a cast so it works)
Eigen::Map<Eigen::MatrixXcf>
B((std::complex<float>*)(data), P, Q);
std::cout << B << "\n\n";
// Start each column at the 10xJ position
Eigen::Map<Eigen::MatrixXcf, 0, Eigen::OuterStride<>>
C((std::complex<float>*)(data), P, Q, Eigen::OuterStride<>(10));
std::cout << C << "\n\n";
// Skip every other value
Eigen::Map<Eigen::MatrixXcf, 0, Eigen::InnerStride<>>
D((std::complex<float>*)(data), P, Q, Eigen::InnerStride<>(2));
std::cout << D << "\n\n";
delete [] data;
return 0;
}
The output is:
(1,2) (7,8) (13,14)
(3,4) (9,10) (15,16)
(5,6) (11,12) (17,18)(1,2) (7,8) (13,14) (19,20) (25,26) (31,32)
(3,4) (9,10) (15,16) (21,22) (27,28) (33,34)
(5,6) (11,12) (17,18) (23,24) (29,30) (35,36)(1,2) (21,22) (41,42) (61,62) (81,82) (101,102)
(3,4) (23,24) (43,44) (63,64) (83,84) (103,104)
(5,6) (25,26) (45,46) (65,66) (85,86) (105,106)(1,2) (13,14) (25,26) (37,38) (49,50) (61,62)
(5,6) (17,18) (29,30) (41,42) (53,54) (65,66)
(9,10) (21,22) (33,34) (45,46) (57,58) (69,70)
Upvotes: 2