【发布时间】:2023-03-29 14:59:01
【问题描述】:
我正在使用标准方法在 Python 中使用 NumPy 创建对称矩阵/数组:
x = rand(500,500)
x = (x+x.T)
all(x==x.T)
> True
现在让我们聪明点:
x = rand(500,500)
x += x.T
all(x==x.T)
> False
等等,什么?
x==x.T
> array([[ True, True, True, ..., False, False, False],
[ True, True, True, ..., False, False, False],
[ True, True, True, ..., False, False, False],
...,
[False, False, False, ..., True, True, True],
[False, False, False, ..., True, True, True],
[False, False, False, ..., True, True, True]], dtype=bool)
左上段和右下段是对称的。如果我选择一个更小的数组呢?
x = rand(50,50)
x += x.T
all(x==x.T)
> True
好的....
x = rand(90,90)
x += x.T
all(x==x.T)
> True
x = rand(91,91)
x += x.T
all(x==x.T)
> False
为了确定……
x = rand(91,91)
x = (x+x.T)
all(x==x.T)
> True
这是一个错误,还是我要学习一些关于 += 和 NumPy 数组的疯狂知识?
【问题讨论】:
-
@AndrewJaffe 这是 Python 3.4.1 上的 Numpy 1.9,分布在 Anaconda 中。
-
@jeffalstott 是的,我误读了这个问题——我也看到了这种行为。