简体   繁体   English

使用 Eigen 的复矩阵矩阵乘法

[英]Complex Matrix matrix multiplication using Eigen

I am doing complex matrix matrix product as shown in the code here.我正在做复杂的矩阵矩阵乘积,如此处的代码所示。 The Eigen * operator doesn't seem to give the right result. Eigen * 运算符似乎没有给出正确的结果。 To verify I have also written another routine to find the product.为了验证我还编写了另一个程序来查找产品。 From this it was seen that the imaginary part of the result was fine but the real part is inaccurate.由此可以看出,结果的虚部很好,但实部不准确。 Can someone let me know why?有人可以让我知道为什么吗?

Eigen::MatrixXcd U2 = Eigen::MatrixXcd::Random(N1, computed_rank);
Eigen::MatrixXcd V2 = Eigen::MatrixXcd::Random(computed_rank, N2);
Eigen::MatrixXcd W2 = Mat::Zero(N1,N2);
for (size_t k = 0; k < computed_rank; k++) {
    W2 += U2.col(k)*V2.row(k);
}
Eigen::MatrixXcd Q = U2*V2;
Eigen::MatrixXcd E2 = Q-W2;
cout << "Err Total: " << E2.norm()/W2.norm() << endl;
cout << "Err Real: " << E2.real().norm()/W2.real().norm() << endl;
cout << "Err Imag: " << E2.imag().norm()/W2.imag().norm() << endl;

which gave the result:结果如下:

Err Total: 0.84969
Err Real: 1.17859
Err Imag: 1.18274e-16

I have observed that this problem didn't occur when this was the only piece of code in the project.我观察到当这是项目中唯一的一段代码时,这个问题没有发生。 But I have this as part of a bigger project, where it seems to fail.但我把它作为一个更大项目的一部分,它似乎失败了。

It turns out that -ffast-math optimization flag of GNU is not compatible with all programs.事实证明,GNU 的 -ffast-math 优化标志并非与所有程序兼容。 I have used it while compiling the bigger project and not while doing the one that is in isolation.我在编译更大的项目时使用了它,而不是在做一个孤立的项目时使用它。 This is the reason it failed!这就是失败的原因! For more details refer https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html .有关更多详细信息,请参阅https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Below is a minimum reproducible example: Filename is test.cpp下面是一个最小的可重现示例:文件名是test.cpp

#include<iostream>
#include<Eigen/Dense>

using namespace std;

int main() {
    int N1 = 12;
    int N2 = 12;
    int computed_rank = 5;
    Eigen::MatrixXcd U2 = Eigen::MatrixXcd::Random(N1, computed_rank);
    Eigen::MatrixXcd V2 = Eigen::MatrixXcd::Random(computed_rank, N2);
    Eigen::MatrixXcd W2 = Eigen::MatrixXcd::Zero(N1,N2);
    for (size_t k = 0; k < computed_rank; k++) {
        W2 += U2.col(k)*V2.row(k);
    }
    Eigen::MatrixXcd Q = U2*V2;
    Eigen::MatrixXcd E2 = Q-W2;
    cout << "Err Total: " << E2.norm()/W2.norm() << endl;
    cout << "Err Real: " << E2.real().norm()/W2.real().norm() << endl;
    cout << "Err Imag: " << E2.imag().norm()/W2.imag().norm() << endl;
}

Compiling the above with -ffast-math flag produces the following output.使用-ffast-math标志编译上述内容会产生以下输出。

g++-9 -O4 -ffast-math test.cpp

./a.out

Err Total: 0.949251
Err Real: 1.41443
Err Imag: 1.35749e-16

Compiling the above without -ffast-math flag produces the following output.没有-ffast-math标志的情况下编译上述内容会产生以下输出。

g++-9 -O4 test.cpp

./a.out

Err Total: 1.35029e-16
Err Real: 1.35181e-16
Err Imag: 1.34903e-16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM