c – 复数矩阵乘法Eigen vs Matlab

有人可以向我解释为什么结果会有所不同.

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.

解决方法:

‘在Matlab中进行转置并采用复共轭(http://uk.mathworks.com/help/matlab/ref/ctranspose.html).如果你想只做转置使用.’ (前面有一个点).

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

testTest*testTest.'

结果应该是一样的.

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

上一篇:c – 单精度矩阵运算的特征性能AVX与SSE没有差异?


下一篇:c – 用于矩阵向量cwiseProduct操作的Eigen3 replicate()