# Simplifying the expression by defining functions

I want to simplify the function below by using FullSimplify:

Function = (6 P1y P1z P2y P2z + 6 P1x P2x (P1y P2y + P1z P2z) +
P1x^2 (4 P2x^2 + P2y^2 + P2z^2) + P1y^2 (P2x^2 + 4 P2y^2 + P2z^2) +
P1z^2 (P2x^2 + P2y^2 + 4 P2z^2));


The result should be

3*(P1x P2x + P1y P2y + P1z P2z)^2  +(P1x^2 + P1y^2 + P1z^2) (P2x^2 + P2y^2 + P2z^2)


But, it does not come out like that.

Or, perhaps I can define some functions as

X = (P1x P2x + P1y P2y + P1z P2z)^2                   %(p1 * p2)^2
Y = (P1x^2 + P1y^2 + P1z^2) (P2x^2 + P2y^2 + P2z^2)   % |p1|^2 *|p2|^2


If we call the function again, I would expect that

3X + Y


But, it does not work.

What is actually the proper way to use Simplify in this case? Thank you very much.

This

f = (6 P1y P1z P2y P2z + 6 P1x P2x (P1y P2y + P1z P2z) +
P1x^2 (4 P2x^2 + P2y^2 + P2z^2) + P1y^2 (P2x^2 + 4 P2y^2 + P2z^2) +
P1z^2 (P2x^2 + P2y^2 + 4 P2z^2));
Simplify[f,Assumptions->{X == (P1x P2x + P1y P2y + P1z P2z)^2,
Y == (P1x^2 + P1y^2 + P1z^2) (P2x^2 + P2y^2 + P2z^2)}]


instantly returns

3*X + Y


If you defined your own functions X and Y then that doesn't mean that Mathematica would see a large complicated expression and use your function definitions to turn parts of your expression back into the function name. Mathematica almost always does exactly the opposite of that, it defaults to expanding function names into the expression they are defined to be. But you can use this Simplify trick to sometimes accomplish what you are looking for. This same trick should also work with FullSimplify. It is only guessing, but it looks like the simple result that you were looking for is just a little too complicated for the methods that Simplify and FullSimplify use to be able to find what you wanted without help.

your should use FullSimplify like this

((6 P1y P1z P2y P2z + 6 P1x P2x (P1y P2y + P1z P2z) +
P1x^2 (4 P2x^2 + P2y^2 + P2z^2) +
P1y^2 (P2x^2 + 4 P2y^2 + P2z^2) +
P1z^2 (P2x^2 + P2y^2 + 4 P2z^2)))~FullSimplify~{X == (P1x P2x +
P1y P2y + P1z P2z)^2,
Y == (P1x^2 + P1y^2 + P1z^2) (P2x^2 + P2y^2 + P2z^2)}


then it returns what you want.