Re: Picking Arguments
- To: mathgroup at smc.vnet.net
- Subject: [mg74278] Re: Picking Arguments
- From: "Drago Ganic" <dganic at vodatel.net>
- Date: Fri, 16 Mar 2007 03:20:31 -0500 (EST)
- Organization: Customer of Vodatel, Zagreb, CROATIA
- References: <esdka9$ou3$1@smc.vnet.net> <et5npe$jij$1@smc.vnet.net>
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 > >