Re: Naming pieces of patterns
- To: mathgroup at smc.vnet.net
- Subject: [mg29845] Re: Naming pieces of patterns
- From: "Alan Mason" <amason2 at austin.rr.com>
- Date: Fri, 13 Jul 2001 04:19:25 -0400 (EDT)
- References: <9ijhj3$rhj$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Dear Cyril, To see why it doesn't work, use FullForm. For instance, in your first example, FullForm[-(I/(2 a))] is Times[Complex[0,Rational[-1,2]],Power[a,-1]]. This does *not* match your pattern (I/(2 a)) , for which FullForm{(I/(2 a)) ] gives Times[Complex[0,Rational[1,2]],Power[a,-1]]; it's the Rational[__] parts that don't match. This is one of several examples of Mathematica pattern matching "quirks", in the sense that you must know how Mathematica represents expressions internally (as obtained by using FullForm). There are only two arithmetic heads, Plus and Times. Minus is represented as Times[-1, ...], and Division by Power[..., -1]. Also, negative integers like -3 are atoms; the minus sign won't match in patterns. These design decisions were presumably made for the sake of efficiency, but they are not user-friendly. A common problem is trying to simplify a square root everywhere in an expression; the obvious rule doesn't work if you are dividing by that square root (it won't be replaced, because a b/(a+b)^(1/2) is represented as Times[a,b,Power[Plus[a,b],Rational[-1,2]]]; it's that pesky Rational[-1, 2], with the -1 that doesn't match the pattern (u_+v_)^(1/2) ). One could write a whole book on the pattern matcher and the design decisions underlying its behavior. Perhaps I will do this, from the viewpoint of an outsider looking out. "Cyril Fischer" <fischerc at itam.cas.cz> wrote in message news:9ijhj3$rhj$1 at smc.vnet.net... > How can I as simply as possible use "substitutions" > 1. > -(I/(2 a)) /. I/(2 a) -> A > does not work, while > (I/(2 a)) /. I/(2 a) -> A > works well > > 2. > {(a + b), -(a + b)}/. a + b -> e > gives > {e, -a - b} > instead of {e,-e} > > 3. > {-Sqrt[a + b], 1/Sqrt[a + b]} /. Sqrt[a + b] -> e > gives > {-e,1/Sqrt[a + b]} > > 4. > {I, 2 I, -I} /. I -> J > gives > {J, 2 \[ImaginaryI], -\[ImaginaryI]} > > I know _why_ these cases do not work, but I would like to know, if there > is a possibilty to use a common pattern rule to substitute all > occurences of an expression. > > Thank you, > Cyril Fischer > >