Re: How do I create a parametric expression?
- To: mathgroup at smc.vnet.net
- Subject: [mg68570] Re: How do I create a parametric expression?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 9 Aug 2006 23:57:15 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <ebc7b6$l58$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
axlq wrote:
> I'm trying to figure out how to simplify a large expression so that it's
> expressed in terms of a sub-expression that's factored into the larger
> one.
>
> My expression looks like this:
>
> -((1 + 2*n)*((a^4*k^2 + a^2*(-1 + k^2*(q - z)^2) + 2*(q - z)^2)
> *Cos[k*Sqrt[a^2 + (q - z)^2]] - k*(a^2 - 2*(q - z)^2)
> *Sqrt[a^2 + (q - z)^2]*Sin[k*Sqrt[a^2 + (q - z)^2]])
> *Sin[((1 + 2*n)*Pi*z)/L])/(8*Pi*w*(a^2 + (q - z)^2)^(5/2))
>
> Now, I *know* there are places in there were Sqrt[a^2+(q-z)^2] occurs,
> either by itself or raised to various powers. If I want to define
>
> R:=Sqrt[a^2+(q-z)^2]
>
> ...then how can I make Mathematica re-state my expression in terms
> of R? The ReplaceRepated[] function doesn't seem to do the job.
>
> I need to do this because I am translating the expressions into
> Visual Basic code for an Excel application, and it would be nice to
> find groupings of terms repeated throughout the expression that I
> need to calculate only once.
>
> -Alex
>
Hi Alex,
Use Replace [1] with level specification [2]:
expr = -((1 + 2*n)*((a^4*k^2 + a^2*(-1 + k^2*(q - z)^2) + 2*(q -
z)^2)*Cos[k*Sqrt[a^2 + (q - z)^2]] - k*(a^2 - 2*(q - z)^2)*Sqrt[a^2 + (q
- z)^2]*Sin[k*Sqrt[a^2 + (q - z)^2]])*Sin[((1 +
2*n)*Pi*z)/L])/(8*Pi*w*(a^2 + (q - z)^2)^(5/2))
Replace[expr, Sqrt[a^2 + (q - z)^2] -> R, {0, Infinity}]
--> -(((1 + 2*n)*((a^4*k^2 + a^2*(-1 + k^2*(q - z)^2) + 2*(q -
z)^2)*Cos[k*R] - k*R*(a^2 - 2*(q - z)^2)*Sin[k*R])*Sin[((1 +
2*n)*Pi*z)/L])/(8*Pi*w*(a^2 + (q - z)^2)^(5/2)))
Why level specifications are important? Compare
Cases[expr, Sqrt[a^2 + (q - z)^2]]
--> {}
vs
Cases[expr, Sqrt[a^2 + (q - z)^2], {0, Infinity}]
--> {Sqrt[a^2 + (q - z)^2], Sqrt[a^2 + (q - z)^2], Sqrt[a^2 + (q - z)^2]}
Best regards,
Jean-Marc
[1] http://documents.wolfram.com/mathematica/functions/Replace
[2] _The Mathematica Book_, "A.3.6 Level Specifications",
http://documents.wolfram.com/mathematica/book/section-A.3.6