本文介绍了如何在Numpy(或其他Python解决方案)的外部产品中利用对称性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们要计算向量与自身的外积:

Suppose that we want to compute the outer product of a vector with itself:

import numpy as np

a = np.asarray([1, 1.5, 2, 2.5, 3])
A = np.outer(a, a)

print(A)

其结果是:

[[ 1.    1.5   2.    2.5   3.  ]
 [ 1.5   2.25  3.    3.75  4.5 ]
 [ 2.    3.    4.    5.    6.  ]
 [ 2.5   3.75  5.    6.25  7.5 ]
 [ 3.    4.5   6.    7.5   9.  ]]

这将导致一个对称矩阵.可以通过仅计算矩阵的一个三角形,然后填充三角形中相应条目的其余条目来利用外部乘积中两个向量相同这一事实的先验知识.

This results in a symmetric matrix. Prior knowledge of the fact that the two vectors in the outer product are the same could be exploited by only computing one triangle of the matrix, and filling in the remaining entries from the corresponding entries in the triangle.

问题:是否有任何简单的方法来利用numpy(或Python中的其他解决方案)中的此类知识?当然,用Python编写自定义解决方案并不难,但是如果以不使用可靠的BLAS为代价,那就不值得.

Question: are there any easy ways to exploit such knowledge in numpy (or other solutions in Python)? Of course, it wouldn't be too difficult to write a custom solution in Python, but if that comes at the cost of not using a solid BLAS it would be unlikely to be worth it.

我主要关心计算时间,而不必关心RAM使用情况.

I am primarily concerned with computation time, not so much necessarily with RAM usage.

推荐答案

如果您只能处理在较低三角形中才有效的结果,则此Numba解决方案是我提出的最佳解决方案:

If you can deal with the result only being valid in the lower triangle, this Numba solution is the best I've come up with:

import numba

@numba.njit
def outer(arr):
    n = len(arr)
    res = np.empty((n,n), arr.dtype)
    for ii in range(n):
        for jj in range(ii+1):
            res[ii,jj] = arr[ii] * arr[jj]
    return res

对于大型矢量,它的速度是np.outer()的两倍,对于小型矢量,它的速度是其五倍.如果您需要完全填充的解决方案,则可以在内部循环中设置res[jj,ii] = res[ii,jj],它仍然比np.outer()快一些.

It is twice as fast as np.outer() for large vectors, and five times as fast for small ones. If you need the fully populated solution you can set res[jj,ii] = res[ii,jj] in the inner loop and it is still somewhat faster than np.outer().

由于某些原因,对于较小的向量,np.multiply.outer()np.outer()更快(对于较大的向量,它并不慢).

For some reason, np.multiply.outer() is faster for small vectors than np.outer() (and no slower for large vectors).

这篇关于如何在Numpy(或其他Python解决方案)的外部产品中利用对称性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 16:01