I have the following question.

An expression, which I want to simplify contains several subexpressions which appear quite frequently all over the place. To optimize simplification I would like to use abbreviations for some of them. Is there any way to do it in a "smart" way, i.e. to account for subexpressions which differ only by sign/multiplication by a number or a variable? Here is an example to illustrate what I mean.

For example, the adverted subexpression is:

-a^2 + b^2/(c^2 - d^2)

and I want to use variable A1 everywhere instead it:

-a^2 + b^2/(c^2 - d^2) -> A1

Now, I want Mathematica to substitute the expressions which are essentially equal to this one, but are simply written in another form like:

-a^2 - b^2/(d^2 - c^2)
-a^2 + (-b^2/(d^2 - c^2))

Also it would be great to use this rule for expressions like

-2*a^2 + 2*b^2/(c^2 - d^2) (*2*A1*)


a^2 - b^2/(c^2 - d^2) (*-A1*)

or even

-x*a^2 + x*b^2/(c^2 - d^2) (*x*A1*)

Is there a way to do it?


2 Answers 2


Short version :

When one wants to do f[a+b] /. a+b->c, it is often more efficient to write f[a+b] /. a-> c-b and simplify the result ( with Simplify, Expand...).

Long version :

You can apply the rule b -> -Sqrt[a^2 + A1] Sqrt[c^2 - d^2] (equivalent to -a^2 + b^2/(c^2 - d^2) -> A1) and afterward try to simplify.

In fact, your example is a little bit more complex because there are to possible rules b-> Sqrt[...] and b-> -Sqrt[...], but it works fine :

rule = Solve[-a^2 + b^2/(c^2 - d^2) == A1, b]

transfomation[x_] := x /. rule // ExpandAll // Together

-a^2 - b^2/(d^2 - c^2) // transfomation
-a^2 + (-b^2/(d^2 - c^2)) // transfomation
-2*a^2 + 2*b^2/(c^2 - d^2) (*2*A1*) // transfomation
a^2 - b^2/(c^2 - d^2) (*-A1*)// transfomation   
-x*a^2 + x*b^2/(c^2 - d^2) (*x*A1*)  // transfomation

{{b -> -Sqrt[a^2 + A1] Sqrt[c^2 - d^2]}, {b -> Sqrt[a^2 + A1] Sqrt[c^2 - d^2]}}

{A1, A1}

{A1, A1}

{2 A1, 2 A1}

{-A1, -A1}

{A1 x, A1 x}

  • 1
    $\begingroup$ Thanks a lot for your answer, but I'm afraid it is not precisely what I needed. Yes, it works in these cases but I also would like it to leave b variable as it is when it does not appear in the combination: -a^2 + b^2/(c^2 - d^2) For example: b/(-a^2 + b^2/(c^2 - d^2)) = b/A1. Secondly, this subexpression was chosen just as an illustration. I think, this method might not work when it is hard (if possible) to express one of the variables through the others. $\endgroup$
    – user43283
    Jan 5, 2019 at 21:54
  • $\begingroup$ I understand, but I have nothing better to propose (These kinds of apparently trivial algebric manipulations are often very frustating). $\endgroup$
    – andre314
    Jan 5, 2019 at 22:00
  • $\begingroup$ I you want, you can add further more complicated examples in your question. Generally speaking, it is not recommended to change the question, but as I'm the only one who has given a answer I can delete it. (I don't mind the +50 of reputation) $\endgroup$
    – andre314
    Jan 5, 2019 at 22:34

It seems to me that this question is similar to other posts here about finding all subexpressions and giving them names. If the goal is simply to simplify the expression by giving names to all the subexpressions, then the links below could be useful. There might also be a way of finding all subexpressions and then replacing only the ones you want. In any case, a nice start can be:


which identifies all subexpressions and all subsubexpressions and so on as is done in Mathematica's Compile function. In fact, as I understand the code above is used internally in Compile.

The output is a bit hard to read but you can use this answer to make it more readable. The answer there uses Compile directly instead but the manipulations are analogous in both cases.

See also the links on this page (the second answer has more links) for other methods. Specifically, this answer at the end of the page (at least at the time of writing) could be interesting.


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.