小编Ste*_*eve的帖子

这个python函数可以被矢量化吗?

我一直在研究这个函数,它生成了我正在开发的模拟代码所需的一些参数,并且已经在增强其性能方面遇到了障碍.

对代码进行分析表明,这是主要的瓶颈,因此我可以对其进行的任何增强都会很小.

我想尝试对这个函数的部分进行矢量化,但我不确定它是否可行.

主要的挑战是存储在我的数组中的参数params取决于params的索引.我看到的唯一直接的解决方案是使用np.ndenumerate,但这看起来很慢.

是否可以对这种类型的操作进行矢量化,其中存储在数组中的值取决于它们存储的位置?或者创建一个只给我数组索引的元组的生成器会更聪明/更快?

import numpy as np
from scipy.sparse import linalg as LA

def get_params(num_bonds, energies):
    """
    Returns the interaction parameters of different pairs of atoms.

    Parameters
    ----------
    num_bonds : ndarray, shape = (M, 20)
        Sparse array containing the number of nearest neighbor bonds for 
        different pairs of atoms (denoted by their column) and next-
        nearest neighbor bonds. Columns 0-9 contain nearest neighbors, 
        10-19 contain next-nearest neighbors

    energies : ndarray, shape = (M, ) …
Run Code Online (Sandbox Code Playgroud)

python performance numpy vectorization python-2.7

5
推荐指数
1
解决办法
112
查看次数

标签 统计

numpy ×1

performance ×1

python ×1

python-2.7 ×1

vectorization ×1