       Re: Ask Mathematica to sub for a long expression

• To: mathgroup at smc.vnet.net
• Subject: [mg71934] Re: Ask Mathematica to sub for a long expression
• From: Peter Pein <petsie at dordos.net>
• Date: Mon, 4 Dec 2006 06:39:24 -0500 (EST)
• References: <ekp4o4\$2fq\$1@smc.vnet.net><ekrk8s\$s3q\$1@smc.vnet.net> <ekuctj\$82t\$1@smc.vnet.net>

```Hi Philipp,

Sqrt[...] is no subexpression of Power[...,3/2].

Try:
In:= expressionToBeReplaced = a_^2 + b_^2 - 2*a_*b_* Cos[theta];
In:= 2*Sqrt[x^2 + y^2 - 2*x*y*Cos[theta]] +
Sqrt[x^2 + y^2 - 2*x*y*Cos[theta]]^3 /. expressionToBeReplaced -> c^2
Out= 2*Sqrt[c^2] + (c^2)^(3/2)
In:= Simplify[%, c >= 0]
Out= c*(2 + c^2)

HTH,
Peter

Philipp schrieb:
> Mark,
>
>
> In:=  expressionToBeReplaced = Sqrt[a_^2 + b_^2 -
>           2*a_*b_*Cos[theta]];
>
> In:=  2 Sqrt[x^2 + y^2 - 2*x*y*Cos[theta]] + Sqrt[x^2 + y^2 -
>            2*x*y*Cos[theta]]^3 /. expressionToBeReplaced -> c
>
> still gives
>
> Out=  2*c + (x^2 + y^2 - 2*x*y*Cos[theta])^(3/2)
>
> Cheers,
>
> Philipp.
>
> Mark Westwood wrote:
>> Hi Aaron
>>
>> The simple answer is that what you want to do is very easy in
>> Mathematica.  For example, define;
>>
>> In:= xpr1 = 2 Sqrt[a^2 + b^2 - 2*a*b*Cos[theta]]  (* an expression
>> involving your long expression *)
>>
>> then execute
>>
>> In:= xpr1/.Sqrt[a^2 + b^2 - 2*a*b*Cos[theta]] -> c
>>
>> Out:= 2c
>>
>> and now your fun begins.  Note the use of the /. which is a shorthand
>> notation for the ReplaceAll function.  Check out the on-line Help for
>> this.  As it stands this replacement rule is very inflexible, it will
>> only replace expressions which exactly match the given one.  What you
>> might want to do is replace expressions which match a more general
>> pattern.  For example, suppose you assign the expression to be replaced
>> to a variable, like this
>>
>> In:= expressionToBeReplaced = Sqrt[a_^2 + b_^2 -
>> 2*a_*b_*Cos[theta]]
>>
>> (* note carefully the underscores appended to the variable names, which
>> transforms them into Patterns -- back to the Help system for further
>> info on this *)
>>
>> and now you can try things like
>>
>> In:= 2 Sqrt[x^2 + y^2 - 2*x*y*Cos[theta]] /. expressionToBeReplaced
>> -> c
>>
>> Out:= 2c
>>
>> Now the replacement works for any expression which has the same
>> structure as the one you are seeking to replace.  OK, over to you, post
>> again when you've mastered simple replacement rules and want more
>> guidance.
>>
>> Regards
>> Mark Westwood
>>
>> aaronfude at gmail.com wrote:
>>> Hi,
>>>
>>> Is it possible to ask Mathematica to replace a long expression, such as
>>> Sqrt[a^2+b^2-2*a*b*Cos[theta]] with "c", whenever it encounters that
>>> long expression?
>>>
>>> Thanks!
>>>
>>> Aaron Fude
>

```

• Prev by Date: Re: NIntegrate that upper limit is infinite
• Next by Date: Re: Reduction of Radicals
• Previous by thread: Re: Re: Ask Mathematica to sub for a long expression