MathGroup Archive 2012

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

Search the Archive

Re: Sorting coefficients

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124101] Re: [mg124021] Sorting coefficients
  • From: Christopher Young <cy56 at comcast.net>
  • Date: Mon, 9 Jan 2012 03:15:45 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201201060914.EAA26796@smc.vnet.net> <D82BC8DC-F48C-4C3A-8E54-C3451B0CAE49@mimuw.edu.pl>

On Jan 7, 2012, at 4:04 AM, Andrzej Kozlowski wrote:

> How about this:
>
> ls = Prepend[
>  Table[{Re, Im}[E^(k (2 \[Pi])/6 I)] // Through, {k, 0, 5}], {0, 0}]
>
> ls1 =
> Sort[ls, Which[#1[[2]] < #2[[2]],
>    True, #1[[2]] == #2[[2]], #1[[1]] < #2[[1]], True, False] &]
>
> {{-(1/2), -(Sqrt[3]/2)}, {1/2, -(Sqrt[3]/2)}, {-1, 0}, {0,
>  0}, {1, 0}, {-(1/2), Sqrt[3]/2}, {1/2, Sqrt[3]/2}}
>
> Graphics[MapIndexed[{Text[First[#2], #1]} &, ls1]]


This certainly works in this case, and looks like a simple way to do it. 
And using MapIndexed is a handy way to plot the points.

I'm still puzzled about what's being handed to Sort. If it's just a True 
or False value for each pair of consecutive values as we go down the 
list, how exactly does that specify ordering by numerical value? I would 
think that Mathematica would revert to the default sorting for irrationals, 
which seemed to be based on complexity of expression. At any rate, I'd 
rather be able to pass the test function explicitly to Sort, as in

Sort[
hexPts,

#1[[2]] < #2[[2]]
     \[Or]
 (#1[[2]] == #2[[2]]   \[And]  #1[[1]] <= #2[[1]])
&
]


ls1 =
 Sort[
  ls,
  Which[
    #1[[2]] <  #2[[2]],                      True,
    #1[[2]] == #2[[2]], #1[[1]] <  #2[[1]],  True,
    False
    ] &
  ]

{{-(1/2), -(Sqrt[3]/2)}, {1/2, -(Sqrt[3]/2)}, {-1, 0}, {0, 0}, {1,
  0}, {-(1/2), Sqrt[3]/2}, {1/2, Sqrt[3]/2}}
Shouldn't the Help for Which say "_either_ True or False":

Conditions are evaluated until one is found that is neither True nor 
False:

In[1]:= x = 0

Out[1]= 0

In[2]:= Which[1 < 0, a,
 x == 0, b,
 0 < 1, c]

Out[2]= b


  • Prev by Date: Re: how to get string in sci. notation to a number?
  • Next by Date: Re: Mantaining the same form
  • Previous by thread: Re: Sorting coefficients
  • Next by thread: Re: Sorting coefficients