我想以编程方式创建这样的图表 http://yaroslavvb.com/upload/junction-tree-decomposition.png
我想我应该使用带有VertexCoordinateRules,VertexRenderingFunction和EdgeRenderingFunction的GraphPlot作为图形.我应该使用什么彩色斜面背景?
编辑 主要使用Simon的想法,这是我最终使用的简化的"不太健壮"的版本
Needs["GraphUtilities`"];
GraphPlotHighlight[edges_, verts_, color_] := Module[{},
vpos = Position[VertexList[edges], Alternatives @@ verts];
coords = Extract[GraphCoordinates[edges], vpos];
(* add .002 because end-cap disappears when segments are almost colinear *)
AppendTo[coords, First[coords] + .002];
Show[Graphics[{color, CapForm["Round"], JoinForm["Round"],
Thickness[.2], Line[coords], Polygon[coords]}],
GraphPlot[edges], ImageSize -> 150]
]
SetOptions[GraphPlot,
VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .15],
Black, Text[#2, #1]} &),
EdgeRenderingFunction -> ({Black, Line[#]} &)];
edges = GraphData[{"Grid", {3, 3}}, "EdgeRules"];
colors = {LightBlue, LightGreen, LightRed, LightMagenta};
vsets = {{8, 5, … 当笔记本超出一些功能时我遇到的一个典型情况 - 我评估一个表达式,但是我得到了Beep而不是正确的答案,然后是几十个无用的警告,接着是"进一步输出...将被抑制"
我觉得有用的一件事 - 在函数内部使用类似Python的"断言"来强制内部一致性.还有其他提示吗?
Assert[expr_, msg_] := If[Not[expr], Print[msg]; Abort[], None]
Run Code Online (Sandbox Code Playgroud)
编辑11/14 警告雪崩的一般原因是子表达式评估为"坏"值.这会导致父表达式计算为"坏"值,并且此"不良"会一直传播到根目录.内置评估一路上注意到不良并产生警告."坏"可能意味着表达错误的头部,列表中的元素数量错误,负的确定矩阵而不是正定义等等.一般来说,它不适合父表达式的语义.
解决这个问题的一种方法是重新定义所有函数,以便在"输入错误"时返回未评估的值.这将处理内置函数生成的大多数消息.执行像"Part"这样的结构操作的内置函数仍将尝试评估您的值并可能产生警告.
将调试器设置为"中断消息"可以防止出现大量错误,尽管将其一直打开似乎有点过头了
我有一个Linux二进制文件,没有源代码,可以在一台机器上运行,我想制作一个可以在同一架构的不同机器上运行的独立软件包.有什么方法可以达到这个目的?
在我的例子中,两台机器都具有相同的体系结构,相同的Ubuntu内核,但目标机器没有,make并且有错误的文件版本/lib和/usr
我的一个想法是使用chroot并重新创建二进制文件使用的文件系统的子集,可能strace用来找出它需要的内容.有没有这样做的工具?
对于后代,这是我如何确定进程打开的文件
#!/usr/bin/python
# source of trace_fileopen.py
# Runs command and prints all files that have been successfully opened with mode O_RDONLY
# example: trace_fileopen.py ls -l
import re, sys, subprocess, os
if __name__=='__main__':
strace_fn = '/tmp/strace.out'
strace_re = re.compile(r'([^(]+?)\((.*)\)\s*=\s*(\S+?)\s+(.*)$')
cmd = sys.argv[1]
nowhere = open('/dev/null','w')#
p = subprocess.Popen(['strace','-o', strace_fn]+sys.argv[1:], stdout=nowhere, stderr=nowhere)
sts = os.waitpid(p.pid, 0)[1]
output = []
for line in open(strace_fn):
# ignore lines like --- SIGCHLD …Run Code Online (Sandbox Code Playgroud) 我正在尝试计算字符串中出现" - "的次数.
例如,它在这里出现两次'a --b --c'
我试过以下,但它给了我4而不是2,任何想法为什么?
argv='a --b --c'
count = 0
for i in string.gfind(argv, " --") do
count = count + 1
end
print(count)
Run Code Online (Sandbox Code Playgroud) 当我在jupyter笔记本中运行命令时,我正在尝试捕获打印到STDERR的内容.特别是,我正在使用TensorFlow,它fprintf来自C部分,通常打印在控制台上,但我想保存到Python变量中.
我一直在使用来自IPython codebase的 FDRedirector,它设置os.pipe为将输出捕获到Python字符串中.
但是,这段代码的问题在于它会挂起内核以获得足够大的输出.我怀疑它会挂起超过65k的输出,因为Linux上的管道缓冲区大小并gdb显示挂起正在发生write.有没有人有一个适用于更大产出的解决方案?
作为我现在正在做的事情的一个例子,使用FDRedirector
STDERR = 2
redirect = FDRedirector(STDERR)
import tensorflow as tf
sess = tf.Session("")
node = tf.Print(tf.constant(1), [tf.constant(1)], "longstringlongstring")
def print_to_stderr():
sess.run(node) # this prints to stderr
redirect.start();
print_to_stderr()
captured_stderr = redirect.stop()
Run Code Online (Sandbox Code Playgroud)
最后,"captured_stderr"包含打印到stderr的所有内容,包括longstringlongstring.如果你做得longstring更长(> 100k),这将冻结.
更新10/27:我已经在答案中提出了实现一致规模的详细步骤.基本上对于每个Graphics对象,您需要将所有填充/边距修复为0并手动指定plotRange和imageSize,使得1)plotRange包含所有图形2)imageSize = scale*plotRange
现在仍然确定1)如何完全通用,给出了一个适用于由点和粗线组成的图形的解决方案(AbsoluteThickness)
我在VertexRenderingFunction和"VertexCoordinates"中使用"Inset"来保证图形子图之间的一致外观.使用"Inset"将这些子图绘制为另一个图的顶点.有两个问题,一个是图形周围没有裁剪得到的框(即,一个顶点的图形仍然放在一个大框中),另一个是尺寸之间有奇怪的变化(你可以看到一个框是垂直的) .任何人都能看到解决这些问题的方法吗?
这与之前关于如何保持顶点大小看起来相同的问题有关,虽然Michael Pilat建议使用Inset可以使顶点渲染保持相同的比例,但总体规模可能不同.例如,在左侧分支上,由顶点2,3组成的图形相对于顶部图形中的"2,3"子图进行拉伸,即使我使用绝对顶点定位
http://yaroslavvb.com/upload/bad-graph.png
(*utilities*)intersect[a_, b_] := Select[a, MemberQ[b, #] &];
induced[s_] := Select[edges, #~intersect~s == # &];
Needs["GraphUtilities`"];
subgraphs[
verts_] := (gr =
Rule @@@ Select[edges, (Intersection[#, verts] == #) &];
Sort /@ WeakComponents[gr~Join~(# -> # & /@ verts)]);
(*graph*)
gname = {"Grid", {3, 3}};
edges = GraphData[gname, "EdgeIndices"];
nodes = Union[Flatten[edges]];
AppendTo[edges, #] & /@ ({#, #} & /@ nodes);
vcoords = Thread[nodes -> GraphData[gname, "VertexCoordinates"]];
(*decompose*)
edgesOuter = {}; …Run Code Online (Sandbox Code Playgroud) 为什么c#不能做任何确切的操作.
Math.Pow(Math.Sqrt(2.0),2) == 2.0000000000000004
Run Code Online (Sandbox Code Playgroud)
我知道双打是如何工作的,我知道舍入误差来自哪里,我知道它几乎是正确的值,而且我知道你不能在有限的双精度中存储无限数.但是为什么没有一种方法可以让c#完全计算出来,而我的计算器可以做到.
编辑
这不是我的计算器,我只是举个例子:
干杯
我正在寻找一个实用程序来获取表达式并提取该表达式中的所有变量.以下五个例子涵盖了我的所有变量模式
a,Subscript[a,....],Subscript[a,...][...],a[...],a[...][...]
这是两个测试用例.
expr1 = -Log[Subscript[\[Mu], 2][]] Subscript[\[Mu], 2][] -
Log[Subscript[\[Mu], 2][2]] Subscript[\[Mu], 2][2] +
Log[Subscript[\[Beta], 1, 2][]] Subscript[\[Beta], 1, 2][] +
Log[2] Subscript[\[Beta], 1, 2][1] +
Log[Subscript[\[Beta], 1, 2][1]] Subscript[\[Beta], 1, 2][1] +
Log[2] Subscript[\[Beta], 1, 2][2] +
Log[Subscript[\[Beta], 1, 2][2]] Subscript[\[Beta], 1, 2][2] +
Log[Subscript[\[Beta], 2, 3][]] Subscript[\[Beta], 2, 3][] +
Log[Subscript[\[Beta], 2, 3][2]] Subscript[\[Beta], 2, 3][2] +
Log[2] Subscript[\[Beta], 2, 3][3] +
Log[Subscript[\[Beta], 2, 3][3]] Subscript[\[Beta], 2, 3][3];
expr2 = Log[\[Beta][{1, 2}][{}]] \[Beta][{1, 2}][{}] +
Log[2] \[Beta][{1, …Run Code Online (Sandbox Code Playgroud) 有没有人想出一种方法来修改Mathematica 8中的Graph对象?特别是,如何获得右键单击图表时获得的相同功能.
一些新的图形函数冲突Combinatorica,有没有办法强迫Mathematica使用函数的内置版本?换句话说,有没有办法CompleteGraph在我Needs["Combinatorica"]输入Combinatorica版本之后获得内置的访问权限CompleteGraph?
为了澄清1,Graph上的Context Menu允许您更改GraphStyle和GraphLayout,我希望能够以编程方式更改它们.这是我发现改变Graph对象GraphStyle的一种方法
g = GridGraph[{4, 4}];
BooleanGraph[Or, g, g, GraphStyle -> "DiagramBlack"]
Run Code Online (Sandbox Code Playgroud)
但是,这会忘记原始图形的选项 VertexCoordinates
在网格图上尝试Brett的食谱
g = GridGraph[{3, 2}, ImageSize -> Tiny]
coords = PropertyValue[{g, #}, VertexCoordinates] & /@ VertexList[g];
Graph[EdgeList[g], GraphStyle -> "BasicGold",
VertexCoordinates -> coords, ImageSize -> Tiny]
Run Code Online (Sandbox Code Playgroud)
http://yaroslavvb.com/upload/save/mathematica-change-style.png
Mathematica如何在图形操作上处理图形坐标似乎存在一个错误.下面第一行置换坐标,第二行给出内部警告,可能与坐标有关.对每个顶点使用非整数顶点和显式坐标没有帮助.一种解决方案是全局存储坐标,并具有在修改后fixCoordinates重新分配正确坐标的功能Graph
VertexDelete[GridGraph[{3, 3}], 1]
NeighborhoodGraph[VertexDelete[GridGraph[{3, 3}], 1], 2]
Run Code Online (Sandbox Code Playgroud)