Re: Naming pieces of patterns

*To*: mathgroup at smc.vnet.net*Subject*: [mg29826] Re: Naming pieces of patterns*From*: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>*Date*: Fri, 13 Jul 2001 04:19:10 -0400 (EDT)*Organization*: Universitaet Leipzig*References*: <9ijhj3$rhj$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Hi, pattern matching work with the internal representation of the equation. In such cases it is better to replace a single symbol. Cyril Fischer wrote: > > How can I as simply as possible use "substitutions" > 1. > -(I/(2 a)) /. I/(2 a) -> A It cant' work because -I ic Complex[0,-1] and I is Complex[0,1] and the pattern can't match ! It is better to "solve" I/(2a)==A for a and replace -(I/(2 a)) /. a -> I/(2A) > 2. > {(a + b), -(a + b)}/. a + b -> e > gives > {e, -a - b} The same reason because In[]:={(a + b), -(a + b)} // FullForm Out[]= List[Plus[a, b], Plus[Times[-1, a], Times[-1, b]]] and -a-b match not a+b but if you solve a+b==e for a the replacment {(a + b), -(a + b)} /. a -> e - b work as expected. > instead of {e,-e} > > 3. > {-Sqrt[a + b], 1/Sqrt[a + b]} /. Sqrt[a + b] -> e > gives > {-e,1/Sqrt[a + b]} Can't work because Sqrt[a] is internal Power[a,Rational[1,2]] and 1/Sqrt[a] is internal Power[a,Rational[-1,2]] but you give only the pattern for Rational[1,2]. But {-Sqrt[a + b], 1/Sqrt[a + b]} /. (a + b)^n_ :> e^(2n) work as expected. > > 4. > {I, 2 I, -I} /. I -> J > gives > {J, 2 \[ImaginaryI], -\[ImaginaryI]} Here: {I, 2 I, -I} /. Complex[a_, b_] :> a + J*b will work. > > 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. a) use the most general pattern, i.e., a^n_ for Sqrt[a] b) use Solve[] to replace only a single symbol of your expression and let Mathematicas algebraic simplifications do the rest. Regards Jens