wolfram mathematica - Patterns with Orderless subexpressions -


i need deal patterns f[{a,b}]=... a , b supposed orderless

so far i've implemented using default sort[] on subexpressions every time f defined or evaluated.

my questions are

  1. is robust orderless?
  2. is there better way?

ps: example application tree decomposition recursively build quantities subtree[bag1->bag2] bag1 , bag2 orderless sets of vertices

answer update

michael pilat's answer shows how define rule automatically sort f's subexpressions. alternative solution define custom head bag orderless attribute , use head orderless sublists

after answered this question consulted few colleagues agreed following indeed best / typical way handle problem:

f[{a_, b_}] :=   f[{sort[a], sort[b]}] /; not[orderedq[a]] || not[orderedq[b]]  in[99]:= f[{{1, 2, 3}, {5, 4, 3}}]  out[99]= f[{{1, 2, 3}, {3, 4, 5}}] 

alternately, replace inner list heads custom head symbol has orderless attribute, , if formatting matters use various formatting techniques have been discussed here =)


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -