首页 文章

将晶格转换为图形?

提问于
浏览
3

假设我在d倍的Z中有一个点的格点,相等的间距,我怎样才能有效地将其转换成一个图形,其中节点是点和两点之间的边,当且仅当这些点相邻时?

例如:假设我们给出了对应于正方形顶点的整数平方的点...我们如何将其转换为4×4矩阵(或图形),条目1或0是否有连接两者的边节点(对应于整数平方的点)

这个例子很简单有两个原因:

  • 点位于R平方,因此输入是一个二维数组(通常输入将是一个d维数组; d> 1

  • 大多数点以明显的方式连接......但是模式(至少我发现)在d维中变得不那么明显,每个轴上有更多的点....(如果我们采取8点,这甚至是明确的躺在立方体的边缘) .

我正在寻找一个代码可以实现这个给定任何这样的数组(作为输入)并输出(必然对称)矩阵表示图上节点之间的边缘 .

我在R编程(并且开放学习Python) .

Ps.s:我为奇怪的语法道歉...这个交换显然与LaTeX不兼容......:0

1 回答

  • 3

    这可以在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
    

    说明:

    • 首先遍历所有维度,选择该维度的所有坐标

    • 通过删除所选尺寸创建一个新晶格,并使用itertools.product迭代剩余尺寸的所有可能坐标组合的Cartesian product

    • 对于选定的维,迭代所有可能的连续坐标对 .

    • 通过将所选尺寸的坐标放回到正确位置的笛卡尔积中,生成边的两个坐标 .

    如果您的应用涉及数百万点并且速度是一个问题,您可以通过使用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)
    

相关问题