Re: How to use Pick[]; Is this a bug?
- To: mathgroup at smc.vnet.net
- Subject: [mg127826] Re: How to use Pick[]; Is this a bug?
- From: Andrzej Kozlowski <akozlowski at gmail.com>
- Date: Sat, 25 Aug 2012 04:24:20 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-newout@smc.vnet.net
- Delivered-to: mathgroup-newsend@smc.vnet.net
- References: <k14kr3$7ip$1@smc.vnet.net> <20120824005054.A0C4E6877@smc.vnet.net>
http://forums.wolfram.com/mathgroup/archive/2005/Dec/msg00240.html What has been will be again, what has been done will be done again; there is nothing new under the sun. (Ecclesiastes 1:9) Andrzej Kozlowski On 24 Aug 2012, at 02:50, awnl <awnl at gmx-topmail.de> wrote: > Hi, > >> Sigh... Yes, documentation can be improved. But it truly isn't >> accurate to say "nothing in the documentation gives a hint...". >> The key point is the syntax specified for Pick, i.e., >> >> Pick[list, sel, patt] >> >> The important part is patt short for pattern, telling you Pick >> is doing pattern matching. Other parts of the documentation >> discussion pattern matching and how Mathematica goes about it >> point out Mathematica tries to match the entire expression with >> the specified pattern. > > I don't think that's the point and tried to make clear that I appreciate > and understand the explanation of Peter about what happens. > > Pattern matching can very well be restricted to only work on certain > levels of an expression and probably is more often used with such > restrictions than without, ReplaceAll probably rather being an exception > than the rule. There are several functions which do use pattern matching > and *do* restrict levels, at least by default (c.f. Cases[{0, 1, 2, 0, > 3, 4}, Except[0]]) because *that makes sense for their intended > purpose*. There are even functions which basically do the same thing > except for the (default) level specification, c.f. MemberQ[expr,pat,{0}] > vs. MatchQ[expr,pat] (not that MatchQ always only works at level 0). I > think it's not unreasonable to expect Pick to choose an appropriate > level specification as well when reading this (from the documentation): > > Pick[list,sel,patt] picks out those list[[Subscript[i, 1],Subscript[i, > 2],...]] for which sel[[Subscript[i, 1],Subscript[i, 2],...]] matches patt. > > especially when there is such an obvious match of the shapes of list and > sel as in this case and. > > Thinking about it I agree that it might not be justified to call this a > plain bug, as the documentation is indeed vague enough to allow the > observed behavior (if you allow Subscript[i, 1],Subscript[i, 2],... to > also mean All) -- just as it would allow something else. But I don't see > the problem here is just that the documentation could be improved, but > believe that the design decision to use all levels for the pattern > matching in Pick without an alternative is not optimal. I say this > because I found that making an explicit level specification in pattern > matching functions has proven to be a much clearer and more robust > approach for me than to try to tweak the patterns to perform the desired > level specification "implicitly". Because of that I think a better > solution would be to add a level specification argument to Pick, which > could do something like: > > myPick[list_, sel_, pat_, levelspec_] := > Pick[list, Replace[sel, {pat :> True, _ :> False}, levelspec]] > > and would allow to get the desired result like this: > > myPick[{1, 2, 3, 4, 5}, {9, 0, 7, 0, 6}, Except[0], {1}] > > Comparing to such an explicit level specification you'll find that Pick > implicitly uses levelspec={0,Infinity}. An alternative would be to use > levelspec={1,Infinity} for a default behavior which would make more > sense IMHO, as making a function call to pick All of an expression seems > a relatively odd corner case of how one would want to use Pick. Note > that e.g. for Cases and many other functions such an admittedly somewhat > arbitrary choice has been made. As it doesn't make a statement about > which levelspec it uses for it's pattern matching, the documentation for > Pick would perfectly well allow to use {1,Infinity} (or any other level > specification) as the internal setting... > > Whatever we call it, I would certainly not blame the user here for > having a wrong expectation of how Mathematica should work in this case. > To make that clear was the only intention of my previous post... > > regards, > > albert >
- References:
- Re: How to use Pick[]; Is this a bug?
- From: awnl <awnl@gmx-topmail.de>
- Re: How to use Pick[]; Is this a bug?