Re: Re: Picking Arguments

*To*: mathgroup at smc.vnet.net*Subject*: [mg74303] Re: Re: Picking Arguments*From*: "Drago Ganic" <dganic at vodatel.net>*Date*: Sun, 18 Mar 2007 00:42:34 -0500 (EST)*References*: <etg4ac$ili$1@smc.vnet.net>

Hi Bob and others, During the years playing with Mathematica I have learned that in Mathematica every function is designed not only for functionality but also for scalability (eg. Total[data] has not the same perfomanse as Plus@@data for specific large data sets). Here are your examples but for a larger data set (Symbols are not included ... just integer and real numbers). data=Join[ Array[Random[ Integer, {0,100}]&, 10^7], Array[Random[ Real, {0,100}]&, 10^7] ]; ---- explample set No 1 Cases[data, _Real ]; // Timing {6.032 Second, Null} Select[data, Head[#] == Real &]; // Timing {33.406 Second, Null} Select[ data, Element[#, Reals] &]; // Timing {25.875 Second, Null} ---- explample set No 2 Select[data, NumberQ]; // Timing {8.312 Second, Null} Cases[data, _?NumberQ]; // Timing {13.485 Second, Null} Cases[data, _Integer | _Rational | _Real | _Complex]; // Timing {10.562 Second, Null} So we see that even the end results are the same, the time to compute the results is not the same meaning we should try to use the right function for the right job. If everything would be the same (including convenience for function names) than having many functions for the same problem would mean bad language design. I think that is not the case in Mathematica. Drago "Bob Hanlon" <hanlonr at cox.net> wrote in message news:etg4ac$ili$1 at smc.vnet.net... > While I agree that functions can be better suited (more natural) to > specific problems, what is natural tends to be whatever comes to one's > mind first. Select and Cases are often interchangeable. > > data={1, 2.3, 3/2,a,Pi}; > > Select[ data, Element[#,Reals]&]== > Select[data,NumericQ]== > Cases[data,_?NumericQ] > > True > > Cases[data, _Real ]== > Select[data,Head[#]==Real&] > > True > > Cases[data,_Integer|_Rational|_Real]== > Cases[data,_?NumberQ] == > Select[data, NumberQ] > > True > > > Bob Hanlon > > ---- Drago Ganic <dganic at vodatel.net> wrote: >> Hi, >> I just want to add to the difference of Select and Case. I think they >> represent two important functions for a different domain of programming: >> semantic Select >> syntax Cases >> >> which sometimes can be used for the same problem but usually one method >> is >> better that the other for a certain problem. >> >> If you look at all the List manipulation functions we have only three >> access >> methods to the elements of lists: >> 1. postion => via an integer number, eg. Part[] >> 2. pattern => via a syntactical pattern, eg. Cases[] >> 3. logical => via a logical boolean expression, eg. Select[] >> >> Here is a example: >> >> In[1]:= Select[ {1, 2.3, a}, # \[Element] Reals &] >> Out[1]= {1, 2.3} >> >> In[2]:= Cases[{ 1, 2.3, a}, _Real ] >> Out[2]= {2.3} >> >> In[3]:= Cases[{ 1, 2.3, a}, _Integer | _Rational | _Real] >> Out[3]= {1, 2.3} >> >> It's a nice example for thinking about what OO inheritance might be in >> Mathematica. >> >> I would says that Mathematica defines "types" in a syntactical way (head >> of >> expression) and uses them in pattern matching function (e.g. Cases), and >> defines "domain" in a more semantical (logical/mathematical) way and uses >> them in other functions (like Select). >> >> Of course types in Mathematica are more something like IntegerQ[] and not >> _Integer and hence are a semantical "thing" and not just syntactical. >> >> Greeting from Croatia, >> Drago >> >> "dimitris" <dimmechan at yahoo.com> wrote in message >> news:et5npe$jij$1 at smc.vnet.net... >> > As a second thought to your query I would suggest you to read >> > carefully the reply of >> > Andrzej Kozlowski to the recent thread with the title: "Map function >> > which adds last two numbers of a list". >> > Not very relevant at first glance but I think it talks about the right >> > "attitude" for attacking a "real" problem. >> > >> > http://groups.google.gr/group/comp.soft-sys.math.mathematica/browse_thread/= >> > thread/3b38366201665b24/9c76eb8c939ce75d?lnk=st&q=&rnum=28&hl=el#9c= >> > 76eb8c939ce75d >> > >> > >> > Anyway... >> > >> > You have many ways to do what you want. You found by yourself (well >> > done!) that Cases gives your desired output. >> > >> > So why hanging around with Select? >> > >> > You could say for programming practise. But (at least!) for me the >> > important thing is to understand that Select is not "designed" for >> > doing things like what you desire. Cases is the key here (or other >> > built in functions suggested in the replies you got by some of the >> > gurus of this beautiful forum; no I don't talk about myself! I talk >> > about Bob Hanlon and Jean-Marc Gulliet!) . Not Select; except if you >> > just want to end with an extravagant code! So what? What's the deal?) >> > >> > As regards myself, Understanding the (BIG!) Difference between Select >> > and Cases is the most important thing. >> > >> > I copy directly from M. Trott's Guidebook for Programming... >> > >> > Select picks the arguments according to the truth value, and it >> > delivers the result with the same head as the selected expression. >> > Cases chooses according to patterns, and it gives a result in the form >> > of a list. The optional third argument in the two functions also has a >> > completely different role. In Select, it defines the number of objects >> > to be selected, whereas in Cases, it gives the level specification at >> > which the first argument is to be tested. >> > >> > Another issue which you should be more aware of (in my point of view >> > of course!) is why RuleDelayed is >> > unnessary here. >> > >> > A = {f[x], g[p, q, r, s], h[u, v, w]} >> > {f[x], g[p, q, r, s], h[u, v, w]} >> > >> > Cases[A, _[args__] :> {args}] >> > {{x}, {p, q, r, s}, {u, v, w}} >> > >> > Cases[A, _[args__] -> {args}] >> > {{x}, {p, q, r, s}, {u, v, w}} >> > >> > Trace[Cases[A, _[args__] :> {args}]] >> > {{HoldForm[A], HoldForm[{f[x], g[p, q, r, s], h[u, v, w]}]}, >> > HoldForm[Cases[{f[x], g[p, q, r, s], h[u, v, w]}, _[args__] :> >> > {args}]], HoldForm[{{x}, {p, q, r, s}, {u, v, w}}]} >> > >> > Trace[Cases[A, _[args__] -> {args}]] >> > {{HoldForm[A], HoldForm[{f[x], g[p, q, r, s], h[u, v, w]}]}, >> > HoldForm[Cases[{f[x], g[p, q, r, s], h[u, v, w]}, _[args__] -> >> > {args}]], HoldForm[{{x}, {p, q, r, s}, {u, v, w}}]} >> > >> > >> > What about here? >> > >> > sols = Solve[x^3 == 1] >> > {{x -> 1}, {x -> -(-1)^(1/3)}, {x -> (-1)^(2/3)}} >> > >> > sols /. (a_ -> b_) -> a -> ComplexExpand[b] >> > {{x -> 1}, {x -> -(-1)^(1/3)}, {x -> (-1)^(2/3)}} >> > >> > sols /. (a_ -> b_) :> a -> ComplexExpand[b] >> > {{x -> 1}, {x -> -(1/2) - (I*Sqrt[3])/2}, {x -> -(1/2) + >> > (I*Sqrt[3])/ >> > 2}} >> > >> > >> > If you have finished the relevant material in the Help Browser about >> > (the so much!) useful Built-in Symbols >> > like Select, Cases, Fold, Nest, Map, Apply of Functional Progarmming >> > and you want more things to read/practise (and avoid unnessary hanging >> > around...) trying >> > >> > http://verbeia.com/mathematica/tips/Tricks.html >> > http://verbeia.com/mathematica/tips/GraphicsTricks.html >> > >> > that is the (very famous and so properly called) Suplementary Help >> > Browser of Ted Ersek! >> > As notebooks see here: >> > >> > http://library.wolfram.com/infocenter/MathSource/4557/ >> > >> > Very challenging material! >> > >> > >> > And here is something truly amazing! >> > >> > http://documents.wolfram.com/flash/ >> > >> > See also here >> > >> > http://library.wolfram.com/infocenter/MathSource/4286/ >> > >> > and here >> > >> > http://www.mathematica.co.kr/ >> > >> > >> > Best Regards >> > Dimitris >> > >> > >> > >> > =CF/=C7 Mr Ajit Sen =DD=E3=F1=E1=F8=E5: >> >> Dear MathGroup, >> >> >> >> Given a list of functions >> >> >> >> A={f[x],g[p,q,r,s],h[u,v,w]}, >> >> >> >> I'd like to pick out their arguments as a list. >> >> >> >> Cases[A,_[args__]:>{args}] >> >> >> >> works fine returning {{x}, {p,q,r,s}, {u,v,w}}. >> >> >> >> How do I achieve the same thing with Select ? >> >> >> >> >> >> Thanks for your help. >> >> >> >> Ajit Sen >> >> ___________________________________________________________ >> >> Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with >> >> voic= >> > email http://uk.messenger.yahoo.com > >

**Boundary Conditions in PDE**

**Plot3D Default ColorFunction**

**Re: Picking Arguments**

**Rigid body equations**