Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: reliably sort?

10 views
Skip to first unread message

Leonid Shifrin

unread,
Dec 15, 2011, 4:51:40 AM12/15/11
to
Union knows nothing about dates. It sorts according to a universal
canonical order, which is
described in the documentation for Sort. You real question then is whether
the Mathematica
canonical order will always coincide with the ordering by date, for your
lists of data. My
first impression is that this is so, but you may want to give it more
thought - there could be
some corner cases. Anyways, my main point is that you can answer this
question unambiguously
by consulting the Sort documentation.

Regards,
Leonid


On Wed, Dec 14, 2011 at 2:02 PM, Michael Stern <nycs...@gmail.com> wrote:

> I have noticed that if combine two lists of date/data objects using
> Union[], the result is sorted by date.
>
> For example
>
> In[]:= mins={{{2001, 1, 31}, 0.993268}, {{2002, 3, 31}, 1.01395},
> {{2003, 6, 30},
> 1.08647}, {{2005, 11, 30}, 1.14752}, {{2006, 9, 30},
> 1.18938}, {{2007, 2, 28}, 1.19658}, {{2008, 1, 31},
> 1.20432}, {{2011, 1, 31}, 1.37501}};
> In[]:= maxs={{{2000, 10, 31}, 1.01816}, {{2001, 12, 31}, 1.02714},
> {{2004, 2, 29},
> 1.12702}, {{2005, 3, 31}, 1.16986}, {{2010, 10, 31}, 1.39026}};
>
> In[]:= Union[maxs, mins]
>
> Out[]= {{{2000, 10, 31}, 1.01816}, {{2001, 1, 31},
> 0.993268}, {{2001, 12, 31}, 1.02714}, {{2002, 3, 31},
> 1.01395}, {{2003, 6, 30}, 1.08647}, {{2004, 2, 29},
> 1.12702}, {{2005, 3, 31}, 1.16986}, {{2005, 11, 30},
> 1.14752}, {{2006, 9, 30}, 1.18938}, {{2007, 2, 28},
> 1.19658}, {{2008, 1, 31}, 1.20432}, {{2010, 10, 31},
> 1.39026}, {{2011, 1, 31}, 1.37501}}
>
> Is this reliable behavior? Is there any case in which Union would /not/
> return results that have been properly sorted by date?
>
> Thank you,
>
> -Michael Stern
>

DrMajorBob

unread,
Dec 15, 2011, 4:58:52 AM12/15/11
to
Union always sorts in "canonical order".

If you want unsorted order:

Tally[Join[max,mins]][[All,1]]

Bobby

On Wed, 14 Dec 2011 05:02:50 -0600, Michael Stern <nycs...@gmail.com>
wrote:

> I have noticed that if combine two lists of date/data objects using
> Union[], the result is sorted by date.
>
> For example
>
> In[]:= mins={{{2001, 1, 31}, 0.993268}, {{2002, 3, 31}, 1.01395},
> {{2003, 6, 30},
> 1.08647}, {{2005, 11, 30}, 1.14752}, {{2006, 9, 30},
> 1.18938}, {{2007, 2, 28}, 1.19658}, {{2008, 1, 31},
> 1.20432}, {{2011, 1, 31}, 1.37501}};
> In[]:= maxs={{{2000, 10, 31}, 1.01816}, {{2001, 12, 31}, 1.02714},
> {{2004, 2, 29},
> 1.12702}, {{2005, 3, 31}, 1.16986}, {{2010, 10, 31}, 1.39026}};
>
> In[]:= Union[maxs, mins]
>
> Out[]= {{{2000, 10, 31}, 1.01816}, {{2001, 1, 31},
> 0.993268}, {{2001, 12, 31}, 1.02714}, {{2002, 3, 31},
> 1.01395}, {{2003, 6, 30}, 1.08647}, {{2004, 2, 29},
> 1.12702}, {{2005, 3, 31}, 1.16986}, {{2005, 11, 30},
> 1.14752}, {{2006, 9, 30}, 1.18938}, {{2007, 2, 28},
> 1.19658}, {{2008, 1, 31}, 1.20432}, {{2010, 10, 31},
> 1.39026}, {{2011, 1, 31}, 1.37501}}
>
> Is this reliable behavior? Is there any case in which Union would /not/
> return results that have been properly sorted by date?
>
> Thank you,
>
> -Michael Stern


--
DrMaj...@yahoo.com

Bill Rowe

unread,
Dec 15, 2011, 5:03:58 AM12/15/11
to
On 12/14/11 at 6:02 AM, nycs...@gmail.com (Michael Stern) wrote:

>I have noticed that if combine two lists of date/data objects using
>Union[], the result is sorted by date.

Example data snipped.

>Is this reliable behavior? Is there any case in which Union would
>/not/ return results that have been properly sorted by date?

