# Replace a sum of squared variables by a new variable squared

Is there any way to tell Mathematica to replace $\kappa^2 + k_z^2 \rightarrow k^2$ in an expression? I want to make a result more readable, e.g. something like this:

$$\frac{2 k_z^3 \sqrt{k_z^2 + \kappa^2} + 2 k_z \kappa^2 \sqrt{k_z^2 + \kappa^2} + k_z \sqrt{k_z^2 + \kappa^2} \mu^2}{2 (k_z^4 + 2 k_z^2 \kappa^2 + \kappa^4)}$$

(2 kz^3 Sqrt[kz^2 + κ^2] + 2 kz κ^2 Sqrt[kz^2 + κ^2] + kz Sqrt[kz^2 + κ^2] μ^2)/
(2 (kz^4 + 2 kz^2 κ^2 + κ^4))


Which can be simplified to:

$$k_z \frac{ (2k^2 + \mu^2) }{2k^3}$$

But I can't get Mathematica to perform this substitution. I have made sure to use the proper assumptions for this to work:

$Assumptions={k>0};  Yet it seems to not see the (arguably trivial) patterns. Does anyone know how to trick Mathematica into doing what I want :-)? • – eldo Jun 6 '14 at 11:53 • Generally speaking this is in a class problem that is difficult, and has many variations. I collected links to related questions here: Can I simplify an expression into form which uses my own definitions? – Mr.Wizard Jun 6 '14 at 11:56 • @rubenvb - please provide your formulas in Mathematica InputForm so that others can copy them. – eldo Jun 6 '14 at 12:00 ## 4 Answers This is your expression: expr = (2 kz^3 Sqrt[kz^2 + \[Kappa]^2] + 2 kz \[Kappa]^2 Sqrt[kz^2 + \[Kappa]^2] + kz Sqrt[kz^2 + \[Kappa]^2] \[Mu]^2)/(2 (kz^4 + 2 kz^2 \[Kappa]^2 + \[Kappa]^4));  This gives us the substitution:  sl = Solve[kz^2 + \[Kappa]^2 == k^2, \[Kappa]][[2, 1]] (* \[Kappa] -> Sqrt[k^2 - kz^2] *)  An here it acts on your expression yielding the desired result:  Simplify[expr /. sl, {k > 0, \[Kappa] > 0}] (* (kz (2 k^2 + \[Mu]^2))/(2 k^3) *)  It is basically almost the same as in the eldo's answer, but has the advantage that when one takes the replacement kz^2 + \[Kappa]^2 -> k^2Mma in some situations understands it, but may miss in some others. So, it may require some additional efforts. In contrast with the above substitution everything goes smooth. The following will work, but it's probably not the best way PowerExpand[FullSimplify[(2 kz^3 Sqrt[kz^2 + κ^2] + 2 kz κ^2 Sqrt[kz^2 + κ^2] + kz Sqrt[kz^2 + κ^2] μ^2)/(2 (kz^4 + 2 kz^2 κ^2 + κ^4)) /. kz^(2 a__) -> (k^2 - κ^2)^a] /. (kz^2 + κ^2) -> k^2]  Output (kz (2 k^2 + μ^2))/(2 k^3)  Well, in exploring the physics of my result, I got an idea that helps to get What I Want™: $Assumptions={k>0};
thing:=(2 kz^3 Sqrt[kz^2 + κ^2] + 2 kz κ^2 Sqrt[kz^2 + κ^2] + kz Sqrt[kz^2 + κ^2] μ^2)/(2 (kz^4 + 2 kz^2 κ^2 + κ^4));

thing /. kz->k Cos[α] /. κ->k Sin[α]//Simplify
% /. α->ArcCos[kz/k]


It's literally a workaround, but it does the trick quite nicely in this and probably similar cases.

no doubt covered in the above links, but since I worked it out:

 thing = (2 kz^3 Sqrt[kz^2 + kappa^2] +
2 kz kappa^2 Sqrt[kz^2 + kappa^2] +
kz Sqrt[kz^2 + kappa^2] mu^2)/(2 (kz^4 + 2 kz^2 kappa^2 +
kappa^4));
f = # /. Table[ (kz^2)^n -> (K^2 - kappa^2)^n , {n, 1, 2}] &;
cpx[e_] := 100 Count[e, kz, {0, Infinity}] + LeafCount[e]
Simplify[ thing , TransformationFunctions -> {Automatic, f},
ComplexityFunction -> cpx, Assumptions -> K > 0]


(kz (2 K^2 + mu^2))/(2 K^3)

The ComplexityFunction is a not-so-obvious key here. The intermediate results of the transformation are evidently deemed more complex by the default and not applied.