问题描述
有没有人想出一种方法来修改 Mathematica 8 中的 Graph 对象?特别是,如何获得与右键单击图表时获得的相同功能.
Has anybody figured out a way to modify Graph objects in Mathematica 8? In particular, how to get the same functionality you get when you right click on the graph.
一些新的图形函数与 Combinatorica
冲突,有没有办法强制 Mathematica 使用该函数的内置版本?换句话说,在我执行 Needs["Combinatorica"]
之后,有没有办法访问内置的 CompleteGraph
,它导入了 CompleteGraph的 Combinatorica 版本代码>?
Some of the new graph functions conflict with Combinatorica
, is there a way to force Mathematica to use a built-in version of the function? In other words, is there a way to get access to built-in CompleteGraph
after I do Needs["Combinatorica"]
which imports Combinatorica version of CompleteGraph
?
为了澄清 1,Graph 上的上下文菜单允许您更改 GraphStyle 和 GraphLayout,我希望能够以编程方式更改它们.这是我发现更改 Graph 对象的 GraphStyle 的一种方法
To clarify 1, Context Menu on Graph lets you change GraphStyle and GraphLayout, and I'd like to be able to change them programmatically. Here's one way I found to change GraphStyle of Graph object
g = GridGraph[{4, 4}];
BooleanGraph[Or, g, g, GraphStyle -> "DiagramBlack"]
然而,这忘记了原始图形的选项,如 VertexCoordinates
However, that forgets options of the original graph like VertexCoordinates
在网格图上尝试 Brett 的食谱
Trying Brett's recipe on grid graph
g = GridGraph[{3, 2}, ImageSize -> Tiny]
coords = PropertyValue[{g, #}, VertexCoordinates] & /@ VertexList[g];
Graph[EdgeList[g], GraphStyle -> "BasicGold",
VertexCoordinates -> coords, ImageSize -> Tiny]
Mathematica 如何处理图形操作中的图形坐标似乎存在错误.下面的第一行置换坐标,第二行给出内部警告,可能与坐标有关.对每个顶点使用非整数顶点和显式坐标无济于事.一种解决方案是全局存储坐标,并使用 fixCoordinates
函数在修改后将正确的坐标重新分配给 Graph
There seems to be a bug with how Mathematica handles Graph coordinates on graph operations. First line below permutes coordinates, second gives internal warning, probably related to coordinates. Using non-integer vertices and explicit coordinates for each vertex doesn't help. One solution is to store coordinates globally and have fixCoordinates
function to reassign correct coordinates to Graph
after modifications
VertexDelete[GridGraph[{3, 3}], 1]
NeighborhoodGraph[VertexDelete[GridGraph[{3, 3}], 1], 2]
推荐答案
新的 Graph 对象在 Mathematica 8 中是原子的.因此,像字符串或图像一样,它们没有可以以正常方式操作的内部结构.特别不寻常的是,新对象具有看起来可以象征性地操作的 FullForm.但是外观可能具有欺骗性——不仅模式匹配无法访问该表示,而且如果您使用复制和粘贴将其反馈给 Mathematica,它甚至都不是有效的图形规范.
The new Graph objects are atomic in Mathematica 8. Thus, like strings or images they do not have internal structure that can be manipulated in the normal fashion. What is particularly unusual is that the new objects have a FullForm that looks like it can be manipulated symbolically. But appearances can be deceiving -- not only is that representation inaccessible to pattern-matching, but it is not even a valid graph specification if you feed it back to Mathematica using copy-and-paste.
我发现了一些可用于操纵图形结构的技巧.第一个尝试使用官方"渠道来提取图的属性:
I found a couple of hacks that can be used to manipulate graph structure. The first tries to use the "official" channels to extract the properties of graphs:
adjustedGraph[g_, newOptions___] :=
Graph[
VertexList@g,
EdgeList@g,
newOptions,
Sequence@@Table[p -> PropertyValue[g, p], {p, PropertyList[g]}]
]
你可以这样使用这个函数:
You can use this function like this:
g = GridGraph[{4, 4}, GraphStyle -> "DiagramBlack", ImageSize -> Tiny]
adjustedGraph[g, GraphStyle -> "BasicGold"]
该函数使用VertexList、EdgeList 和PropertyValue 来提取图形属性.不幸的是,某些选项无法通过这种方式恢复.例如,使用此方法将丢失 Graphics 选项 ImageSize.
This function uses VertexList, EdgeList and PropertyValue to extract the graph properties. Unfortunately, some options are not recoverable by this means. For example, the Graphics option ImageSize will be lost using this method.
一个更令人发指的黑客利用了 FullForm 的伪符号表示:
An even more heinous hack exploits the pseudo-symbolic representation of FullForm:
adjustedGraph2[g_, newOptions___] :=
"Hold@" ~~ ToString[g, InputForm] //
ToExpression //
#[[1, 3]] & //
Graph[VertexList@g, EdgeList@g, newOptions, Sequence @@ #] &
尽管它的本质是邪恶的,但第二个函数的表现更令人满意,因为它似乎保留了大多数图形选项.我说大多数",因为我还没有尝试过更深奥的选项,比如包装器、形状函数和事后分配的图形属性.无法保证此方法会在 Wolfram 更改图形对象的表示时不变地工作(甚至现在它适用于所有可能的图形定义).
Despite its evil nature, this second function performs more satisfactorily as it appears to retain most graph options. I say "most", because I have not yet experimented with more esoteric options like wrappers, shape functions and graph properties assigned after the fact. There are no guarantees that this method will work unchanged as Wolfram changes the representation of graph objects (or even that it works for all possible graph definitions now).
应该有一种方法可以在没有黑客的情况下实现这一目标.我仍然希望有一些函数潜伏在那里,可以给出图形对象的完整符号表示.
There ought to be a way to achieve this without hacks. I still hold out hope that there is some function lurking out there that will give the complete symbolic representation of a graph object.
至于加载 Combinatorica 包后出现的符号冲突,您仍然可以通过明确指定包名来访问原始符号,例如系统`CompleteGraph.如果您希望系统符号优先于 Combinatorica 符号,您可以计算以下表达式以更改包搜索顺序:
As for the symbol conflicts that arise after loading the Combinatorica package, you can still access the original symbols by explicitly specifying the package name, e.g. System`CompleteGraph. If you prefer to have the system symbols take precedence over the Combinatorica symbols, you could evaluate the following expression to change the package search order:
$ContextPath =
$ContextPath /.
{x___, c : "Combinatorica`", y___, s:"System`", z___} :> {x, y, s, c, z}
我注意到 Wolfram 通过在加载包时发出可怕的警告消息来有效地弃用 Combinatorica 包.
I note that Wolfram is effectively deprecating the Combinatorica package by issuing a scary warning message when you load the package.
这篇关于Mathematica 8.0 中的新图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!