簡體   English   中英

用這些噪聲參數填充二維 numpy 數組的更快方法? 當前循環遍歷每個元素

[英]Faster way to fill 2d numpy array with these noise parameters? Currently looping over each element

是否有更快的方法來使用此處看到的相同算法(具有相同輸入參數的 pnoise3,特別是 i/scale j/scale)來填充 2d numpy 數組? self.world 是 np 數組,像這樣遍歷它非常大(2048,1024)。

for i in range(self.height):
    for j in range(self.width):

        self.world[i][j] = noise.pnoise3(i/self.noise['scale'], 
                                        j/self.noise['scale'], 
                                        SEED,
                                        octaves = self.noise['octaves'], 
                                        persistence = self.noise['persistence'], 
                                        lacunarity = self.noise['lacunarity'], 
                                        repeatx= self.width, 
                                        repeaty= self.height, 
                                        base= 0)

在學習了布爾索引之后,我能夠擺脫程序中其他地方的嵌套 for 循環,並且對它的效率如此之高感到驚訝。 以上是否有改進的余地?

我想過做類似self.world[self.world is not None] = noise.pnoise3(arg, arg, etc...)事情, self.world[self.world is not None] = noise.pnoise3(arg, arg, etc...)遞增的 i 和 j 值。 並將其設置為函數輸出是否意味着每個值都是相同的? 我還考慮過創建一個單獨的數組,然后將它們組合起來,但我仍然無法弄清楚如何在該場景中重現遞增的 i 和 j 值。

另外, used self.world[self.world is not None] ,我used self.world[self.world is not None]作為布爾索引的示例,該索引將對所有內容返回 true,但我想這不是做我想做的最好的方法。 我缺少一個明顯的替代方案嗎?

如果 pnoise 是柏林噪聲,則有 numpy 向量化實現。 這是一個

事實上,我認為你不能做得更快。 當 Numpy 可以在 C 中執行內部循環時,它會很快。對於像 np.sin 這樣的內置 numpy 函數就是這種情況。

這里有一個向量運算,其中運算是一個 python 函數。

但是,可以重新實現噪聲函數,以便它在內部使用 numpy 向量化函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM