- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个 d 乘以 Z 且间距相等的点格,我如何才能有效地将其转换为以节点为点、两点之间有一条边的图形,当且仅当这些点相邻时?
例如:假设我们在对应于正方形顶点的整数平方中给出点...我们如何将其转换为具有条目 1 或 0 的 4 x 4 矩阵(或图形)是否存在连接两个节点的边(对应于整数平方中的点)
这个例子很简单有两个原因:
我正在寻找一种代码,它可以在给定任何此类数组(作为输入)的情况下实现此代码,并输出一个(必须对称的)矩阵,该矩阵表示图上节点之间的边。
我用 R 编程(并且愿意学习 Python)。
Ps.s:我为奇怪的语法道歉......这个交换显然与 LaTeX 不兼容......:0
最佳答案
这可以像这样用 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
解释:
首先遍历所有维度,选择该维度的所有坐标
通过删除选定的维度创建一个新的晶格,并遍历 Cartesian product使用 itertools.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)
关于python - 将格子转换为图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35672934/
我试图通过设置 par(mfrow=c(2,1)) 使用 levelplot 在一个窗口中放置多个格子图,但它似乎忽略了这个。 是否有一个特定的函数可以在lattice中设置多个绘图? 最佳答案 “l
我是 R 编程的新手,我想制作一个关于性别和相应工作的直方图。 我遇到的问题是无法在图表中添加图例。 此外,我想在每个栏的顶部添加频率数字。 你能帮帮我吗? library(lattice) hist
我正在使用格子呢。使用交换 api 生成公共(public) token 创建者访问 token 后。现在访问 token 用于创建 api 调用但会出错。 The provided API keys
我使用 R 的点阵包制作了点图和 fiddle 图。点图为因子的每个值都有网格线,以便更容易找到相应的点。 是否可以在不显示此类线条的格子 fiddle 图中生成此类线条? 在我的情况下,数据分布在更
我正在尝试为选项评估生成一个二叉树,并且我想使用来自已经存在值的数组的值绘制一个格子。 所以基本上我的 VBA 中有一个与此类似的数组: My array in vba 我想以这种形式粘贴到 Exce
我在我的 Android 应用程序中使用 plaid-java:0.2.10 作为依赖项。 Plaid 使用 org.apache.httpcomponents:httpclient:4.4.1 作为
我想根据 z 的值为每个面板中的点着色。 我目前拥有的代码无法区分每个面板的 z 值。 我意识到我需要一个面板功能,并且 panel.superpose 可能会有所帮助,但我迷路了。 x <- c(1
在lattice中使用auto.key时,是否可以交换符号和文本的默认位置?也就是说,我希望图例中的文本相对于符号位于右侧。我知道使用 key 很容易做到这一点,但是将颜色与每个图的组分开链接不仅乏味
在lattice中使用auto.key时,是否可以交换符号和文本的默认位置?也就是说,我希望图例中的文本相对于符号位于右侧。我知道使用 key 很容易做到这一点,但是将颜色与每个图的组分开链接不仅乏味
我生成了一个点阵图,其中在单个 Y 轴上绘制了 2 个响应变量)。我已将其中一个响应变量绘制为一条线(下面的 Response1),另一个(Response2)绘制为同一 Y 轴上的点。我希望能够根据
我目前尝试通过更改其 Y 轴标签颜色来自定义格子平行图,具体取决于这些相同标签的特征。我创建了一个自定义的 y.scale.components 函数,如许多书籍/论坛中所述。但是,在将新颜色向量分配
我第一次尝试运行vagrant up,但收到此错误: ==> default: Mounting NFS shared folders... The following SSH command res
我有一个动物园对象,如下所示: z <- structure(c(6, 11, 3.6, 8.4, 8.9, 0, NA, 0.5, 7, NA, 9, NA), .Dim = c(6L, 2
我目前正在用 python 编写一个简单的程序来模拟 1 + 1 维 SU(2) yang mills 理论。对于 SU(2) 的情况,存在用于更新链接变量的特定热浴算法。但是,为了实现此算法,我需要
我正在尝试将格子 gem 整合到我的一个项目中。从文档中它说要这样配置它。 https://github.com/plaid/plaid-ruby Plaid.config do |p| p.cu
鉴于 code , map和 sample data以下: 而不是绘制 watersheds 的名称 map 上的等,如何使用 ggplot2 生成单独的图例或 levelplot就像使用 ArcGI
我以这个数据框为例: >mydata head(mydata) Col1 Col2 Col3 1 -0.1213684 A YY 2 0.1846364 A NN
我想在 2x3 布局中创建一系列包含 6 个或更少绘图的图形。如果有少于 6 个图的图形,我希望有边框来查看布局的结构。 例如这段代码给出了下图: n <- 3 data0 <- expand.gri
我是一名优秀的程序员,十分优秀!