       RE: Replacement problem

• To: mathgroup at smc.vnet.net
• Subject: [mg36926] RE: [mg36906] Replacement problem
• From: "DrBob" <drbob at bigfoot.com>
• Date: Wed, 2 Oct 2002 03:32:14 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Look at the FullForm of your expressions:

FullForm[Sqrt[X + Y + Z]]
Power[Plus[X, Y, Z], Rational[1, 2]]

FullForm[f]
Plus[Times[Rational[1, 4], C, Power[F, -2], Power[Plus[X, Y,
Z], Rational[-1, 2]]], Times[B, Plus[A,
Power[Plus[X, Y, Z], Rational[1, 2]]]]]

FullForm[h]
Plus[Times[B, Plus[A, Power[Plus[X, Y, Z],
Rational[1, 2]]]], Times[C, Power[Plus[Times[4, Power[F, 2]],
Power[Plus[X, Y, Z], Rational[1, 2]]], -1]]]

The square root appears in two different forms!  A solution is to
replace both patterns:

f = B*(A + Sqrt[X + Y + Z]) + C/(Sqrt[X + Y + Z]/4*F^2);
f /. {Sqrt[X + Y + Z] -> Q, 1/Sqrt[X + Y + Z] -> 1/Q}

(4*C)/(F^2*Q) + B*(A + Q)

This is very annoying, of course, and it may not take care of every
case.  Looking at the FullForm should help, when it fails.

Bobby

-----Original Message-----
From: Carlos Felippa [mailto:carlos at colorado.edu]
To: mathgroup at smc.vnet.net
Subject: [mg36926] [mg36906] Replacement problem

These expressions are condensation of larger ones
(about 700 lines or so each) but they illustrate random
substitution failures in 4.2.  Question: how can the
substitution Sqrt[...]->Q always be made to work?
The help file under ReplaceAll, ReplaceRepeated, etc,

Thanks

f=B*(A+Sqrt[X+Y+Z])+C/(Sqrt[X+Y+Z]/4*F^2);
Print[(f/.Sqrt[X+Y+Z]->Q)//InputForm];

B*(A + Q) + (4*C)/(F^2*Sqrt[X + Y + Z])  (* fails *)

g=B*(A+Sqrt[X+Y+Z])+C/(Sqrt[X+Y+Z]*4*F^2);
Print[(g/.Sqrt[X+Y+Z]->Q)//InputForm];

B*(A + Q) + C/(4*F^2*Sqrt[X + Y + Z])    (* fails *)

h=B*(A+Sqrt[X+Y+Z])+C/(Sqrt[X+Y+Z]+4*F^2);
Print[(h/.Sqrt[X+Y+Z]->Q)//InputForm];

B*(A + Q) + C/(4*F^2 + Q)                (* works *)

```

• Prev by Date: simple two step optimization
• Next by Date: Re: Linux Users?
• Previous by thread: Re: Replacement problem
• Next by thread: Re: Replacement problem