I have the following code which solves for Integers the equation $$a_1+a_2+a_3+a_4+b_1+b_2+b_3+b_4+c_1+c_2+c_3+c_4=tot$$ with $$ -4<a_i\in \mathbb{Z}<4$$ $$ -7<b_i\in \mathbb{Z}<7$$ $$ -4<c_i\in \mathbb{Z}<4$$ Notice that the constraint on b is different
My method for the moment is the following :
1- find the solutions to
$$a+b+c=tot$$
with
$$ -13<a=\sum a_i<13$$
$$ -25<b=\sum b_i<25$$
$$ -13<c=\sum c_i<13$$
I use this code to do it (forget about the nbLd argument, it's zero for all you care), it gives a List
of solutions.
Solutions[tot_, nbLd_: 0(*forget about nbLd, it's zero*)] := Block[{},
Values@
Solve[a + b + c == tot && -13 < a < 13 && -25 + 6*nbLd < b <
25 - 6*nbLd && -13 < c < 13 , {a, b, c}, Integers]];
2- I use these solutions to find the $\{a_i\},\{b_i\},\{c_i\}$ using IntegerPartitions
and Permutations
(see here) as well as Reap
and Sow
to find all the solutions to the initial equation. (There is some additional formatting done with Flatten
). I use this code to do it:
GenNodes[tot_] :=
Block[{partitions = Solutions[tot], temp},
(Partition[Flatten[#], 2]) & /@ (*Formatting line*)
Flatten[Table[{#[[1, j]], #[[2, i]], #[[3, k]]}, {j,
Length[#[[1]]]}, {i, Length[#[[2]]]}, {k,
Length[#[[3]]]}](*with this Table line,
I cobine all the different a_i,b_i,
c_i solutions to get all possible solutions,
I need all permutations*)& /@ (Reap[(
temp = {
IntegerPartitions[#[[1]], {4},
Range[-3, 3]],(*a_i solutions*)
IntegerPartitions[#[[2]], {4},
Range[-6, 6]],(*b_i solutions*)
IntegerPartitions[#[[3]], {4},
Range[-3, 3]]}; (*c_i solutions*)
If[temp[[1]] != {} && temp[[2]] != {} && temp[[3]] != {},
Sow[{Join @@ Permutations /@ temp[[1]],
Join @@ Permutations /@ temp[[2]],
Join @@ Permutations /@ temp[[3]]}],
Unevaluated[Sequence[]]]) & /@ partitions, e][[1]]), 3]];
The final result is for example :
In[82]:= GenNodes[47]
Out[82]= {{{3, 3}, {3, 2}, {6, 6}, {6, 6}, {3, 3}, {3, 3}}, {{3,
3}, {2, 3}, {6, 6}, {6, 6}, {3, 3}, {3, 3}}, {{3, 2}, {3, 3}, {6,
6}, {6, 6}, {3, 3}, {3, 3}}, {{2, 3}, {3, 3}, {6, 6}, {6, 6}, {3,
3}, {3, 3}}, {{3, 3}, {3, 3}, {6, 6}, {6, 5}, {3, 3}, {3, 3}}, {{3,
3}, {3, 3}, {6, 6}, {5, 6}, {3, 3}, {3, 3}}, {{3, 3}, {3, 3}, {6,
5}, {6, 6}, {3, 3}, {3, 3}}, {{3, 3}, {3, 3}, {5, 6}, {6, 6}, {3,
3}, {3, 3}}, {{3, 3}, {3, 3}, {6, 6}, {6, 6}, {3, 3}, {3, 2}}, {{3,
3}, {3, 3}, {6, 6}, {6, 6}, {3, 3}, {2, 3}}, {{3, 3}, {3, 3}, {6,
6}, {6, 6}, {3, 2}, {3, 3}}, {{3, 3}, {3, 3}, {6, 6}, {6, 6}, {2,
3}, {3, 3}}}
Which, hopefully, are all the solutions to the initial equation for $tot=47$, formatted in a certain way.
My goal is to find the solutions of this equation for $tot =32$, as it is right now, my computer cannot do it. It freezes (I don't really know why, I'm not an expert, I don't understand why it can't just do it slowly, it maybe a memory problem ? but it freezes super fast, so I don't think it is. It would be helpful if someone could explain this to me !).
Question: basically, can you do it ?
Alternative question: Do yo have a better (memory usage wise or speed wise) way to find the solutions ? or to improve any of the steps?
I'll take any advice
Note: Beware, for 32 the number of solutions is huge.