I have a function which takes real numbers as inputs:
MyFunc:=[v1_,v2_,v3_,v4_]
and outputs a single real number. Now I want to evaluate MyFunc
for many different combinations of input values of v1, v2, v3 and v4.
Let's assume my input values are:
v1 = {1,2.5,3,4}
v2 = {4,2}
v3 = {0.5,1}
v4 = {0.9,0.92,0.94}
Now I want to input each combination of the different values of the variables and input it in MyFunc
. Then, for each set of input values I want to store them in a list, along with the output values.
So let's say MyFunc[1,4,0.5,0.9]
outputs a value of 0.68, I want to store this in a list: {1,4,0.5,0.9,0.68}, where the first four values or the input values and the last value is the output value.
I know how to do this sequentially:
combinations = Tuples[{v1, v2, v3, v4}]
outputs = Flatten[MyFunc @@@ combinations];
And then I parse combinations and outputs
results = MapThread[Append, {"combinations", "outputs"}];
Now I have set up Mathematica on a server with 32 cores, so I would like to take advantage of this by parallelizing my task. I know how to parallelize things using Table, but it is important that the correct output is appended to the corresponding input values. Since things are happening on different Kernels, I have no clue how to parallelize my job producing the output results I want.
myFunc
return exactly that, instead of just a number, or to make a wrapper functionmyFunc2[v1_,v2_,v3_,v4_] := {v1,v2,v3,v4,myFunc[v1,v2,v3,v4]}
andMap
orTable
that instead. That also saves you from the very costlyAppend
thing you seem to be doing;Append
has high complexity in Mathematica so one should avoid using it for long lists. $\endgroup$