我想出了自己的想法,所以這裏有一個解釋任何被這個困惑的未來人士。
舉個例子,讓我們使用,我是在我的代碼工作點,這是我生成的簡單的格子這裏如下
import numpy as np
import itertools as it
from matplotlib import pyplot as plt
import scipy as sp
inputs = list(it.product([0,1,2],[0,1,2]))
i = 0
lattice = range(0,len(inputs))
for pair in inputs:
lattice[i] = mksite(pair[0], pair[1])
i = i +1
細節並不重要,我只想說,它會產生一個正三角形晶格,其中一個點和它的任何六個最接近的鄰居之間的距離爲1。
要繪製它
plt.plot(*np.transpose(lattice), marker = 'o', ls = '')
axes().set_aspect('equal')
![enter image description here](https://i.stack.imgur.com/xJFev.png)
現在計算三角:
dela = sp.spatial.Delaunay
triang = dela(lattice)
讓我們來看看這是什麼給了我們。
triang.points
輸出:
array([[ 0. , 0. ],
[ 0.5 , 0.8660254 ],
[ 1. , 1.73205081],
[ 1. , 0. ],
[ 1.5 , 0.8660254 ],
[ 2. , 1.73205081],
[ 2. , 0. ],
[ 2.5 , 0.8660254 ],
[ 3. , 1.73205081]])
簡單,只要在上面示出的晶格所有九個點的陣列。如何讓我們來看看:
triang.vertices
輸出:
array([[4, 3, 6],
[5, 4, 2],
[1, 3, 0],
[1, 4, 2],
[1, 4, 3],
[7, 4, 6],
[7, 5, 8],
[7, 5, 4]], dtype=int32)
在這個陣列中,每一行代表了一個三角單(三角形)。每行中的三個條目都是我們剛纔看到的點數組中該單形的頂點索引。因此,例如這個陣列中的第一單工,[4, 3, 6]
是由點:
[ 1.5 , 0.8660254 ]
[ 1. , 0. ]
[ 2. , 0. ]
它容易通過根據其索引繪製晶格上一張紙,標記每個點,然後跟蹤看到這通過triang.vertices
中的每一行。
這是我需要編寫我在我的問題中指定的函數的所有信息。 它看起來像:
def find_neighbors(pindex, triang):
neighbors = list()
for simplex in triang.vertices:
if pindex in simplex:
neighbors.extend([simplex[i] for i in range(len(simplex)) if simplex[i] != pindex])
'''
this is a one liner for if a simplex contains the point we`re interested in,
extend the neighbors list by appending all the *other* point indices in the simplex
'''
#now we just have to strip out all the dulicate indices and return the neighbors list:
return list(set(neighbors))
就是這樣!我相信上面的功能可以做一些優化,就是我在幾分鐘內想到的。如果有人有任何建議,請隨時發佈。希望這有助於未來的某些人對我和我一樣感到困惑。
沒關係,我想出了自己! –
關於堆棧溢出,我們支持人們[回答他們自己的問題](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/)。您能否努力回答自己的問題並將其標記爲已解決(通過勾選答覆帖左側的複選標記)? – Sicco
嘗試過,明顯的用戶w /少於10個聲望無法在發佈後8小時回答自己的問題:/我將保存我在txt文件中寫下的內容並等到今天晚上發佈。 –