Re: Rule replacement doesn't work after NDSolve?
- To: mathgroup at smc.vnet.net
- Subject: [mg124019] Re: Rule replacement doesn't work after NDSolve?
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Fri, 6 Jan 2012 04:13:35 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <je3vls$eao$1@smc.vnet.net>
On 2012.01.05. 11:57, Bill Rowe wrote:
> On 1/4/12 at 5:03 AM, g.crlsn at gmail.com (gac) wrote:
>
>> Thanks very much for the reply. Your suggestion works as you say,
>> but I'm still confused. I thought that the replacement is for the
>> function, not the arguments.
>
> Replacement rules are not limited to either functions or
> arguments. For example, I can do:
>
> In[3]:= Cos[y] /. Cos -> Sin
>
> Out[3]= Sin[y]
>
> replacing the Cos function with Sin or
>
> In[4]:= Cos[y] /. y -> 2 x
>
> Out[4]= Cos[2*x]
>
> replacing the argument y with 2 x
>
> The thing to keep in mind with replacement rules is a litteral
> match is made between the FullForm of the expression you want to
> operate on and the left hand side of the replacement rule. For
> example, this does nothing:
>
> In[5]:= -2 I /. I -> -I
>
> Out[5]= -2*I
>
> since
>
> In[6]:= FullForm[-2 I]
>
> Out[6]//FullForm= Complex[0,-2]
>
> and there is nothing to match the left hand side of the
> replacement rule
>
It is an interesting example your bring up here.
Although FullForm[I+1] shows Complex[1,1], we can also check that
AtomQ[I+1] === True, i.e. Complex[1,1] doesn't have a structure as an
expression. In this sense it is quite different from the expressions
Complex[a,b] or Complex[_,_] (a pattern), which *do* have a structure
that can be broken down: check that AtomQ[Complex[_,_]] === False.
Despite this MatchQ[ 1+I, Complex[_,_] ] *does* match. This was
surprising for me.
SparseArray is atomic too, and it matches too in similar situations.
Graph (also atomic) doesn't, however:
MatchQ[Graph[{1 -> 2}], Graph[_, __]]
even though FullForm gives Graph[List[1,2],List[DirectedEdge[1,2]]]
My point is that one should be careful when looking at the *apparent*
FullForm of atomic objects and matching against them.
Or perhaps the different behaviour of Graph is an oversight or bug?
--
Szabolcs Horvát
Mma QA site proposal: http://area51.stackexchange.com/proposals/37304