1
$\begingroup$

I am still struggling on how to nicely handle list operations in Mathematica, and perform simultaneously parallel computation. We consider a tab of the form

tab = {{x1,y1,z1},{x2,y2,z2},...,{xn,yn,zn}};

I know how to perform the following replacement

tab2 = tab /. {x_,y_,z_} -> {x,f[y_,z_]} ;

so that tab2 is of the form

tab2 = {{x1,f[y1,z1]},{x2,f[y2,z2]},...,{xn,f[yn,zn]}};

However, because f[y,z] is an expensive function to compute, I would like to construct tab2 in a parallel fashion. I tried using ParallelMap, but didn't find a neat way of performing the calculation, mainly because it requires to perform an incomplete mapping.

How should one proceed in order to build-up in parallel the second list tab2 ? What would be the most efficient way to do it ?

$\endgroup$
1
  • 1
    $\begingroup$ how about ParallelMap[{First[#], f @@ Rest[#]} &, tab] $\endgroup$
    – chris
    Jan 13, 2015 at 16:47

2 Answers 2

2
$\begingroup$
tab = Table[{x[n], y[n], z[n]}, {n, 4}]
{{x[1], y[1], z[1]}, {x[2], y[2], z[2]},
 {x[3], y[3], z[3]}, {x[4], y[4], z[4]}}
ParallelMap[{#, f[##2]} & @@ # &, tab]
{{x[1], f[y[1], z[1]]}, {x[2], f[y[2], z[2]]},
 {x[3], f[y[3], z[3]]}, {x[4], f[y[4], z[4]]}}

See Apply and SlotSequence for clarification.

$\endgroup$
4
$\begingroup$

Calling

tab = {{x1,y1,z1},{x2,y2,z2},{xn,yn,zn}};

We have

ParallelMap[{First[#], f @@ Rest[#]} &, tab]
$\endgroup$

Your Answer

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

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