0
$\begingroup$

I am trying to parallelize a table operation, but I find that the table is not the same with and without the parallel function. I have looked at this question, but I cannot transfer it to my own problem.

I am calculating a function of two variables like

Table[{ {r, t}, function[r, t]}, {r, 1, 10}, {t, 1, 10} ]

but in my example the function changes for each set of {r, t}. The actual code is this

Table[
  newfun[s_] = originialfun[r, s];


  {{r, t}, FT[newfun, t, 10]},

  {r, posmin, posmax}, {t, tmin, tmax, 1/tgrid}
]

I need to define newfun[s_] every time r updates because the FT function only accepts a function of one variable.

When I compare the output, some of the table is correct, but some is not. I print below some sample output.

Non-parallel table:

{{{{0.1, 0.05}, 7.74305*10^-8}, {{0.1, 0.1},  0.000314487}, {{0.1, 0.15}, 0.00462058}, 
  {{0.1, 0.2},  0.0159923},     {{0.1, 0.25}, 0.0312726},   {{0.1, 0.3},  0.0462548}}, 
 {{{0.2, 0.05}, 7.50678*10^-6}, {{0.2, 0.1},  0.00248056},  {{0.2, 0.15}, 0.0148101}, 
  {{0.2, 0.2},  0.0333674},     {{0.2, 0.25}, 0.0514979},   {{0.2, 0.3},  0.0661646}}, 
 {{{0.3, 0.05}, 0.000452737},   {{0.3, 0.1},  0.0158874},   {{0.3, 0.15}, 0.045333}, 
  {{0.3, 0.2},  0.071204},      {{0.3, 0.25}, 0.089263},    {{0.3, 0.3}, 0.100696}}}

Parallel table:

{{{{0.1, 0.05}, 0.000452737}, {{0.1, 0.1},  0.0158874}, {{0.1, 0.15}, 0.045333}, 
  {{0.1, 0.2},  0.071204},    {{0.1, 0.25}, 0.089263},  {{0.1, 0.3},  0.100696}}, 
 {{{0.2, 0.05}, 0.000452737}, {{0.2, 0.1},  0.0158874}, {{0.2, 0.15}, 0.045333}, 
  {{0.2, 0.2},  0.071204},    {{0.2, 0.25}, 0.089263},  {{0.2, 0.3},  0.100696}},
 {{{0.3, 0.05}, 0.000452737}, {{0.3, 0.1},  0.0158874}, {{0.3, 0.15}, 0.045333}, 
  {{0.3, 0.2},  0.071204},    {{0.3, 0.25}, 0.089263},  {{0.3, 0.3},  0.100696}}}

It is clear from the parallel table, that the rows where {{r, t}, func[r, t]} has r = 0.3 are correct, and that it overwrites all other output to that where r = 0.3.

How would I go about fixing this?

EDIT:

I tried the two options put by Szabolcs, fun[r_][s_] and originalfun[r, #]& but they don't work. I am not very experienced with the advanced Mathematica syntax, so I cannot completely understand why, but I believe FT needs as input a function without argument, as given in my code above.

This is the basic implementation of FT:

FT[F_, t_, M_:32]:=
    Module[{np, r, S, theta, sigma},
    np = Max[M, $MachinePrecision];
    r = SetPrecision[2M/(5t), np];
    S = r theta (Cot[theta] + I);
    sigma = theta + (theta Cot[theta] - 1)Cot[theta];
    (r/M)Plus @@ Append[Table[Re[Exp[t S](1 + I sigma)F[S]],
        {theta, Pi/M, (M - 1)Pi/M, Pi/M}],
            (1/2) Exp[r t] F[r]]
]
$\endgroup$
  • $\begingroup$ Don't keep redefining the function. Define a function once that takes all the parameters you need. You can always pass originalfun[r, #]& to FT, or define FT in a different way, or define fun[r_][s_] := originalfun[r,s] once then pass fun[r] to FT, etc. $\endgroup$ – Szabolcs Jan 11 '17 at 10:59
  • $\begingroup$ @Szabolcs Thanks for your suggestions. I could not get it to work though. I have updated my question with more information. $\endgroup$ – Kaspar H Feb 2 '17 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.