复数矩阵乘法:Eigen与Matlab的比较

3

请问为什么结果不同?

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的double类型变量。
1个回答

6

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

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

testTest*testTest.'

结果应该相同。

如果您想在 Eigen 中进行复杂的转置,则可以使用 matrix.adjoint()(或 matrix.conjugate().transpose())。


谢谢您快速而准确的回答。我更专注于在C中实现它,所以除了.transpose()之外,我还需要在我的MatrixXcd上调用.conjugate()吗?(我会接受您的答案,但我不能在6分钟内) - F1sher
我在答案中添加了一行来说明如何使Eigen版本匹配。你是对的 - 你可以添加.conjugate(),但如果你使用.adjoint(),它会一次完成两个操作。 - DavidW
谢谢,这是一个很小的问题,但我因为没有注意到Matlab中的.translate()不等于'而浪费了很多时间,所以一直认为问题出在其他地方。 - F1sher
4
@F1sher的意思是:Matlab用户中有98%不知道这一点。他们第一次意识到这一点,通常是在遇到像你一样的问题时。 - Robert Seifert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接