Re: Inconsistencies in pattern matching.
- To: mathgroup at smc.vnet.net
- Subject: [mg12985] Re: Inconsistencies in pattern matching.
- From: Tobias Oed <tobias at physics.odu.edu>
- Date: Sun, 28 Jun 1998 02:52:08 -0400
- Organization: Old Dominion University
- References: <6mqf4r$3he@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Sean Ross wrote:
> This is an example taken from Ken Wagners Power Programming book:
>
> In[1]:={{x1,y1},{x2,y2},{x3,y3}}/.{x_,y_}->{x,Log[y]}
>
> Out[1]:={{x1,Log[y1]},{x2,Log[y2]},{x3,Log[y3]}}
>
> In[2]:={{x1,y1}}/.{x_,y_}->{x,Log[y]}
>
> Out[2]:={{x1,Log[y1]}}
>
> As long as the list of {x,y} data points has one point or greater than
> two points, it transforms as one would expect. If there are two {x,y}
> data points, it transforms differently.
>
> In[3]:={{x1,y1},{x2,y2}}/.{x_,y_}->{x,Log[y]}
>
> Out[3]:={{x1,y1},{Log[x2],Log[y2]}}
>
> Now, if x_ is seen to match {x1,y1} in example 3, then why doesn't it
> also match it in example number 1 and 2? This behavior can be fixed
> with a /;Head[x]=!=List pattern restricting rule, but that is not the
> point. This seems grossly inconsistent to me. Can anyone explain why
> it does this and/or justify that this is a good thing?
In the last example the pattern x_ matches is {x1,y1} and y_ matches
{x2,y2}
(x_ matches any mathematica expression) so the result is consistent.
Since Log
is Listable Log[{x2,xy}] gives {Log[x2],Log[y2]}. The following works:
In[12]= {{x1,y1},{x2,y2}}/.{x_?AtomQ,y_?AtomQ}->{x,Log[y]} Out[12]=
{{x1, Log[y1]}, {x2, Log[y2]}}
Hope this helps Tobias