小编Cra*_*opi的帖子

在numpy中没有临时性的外部产品的非平凡总和

我希望解决的实际问题是,给定一组N个单位矢量和另一组M个矢量,为每个单位矢量计算它的点积的绝对值与M个矢量中的每一个的平均值.本质上,这是计算两个矩阵的外积并求和并求平均值的绝对值.

对于NM不太大,这并不难,并且有很多方法可以继续(见下文).问题是当NM很大时,所创建的临时数量巨大并且对所提供的方法提供实际限制.这个计算可以在不创建临时工的情况下完成吗?我遇到的主要困难是由于存在绝对值.是否存在"线程化"此类计算的一般技术?

作为示例,请考虑以下代码

N = 7
M = 5

# Create the unit vectors, just so we have some examples,
# this is not meant to be elegant
phi = np.random.rand(N)*2*np.pi
ctheta = np.random.rand(N)*2 - 1
stheta = np.sqrt(1-ctheta**2)
nhat = np.array([stheta*np.cos(phi), stheta*np.sin(phi), ctheta]).T

# Create the other vectors
m = np.random.rand(M,3)

# Calculate the quantity we desire, here using broadcasting.
S = np.average(np.abs(np.sum(nhat*m[:,np.newaxis,:], axis=-1)), axis=0) …
Run Code Online (Sandbox Code Playgroud)

python optimization numpy

7
推荐指数
1
解决办法
376
查看次数

标签 统计

numpy ×1

optimization ×1

python ×1