我正在编写一个来自Matlab的Python脚本。碰巧在Matlab中,代码运行得更快(比少10分钟),所以也许你可以帮我弄清楚我做错了什么。我在Python中运行了所有会话,我发现最慢的是以下两个:
X = xi * math.cos(theta) + zi * (math.sin(theta))
Y = yi
Z = xi * ((-1) * math.sin(theta)) + zi * (math.cos(theta))
x2 = [a * b * c * X[i] / np.sqrt((a * c * Y[i])**2 + b**2 * (
c**2 * (X[i])**2 + a**2 * (Z[i])**2)) for i in range(len(X))]
y2 = [a * b * c * Y[i] / np.sqrt((a * c * Y[i])**2 + b**2 * (
c**2 * X[i]**2 + a**2 * Z[i]**2)) for i in range(len(X))]
zt = c**2 * (1 - (x2 / a)**2 - (y2 / b)**2)
i = 0
z2 = []
for i in range(len(Z)):
z2.append((-1 if Z[i] < 0 else 1) * np.sqrt(zt[i]))
其中xi,yi和zi是超过100万个元素的numpy浮点数
谢谢
答案 0 :(得分:1)
for for z2 part
In [1]: import numpy as np
In [2]: zz =np.array([1, 2, 3, -1, -2, -3, 0])
In [3]: aa = np.array([1, 2, 3, 4, 5, 6, 64])
In [4]: Z =np.array([1, 2, 3, -1, -2, -3, 0])
In [5]: zt = np.array([1, 2, 3, 4, 5, 6, 64])
In [6]: np.sign(Z) * np.sqrt(zt)
Out[7]:
array([ 1., 1.41421356, 1.73205081, -2., -2.23606798, -2.44948974, 0. ])