将平面阵列重塑为复杂的特征类型
Reshaping flat array to complex Eigen type
如何将大小为 1×2N
的 data
重塑为 Eigen 中的复数形式,形成 P×Q
复数矩阵,具有 N
个复数,P×Q=N
?在data
中,实部和虚部紧挨着。我想动态重塑 data
因为数据可以有不同的大小。我正在尝试防止复制并将数据映射到复杂类型。
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
// ]
我尝试先将data
转换为复杂的Eigen数组(最终转换为MatrixXcf
),这也不起作用:
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.
Eigen::Map
中的 stride
有帮助吗?
最简单的解决方案是遍历所有元素并将data
转换为std::complex<float> *datac = new std::complex<float>[N];
的数组。我想知道 Eigen 是否可以将 data
映射到 datac
。提前致谢。
这里是 MCVE 答案 (online example),还有一些额外的例子,说明如何使用步幅来获得不同的结果:
#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;
}
输出为:
(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)
如何将大小为 1×2N
的 data
重塑为 Eigen 中的复数形式,形成 P×Q
复数矩阵,具有 N
个复数,P×Q=N
?在data
中,实部和虚部紧挨着。我想动态重塑 data
因为数据可以有不同的大小。我正在尝试防止复制并将数据映射到复杂类型。
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
// ]
我尝试先将data
转换为复杂的Eigen数组(最终转换为MatrixXcf
),这也不起作用:
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.
Eigen::Map
中的 stride
有帮助吗?
最简单的解决方案是遍历所有元素并将data
转换为std::complex<float> *datac = new std::complex<float>[N];
的数组。我想知道 Eigen 是否可以将 data
映射到 datac
。提前致谢。
这里是 MCVE 答案 (online example),还有一些额外的例子,说明如何使用步幅来获得不同的结果:
#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;
}
输出为:
(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)