gpt4 book ai didi

python - 使用 numexpr 优化 numpy 中的数值运算

转载 作者:行者123 更新时间:2023-12-01 09:27:04 24 4
gpt4 key购买 nike

我刚刚开始使用 numexpr,虽然 github 存储库似乎有一些如何使用它的基本示例,但我无法清楚地理解它们如何应用于某些复杂的情况。假设我有一个函数:

def func(x):
#x is a numpy array of very large size
return 0.5*np.exp(-x**2)*x**(1/3)+np.exp(-x)*(1.5*np.sqrt(x)+0.3/(1.+x))

使用 numexpr 编写此代码的等效方法是什么?

最佳答案

np.sqrtsqrt; np.expexp

import numexpr as ne
y = ne.evaluate(
'.5 * exp(-x ** 2) * x ** (1 / 3)'
'+ exp(-x) * (1.5 * sqrt(x)'
'+ .3 / (1. + x))'
)

关于python - 使用 numexpr 优化 numpy 中的数值运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50298590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com