【问题标题】:Complex Number Matrix multiplication Eigen vs Matlab复数矩阵乘法特征与 Matlab
【发布时间】:2015-05-26 06:30:50
【问题描述】:

有人可以向我解释为什么结果不同。

C++ 代码:

MatrixXcd testTest;
testTest.resize(3,3);
testTest.real()(0,0) = 1;
testTest.real()(0,1) = 2;
testTest.real()(0,2) = 3;
testTest.real()(1,0) = 1;
testTest.real()(1,1) = 2;
testTest.real()(1,2) = 3;
testTest.real()(2,0) = 1;
testTest.real()(2,1) = 2;
testTest.real()(2,2) = 3;

testTest.imag()(0,0) = 1;
testTest.imag()(0,1) = 2;
testTest.imag()(0,2) = 3;
testTest.imag()(1,0) = 1;
testTest.imag()(1,1) = 2;
testTest.imag()(1,2) = 3;
testTest.imag()(2,0) = 1;
testTest.imag()(2,1) = 2;
testTest.imag()(2,2) = 3;

cout<< endl << testTest << endl;
cout<< endl << testTest.transpose() << endl;
cout<< endl << testTest*testTest.transpose() << endl;
cout<< endl << testTest << endl;

C++ 的结果:

(1,1) (2,2) (3,3)
(1,1) (2,2) (3,3)
(1,1) (2,2) (3,3)

(1,1) (1,1) (1,1)
(2,2) (2,2) (2,2)
(3,3) (3,3) (3,3)

(0,28) (0,28) (0,28)
(0,28) (0,28) (0,28)
(0,28) (0,28) (0,28)

(1,1) (2,2) (3,3)
(1,1) (2,2) (3,3)
(1,1) (2,2) (3,3)

和用 Matlab 写的一样:

testTest = [ complex(1,1) complex(2,2) complex(3,3); 
             complex(1,1) complex(2,2) complex(3,3); 
             complex(1,1) complex(2,2) complex(3,3)];

testTest
testTest'
testTest*testTest'
testTest

Matlab 结果:

testTest =

1.0000 + 1.0000i   2.0000 + 2.0000i   3.0000 + 3.0000i
1.0000 + 1.0000i   2.0000 + 2.0000i   3.0000 + 3.0000i
1.0000 + 1.0000i   2.0000 + 2.0000i   3.0000 + 3.0000i


ans =

1.0000 - 1.0000i   1.0000 - 1.0000i   1.0000 - 1.0000i
2.0000 - 2.0000i   2.0000 - 2.0000i   2.0000 - 2.0000i
3.0000 - 3.0000i   3.0000 - 3.0000i   3.0000 - 3.0000i


ans =

28    28    28
28    28    28
28    28    28


testTest =

1.0000 + 1.0000i   2.0000 + 2.0000i   3.0000 + 3.0000i
1.0000 + 1.0000i   2.0000 + 2.0000i   3.0000 + 3.0000i
1.0000 + 1.0000i   2.0000 + 2.0000i   3.0000 + 3.0000i

C 中 testTest * testTest' 的乘法返回返回复数,实部为 0,虚部为 28。Matlab 仅返回值为 28 的 dobule。

【问题讨论】:

    标签: c++ matlab eigen


    【解决方案1】:

    ' 在 Matlab 中进行转置并采用复共轭 (http://uk.mathworks.com/help/matlab/ref/ctranspose.html)。如果您只想进行转置,请使用.'(前面带有一个点)。

    因此,如果您将 MATLAB 测试更改为

    testTest*testTest.'
    

    结果应该是一样的。

    如果你想在特征中进行复杂的转置,那么你可以去matrix.adjoint()(或matrix.conjugate().transpose()

    【讨论】:

    • 感谢您快速正确的回答。我更专注于在 C 中实现它,所以除了 .transpose() 我还需要在我的 MatrixXcd 上调用 .conjugate() 吗? (会接受你的回答,但我不能在 6 分钟内)
    • 我在答案中添加了一行来说明如何让 Eigen 版本匹配。你是对的 - 你可以添加.conjugate(),但如果你使用.adjoint(),它会一次性完成。
    • 谢谢,这是一个小问题,我为此浪费了很多时间,因为我没有注意到 .translate() 在 Matlab 中不等于 ' 所以我认为问题出在其他地方。跨度>
    • @F1sher 98% 的 Matlab 用户不知道这一点。当他们遇到像你这样的问题时,他们第一次意识到这一点;)
    猜你喜欢
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多