如何快速平滑 1000x1000 的数组

Posted

技术标签:

【中文标题】如何快速平滑 1000x1000 的数组【英文标题】:How to smooth an array 1000x1000 quickly 【发布时间】:2018-07-16 06:28:02 【问题描述】:

我们正在模拟法国海湾(圣米歇尔山)的沉积物,为此,我们将沉积物(以数字表示)放入数组中。 存款在我们的 1000x1000 阵列中是随机的,中间有一个岛。

在程序结束时,我们希望将我们的海湾除数字为零的岛屿之外的所有地方都进行平滑处理。

我们做到了,但是运行时间太长了。有人可以帮助我们找到一种方法来加速我们的程序吗?

for j in range (0,375):
    for decale in range (250): 

        diff=M[749+decale][j]-M[749+decale+1][j] 
        if diff>=3: 
            M[749+decale][j]-=floor(19*diff/20) 
            M[749+decale+1][j]+=floor(19*diff/20)

for j in range (625,1000):  #for the S-W of the bay
    for decale in range (250): 

        diff=M[749+decale][j]-M[749+decale+1][j] 
        if diff>=3:
            M[749+decale][j]-=floor(19*diff/20) 
            M[749+decale+1][j]+=floor(19*diff/20) 

for i in range (800,1000):


    for decale in range (799): For the West of the bay

        diff=M[i][200+decale]-M[i][200+decale+1]
        if diff>=3: 
            M[i][200+decale]-=floor(19*diff/20) 
            M[i][200+decale+1]+=floor(19*diff/20)

    for decale in range (799): #Idem

        diff=M[i][799-decale]-M[i][799-decale-1] 
        if diff>=3: 
            M[i][799-decale]-=floor(19*diff/20) 
            M[i][799-decale-1]+=floor(19*diff/20) 

for j in range (0,375): #Along the island
    for decale in range (350):
        diff=M[850-decale][j]-M[850-decale-1][j] 
        if diff>=3: 
            M[850-decale][j]-=floor(19*diff/20)
            M[850-decale-1][j]+=floor(19*diff/20) 

for j in range(625,1000): #Idem
    for decale in range (350):
        diff=M[850-decale][j]-M[850-decale-1][j] 
        if diff>=3: 
            M[850-decale][j]-=floor(19*diff/20) 
            M[850-decale-1][j]+=floor(19*diff/20)

for i in range (500,549): #Idem
    for decale in range (500): 

        diff=M[i][499+decale]-M[i][499+decale+1] 
        if diff>=3:
            M[i][499+decale]-=floor(19*diff/20) 
            M[i][499+decale+1]+=floor(19*diff/20) 

    for decale in range (500):  #Idem

        diff=M[i][500-decale]-M[i][500-decale-1] 
        if diff>=3: 
            M[i][500-decale]-=floor(19*diff/20)   
            M[i][500-decale-1]+=floor(19*diff/20) 

return M

【问题讨论】:

在您的应用程序上下文中,smooth 是什么意思? 有没有机会考虑使用 Numpy? 【参考方案1】:

我不能说这些循环的正确性。我只是将它们翻译成 Numpy 数组,所以它可以更具可读性。

import numpy as np

M = np.array(M)

diff = M[749:-1,:375]-M[750:,:375]
M[749:-1,:375][diff>=3] -= np.floor(19*diff[diff>=3]/20)
M[750:  ,:375][diff>=3] += np.floor(19*diff[diff>=3]/20)

diff = M[749:-1,625:]-M[750:,625:] # for the S-W of the bay
M[749:-1,625:][diff>=3] -= np.floor(19*diff[diff>=3]/20)
M[750:  ,625:][diff>=3] += np.floor(19*diff[diff>=3]/20)

diff = M[800:,200:-1]-M[800:,201:] # for the West of the bay
M[800:,200:-1][diff>=3] -= np.floor(19*diff[diff>=3]/20)
M[800:,201:  ][diff>=3] += np.floor(19*diff[diff>=3]/20)

diff = M[800:,1:800]-M[800:,:799] #Idem 
M[800:,1:800][diff>=3] -= np.floor(19*diff[diff>=3]/20)
M[800:, :799][diff>=3] += np.floor(19*diff[diff>=3]/20)

diff = M[501:851,:375]-M[500:850,:375] #Along the island
M[501:851,:375][diff>=3] -= np.floor(19*diff[diff>=3]/20)
M[500:850,:375][diff>=3] += np.floor(19*diff[diff>=3]/20)

diff = M[501:851,625:]-M[500:850,625:] #Idem
M[501:851,625:][diff>=3] -= np.floor(19*diff[diff>=3]/20)
M[500:850,625:][diff>=3] += np.floor(19*diff[diff>=3]/20)

diff = M[500:549,499:-2]-M[500:549,500:-1] #Idem
M[500:549,499:-2][diff>=3] -= np.floor(19*diff[diff>=3]/20)
M[500:549,500:-1][diff>=3] += np.floor(19*diff[diff>=3]/20)

diff = M[500:549,1:501]-M[500:549,:500] #Idem
M[500:549,1:501][diff>=3] -= np.floor(19*diff[diff>=3]/20)
M[500:549,:500][diff>=3] += np.floor(19*diff[diff>=3]/20)

【讨论】:

以上是关于如何快速平滑 1000x1000 的数组的主要内容,如果未能解决你的问题,请参考以下文章

排序算法杂谈 —— 量化数组的有序程度

numpy数据平滑

在 MPI C++ 中传递大型二维数组

快速平滑圆角

如何快速解析数组的json数组

如何更快速地转置数组?