Skip to main content

equation solving - How to merge permutations obtained from Solve on multiple variables?


When I compute the following expression to find integer solutions of the equation (x^2 + y^2 + z^2 == 14^2)


Solve[x^2 + y^2 + z^2 == 14^2 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers]

Mathematica returns


{{x -> 4, y -> 6, z -> 12}, {x -> 4, y -> 12, z -> 6}, {x -> 6, y -> 4, z -> 12}, 
{x -> 6, y -> 12, z -> 4}, {x -> 12, y -> 4, z -> 6}, {x -> 12, y -> 6, z -> 4}}


which are permutations of the only solution: x=4, y=6, z=12.


How can I remove other "solutions" by somehow merging the permutations? For me, x, y, and z are equivalent.




I don't want to do the following:


First[Solve[x^2 + y^2 + z^2 == 14^2 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers]]

Because I will also solve:


Solve[x^2 + y^2 + z^2 == 14^3 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers]


which has more than one solution.




Edit (by belisarius)


Is there a way to specify the equivalency to Solve[] or Reduce[] so to spare the results post-processing stage?



Answer



You can use DeleteDuplicates, DeleteDuplicatesBy (Version 10) or GatherBy as follows:


ddF = DeleteDuplicates[#, Sort[Last /@ #] == Sort[Last /@ #2] &] &;
ddbF = DeleteDuplicatesBy[#,Sort[Last/@#]&]&;
fgbF = First /@ GatherBy[#, Sort[Last /@ #] &] &;


Examples:


sol1 = Solve[ x^2 + y^2 + z^2 == 14^2 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers];
sol2 = Solve[x^2 + y^2 + z^2 == 14^3 && x > 0 && y > 0 && z > 0, {x, y, z}, Integers];

ddF[sol1]
(* {{x -> 4, y -> 6, z -> 12}} *)

ddF[sol2]
(* {{x -> 2, y -> 6, z -> 52}, {x -> 2, y -> 36, z -> 38},
{x -> 10, y -> 12, z -> 50}, {x -> 12, y -> 22, z -> 46},

{x -> 12, y -> 34, z -> 38}, {x -> 14, y -> 28, z -> 42},
{x -> 18, y -> 22, z -> 44}, {x -> 20, y -> 30, z -> 38}} *)

ddbF and fgbF produce the same output.


Comments