用numpy最快的方式对内存最少的上三角元素求和 [英] Fastest way in numpy to sum over upper triangular elements with the least memory
问题描述
我需要对对称矩阵执行类型i<j
的求和.这等于对矩阵的上三角元素求和,不包括对角线.
I need to perform a summation of the kind i<j
on symmetric matrices. This is equivalent to sum over the upper triangular elements of a matrix, diagonal excluded.
给出A
一个对称的N x N
数组,最简单的解决方案是np.triu(A,1).sum()
,但是我想知道是否存在更快的方法,它们需要更少的内存.
似乎在大型数组上(A.sum() - np.diag(A).sum())/2
更快,但是如何避免从np.diag
创建甚至N x 1
数组呢?
双重嵌套的for循环不需要额外的内存,但是显然这不是在Python中使用的方式.
Given A
a symmetric N x N
array, the simplest solution is np.triu(A,1).sum()
however I was wondering if faster methods exist that require less memory.
It seems that (A.sum() - np.diag(A).sum())/2
is faster on large array, but how to avoid creating even the N x 1
array from np.diag
?
A doubly nested for loop would require no additional memory, but it is clearly not the way to go in Python.
推荐答案
您可以将np.diag(A).sum()
替换为np.trace(A)
;这不会创建临时的Nx1
数组
You can replace np.diag(A).sum()
with np.trace(A)
; this will not create the temporary Nx1
array
这篇关于用numpy最快的方式对内存最少的上三角元素求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!