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


Groups > comp.soft-sys.math.mathematica > #15288

Cases or Select?

From W Craig Carter <ccarter@MIT.EDU>
Newsgroups comp.soft-sys.math.mathematica
Subject Cases or Select?
Date 2013-08-08 04:02 +0000
Message-ID <ktv58t$agp$1@smc.vnet.net> (permalink)
References <ktaj20$llc$1@smc.vnet.net> <ktcnaf$r2b$1@smc.vnet.net> <20130802065025.0EF4969D7@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


Hello Danny,
I have the same subjective opinion, Select seems more natural than Cases in this case.
I can never remember which of the two goes with patterns and which with functions in any case--I think I've looked up the syntax on these functions more than any others.

So, I decided to run a little test:

s = List @@ Expand[5 (x + y)^100000];

ts = First@
  Timing[Do[
    Select[s, Exponent[#, x] >= 1 && Exponent[#, y] <= 1 &], {100}]]

(*roughly 75*)

tc = First@
  Timing[Do[
    Cases[s,
     aa_ /; Exponent[aa, x] >= a && Exponent[aa, y] <= a], {100}]]

(* roughly 73*)

tc/ts
 
(*roughly 1.03*)

I had guessed that the function call would have been faster... but I guessed wrong.  I don't believe there is any caching of results here, so it seems a fair comparison.

I suppose that if the function in Select had only numeric arguments, then using Compile might make Select a better choice.


Craig



On Aug 2, 13, at 2:50 AM, danl@wolfram.com wrote:

> In[20]:= a = 1;
>
> In[21]:= Select[s, Exponent[#, x] >= a && Exponent[#, y] <= a &]
>
> Out[21]= {5 x^3, 15 x^2 y}
>
> Obviously this can also be done with Cases, but that seems a bit more awkward to me.
>
> In[22]:= Cases[s, aa_ /; Exponent[aa, x] >= a && Exponent[aa, y] <= a]
>
> Out[22]= {5 x^3, 15 x^2 y}
>
> Since "natural" and "awkward" are in the mind of the beholder, one might well hold an opinion the reverse of my own.

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: Pattern with powers Alexei Boulbitch <Alexei.Boulbitch@iee.lu> - 2013-07-31 08:49 +0000
  Re: Pattern with powers "Dr. Wolfgang Hintze" <weh@snafu.de> - 2013-08-01 04:14 +0000
    Re: Pattern with powers danl@wolfram.com - 2013-08-02 06:49 +0000
    Cases or Select? W Craig Carter <ccarter@MIT.EDU> - 2013-08-08 04:02 +0000

csiph-web