gpt4 book ai didi

c++ - Eigen:将实数和虚数 MatrixXd 映射到 MatrixXcd

转载 作者:行者123 更新时间:2023-11-30 03:15:35 28 4
gpt4 key购买 nike

我正在处理一些大型数据集,其中复数矩阵的实部和虚部分别存储在一个文件中,我想从此类数据创建一个 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 数据 block 用了 22 秒。

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

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

有什么建议吗?

最佳答案

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

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

还要确保使用编译器优化进行基准测试,初始版本的 22s 似乎太多了。

关于c++ - Eigen:将实数和虚数 MatrixXd 映射到 MatrixXcd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927720/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com