我有一个由元组的所有组合组成的列表,每个元素只能是-1或1。列表可以生成为:
N=2
list0 = [p for p in itertools.product([-1, 1], repeat=N)]
例如,如果元组有N=2
个元素:
list0 = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
因此,元组的总数是2^2=4
。你知道吗
如果元组有N=3
个元素:
list0 = [(-1, -1, -1), (-1, -1, 1), (-1, 1, -1), (-1, 1, 1), (1, -1, -1), (1, -1, 1), (1, 1, -1), (1, 1, 1)]
我关心的是:
现在我想得到列表中任意一对元组(包括元组本身的元组)之间的点积的所有结果。所以对于N=2
会有6(pairs) + 4(itself) = 10 combinations;
对于N=3
会有28(pairs) + 8(itself) = 36 combinations.
对于小型N
,我可以做如下操作:
for x in list0:
for y in list0:
print(np.dot(x,y))
但是,假设我已经有了list0,如果N很大,比如~50,那么计算所有点积可能性的最佳方法是什么?你知道吗
你可以坚持用numpy
您可以使用
np.dot
本身:输出
你要问的是
a
与自身的矩阵乘法,从documentation:请注意,最具pythonic解决方案是使用操作符
@
:输出
注意:代码是在Python3.5中运行的
相关问题 更多 >
编程相关推荐