Say, you have an expression which looks like this:

myexpr = (mess)+(some other mess)/( (mess)^3+(some other mess)^2 )^(1/3)

Here the 'mess' in the name of terms stands for what it is - a mess. To make it look nicer I use replacement rules and substitute (mess)->u, (some other mess)->v as follows

myexpr /. {(mess)->u,(some other mess)->v}

so that the output would look as nice as


Now, I actually don't get this result because when done as above the rules are not applied inside the cubic root, so the output I get instead is

u+v/( (mess)^3+(some other mess)^2 )^(1/3) 

The question is, naturally, how can the full replacement be achieved with minimum pain? Thanks.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Aug 8 '15 at 12:35
  • 1
    $\begingroup$ We will probably need to see the messes you are dealing with to help you find your problem. Sometimes problems like this are caused by a rule being applied to an outer expression, and not to an inner one. You might look at ReplaceRepeated $\endgroup$ – mfvonh Aug 8 '15 at 12:52

Your have to use RuleDelayed (:>) in such a case, so that the evaluation is not done immediately:

myexpr = (mess) + (someOtherMess)/((mess)^3 + (someOtherMess)^2)^(1/3)

and then:

 myexpr /. {mess :> x, someOtherMess :> y}

Then you get what you want.

  • $\begingroup$ @Patrick: Thanks for removing my typo - I didn´t see it.. $\endgroup$ – mgamer Aug 8 '15 at 15:14
  • 1
    $\begingroup$ I'm not sure that I follow your reasoning here. RuleDelayed will delay the evaluation of $x$ and $y$ in your example, but won't affect the (mess) expressions. Since I expect that $x$ and $y$ might not even have been assigned a value in this case, I don't see the difference between this and a simple Rule. The opposite behavior might be required, i.e. we might have to prevent evaluation of mess with one of the Hold* functions so it can be matched literally to what is in the original expression. Of course, it's hard to be specific without seeing the OP's actual expressions. $\endgroup$ – MarcoB Aug 8 '15 at 15:24
  • $\begingroup$ I agree. In most cases ReplaceRepeated would be the right choice. But without having the original code, it´s hard to find an appropriate solution $\endgroup$ – mgamer Aug 8 '15 at 16:03

Thanks to mfvonh for the answer - using ReplaceRepeated (//.) as shown below replaces everything.

myexpr //. {(mess)->u,(some other mess)->v}

To those in power: I unfortunately cannot accept the answer properly because after setting up the account the question is not listed as mine (and can't leave a comment because low rep = having no rights).


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.