本征:将real和imag MatrixXd映射到MatrixXcd

时间:2019-07-08 02:28:48

标签: c++ eigen eigen3

我正在处理一些大型数据集,其中复杂矩阵的实部和imag部分分别存储在文件中,我想根据此类数据创建一个Eigen :: MatrixXcd:

   // read data, note that real and imag data are stored separately
   // and the file reader API only allow read them in continuous fashion. 
   MatrixXd buf(nrow, ncol * 2);
   get_realpart_from_file(buf.data(), nrow * ncol);
   get_imagpart_from_file(buf.data() + nrow * ncol, nrow * ncol);
   // the above takes about 4 sec for a ~2 GB data block.
   // now we have buf contains [<real data> ... <imag data>]
   // but we would like access the data as complex matrix
   ???? what to do there?

天真的方法是像下面这样复制数据:

   MatrixXd data;
   data = buf.block(0, 0, nrow, ncol) + buf.block(0, ncol, nrow, ncol) * std::complex<double>(0, 1);

但是对于2GB的数据块,这花费了22秒。

我想知道是否有更聪明的方法来做到这一点,类似于:

   Map<MatrixXcd, {some magic}> data(buf.data(), {some magic});
   // use data like a complex matrix

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

无论如何都需要复制数据,因为期望MatrixXcd是交错的实/复杂项。尽管如此,您可以通过以下方式避免使用昂贵的复杂产品:

MatrixXcd data(nrow,ncol);
data.real() = buf.leftCols(ncol);
data.imag() = buf.rightCols(ncol);

还要确保在编译器优化为ON的情况下进行基准测试,初始版本的22s似乎太多了。

相关问题