MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Naming pieces of patterns


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]}


{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.


  • Prev by Date: Re: Changing Pure Function in Module
  • Next by Date: Re: Integration by substitution
  • Previous by thread: Re: Naming pieces of patterns
  • Next by thread: Re: Naming pieces of patterns