2
$\begingroup$

I need to solve some diophantine equations, in some cases, results returned by Solve the included parameters are not all independent. For example, the flowing case I want $c_{14}$ as a separate parameter, since the range of each parameter is deterministic, otherwise, subsequent processing is inconvenient. Are there any suitable options or workarounds?

mat = Partition[Array[c, 25], 5];
equations = {Equal @@ Join[Total[mat], Total[mat, {2}], 
  {Tr[mat], Tr[Reverse[mat]]}], Total[mat, 2] == Total[Range[25]]};
sol = Solve[equations, Flatten[mat], Integers][[1]] // Normal;
TextGrid[mat /. sol, Frame -> All]

enter image description here The result I hope is like this enter image description here

$\endgroup$

2 Answers 2

3
$\begingroup$

You can do an algebraic replacement for that position.

Start as above:

mat = Partition[Array[c, 25], 5];
equations = {Equal @@ 
    Join[Total[mat], Total[mat, {2}], {Tr[mat], Tr[Reverse[mat]]}], 
   Total[mat, 2] == Total[Range[25]]};
sol = Solve[equations, Flatten[mat], Integers][[1]];

mat2 = mat /. sol /. ConditionalExpression[a_, __] :> a

(* Out[21]= {{C[1], C[2], C[3], C[4], 
  65 - C[1] - C[2] - C[3] - C[4]}, {C[5], C[6], C[7], C[8], 
  65 - C[5] - C[6] - C[7] - C[8]}, {C[9], C[10], C[11], C[12], 
  65 - C[9] - C[10] - C[11] - C[12]}, {C[13], -65 + 2 C[1] + C[2] + 
   C[3] + C[4] + C[5] - C[8] + C[9] - C[11] + C[13], 
  1 + C[7] + C[10] + C[11] + C[12] + 2 C[14], 
  162 - 2 C[1] - C[2] - C[3] - C[4] - C[5] - C[6] - C[7] - C[9] - 
   C[10] - C[11] - C[12] - C[13] - C[14], -33 + C[6] + C[8] + C[11] - 
   C[13] - C[14]}, {65 - C[1] - C[5] - C[9] - C[13], 
  130 - 2 C[1] - 2 C[2] - C[3] - C[4] - C[5] - C[6] + C[8] - C[9] - 
   C[10] + C[11] - C[13], 
  64 - C[3] - 2 C[7] - C[10] - 2 C[11] - C[12] - 2 C[14], -97 + 
   2 C[1] + C[2] + C[3] + C[5] + C[6] + C[7] - C[8] + C[9] + C[10] + 
   C[11] + C[13] + C[14], -97 + C[1] + C[2] + C[3] + C[4] + C[5] + 
   C[7] + C[9] + C[10] + C[12] + C[13] + C[14]}} *)

Now use PolynomialReduce to do the desired replacement.

mat3 = PolynomialReduce[mat2, newC[14] - mat2[[4, 4]], C[14]][[All, All, 2]]

TextGrid[mat3, Frame -> All]

enter image description here

$\endgroup$
3
$\begingroup$

If you don't specify which of the variables that you want excluded, Mathematica will use which ever ones fall out of the algorithms naturally. To preclude solving for c[14] use Drop[Flatten[mat], {14}] for the variable list.

mat = Partition[Array[c, 25], 5];

equations = {Equal @@ 
    Join[Total[mat], Total[mat, {2}], {Tr[mat], Tr[Reverse[mat]]}], 
   Total[mat, 2] == Total[Range[25]]};

sol = Solve[equations, Drop[Flatten[mat], {14}], Integers][[1]] // Normal;

TextGrid[mat /. sol, Frame -> All]

enter image description here

Similarly, if you want to exclude c[5] and c[14]

sol2 = Solve[equations, Delete[Flatten@mat, {{5}, {14}}], Integers][[1]] // 
   Normal;

TextGrid[mat /. sol2, Frame -> All]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.