Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #11475

Re: How to use Pick[]; Is this a bug?

From awnl <awnl@gmx-topmail.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to use Pick[]; Is this a bug?
Date 2012-08-24 00:56 +0000
Message-ID <k16jf1$bej$1@smc.vnet.net> (permalink)
References <k14kr3$7ip$1@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


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

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web