6

The following code creates a diagram of a certain calculation. My problem is that even though the terms in denominator are in a nice order, after applying Plus on it, they get rearranged arbitrarily. Any suggestions how to force the original order to be kept?


(source: yaroslavvb.com)

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]
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197
  • This is a duplicate, in intent, if not form, of another question (http://stackoverflow.com/questions/3947071/controlling-order-of-variables-in-an-expression). Otherwise, I'd give you a +1 for the graphics alone. What is the calculation for? – rcollyer Nov 05 '10 at 19:38
  • Yes, that's similar. The suggestion there is to define own function that's not orderless like "myPlus", but how do I get it to render the same as "Plus"? – Yaroslav Bulatov Nov 05 '10 at 19:42
  • The calculation is to find per-site occupation probabilities of hard-core repulsive local gas on a finite grid (ie, grand canonical gas with hard-core self-repulsion and hard-core pair interaction) – Yaroslav Bulatov Nov 05 '10 at 20:12
  • Seconded on the nice graphics. I answered the formatting question below =) – Michael Pilat Nov 05 '10 at 20:22
  • +1, @Yaroslav Bulatov, you're right. It's definitely related, but not the same. – rcollyer Nov 05 '10 at 20:51

1 Answers1

5

The trick to formatting something like myPlus to look like Plus on output is to use Format. Here's a simple example to get you started:

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

Then, what you will see visually in the notebook is:

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

... but x will still have head myPlus.

Here is a tutorial from the documentation that goes into more details about formatting output, operator precedence, etc.

Hope that helps!

Michael Pilat
  • 6,480
  • 27
  • 30
  • 1
    +1, I was trying to do that with `Notation` and it was failing miserably. I forgot to look at `Format` directly. – rcollyer Nov 05 '10 at 20:52
  • I tried to incorporate `Interpretation` with your answer so that it can be copied and pasted properly, but ran into problems. See the SO question http://stackoverflow.com/q/4112299/421225 – Simon Nov 06 '10 at 07:18