MathGroup Archive 2008

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

Search the Archive

Re: List complement operator

  • To: mathgroup at smc.vnet.net
  • Subject: [mg85087] Re: List complement operator
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Sun, 27 Jan 2008 05:51:18 -0500 (EST)
  • Organization: University of Bergen
  • References: <fn1ndi$97t$1@smc.vnet.net> <fn21s3$irs$1@smc.vnet.net> <fnessh$i8c$1@smc.vnet.net>

sashap wrote:
> Under the assumption that lists a and b contain positive integers the
> following
> version is the fastest:
> 
> f5[a_List, b_List] :=
>  Block[{x},
>   Join @@ (ConstantArray @@@
>      DeleteCases[
>       List @@ (Total[(#2 x^#1 & @@@ Tally[a])] -
>           Total[(#2 x^#1 & @@@ Tally[b])]) /. {c_. x^(n_.) :> {n,
>           c}}, {_, _?NonPositive}])]
> 
> In[85]:= Timing[r1 = f1[a, b];]
> 
> Out[85]= {0.547, Null}
> 
> In[86]:= Timing[r3 = f3[a, b];]
> 
> Out[86]= {0.719, Null}
> 
> In[87]:= Timing[r5 = f5[a, b];]
> 
> Out[87]= {0.015, Null}
> 
> In[88]:= r1 === r3 === r5
> 
> Out[88]= True
> 

That's a very smart trick, exploiting the properties of Plus and Times, 
and avoiding the need to align the results from Tally[a] and Tally[b]!

:-)

Szabolcs


  • Prev by Date: Re: Better way to write this
  • Next by Date: Re: Wavelet "filter"?
  • Previous by thread: Re: List complement operator
  • Next by thread: Re: List complement operator