结果不正确:映射C++阵列到特征矩阵

发布于 2025-02-12 10:17:20 字数 2726 浏览 0 评论 0原文

我已经使用了以前使用的地图功能将现有内存映射到特征矩阵中,但是,当尝试映射fftw c ++数组时,我的结果几乎是错误的,如果将数组的部分(切片?)映射到特征矩阵中。而不是整个内存块。这是我正在使用的代码:

static const int nx = 10;
static const int ny = 10; 
static const int nyk = ny/2 + 1;
static const int nxk = nx/2 + 1;
static const int ncomp = 2;

fftw_complex *uhk; // this is pert Te
uhk= (fftw_complex*) fftw_malloc((((nx)*(ny+1))*nyk)* sizeof(fftw_complex)); 
memset(uhk, 42, (((nx))*nyk)* sizeof(fftw_complex));


    for (int i = 0; i < nx; i++){
        for (int j = 0; j < nyk; j++){
            for (int k = 0; k < ncomp; k++){
                uhk[i + nyk*j][k] = //taking fft of some expression
        }

    }
    

Eigen::Map<Eigen::MatrixXcd, Eigen::Unaligned> uhOut(reinterpret_cast<std::complex<double>*>(uhkOut),nyk,nx);

 std::cout << uhOut<< '\n';

我要获得的结果是,

 (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00) (0.58,-0.28) (0.95,-0.46) (0.95,-0.46) (0.58,-0.28) (0.00,-0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00) (0.59,-0.09) (0.95,-0.14) (0.95,-0.14) (0.59,-0.09) (0.00,-0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)

但是,正确的结果应该是:

     (0.00,0.00)     (5.14,0.00)     (8.32,0.00)     (8.32,0.00)     (5.14,0.00)     (0.00,0.00)    (-5.14,0.00)    (-8.32,0.00)    (-8.32,0.00)    (-5.14,0.00)
     (0.00,0.00)    (2.43,-2.35)    (3.93,-3.81)    (3.93,-3.81)    (2.43,-2.35)    (0.00,-0.00)    (-2.43,2.35)    (-3.93,3.81)    (-3.93,3.81)    (-2.43,2.35)
     (0.00,0.00)    (0.52,-1.04)    (0.85,-1.68)    (0.85,-1.68)    (0.52,-1.04)    (0.00,-0.00)    (-0.52,1.04)    (-0.85,1.68)    (-0.85,1.68)    (-0.52,1.04)
     (0.00,0.00)    (0.57,-0.55)    (0.93,-0.89)    (0.93,-0.89)    (0.57,-0.55)    (0.00,-0.00)    (-0.57,0.55)    (-0.93,0.89)    (-0.93,0.89)    (-0.57,0.55)
     (0.00,0.00)    (0.58,-0.28)    (0.95,-0.46)    (0.95,-0.46)    (0.58,-0.28)    (0.00,-0.00)    (-0.58,0.28)    (-0.95,0.46)    (-0.95,0.46)    (-0.58,0.28)
     (0.00,0.00)    (0.59,-0.09)    (0.95,-0.14)    (0.95,-0.14)    (0.59,-0.09)    (0.00,-0.00)    (-0.59,0.09)    (-0.95,0.14)    (-0.95,0.14)    (-0.59,0.09)

我在这里误解了MAP的点吗?为什么我的结果切成薄片?

I have used the map feature before to map existing memory into Eigen matrices, however when trying to map an fftw c++ array, I am getting wrong results, almost if a portion (a slice?) of the array is being mapped into an Eigen matrix and not the entire memory block. This is the code I am using:

static const int nx = 10;
static const int ny = 10; 
static const int nyk = ny/2 + 1;
static const int nxk = nx/2 + 1;
static const int ncomp = 2;

fftw_complex *uhk; // this is pert Te
uhk= (fftw_complex*) fftw_malloc((((nx)*(ny+1))*nyk)* sizeof(fftw_complex)); 
memset(uhk, 42, (((nx))*nyk)* sizeof(fftw_complex));


    for (int i = 0; i < nx; i++){
        for (int j = 0; j < nyk; j++){
            for (int k = 0; k < ncomp; k++){
                uhk[i + nyk*j][k] = //taking fft of some expression
        }

    }
    

Eigen::Map<Eigen::MatrixXcd, Eigen::Unaligned> uhOut(reinterpret_cast<std::complex<double>*>(uhkOut),nyk,nx);

 std::cout << uhOut<< '\n';

The results I am getting are,

 (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00) (0.58,-0.28) (0.95,-0.46) (0.95,-0.46) (0.58,-0.28) (0.00,-0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)
 (0.00,0.00) (0.59,-0.09) (0.95,-0.14) (0.95,-0.14) (0.59,-0.09) (0.00,-0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)  (0.00,0.00)

But, the correct result should be:

     (0.00,0.00)     (5.14,0.00)     (8.32,0.00)     (8.32,0.00)     (5.14,0.00)     (0.00,0.00)    (-5.14,0.00)    (-8.32,0.00)    (-8.32,0.00)    (-5.14,0.00)
     (0.00,0.00)    (2.43,-2.35)    (3.93,-3.81)    (3.93,-3.81)    (2.43,-2.35)    (0.00,-0.00)    (-2.43,2.35)    (-3.93,3.81)    (-3.93,3.81)    (-2.43,2.35)
     (0.00,0.00)    (0.52,-1.04)    (0.85,-1.68)    (0.85,-1.68)    (0.52,-1.04)    (0.00,-0.00)    (-0.52,1.04)    (-0.85,1.68)    (-0.85,1.68)    (-0.52,1.04)
     (0.00,0.00)    (0.57,-0.55)    (0.93,-0.89)    (0.93,-0.89)    (0.57,-0.55)    (0.00,-0.00)    (-0.57,0.55)    (-0.93,0.89)    (-0.93,0.89)    (-0.57,0.55)
     (0.00,0.00)    (0.58,-0.28)    (0.95,-0.46)    (0.95,-0.46)    (0.58,-0.28)    (0.00,-0.00)    (-0.58,0.28)    (-0.95,0.46)    (-0.95,0.46)    (-0.58,0.28)
     (0.00,0.00)    (0.59,-0.09)    (0.95,-0.14)    (0.95,-0.14)    (0.59,-0.09)    (0.00,-0.00)    (-0.59,0.09)    (-0.95,0.14)    (-0.95,0.14)    (-0.59,0.09)

Am I misunderstanding the point of map here? Why are my results sliced like that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

且行且努力 2025-02-19 10:17:20

您正在计算错误的偏移。 [I+NYK*J]不正确。 J的最大价值是(NYK-1)。我认为您的意思是要成为[i*njk+j]

为了诊断这种问题,我建议做

uhk [J + NYK*i] [k] =(k == 0)?i:j; //注意我在偏移量中交换了I/J

这将创建一个矩阵,其中实际部分与列号匹配,而虚构部分是行号。这样的简单模式使很明显这些值无法正确编写预期的内存位置。

You're computing your offsets wrong. [i+nyk*j] is not correct. The largest value of j is (nyk-1). I think you meant for it to be [i*njk+j].

To diagnose this sort of problem, I suggest doing

uhk[j + nyk*i][k] = (k==0)?i:j; // Note I swapped i/j in the offsets

This will create a matrix where the real portions match the column number, and the imaginary portions are the row number. A simple pattern like this makes it obvious the values aren't being correctly written the expected memory locations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文