By default with multi-column data, Union sorts on the first
column. Since your example data two columns with dates in the
first column, it was sorted by date. This behavior can be relied
on with similarly formatted data.


Andy Ross

unread,
Dec 16, 2011, 5:53:56 AM12/16/11
to
Personally I prefer DeleteDuplicates for this. At least for packed
arrays it tends to be about twice as fast. For arbitrary arrays it can
be many times faster (though I'm sure instances could be found where it
loses).

In[28]:= data = RandomChoice[{"A", "B", "C"}, 10^7];

In[29]:= DeleteDuplicates[data] // AbsoluteTiming

Out[29]= {0.609038, {"B", "A", "C"}}

In[30]:= Tally[data][[All, 1]] // AbsoluteTiming

Out[30]= {2.946952, {"B", "A", "C"}}

-Andy

On 12/15/2011 3:53 AM, DrMajorBob wrote:
> Union always sorts in "canonical order".
>
> If you want unsorted order:
>
> Tally[Join[max,mins]][[All,1]]
>
> Bobby
>
> On Wed, 14 Dec 2011 05:02:50 -0600, Michael Stern<nycs...@gmail.com>
> wrote:
>
>> I have noticed that if combine two lists of date/data objects using
>> Union[], the result is sorted by date.
>>
>> For example
>>
>> In[]:= mins={{{2001, 1, 31}, 0.993268}, {{2002, 3, 31}, 1.01395},
>> {{2003, 6, 30},
>> 1.08647}, {{2005, 11, 30}, 1.14752}, {{2006, 9, 30},
>> 1.18938}, {{2007, 2, 28}, 1.19658}, {{2008, 1, 31},
>> 1.20432}, {{2011, 1, 31}, 1.37501}};
>> In[]:= maxs={{{2000, 10, 31}, 1.01816}, {{2001, 12, 31}, 1.02714},
>> {{2004, 2, 29},
>> 1.12702}, {{2005, 3, 31}, 1.16986}, {{2010, 10, 31}, 1.39026}};
>>
>> In[]:= Union[maxs, mins]
>>
>> Out[]= {{{2000, 10, 31}, 1.01816}, {{2001, 1, 31},
>> 0.993268}, {{2001, 12, 31}, 1.02714}, {{2002, 3, 31},
>> 1.01395}, {{2003, 6, 30}, 1.08647}, {{2004, 2, 29},
>> 1.12702}, {{2005, 3, 31}, 1.16986}, {{2005, 11, 30},
>> 1.14752}, {{2006, 9, 30}, 1.18938}, {{2007, 2, 28},
>> 1.19658}, {{2008, 1, 31}, 1.20432}, {{2010, 10, 31},
>> 1.39026}, {{2011, 1, 31}, 1.37501}}
>>
>> Is this reliable behavior? Is there any case in which Union would /not/
>> return results that have been properly sorted by date?
>>
>> Thank you,
>>
>> -Michael Stern
>


DrMajorBob

unread,
Dec 16, 2011, 6:05:23 AM12/16/11
to
There are no "mathematical sets" in Mathematica.

Bobby

On Thu, 15 Dec 2011 20:10:58 -0600, Shizu <slivo...@msa.hinet.net> wrote:

> Union[] is defined for "mathematical sets". The elements are sorted and
> repeated elements are deleted.
>
> With Join[], we can just concatenate lists together, without sorting or
> dropping.


--
DrMaj...@yahoo.com

Shizu

unread,
Dec 17, 2011, 2:42:27 AM12/17/11
to
Search "tutorial/ListsAsSets".

Then you can find the tutorial about "lists as sets".
When I say {1, 4, 2, 5, 3}, it's a set of the first 5 integers, isn't it?
Also in Mathematica, there are some built-in sets like Reals, Integers, Rationals, etc.

For example, we can use Element[] to check and see whether a number is rational:

In[]= Element[1/3, Rationals]
Out[]= True

Bill Rowe

unread,
Dec 17, 2011, 2:52:41 AM12/17/11
to
On 12/16/11 at 5:54 AM, ulrich...@data2knowledge.de (Ulrich Arndt)
wrote:

>Union and Sort seems not naturally connected to me. Union is a set
>operation and according to the documentation it seems that the
>implementation is using some sorting...

As mathematical operations union and sort are not connected.
That is there is no mathematical reason to sort a group of items
to do the mathematical operation of union. But, you need to keep
in mind the practical implementation of these operations on a computer.

To perform an union (on an unordered set) you would need to
compare each element with every other element, i.e., n^2
compares. But if the set is sorted, you only need compare each
element to the next element for n compares. Since there are sort
algorithms that are more efficient than n^2, the most efficient
way to implement union is to first use an efficient sort
algorithm then do the union. Although I don't work for Wolfram
and do not have access to the source code for Mathematica, I am
certain Union does a Sort for the reason I've outlined.


0 new messages