Monday 13 January 2020

Efficient way to perform a triangular sum with .sum over a matrix F(a[i], a[j]), given the vector a

I have a vector a and need to perform a summation over two indices, something as

for i in (range, n): 
    for j in (i+1, n):
        F(a[i] - a[j])

where F is a function: the sum is reminding of summing over the upper triangle of an array.

I read the interesting thread on Fastest way in numpy to sum over upper triangular elements with the least memory and did my trials: indeed ARRAY.sum is a very fast way to sum over the elements of an upper triangular matrix.

To apply the method to my case, I first need to define an array such as

A[i,j] = F(a[i],a[j])

and then compute

(A.sum() - np.diag(A).sum())/2

I could define the array A via two for loops of course, but I wondering if there is a faster, numpy way.

In another case, the function F was simply equal to

F = a[i]*a[j]

and I could write

def sum_upper_triangular(vector):
    A = np.tensordot(vector,vector,0)
    return (A.sum() - np.diag(A).sum())/2

which is incredibly faster than summing directly with sum(), or nested for loops.

If F is more articulated, for example

np.exp(a[i] - a[j])

I would like to know what the most efficient way.

Thanks a lot



from Efficient way to perform a triangular sum with .sum over a matrix F(a[i], a[j]), given the vector a

No comments:

Post a Comment