# Simplifying an expression by using custom letters or rules

Quite often in mathematics, when we have a complex expression, we name part of the complex expression with new letters in order to have a more readable result and continue with these new names. As a proof of concept, I want to denote a^2+b^2+2ab by p. Now I want Mathematica to be able to simplify complex expressions according to this new rule. What I have tried:

In[1]:= t[a^2+b^2+2*a*b]=p;

In[2]:= Simplify[a^2+b^2+2*a*b, TransformationFunctions -> {Automatic,t}]

Out[2]= p

In[3]:= Simplify[(a+b)^2, TransformationFunctions -> {Automatic,t}]

2
Out[3]= (a + b)


So far, Mathematica seems to be able to replace a^2+b^2+2ab by pif it appears literally. But what I truly want is Mathematica being able to use this new rule to find a more simple expression with this new notation even if previous transformations are needed. Is this even possible in general, without me having to force an Expand in this case? The expression (a+b)^2has a larger LeafCount than p so I would have expected Mathematica to automatically simplify (a+b)^2 by p since there exists a chain of valid transformations that transforms (a+b)^2to p. At this point I am unsure of what exactly TransformationFunctions is; Mathematica seems far more clever with its built-in rules.

This is one of the places where science steplessly transforms into art. There is no special function in Mathematica that would always successfully do the replacement you need. In some cases, the way you discussed above, works. If it does that's OK. If you have a large expression where a^2 stays in one place, 2ab in another, it can fail to recognize the replacement.

What I would recommend is in addition to your approach try to use rules, since they give you more control. To give a few examples:

Example 1:

expr1 = (Sqrt[Sqrt[m] - Sqrt[(m^2 - 9)/m]] + Sqrt[
Sqrt[m] + Sqrt[(m^2 - 9)/m]])^2*Power[m^2/4, (4)^-1];

expr1 /. Sqrt[(m^2 - 9)/m] -> p1

(*  ((m^2)^(1/4) (Sqrt[Sqrt[m] - p1] + Sqrt[Sqrt[m] + p1])^2)/Sqrt[2]  *)


Example 2:

expr2 = (x + 2 Sqrt[2 x - 4])^(1/2) + (x - 2 Sqrt[2 x - 4])^(1/2);

expr2 /. Sqrt[2 x - 4] -> p2

(*  Sqrt[-2 p2 + x] + Sqrt[2 p2 + x]   *)


Example 3:

expr3 = Sqrt[a^3 - b^3 + Sqrt[a]]*(
Sqrt[a^(3/2) + Sqrt[b^3  + Sqrt[a]]]*Sqrt[
a^(3/2) + Sqrt[b^3  + Sqrt[a]]])/
Sqrt[(a^3 + b^3)^2 - a*(4 a^2 b^3 + 1)] /. {a -> u^(1/3),
b -> v^(1/3)}

(*  (Sqrt[u^(1/6) + u - v] (Sqrt[u] + Sqrt[u^(1/6) + v]))/Sqrt[(u + v)^2 -
u^(1/3) (1 + 4 u^(2/3) v)]   *)

expr3A = (ExpandNumerator[expr] /. Sqrt[a_]*Sqrt[b_] -> Sqrt[a*b] //
Simplify) /. u^2 - 2 u v + v^2 -> p3

(*  (Sqrt[u (u^(1/6) + u - v)] + Sqrt[
u^(1/3) + u^(7/6) + u v - v^2])/Sqrt[p3 - u^(1/3)]  *)


You have a bit more control because you can not only do the replacement of the whole expression (as u^2 - 2 u v + v^2 -> p3 in the latter example) but also make Solve[u^2 - 2 u v + v^2 == p3, u][[2, 1]] and substitute its result u -> Sqrt[p3] + v. In the case of this latter example, it is not a good idea, but in many cases, it is.

Have fun!

• I will accept that as an answer. I was aware that what I asked was not possible in full generality but I would have expected that Mathematica would be able to do it in simple cases as in my proof of concept question. I am somewhat confident that such a thing will be possible in the future with the advance of AI that we are seeing. Commented Apr 27, 2023 at 15:46
• It is not necessary to accept it. What will be possible, in the future, we will see. Commented Apr 27, 2023 at 21:34