MathGroup Archive 2007

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

Search the Archive

Re: Re: programming problem about elements taken

On 3 Jan 2007, at 06:03, Ray Koopman wrote:

> Andrzej Kozlowski wrote:
>> Consider this list of 5 numbers:
>>   ls = Sort[Table[Random[], {5}]]
>>   {0.165874, 0.256035, 0.556211, 0.811305, 0.865799}
>> Suppose we take epsilon = 0.3 and identify the numbers distant by
>> less than that from one another. Then
>> Union[ls, SameTest -> (Abs[#1 - #2] < 0.3 &)]
>>   {0.165874, 0.556211, 0.865799}
>> gives us just three numbers.
>> [...]
>> Note that when two numbers are identified it is the smaller one that
>> is kept.
>> [...]
> Is this just an observation, or official-but-not-publicly-documented?
> The arguments to SameTest seem always to be reverse-ordered (i.e.,
> #2 >= #1), even when the input list is unsorted, and the sequence of
> argument pairs seems to be the same regardless of whether the input
> list is sorted or unsorted. Can we conclude that Union sorts the input
> list before using SameTest, and that when SameTest returns True it is
> the second argument that is kept?

Yes, it is all true and actually I have known it fro a very long  
time, so it is probably documented (but I can't check that right  
now). Therefore there was no need to sort the list before running the  
code  and SameTest could be simply (#2-#1<0.3&). I suppose I faield  
to think of  that because I  so used to working with spaces more  
general the just the real line. The code will work, I think, with a  
sequence of complex numbers (and Abs can even be replaced by Norm)  
where we identify them if they are less than espilon apart in the  
complex plane.
I have never tested how fast it is; probably not very fast,  since   
Union works much faster with the default value of SameTest (the same  
is true for Sort which is much slower with a user defined ordering  

Andrzej Kozlowski

  • Prev by Date: Re: List representation using element position
  • Next by Date: Re: List representation using element position
  • Previous by thread: Re: programming problem about elements taken
  • Next by thread: Re: Re: Re: programming problem about elements