Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
{{{x11, 31}, {1, {{x10, 19}}}, {0, {{x9, 12}}}}} /. {___, 
   s : {{a_, b_}, {c_, {{d_, e_}}}, {f_, {{g_, h_}}}}, ___} -> 
  Graph[{a \[UndirectedEdge] d, a \[UndirectedEdge] g}, 
   VertexLabels -> {a -> a, d -> d, g -> g}, EdgeWeight -> {c, f}, 
   ImagePadding -> 10, EdgeLabels -> {{a \[UndirectedEdge] d -> 
       c}, {a \[UndirectedEdge] g -> f}}]

It generates a graph but it doesn't replace the variables as is expected,variables stay same without replacement. I am also facing problem with labeling vertices using patterns.

Does someone know how to achieve this replacement ?

share|improve this question

closed as off-topic by Szabolcs, bobthechemist, m_goldberg, rm -rf Feb 25 at 1:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Szabolcs, bobthechemist, m_goldberg, rm -rf
If this question can be reworded to fit the rules in the help center, please edit the question.

You need RuleDelayed or :> instead of Rule or ->. In general, always use :> with named patterns in replacements to properly localize the variable. –  rm -rf Feb 24 at 18:43
Yes it works, thanks, please post it as answer, so that I can accept and close this question. –  Rorschach Feb 24 at 18:45
This is covered here (the situation is essentially the same as with Set/SetDelayed you you might want to google more for that.) –  Szabolcs Feb 24 at 18:45
@Szabolcs : thanks for link, I will go through it as well. –  Rorschach Feb 24 at 18:50
@Rorschach If you are already familiar with the difference between Set and SetDelay and Rule and RuleDelayed it won't give any additional information ... I only realized where the misunderstanding may have come from after posting that comment so I wrote an asnwer. –  Szabolcs Feb 24 at 19:05

1 Answer 1

up vote 1 down vote accepted

As @rm-rf said in the comments, you need to use RuleDelayed (i.e. :> instead of ->) to ensure that Graph evaluates only after the substitutions have been made.

There is a little subtlety here: you may have assumed that Graph does not evaluate to anything, it is simply a representation of a graph, the same way as Graphics is just a representation of graphics. If this were the case then your code would work. But in reality Graphs are atomic object in the AtomQ sense, so it does in fact evaluate: The compound expression with Graph head becomes an atomic Graph object, and the symbols a, b, c, etc. within will be shielded from substitution. This is why RuleDelayed is needed here.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.