I have two sympy matrices, U
and B
:
我有两个同情矩阵,U和B:
>> U
<< Matrix([
[1.0, 0, 0, 0],
[ 0, 1.0, 0, 0],
[ 0, 0, 1.0, 0],
[ 0, 0, 0, 1.0]])
>> B
<< Matrix([
[sqrt(2)/2, 0.5*sqrt(2)*I, 0, 0],
[ 0, 0, 0.5*sqrt(2)*I, sqrt(2)/2],
[ 0, 0, 0.5*sqrt(2)*I, -sqrt(2)/2],
[sqrt(2)/2, -0.5*sqrt(2)*I, 0, 0]])
Applying dot product to them results in a list, instead of a 4x4 matrix:
将点积应用于它们会产生一个列表,而不是4x4矩阵:
>> U.dot(B)
<< [0.5*sqrt(2),
0,
0,
0.5*sqrt(2),
0.5*sqrt(2)*I,
0,
0,
-0.5*sqrt(2)*I,
0,
0.5*sqrt(2)*I,
0.5*sqrt(2)*I,
0,
0,
0.5*sqrt(2),
-0.5*sqrt(2),
0]
In contrast, numpy looks like doing the right thing:
相比之下,numpy看起来像做正确的事情:
>> numpy.dot(sympy.matrix2numpy(U),sympy.matrix2numpy(B))
<< array([[0.5*sqrt(2), 0.5*sqrt(2)*I, 0, 0],
[0, 0, 0.5*sqrt(2)*I, 0.5*sqrt(2)],
[0, 0, 0.5*sqrt(2)*I, -0.5*sqrt(2)],
[0.5*sqrt(2), -0.5*sqrt(2)*I, 0, 0]], dtype=object)
What am I doing wrong? Is this the expected behavior?
我究竟做错了什么?这是预期的行为吗?
2 个解决方案
#1
1
Unlike NumPy, in SymPy, dot
means the dot product of vectors. It's designed so that you can get the dot product of two row or column vectors without having to worry about using .T
, but perhaps the laxness in shape is a bit much here, as it's literally taking the dot product of list(U)
and list(B)
.
与NumPy不同,在SymPy中,dot表示向量的点积。它的设计使你可以得到两个行或列向量的点积,而不必担心使用.T,但也许这里的形状松弛有点多,因为它实际上采用列表(U)的点积和列表(B)。
It is probably being too lax here. I've opened a SymPy issue for this. It would be more correct for SymPy to raise an exception in this case.
这可能太松懈了。我为此打开了一个SymPy问题。在这种情况下,SymPy引发异常更为正确。
As @Wrzlprmft correctly pointed out, SymPy uses *
to multiply matrices (or @
if you are using Python 3.5 or greater).
正如@Wrzlprmft正确指出的那样,SymPy使用*来乘以矩阵(如果使用的是Python 3.5或更高版本,则使用@)。
#2
1
SymPy uses the *
operation for the multiplication of matrices, i.e., you want to use:
SymPy使用*运算来表示矩阵的乘法,即你想使用:
U*B
# Matrix([
# [0.5*sqrt(2), 0.5*sqrt(2)*I, 0, 0],
# [ 0, 0, 0.5*sqrt(2)*I, 0.5*sqrt(2)],
# [ 0, 0, 0.5*sqrt(2)*I, -0.5*sqrt(2)],
# [0.5*sqrt(2), -0.5*sqrt(2)*I, 0, 0]])
As you can see, the elements are the same as your list, but with the desired structure.
如您所见,元素与列表相同,但具有所需的结构。
#1
1
Unlike NumPy, in SymPy, dot
means the dot product of vectors. It's designed so that you can get the dot product of two row or column vectors without having to worry about using .T
, but perhaps the laxness in shape is a bit much here, as it's literally taking the dot product of list(U)
and list(B)
.
与NumPy不同,在SymPy中,dot表示向量的点积。它的设计使你可以得到两个行或列向量的点积,而不必担心使用.T,但也许这里的形状松弛有点多,因为它实际上采用列表(U)的点积和列表(B)。
It is probably being too lax here. I've opened a SymPy issue for this. It would be more correct for SymPy to raise an exception in this case.
这可能太松懈了。我为此打开了一个SymPy问题。在这种情况下,SymPy引发异常更为正确。
As @Wrzlprmft correctly pointed out, SymPy uses *
to multiply matrices (or @
if you are using Python 3.5 or greater).
正如@Wrzlprmft正确指出的那样,SymPy使用*来乘以矩阵(如果使用的是Python 3.5或更高版本,则使用@)。
#2
1
SymPy uses the *
operation for the multiplication of matrices, i.e., you want to use:
SymPy使用*运算来表示矩阵的乘法,即你想使用:
U*B
# Matrix([
# [0.5*sqrt(2), 0.5*sqrt(2)*I, 0, 0],
# [ 0, 0, 0.5*sqrt(2)*I, 0.5*sqrt(2)],
# [ 0, 0, 0.5*sqrt(2)*I, -0.5*sqrt(2)],
# [0.5*sqrt(2), -0.5*sqrt(2)*I, 0, 0]])
As you can see, the elements are the same as your list, but with the desired structure.
如您所见,元素与列表相同,但具有所需的结构。