MathGroup Archive 2011

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

Search the Archive

Re: list equation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121931] Re: list equation
  • From: "Dr. Wolfgang Hintze" <weh at snafu.de>
  • Date: Fri, 7 Oct 2011 04:43:15 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <j6jpek$me8$1@smc.vnet.net>

Use Sort/@ to sort each element {x,y} and then remove duplicates using 
Union.

This is about 2.500 times faster, as you can see here:

(* your function on my PC *)
lst = Tuples[Range[100], 2]; Timing[lstnew = Union[lst, SameTest ->(#1 
== Reverse[#2] & )]][[1]]
40.797*Second

(* the improved function *)
Table[lst = Tuples[Range[10^k], 2]; Timing[Union[Sort /@ lst]][[1]], 
{k, 1, 3}]
{0.*Second, 0.016*Second, 0.828*Second}

(* comparison *)
40.797/0.016
2549.8125

Wolfgang

"dimitris" <dimmechan at yahoo.com> schrieb im Newsbeitrag 
news:j6jpek$me8$1 at smc.vnet.net...
> Hello to all.
>
> Let
>
> lst = Tuples[Range[100], 2];
>
> In the previous list appear elements such us {x,y} and {y,x}. (e.g.
> {3,4} and {4,3}).
> I want to create a new list with {y,x} dropped (that is, in the new
> list appears only {3,4}
> and not {4,3}).
>
> I use
>
> lstnew = Union[lst, SameTest -> (#1 == Reverse[#2] &)]
>
> However it is needed almost 150 sec for this procedure ($Version->5.2
> for Windows)
> I know that my laptop is too old but I guess there is a more 
> efficient
> way to
> create lstnew.
>
> Any ideas?
>
> Thanks a lot
>
> Dimitris
> 




  • Prev by Date: Re: average of the consecutive coin tosses
  • Next by Date: Re: list equation
  • Previous by thread: Re: list equation
  • Next by thread: Re: list equation