MathGroup Archive 2001

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

Search the Archive

Re: Naming pieces of patterns

  • To: mathgroup at
  • Subject: [mg29845] Re: Naming pieces of patterns
  • From: "Alan Mason" <amason2 at>
  • Date: Fri, 13 Jul 2001 04:19:25 -0400 (EDT)
  • References: <9ijhj3$rhj$>
  • Sender: owner-wri-mathgroup at

Dear Cyril,
To see why it doesn't work, use FullForm.  For instance, in your first
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> wrote in message
news:9ijhj3$rhj$1 at
> 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

  • Prev by Date: IMO 2001 Medalists Announced
  • Next by Date: Re: Re: Exponential Equations
  • Previous by thread: Re: Naming pieces of patterns
  • Next by thread: Clearing cache