MathGroup Archive 2001

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

Search the Archive

Re: Naming pieces of patterns


In a message dated 2001/7/12 3:03:40 AM, fischerc at itam.cas.cz writes:

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

In general, use an atomic expression on the LHS of a rule

-(I/(2 a)) /. a -> I/(2A)

-A

{(a + b), -(a + b)} /. a -> e-b

{e, -e}

{-Sqrt[a + b], 1/Sqrt[a + b]} /. a -> e^2-b

{-Sqrt[e^2], 1/Sqrt[e^2]}

Simplify[%, e >= 0]

{-e, 1/e}

Use a rule that is general enough to encompass all of the cases of interest

{I, 2 I, -I} /. Complex[0, n_] -> n*J

{J, 2*J, -J}

{I, 2 I, -I} /. Complex[a_, n_] -> a+n*J

{J, 2*J, -J}


Bob Hanlon
Chantilly, VA  USA


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