防止“加号”重新排列

时间:2010-11-05 19:26:01

标签: wolfram-mathematica

以下代码创建了某个计算的图表。我的问题是,即使分母中的术语处于一个很好的顺序,在应用Plus后,它们会被任意重新排列。有关如何强制保留原始订单的任何建议吗?

http://yaroslavvb.com/upload/ind-sets-grid.png

r[i_] := Floor[(i - 1)/n] + 1;
c[i_] := Mod[i, n, 1];
adj[a_, b_] := Abs[r[a] - r[b]] + Abs[c[a] - c[b]] == 1;
indsetQ[s_] := Not[Or @@ (adj @@@ Subsets[s, {2}])];
indsets[k_] := Select[Subsets[Range[n^2], {k}], indsetQ];
twoColorGraph[g_, seen_, lbl_] := Module[{radius = .22},
   vcoords = # -> {c[#], n - r[#]} & /@ Range[n^2];
   fv = Function[{p, v}, {EdgeForm[Thick], 
      If[MemberQ[seen, v], Pink, White], Disk[p, radius]}];
   GraphPlot[g, VertexLabeling -> True, VertexRenderingFunction -> fv,
     PlotLabel -> Style[lbl, 20], LabelStyle -> Directive[Bold], 
    VertexCoordinateRules -> vcoords, ImageSize -> 80]
   ];
n = 2;
g = Array[Boole[adj[#1, #2]] &, {n^2, n^2}];
weight[set_] := Times @@ (Subscript[\[Lambda], c[#], r[#]] & /@ set);
denominator = 
  twoColorGraph[g, #, weight[#]] & /@ 
   Join @@ (indsets[#] & /@ Range[2]);
numerator = twoColorGraph[g, {1}, weight[{1}]];
Style[numerator/(Plus @@ denominator), FontSize -> 30]

1 个答案:

答案 0 :(得分:5)

在输出中将myPlus格式化为外观(例如Plus)的技巧是使用Format。这是一个让你入门的简单例子:

Format[myPlus[expr__]] := Row[Riffle[{expr}, "+"]]

然后,您将在笔记本中直观地看到:

In[7]:= x = myPlus[3, 2, 1]
Out[7]= 3+2+1

...但x仍有头myPlus

以下是文档中的tutorial,其中详细介绍了格式输出,运算符优先级等。

希望有所帮助!

相关问题