Re: Picking Arguments
- To: mathgroup at smc.vnet.net
- Subject: [mg74157] Re: Picking Arguments
- From: "dimitris" <dimmechan at yahoo.com>
- Date: Tue, 13 Mar 2007 03:35:07 -0500 (EST)
- References: <esdka9$ou3$1@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