The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Pattern with powers
Replies: 7   Last Post: Aug 8, 2013 12:02 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
W. Craig Carter

Posts: 266
Registered: 9/10/05
Cases or Select?
Posted: Aug 8, 2013 12:02 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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@
Select[s, Exponent[#, x] >= 1 && Exponent[#, y] <= 1 &], {100}]]

(*roughly 75*)

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

(* roughly 73*)


(*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.


On Aug 2, 13, at 2:50 AM, 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.

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.