1
$\begingroup$

Suppose I have a function like

$\qquad f=\sinh(\eta)^2\times \sin(\theta)^2\times \cos(\phi)^2-\cosh(\eta)^2\times \sin(\theta)^2\times \sin(\phi)^2+\cdots$

I want to replace $\sinh(\eta)\times \sin(\theta)\times \cos(\phi)$ by $u_1$

I have tried with

Replace[f, Sinh[η] Sin[θ] Cos[ϕ] -> u1]

but unfortunately it's not working. I have come to know from the Mathematica Documentation Center that Replace does not map down to subparts.

Would anyone be able to help me regarding this issue?

$\endgroup$
  • 1
    $\begingroup$ Look at ReplaceAll. $\endgroup$ – gwr Aug 19 '16 at 7:06
2
$\begingroup$

Familiarize yourself with the structure of expressions in general. The part of the expression you would like to replace doesn't exists in one chunk, because each term is wrapped in Power.

expr = Sinh[e]^2 Sin[t]^2 Cos[f]^2 - Cosh[e]^2 Sin[t]^2 Sin[f]^2 - 
  Sinh[e]^3 Sin[t]^3 Cos[f]^3
subs = Sinh[e] Sin[t] Cos[f] -> x

You can do this with your specific example:

expr /. Times[a___ , p : Power[_, n_] .., b___] :> 
  a b Power[Times @@ First /@ {p} /. subs, n]

x^2 - x^3 - Cosh[e]^2 Sin[f]^2 Sin[t]^2

And this works all right with products with same powers. But what if you would like to do the same substitution on this:

Sinh[e]^4 Sin[t]^12 Cos[f]^4

The target expression can be "dug out", with a more complicated code. Yet which could subsume the first case.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.