這可以在Python中實現像這樣:
from itertools import product
def print_lattice_edges(lattice):
"""prints all edges of a lattice, given as a list of lists of coordinates"""
for idim, dim_coords in enumerate(lattice):
for other_coords in product(*lattice[:idim] + lattice[idim+1:]):
for coord1, coord2 in zip(dim_coords[:-1], dim_coords[1:]):
edge1 = other_coords[:idim] + (coord1,) + other_coords[idim:]
edge2 = other_coords[:idim] + (coord2,) + other_coords[idim:]
print edge1, '->', edge2
說明:
如果您的應用程序涉及數百萬個點和速度是一個問題,你也許可以做到通過生成使用numpy笛卡爾乘積類似的東西。
一些快速測試:
In [23]: print_lattice_edges([[0, 1], [0, 1]]) # your example
(0, 0) -> (1, 0)
(0, 1) -> (1, 1)
(0, 0) -> (0, 1)
(1, 0) -> (1, 1)
In [24]: print_lattice_edges([[0, 1], [3, 4, 5]]) # 2x3 points, 7 edges
(0, 3) -> (1, 3)
(0, 4) -> (1, 4)
(0, 5) -> (1, 5)
(0, 3) -> (0, 4)
(0, 4) -> (0, 5)
(1, 3) -> (1, 4)
(1, 4) -> (1, 5)
In [25]: print_lattice_edges([[0, 1], [0, 1], [0, 1]]) # cube, 12 edges
(0, 0, 0) -> (1, 0, 0)
(0, 0, 1) -> (1, 0, 1)
(0, 1, 0) -> (1, 1, 0)
(0, 1, 1) -> (1, 1, 1)
(0, 0, 0) -> (0, 1, 0)
(0, 0, 1) -> (0, 1, 1)
(1, 0, 0) -> (1, 1, 0)
(1, 0, 1) -> (1, 1, 1)
(0, 0, 0) -> (0, 0, 1)
(0, 1, 0) -> (0, 1, 1)
(1, 0, 0) -> (1, 0, 1)
(1, 1, 0) -> (1, 1, 1)
看來,乳膠語法不上計算器的工作像它在其他stackexchange網站,所以請修正問題... –
完成,這一切現在清理,再次感謝:) – CSA