Writing:
Tuples[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]
I get:
{{1, 4, 7}, {1, 4, 8}, {1, 4, 9}, {1, 5, 7}, {1, 5, 8}, {1, 5, 9}, {1,6, 7},
{1, 6, 8}, {1, 6, 9}, {2, 4, 7}, {2, 4, 8}, {2, 4, 9}, {2, 5, 7}, {2,5, 8},
{2, 5, 9}, {2, 6, 7}, {2, 6, 8}, {2, 6, 9}, {3, 4, 7}, {3, 4, 8}, {3,4, 9},
{3, 5, 7}, {3, 5, 8}, {3, 5, 9}, {3, 6, 7}, {3, 6, 8}, {3, 6, 9}}
Similarly, by defining the function:
tuples[matrix_] := Module[{A, i, h, j, k, m, n},
{m, n} = {Length[matrix], Length[Transpose[matrix]]};
A = ConstantArray[0, {n^m, m}];
h = 0; k = 1;
For[j = 1, j <= m, j++,
h = h + 1;
For[i = 1, i <= n^m, i++,
A = ReplacePart[A, {i, j} -> matrix[[h]][[k]]];
If[Mod[i, n^m/n^h] == 0, k = k + 1];
If[k > n, k = 1]
] ]; Return[A]]
and writing:
tuples[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]
I get:
{{1, 4, 7}, {1, 4, 8}, {1, 4, 9}, {1, 5, 7}, {1, 5, 8}, {1, 5, 9}, {1,6, 7},
{1, 6, 8}, {1, 6, 9}, {2, 4, 7}, {2, 4, 8}, {2, 4, 9}, {2, 5, 7}, {2,5, 8},
{2, 5, 9}, {2, 6, 7}, {2, 6, 8}, {2, 6, 9}, {3, 4, 7}, {3, 4, 8}, {3,4, 9},
{3, 5, 7}, {3, 5, 8}, {3, 5, 9}, {3, 6, 7}, {3, 6, 8}, {3, 6, 9}}
But if I write now:
Tuples[{{1, 2, 3, 4}, {5, 6}, {7, 8, 9}}]
I get:
{{1, 5, 7}, {1, 5, 8}, {1, 5, 9}, {1, 6, 7}, {1, 6, 8}, {1, 6, 9},
{2, 5, 7}, {2, 5, 8}, {2, 5, 9}, {2, 6, 7}, {2, 6, 8}, {2, 6, 9},
{3, 5, 7}, {3, 5, 8}, {3, 5, 9}, {3, 6, 7}, {3, 6, 8}, {3, 6, 9},
{4, 5, 7}, {4, 5, 8}, {4, 5, 9}, {4, 6, 7}, {4, 6, 8}, {4, 6, 9}}
and defining the function:
tuples[matrix_] := Module[{A, i, h, j, k, m, n},
m = Length[matrix];
n = 1; For[i = 1, i <= m, i++, n = n Length[matrix[[i]]]];
A = ConstantArray[0, {n, m}];
h = 0; k = 1;
For[j = 1, j <= m, j++,
h = h + 1;
For[i = 1, i <= n, i++,
A = ReplacePart[A, {i, j} -> matrix[[h]][[k]]];
If[Mod[i, m / h] == 0, k = k + 1];
If[k > Length[matrix[[j]]], k = 1]
] ]; Return[A]]
and writing:
tuples[{{1, 2, 3, 4}, {5, 6}, {7, 8, 9}}]
I get:
{{1, 5, 7}, {1, 5, 8}, {1, 5, 9}, {2, 6, 7}, {2, 6, 8}, {2, 6, 9},
{3,5, 7}, {3, 5, 8}, {3, 5, 9}, {4, 6, 7}, {4, 6, 8}, {4, 6, 9},
{1, 5, 7}, {1, 5, 8}, {1, 5, 9}, {2, 6, 7}, {2, 6, 8}, {2, 6, 9},
{3, 5,7}, {3, 5, 8}, {3, 5, 9}, {4, 6, 7}, {4, 6, 8}, {4, 6, 9}}
which does not match with the output of MMA.
Most likely the problem lies in mod(...)
but I just can not figure out how to correct it.
A[[i,j]] = matrix[[h,k]]]
instead ofA = ReplacePart[A, {i, j} -> matrix[[h]][[k]]]
. This way, the matrixA
is not copied in each iteration. $\endgroup$