Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #11464 > unrolled thread
| Started by | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| First post | 2012-08-23 07:07 +0000 |
| Last post | 2012-08-25 08:34 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: How to use Pick[]; Is this a bug? Bill Rowe <readnews@sbcglobal.net> - 2012-08-23 07:07 +0000
Re: How to use Pick[]; Is this a bug? awnl <awnl@gmx-topmail.de> - 2012-08-24 00:51 +0000
Re: How to use Pick[]; Is this a bug? awnl <awnl@gmx-topmail.de> - 2012-08-24 00:56 +0000
Re: How to use Pick[]; Is this a bug? Andrzej Kozlowski <akozlowski@gmail.com> - 2012-08-25 08:34 +0000
| From | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| Date | 2012-08-23 07:07 +0000 |
| Subject | Re: How to use Pick[]; Is this a bug? |
| Message-ID | <k14kr3$7ip$1@smc.vnet.net> |
On 8/22/12 at 2:24 AM, awnl@gmx-topmail.de (awnl) wrote:
>>>In[1]:= Pick[{1, 2, 3, 4, 5}, {9, 0, 7, 0, 6}, Except[0]]
>>>Out[1]:= {1, 2, 3, 4, 5}
>>>Shouldn't this return {1, 3, 5}, since these are the corresponding
>>>elements that match Except[0]?
>>the whole list {9, 0, 7, 0, 6} is unequal to the Integer 0.
>that's an explanation of what goes wrong, but it still looks very
>much like a bug to me: Nothing in the documentation gives a hint
>that the second argument as a whole would ever be tried to match the
>third argument -- and it wouldn't seem to make any sense to do so
>for the given functionality...
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.
That is:
In[1]:= MatchQ[{9, 0, 7, 0, 6}, Except[0]]
Out[1]= True
which is why the entire list is returned.
>> What you want is:
>> Pick[{1, 2, 3, 4, 5}, {9, 0, 7, 0, 6}, Except[0, _Integer]]
>that's of course a very useful workaround
This truly isn't a workaround. What is being done here is to
specify a more specific pattern that ensures it no longer
matches the entire expression. So, Mathematica tries to match
parts of the expression.
This same issue comes up anytime you use pattern matching. For
example consider:
In[2]:= data = RandomInteger[10, {3, 2}]
Out[2]= {{9, 2}, {0, 9}, {1, 3}}
In[3]:= data /. {a_, b_} -> {a - b, a + b}
Out[3]= {{7, 11}, {-9, 9}, {-2, 4}}
My pattern looks for a two element list an replaces each two
element list with the difference and sum of the elements it
contains. And since the entire expression is a 3 element list
with each element being a two element list, the entire list is
not matched and I get the expected result.
But look what happens if I drop the first element of data using Rest
In[4]:= Rest[data] /. {a_, b_} -> {a - b, a + b}
Out[4]= {{-1, 6}, {1, 12}}
Now the pattern does match the entire list and I get a much
different result. And like the example with Pick, I can specify
the pattern as {a_Integer,b_} which will prevent matching the
entire expression.
These details of pattern matching are not repeated in the
documentation for every function that uses pattern matching. But
this information is in the documentation. And pattern matching
in this case is working as documented.
[toc] | [next] | [standalone]
| From | awnl <awnl@gmx-topmail.de> |
|---|---|
| Date | 2012-08-24 00:51 +0000 |
| Message-ID | <k16j5n$bbs$1@smc.vnet.net> |
| In reply to | #11464 |
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
[toc] | [prev] | [next] | [standalone]
| From | awnl <awnl@gmx-topmail.de> |
|---|---|
| Date | 2012-08-24 00:56 +0000 |
| Message-ID | <k16jf1$bej$1@smc.vnet.net> |
| In reply to | #11464 |
Hi,
just a followup to my previous answer to this...
> This same issue comes up anytime you use pattern matching. For
> example consider:
>
> In[2]:= data = RandomInteger[10, {3, 2}]
>
> Out[2]= {{9, 2}, {0, 9}, {1, 3}}
>
> In[3]:= data /. {a_, b_} -> {a - b, a + b}
>
> Out[3]= {{7, 11}, {-9, 9}, {-2, 4}}
>
> My pattern looks for a two element list an replaces each two
> element list with the difference and sum of the elements it
> contains. And since the entire expression is a 3 element list
> with each element being a two element list, the entire list is
> not matched and I get the expected result.
>
> But look what happens if I drop the first element of data using Rest
>
> In[4]:= Rest[data] /. {a_, b_} -> {a - b, a + b}
>
> Out[4]= {{-1, 6}, {1, 12}}
>
> Now the pattern does match the entire list and I get a much
> different result. And like the example with Pick, I can specify
> the pattern as {a_Integer,b_} which will prevent matching the
> entire expression.
that's exactly one of those cases where I consider a "smarter pattern"
to be rather a workaround than a solution. An explicit level
specification is very often a much better solution, in this case it will
allow a solution that also work for reals, symbols or arbitrary expressions:
Replace[Rest[data], {a_, b_} -> {a - b, a + b},{1}]
and also, for example:
Replace[{{0.5, 0.2}, {x, f[y]}}, {a_, b_} -> {a - b, a + b}, {1}]
The point is that I think you should, whenever possible, be able to
separate the concerns of pattern matching and level specification...
regards,
albert
[toc] | [prev] | [next] | [standalone]
| From | Andrzej Kozlowski <akozlowski@gmail.com> |
|---|---|
| Date | 2012-08-25 08:34 +0000 |
| Message-ID | <k1a2mi$igc$1@smc.vnet.net> |
| In reply to | #11464 |
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@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
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web