MathGroup Archive 2005

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

Search the Archive

Re: Canonical order...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55040] Re: [mg54989] Canonical order...
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Thu, 10 Mar 2005 05:24:50 -0500 (EST)
  • References: <200503091134.GAA06921@smc.vnet.net> <caec416ba0760044b7e5574d02bf3213@mimuw.edu.pl>
  • Sender: owner-wri-mathgroup at wolfram.com

I just noticed that in the second case (Sort) the trick with sortF, 
which is used for trapping built-in functions, is not needed at all, 
since Sort is being called with a custom sort function and there is no 
danger of an infinite loop, so the following simpler code will work:


In[1]:=
Unprotect[Sort];

In[2]:=
Sort[{a_F,b_F}]:=Sort[{a,b},Not[OrderedQ[{#1,#2}]]&]


In[3]:=
Protect[Sort];

In[4]:=
Sort[{F[a],F[b]}]

Out[4]=
{F(b),F(a)}


Andrzej

On 9 Mar 2005, at 19:09, Andrzej Kozlowski wrote:

> On 9 Mar 2005, at 12:34, Josef Karthauser wrote:
>
>>
>> I'm trying to adjust what Mathematica thinks is canonical order for
>> a particular function F[], so that orderless functions such as
>> Times[F[c], F[a], F[b]] automatically gets ordered according to my
>> specified sort ordering function on F.
>>
>> I thought that I could do it by tweaking with Order, i.e.
>>
>>     Order[F[a_], F[b_]] := -Order[a,b]
>>
>> for instance.
>>
>> I was hoping then that,
>>
>>     Times[F[a],F[b]]
>>
>> would then become the canonical,
>>
>>     Times[F[b],F[a]],
>>
>> but this doesn't happen:
>>
>>     : Order[F[a], F[b]]
>>     = -1
>>
>>     : Times[F[b], F[a]]
>>     = F[a] F[b]
>>
>>     : % // FullForm
>>     = Times[F[a], F[b]]
>>
>> Ok, so it looks like Sort, isn't paying attention to Order[].  Should
>> it? How do I go about changing the default sort order for a particular
>> function?
>>
>>     : Sort[{F[a], F[b]}]
>>     = {F[a], F[b]}
>>
>> Joe
>>
>> p.s. I've asked for a lot of help recently, but not given much in
>> return.  Many thanks to all of you who are helping me out with this
>> learning curve! :)  You're all stars!
>> --
>> Josef Karthauser (joe at tao.org.uk)	       http://www.josef-k.net/
>> FreeBSD (cvs meister, admin and hacker)     http://www.uk.FreeBSD.org/
>> Physics Particle Theory (student)   http://www.pact.cpes.sussex.ac.uk/
>> ================ An eclectic mix of fact and theory. =================
>>
>>
>>
>
> There are really two questions here, one about Times and one about 
> Sort. They are not actually related so I will answer them in turn.
>
> The short answer is "don't do it".
> But if you want to mess around with unprotecting Times and accept the 
> risks involved you can do this:
>
> In[3]:=
> Unprotect[Times];
>
> In[4]:=
> Format[Times[a_F,b_F]]:=HoldForm[b*a]/;OrderedQ[{a,b}]
>
> In[5]:=
> Protect[Times];
>
> Now
> In[6]:=
> F[a]*F[c]
>
> Out[6]=
> F(c) F(a)
> Or
>
> In[7]:=
> F[1]*F[2]
>
> Out[7]=
> F(2) F(1)
>  But of course you still get:
> In[8]:=
> F[1]*F[2]*F[3]
>
> Out[8]=
> F(1) F(2) F(3)
>
>
> Now the question about Sort: here is a somewhat different approach, 
> again it requires you to unprotect Sort:
>
>
> In[9]:=
> sortF=True;
>
> In[10]:=
> Unprotect[Sort];
>
> In[11]:=
> Sort[{a_F,b_F}]/;sortF:=Block[{sortF=False},Sort[{a,b},Not[
>     OrderedQ[{#1,#2}]]&]]
>
> In[12]:=
> Protect[Sort];
>
> In[13]:=
> Sort[{F[a],F[b]}]
>
> Out[13]=
> {F(b),F(a)}
>
> Note that changing the behaviour of Sort in this way will have no 
> effect on the behaviour of Times.
>
> Andrzej Kozlowski
> Chiba, Japan
> http://www.akikoz.net/andrzej/index.html
> http://www.mimuw.edu.pl/~akoz/
>


  • Prev by Date: Re: findfit or solve?
  • Next by Date: Re: Google's aptitude test equation
  • Previous by thread: Re: Canonical order...
  • Next by thread: Re: Canonical order...