MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Orderless indexed functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg20919] Re: [mg20891] Re: [mg20874] Orderless indexed functions
  • From: "N Monomachoff" <nmonomachoff at email.msn.com>
  • Date: Wed, 1 Dec 1999 01:49:51 -0500 (EST)
  • References: <819lat$3j9$4@dragonfly.wolfram.com>
  • Sender: owner-wri-mathgroup at wolfram.com

If you can be sure that {n_Integer} never appears in your expressions, then
by far the simplest method is

SetAttributes[f,Orderless]

and representing the indexed form by

f[{n_},x___]

It doesn't matter where in the function the 'index list' appears, since it
can always be distinguished. The full complement of pattern matching is
retained, with little overhead.

For example, my definition

f[n_][x___,f[0][y___]]:=f[n][x,y]

is fully represented by

f[{n_},x___,f[{0},y___]]:=f[{n},x,y]

N Monomachoff
nmonomachoff at msn.com


Andrzej Kozlowski wrote in message <819lat$3j9$4 at dragonfly.wolfram.com>...
>Just one more brief remark on this topic. It seems to me now that my usage
>of the Notation package in this context was really a bit of a red herring.
>One can get the same result more simply as follows:
>
>In[1]:=
>f[i_] := ToExpression[ToString[f] <> ToString[i]]
>
>In[2]:=
>f[3]
>Out[2]=
>f3
>
>In[4]:=
>Do[SetAttributes[Evaluate[f[i]], Orderless], {i, 1, 50}]
>
>In[5]:=
>f[1][c, a, b]
>Out[5]=
>f1[a, b, c]
>
>As long as one needs to deal only with a finite (reasonably large) number
of
>functions indexed by integers this ought to be enough.
>
>
>>
>> This seems to be a very hard problem. All I can suggest at this time is
the
>> following. If you  can be satisfied with defining only a finite number of
>> f[n]'s with n an integer between, say, 0 and 100 (in principle you could
use
>> a very large number) you could do something like this:
>>
>> << Utilities`Notation`
>>
>> In[2]:=
>> \!\(\*
>> RowBox[{"Symbolize", "[",
>> TagBox[\(f\__\),
>> NotationBoxTag,
>> TagStyle->"NotationTemplateStyle"], "]"}]\)
>>
>> In[3]:=
>> f[i_] := ToExpression[
>> "f\[UnderBracket]Subscript\[UnderBracket]" <> ToString[i]]
>>
>> In[4]:=
>> Do[SetAttributes[Evaluate[f[i]], Orderless], {i, 1, 100}]
>> In[5]:=
>> Do[f[i][x___, f[0][y___]] = f[i][x, y], {i, 1, 100}];
>>
>> Now you get
>>
>> In[9]:=
>> f[5][c, b, f[0][a]]
>> Out[9]=
>> f\[UnderBracket]Subscript\[UnderBracket]5[a, b, c]
>>
>> which seems to be right. You could also use the same method to define
other
>> values for f[i]. The only problem I see at the moment is that you can't
get
>> more than a finite number of f[i]'s. However, for some purposes this
maybe
>> enough. I can's see at this time how to overcome this restriction.
>>
>> Andrzej
>>
>>
>> --
>>
>>
>>> From: "N Monomachoff" <nmonomachoff at email.msn.com>
To: mathgroup at smc.vnet.net
>>> Date: Sun, 21 Nov 1999 00:59:58 -0500
>>> To: "Andrzej Kozlowski" <andrzej at tuins.ac.jp>,
<hay at haystack.demon.co.uk>,
>>> <mathgroup at smc.vnet.net>, <hwolf at debis.com>
>>> Subject: [mg20919] Re: [mg20891] Re: [mg20874] Orderless indexed functions
>>>
>>> Dear Andrzej Kozlowski,
>>>
>>> I want to thank you for the extraordinary efforts you've expended on
this
>>> problem. Had I appreciated how nontrivial my original question was, I
>>> would have been much more explicit:
>>>
>>> Not only do I need pattern matching within the body of the function, I
need
>>> to pattern match the index itself, as in the following:
>>>
>>> f[n_][x___,f[0][y___]]:=f[n][x,y]
>>>
>>> The ability to do this is precisely why I chose the indexed form.
>>> Unfortunately, none of the proposed solutions, not even your last, seems
to
>>> allow for such definitions. Apparently the Notation package is unable to
>>> stretch this far. Even a definition as simple as:
>>>
>>> \!\(f\_n_ := h[n]\)
>>>
>>> doesn't work. On the other hand, I haven't delved into the deeper parts
of
>>> the Notation package, so perhaps I'm wrong.
>>>
>>> N Monomachoff
>>> nmonomachoff at msn.com
>>>
>>>
>>>
>>>> From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
To: mathgroup at smc.vnet.net
>>>> Date: Sat, 20 Nov 1999 17:28:49 +0900
>>>> To: Andrzej Kozlowski <andrzej at tuins.ac.jp>, <mathgroup at smc.vnet.net>
>>>> Subject: [mg20919] Re: [mg20891] Re: [mg20874] Orderless indexed functions
>>>>
>>>> I think I have worked out the right solution to your problem, which
will
>>>> produce an indexed function with an Orderless attribute. This is what
you
>>>> have to do. First, you need to load the Notation package:
>>>>
>>>> In[2]:=
>>>> << Utilities`Notation`
>>>>
>>>> (I was wrong to say you need ot move it inot the Autoload folder since
the
>>>> ExtraPackages folder is actually included in  $Path )
>>>>
>>>> Okay, now you symbolize "f\[UnderBracket]Subscript\[UnderBracket]i"
using
>>>> the Notation palette:
>>>>
>>>> In[4]:=
>>>> \!\(\*
>>>> RowBox[{"Symbolize", "[",
>>>> TagBox[\(f\__\),
>>>> NotationBoxTag,
>>>> TagStyle->"NotationTemplateStyle"], "]"}]\)
>>>>
>>>>
>>>>
>>>> Once you have done this you need a function that will convert f[i] to
>>>> "f\[UnderBracket]Subscript\[UnderBracket]i". The following will work:
>>>>
>>>>
>>>> In[9]:=
>>>> f[i_] := ToExpression[
>>>> "f\[UnderBracket]Subscript\[UnderBracket]" <> ToString[i]]
>>>>
>>>> e.g.
>>>>
>>>> In[10]:=
>>>> f[3]
>>>> Out[10]=
>>>> f\[UnderBracket]Subscript\[UnderBracket]3
>>>>
>>>> Now all you need to do is
>>>>
>>>> In[13]:=
>>>> \!\(SetAttributes[f\_3, Orderless]\)
>>>>
>>>> and you have what you wanted:
>>>>
>>>> In[14]:=
>>>> f[3][a, c, b]
>>>> Out[14]=
>>>> \!\(f\_3[a, b, c]\)
>>>>
>>>> Unlike with all the other solutions that have been proposed (if I
didn't
>>>> miss something) you get the benefit of correct behaviour with respect
to
>>>> pattern matching:
>>>>
>>>> In[15]:=
>>>> F[f[3][a, b]] + F[f[3][a, c]] + F[f[3][b, d]] /.
>>>> F[f[3][a, x_]] + F[f[3][c, y_]] -> p[x, y]
>>>> Out[15]=
>>>> \!\(F[f\_3[b, d]] + p[b, a]\)
>>>>
>>>>
>>>>
>>>> --
>>>> Andrzej Kozlowski
>>>> Toyama International University
>>>> JAPAN
>>>> http://sigma.tuins.ac.jp
>>>>
>>>>
>>>>> From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
To: mathgroup at smc.vnet.net
>>>>> Date: Sat, 20 Nov 1999 01:07:05 -0500 (EST)
>>>>> To: mathgroup at smc.vnet.net
>>>>> Subject: [mg20919] [mg20891] Re: [mg20874] Orderless indexed functions
>>>>>
>>>>> 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
>>>>> 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: [mg20919] [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
>>>>> To: mathgroup at smc.vnet.net
>>>>>>> Date: Thu, 18 Nov 1999 20:46:23 -0500 (EST)
>>>>>>> To: mathgroup at smc.vnet.net
>>>>>>> Subject: [mg20919] [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
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>>
>>>
>>>
>
>
>
>
>




  • Prev by Date: Re: Q: smooth 3Dplot with big mesh
  • Next by Date: MathLink Data Types
  • Previous by thread: Re: Orderless indexed functions
  • Next by thread: How to: Read Notebook and auto-evaluate ?