Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7678 > unrolled thread

Re: Sorting coefficients

Started byChristopher Young <cy56@comcast.net>
First post2012-01-09 08:18 +0000
Last post2012-01-09 08:18 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re:  Sorting coefficients Christopher Young <cy56@comcast.net> - 2012-01-09 08:18 +0000

#7678 — Re: Sorting coefficients

FromChristopher Young <cy56@comcast.net>
Date2012-01-09 08:18 +0000
SubjectRe: Sorting coefficients
Message-ID<jee7t9$cn6$1@smc.vnet.net>
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

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web