Re: Orderless indexed functions
- To: mathgroup at smc.vnet.net
- Subject: [mg20891] Re: [mg20874] Orderless indexed functions
- From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
- Date: Sat, 20 Nov 1999 01:07:05 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
I just noticed that there is a third way to do what you want, which is possibly the simplest. Just define your function like this: f[1][x__]:= your definition; f[1][x__] /; Not[OrderedQ[{x}]] := f[1][Apply[Sequence, Sort[{x}]]] However, I should also add that only the first of the three approaches I have suggested (the one based on using the Notation package) actually gives you the full functionality of having the Orderless attribute. All the functions defined in these ways will be "commutative", but they will not behave like Orderless functions for the purpose of Pattern matching. For example, consider the function f, which has a genuine Orderless attribute: In[4]:= SetAttributes[f, Orderless] Now consider the following example of pattern matching: In[5]:= F[f[a, b]] + F[f[a, c]] + F[f[b, d]] /. F[f[a, x_]] + F[f[c, y_]] -> p[x, y] Out[5]= F[f[b, d]] + p[b, a] If we use the definition above we won't get this behaviour: In[6]:= F[f[1][a, b]] + F[f[1][a, c]] + F[f[1][b, d]] /. F[f[1][a, x_]] + F[f[1][c, y_]] -> p[x, y] Out[6]= F[f[1][a, b]] + F[f[1][a, c]] + F[f[1][b, d]] The second method I gave in my first reply is even worse, as you can check yourself. So if you need your functions to be really orderless for the purpose of pattern matching I can only suggest using subscripts and the Notation package. > From: Andrzej Kozlowski <andrzej at tuins.ac.jp> To: mathgroup at smc.vnet.net > Date: Fri, 19 Nov 1999 15:12:49 +0900 > To: <nmonomachoff at email.msn.com>, <mathgroup at smc.vnet.net> > Subject: [mg20891] [mg20874] Orderless indexed functions > > I can think of two approaches. If you are willing to abandon the notation > f[k] and instead use subscripts than probably the simplest way to get what > you want is to use the Notation package, which you can find in the > ExtraPackages folder in the AddOns folder (you will have ot move it to > somewhere like the AutoLoad folder to make things work as in y example > below) > > In[23]:= << Utilities`Notation` > > In[27]:= Symbolize[NotationBoxTag[\(f\__\)]] > > In[28]:= SetAttributes[f\[UnderBracket]Subscript\[UnderBracket]2, Orderless] > > In[29]:= f\[UnderBracket]Subscript\[UnderBracket]2[b, c, a] > Out[29]= f\[UnderBracket]Subscript\[UnderBracket]2[a, b, c] > > If, on the other hand, you insist on the f[k] notation or you find the > Notation package just too much bother (as I do) you could try something like > this. > Suppose you want to define a function of the form: f[1][x__]:=B[x] (where B > is the body of your function) and give it the attribute Orderless. The > following code will do it for you: > > f[1][x__] := Unevaluated[(Function[y, B[Sequence @@ y], > {Orderless}])[x]] /. y -> Table[Unique[z],{Length[{x}]}] > > e.g. > > In[5]:= > f[1][b, c, a, d] > Out[5]= > B[a, b, c, d] > > > -- > > >> From: "N Monomachoff" <nmonomachoff at email.msn.com> To: mathgroup at smc.vnet.net >> Date: Thu, 18 Nov 1999 20:46:23 -0500 (EST) >> To: mathgroup at smc.vnet.net >> Subject: [mg20891] [mg20874] Orderless indexed functions >> >> Hello, >> >> I need to use 'indexed functions' like f[0][x__] and f[1][x__]. >> It's important that the functions be Orderless, but >> >> SetAttributes[f[0],Orderless] >> >> isn't accepted because f[0] isn't a symbol, and >> >> SetAttributes[f,Orderless] >> >> doesn't affect the ordering in the second set of brackets. >> How does one make a nonsymbolic head orderless? >> >> Thanks for any help. >> >> N Monomachoff >> nmonomachoff at msn.com >> >> >> >