NumPy转置出错

时间:2017-01-17 12:55:29

标签: python numpy matrix linear-algebra transpose

我正在试图弄清楚这里发生了什么,但我有点困惑。我使用转置的NumPy单位矩阵(应该没有效果)得到意想不到的结果。例如:

import numpy as np

N = 1000

# case 1:
A = np.eye(N) # the identity matrix
At = A.T # it's transpose
print 'A == At: {}'.format(np.all(A==At)) # should be true
Ad = At.dot(A) # identity * identity = identity
print 'A == Ad: {}'.format(np.all(A==Ad)) # should also be true

输出:

A == At: True
A == Ad: False

这是不正确的,因为第二个语句应为true。现在,如果我们这样做:

import numpy as np

N = 1000

# case 2:
B = np.eye(N) # the identity matrix
Bt = np.copy(B.T) # it's transpose <==== added copy here
print 'B == Bt: {}'.format(np.all(B==Bt)) # should be true
Bd = Bt.dot(B) # identity * identity = identity
print 'B == Bd: {}'.format(np.all(B==Bd)) # should also be true

输出:

B == Bt: True
B == Bd: True

这是期望的结果。唯一的区别是在第二种情况下添加了复制操作。另一个有趣的事情是如果我将N设置为较小的数字(比如100而不是1000),答案在两种情况下都是正确的。

发生了什么事?

(编辑:我正在使用Python 2.7.10和IPython 4.0.0在OS X 10.10.5上运行Numpy版本'1.11.1')

0 个答案:

没有答案