Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Canonical order...

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

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: Nested iterators in Compile
  • Next by Date: Re: findfit or solve?
  • Previous by thread: Re: Canonical order...
  • Next by thread: Re: Canonical order...