MathGroup Archive 2005

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

Search the Archive

Re: Canonical order...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55061] Re: Canonical order...
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Fri, 11 Mar 2005 04:20:35 -0500 (EST)
  • Organization: University of Washington
  • References: <d0mnbf$70o$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Josef,

A few more details about what you are trying to achieve would probably 
produce more helpful answers in this case. At any rate, if you are only 
interested in the output form, you can write special rules for MakeBoxes 
that will produce the output you desire. (this approach only works if you 
use StandardForm or TraditionalForm). For example, if you want the order to 
be reversed, then you could try:

MakeBoxes[Times[a__], f_] := RowBox[MakeBoxes[#, f] & /@ Reverse[{a}]]

Then we would have

In[11]:=
F[a]F[b]F[c]
Out[11]=
F[c]F[b]F[a]

The output has been sorted in reverse order. However, note that internally, 
Mathematica still has the arguments to Times sorted in the usual way:

In[12]:=
FullForm[%11]
Out[12]//FullForm=
Times[F[a], F[b], F[c]]

If you have a more complicated sorting algorithm, just use Sort with the 
appropriate sorting function instead of Reverse. Reversing the order of 
output will produce ugly results (e.g. F[a] 10 instead of 10 F[a]) so you 
will probably want to use Sort. Presumably you have a more limited context 
where you want your sorting algorithm instead of the default. If you could 
provide these details, it should be possible to achieve your sorting 
criteria only where needed.

Carl Woll


"Josef Karthauser" <joe at tao.org.uk> wrote in message 
news:d0mnbf$70o$1 at smc.vnet.net...
>
> 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. =================
> 



  • Prev by Date: LUDecomposition
  • Next by Date: Re: Determinant problem
  • Previous by thread: Re: Canonical order...
  • Next by thread: Google's aptitude test equation