双 for 循环的 Python 方式

问题描述 投票:0回答:2

我有以下代码:

import numpy as np
epsilon = np.array([[0.        , 0.00172667, 0.00071437, 0.00091779, 0.00154501],
           [0.00128983, 0.        , 0.00028139, 0.00215905, 0.00094862],
           [0.00035811, 0.00018714, 0.        , 0.00029365, 0.00036993],
           [0.00035631, 0.00112175, 0.00022906, 0.        , 0.00291149],
           [0.00021527, 0.00017653, 0.00010341, 0.00104458, 0.        ]])
Sii = np.array([19998169., 14998140.,  9997923.,  7798321.,  2797958.])
n = len(Sii)

epsilonijSjj = np.zeros((n,n))
for i in range(n):
    for j in range(n):
        epsilonijSjj[i,j] = epsilon[i][j]*Sii[j]

print (epsilonijSjj)

如何避免双重 for 循环并以快速的 Python 方式编写代码?

提前谢谢您

python numpy performance for-loop vectorization
2个回答
1
投票

Numpy 允许您直接将 2 个数组相乘。

因此,您不必定义一个基于

0
的数组并用另一个数组的更改后的元素填充它,您可以简单地创建另一个数组的副本并直接应用乘法,如下所示:

import numpy as np

epsilon = np.array([[0.        , 0.00172667, 0.00071437, 0.00091779, 0.00154501],
           [0.00128983, 0.        , 0.00028139, 0.00215905, 0.00094862],
           [0.00035811, 0.00018714, 0.        , 0.00029365, 0.00036993],
           [0.00035631, 0.00112175, 0.00022906, 0.        , 0.00291149],
           [0.00021527, 0.00017653, 0.00010341, 0.00104458, 0.        ]])
Sii = np.array([19998169., 14998140.,  9997923.,  7798321.,  2797958.])

epsilonijSjj = epsilon.copy()
epsilonijSjj *= Sii

print(epsilonijSjj)

输出:

[[    0.         25896.8383938   7142.21625351  7157.22103059
   4322.87308958]
 [25794.23832127     0.          2813.31555297 16836.96495505
   2654.19891796]
 [ 7161.54430059  2806.7519196      0.          2289.97696165
   1035.04860294]
 [ 7125.54759639 16824.163545    2290.12424238     0.
   8146.22673742]
 [ 4305.00584063  2647.6216542   1033.88521743  8145.97015018
      0.        ]]

或者,只需这样做,这样更快,因为它不需要创建数组的副本:

import numpy as np

epsilon = np.array([[0.        , 0.00172667, 0.00071437, 0.00091779, 0.00154501],
           [0.00128983, 0.        , 0.00028139, 0.00215905, 0.00094862],
           [0.00035811, 0.00018714, 0.        , 0.00029365, 0.00036993],
           [0.00035631, 0.00112175, 0.00022906, 0.        , 0.00291149],
           [0.00021527, 0.00017653, 0.00010341, 0.00104458, 0.        ]])
Sii = np.array([19998169., 14998140.,  9997923.,  7798321.,  2797958.])

epsilonijSjj = epsilon * Sii

0
投票
import numpy as np
epsilon = np.array([[0.        , 0.00172667, 0.00071437, 0.00091779, 0.00154501],
           [0.00128983, 0.        , 0.00028139, 0.00215905, 0.00094862],
           [0.00035811, 0.00018714, 0.        , 0.00029365, 0.00036993],
           [0.00035631, 0.00112175, 0.00022906, 0.        , 0.00291149],
           [0.00021527, 0.00017653, 0.00010341, 0.00104458, 0.        ]])
Sii = np.array([19998169., 14998140.,  9997923.,  7798321.,  2797958.])
n = len(Sii)

# Create the diagonal matrix
Sii_diag = np.diag(Sii)

# Perform matrix multiplication
result = epsilon @ Sii_diag
print(result)

'''
[[    0.         25896.8383938   7142.21625351  7157.22103059
   4322.87308958]
 [25794.23832127     0.          2813.31555297 16836.96495505
   2654.19891796]
 [ 7161.54430059  2806.7519196      0.          2289.97696165
   1035.04860294]
 [ 7125.54759639 16824.163545    2290.12424238     0.
   8146.22673742]
 [ 4305.00584063  2647.6216542   1033.88521743  8145.97015018
      0.        ]]

'''
© www.soinside.com 2019 - 2024. All rights reserved.