Efficiently Evaluate a Multivariable Function on a List of Tuples
Here are several ways to perform the computations along with timings:
G2[X_] := X[[1]] X[[2]] X[[3]] (X[[4]]^3 + X[[5]]^3 + X[[6]]^3) -
X[[4]] X[[5]] X[[6]] (X[[1]]^3 + X[[2]]^3 + X[[3]]^3);
cG2 = With[{code = G2[Array[Compile`GetElement[X, #] &, {6}]]},
Compile[{{X, _Integer, 1}},
code,
CompilationTarget -> "C",
RuntimeAttributes -> {Listable},
Parallelization -> True,
RuntimeOptions -> "Speed"
]
];
data = Tup[10];
a = G @@@ data; // RepeatedTiming // First
b = G @@ Transpose[data]; // RepeatedTiming // First
c = G2 /@ data; // RepeatedTiming // First
d = cG2[data]; // RepeatedTiming // First
e = Flatten[Outer[G, ## & @@ ConstantArray[F[10], 6]]]; // RepeatedTiming // First
a == b == c == d == e
4.012
0.068
7.30
0.0312
3.80
True
tup1[N_] := Tuples[G @@ F[N], 6]
or
tup2[N_] := Flatten[Outer[G, ## & @@ ConstantArray[F[N], 6]]]
They both give the same result as G @@@ Tup[N]
suggested by Henrik:
tup1[7] == tup2[7] == G @@@ Tup[7]
True