# Map of an expression in a 3D grid is very slow

I have a quite large expression as function of $$R$$, $$\phi$$ and $$Z$$ as you can see:

expression = (0. + 1/10 (5/R^6 + 5 R^4) Z (0. + 1.5 Cos[5 \[Phi]]) + ((
30 R + 90 R^9 - 120 R^11)/(
480 R^5) - (-14 + 15 R^2 + 9 R^10 - 10 R^12)/(96 R^6) +
1/4 (-(5/R^6) + 5 R^4) Z^2) (0. +
1.5 Sin[5 \[Phi]]) + (((-1 + R^2)^4 (46 R + 76 R^3 + 60 R^5))/(
26880 R^5) + ((-1 + R^2)^3 (18 + 23 R^2 + 19 R^4 + 10 R^6))/(
3360 R^4) - ((-1 + R^2)^4 (18 + 23 R^2 + 19 R^4 + 10 R^6))/(
5376 R^6) +
1/2 ((30 R + 90 R^9 - 120 R^11)/(
480 R^5) - (-14 + 15 R^2 + 9 R^10 - 10 R^12)/(96 R^6)) Z^2 +
1/48 (-(5/R^6) + 5 R^4) Z^4) (0. +
10. Sin[5 \[Phi]]))/(\[Sqrt]((0. + 1/R + (
5 ((-14 + 15 R^2 + 9 R^10 - 10 R^12)/(480 R^5) +
1/4 (1/R^5 + R^5) Z^2) (0. + 1.5 Cos[5 \[Phi]]))/R + (
5 (((-1 + R^2)^4 (18 + 23 R^2 + 19 R^4 + 10 R^6))/(
26880 R^5) + ((-14 + 15 R^2 + 9 R^10 - 10 R^12) Z^2)/(
960 R^5) + 1/48 (1/R^5 + R^5) Z^4) (0. +
10. Cos[5 \[Phi]]))/
R + ((-(1/R^5) + R^5) Z (0. - 1.5 Sin[5 \[Phi]]))/(
2 R))^2 + (0. +
1/10 (-(1/R^5) + R^5) (0. + 1.5 Cos[5 \[Phi]]) +
1/2 (1/R^5 + R^5) Z (0. +
1.5 Sin[5 \[Phi]]) + (((-14 + 15 R^2 + 9 R^10 -
10 R^12) Z)/(480 R^5) + 1/12 (1/R^5 + R^5) Z^3) (0. +
10. Sin[5 \[Phi]]))^2 + (0. +
1/10 (5/R^6 + 5 R^4) Z (0. + 1.5 Cos[5 \[Phi]]) + ((
30 R + 90 R^9 - 120 R^11)/(
480 R^5) - (-14 + 15 R^2 + 9 R^10 - 10 R^12)/(96 R^6) +
1/4 (-(5/R^6) + 5 R^4) Z^2) (0. +
1.5 Sin[5 \[Phi]]) + (((-1 + R^2)^4 (46 R + 76 R^3 +
60 R^5))/(
26880 R^5) + ((-1 + R^2)^3 (18 + 23 R^2 + 19 R^4 +
10 R^6))/(
3360 R^4) - ((-1 + R^2)^4 (18 + 23 R^2 + 19 R^4 + 10 R^6))/(
5376 R^6) +
1/2 ((30 R + 90 R^9 - 120 R^11)/(
480 R^5) - (-14 + 15 R^2 + 9 R^10 - 10 R^12)/(
96 R^6)) Z^2 + 1/48 (-(5/R^6) + 5 R^4) Z^4) (0. +
10. Sin[5 \[Phi]]))^2))


I need to evaluate this expression in the following 3D grid

grid = Table[{(i+1/2)*5,(j+1/2)*5,k*0.0785},{i,0,119},{j,0,159},{k,0,79}]


and I'm doing that with the ParallelMap:

result = ParallelMap[expression /. {R -> #[], \[Phi] -> #[], Z -> #[]} &, grid, {3}]


However this takes about 4min which is quite a lot since I have to do this for a bunch of other large expressions. Is there any way to speed it up?

• Without expression, we cannot tell you anything. Place the code in place of the picture. – Alex Trounev Mar 31 at 16:32
• I just added the code for the expression. – AJHC Mar 31 at 17:45
• Thank you. It looks like expression is periodic on $\phi$ with a period $2\pi /5$. Why do you use (j+1/2)*5,{j,0,159} to map it? – Alex Trounev Mar 31 at 17:58
• Yes, indeed this particular expression has period of 5 in $\phi$, but I have a few more expressions which I need to evaluate, and which are not $\phi$ periodic... – AJHC Mar 31 at 18:03
• What are these huge result for? – Alex Trounev Mar 31 at 19:21

We can reduce the time by 100 times using Compile[] and ParallelTable[]

cf = Compile[{{R, _Real}, {\[Phi], _Real}, {Z, _Real}}, (0. +
1/10 (5/R^6 + 5 R^4) Z (0. +
1.5 Cos[5 \[Phi]]) + ((30 R + 90 R^9 -
120 R^11)/(480 R^5) - (-14 + 15 R^2 + 9 R^10 -
10 R^12)/(96 R^6) + 1/4 (-(5/R^6) + 5 R^4) Z^2) (0. +
1.5 Sin[5 \[Phi]]) + (((-1 + R^2)^4 (46 R + 76 R^3 +
60 R^5))/(26880 R^5) + ((-1 + R^2)^3 (18 + 23 R^2 +
19 R^4 + 10 R^6))/(3360 R^4) - ((-1 + R^2)^4 (18 +
23 R^2 + 19 R^4 + 10 R^6))/(5376 R^6) +
1/2 ((30 R + 90 R^9 - 120 R^11)/(480 R^5) - (-14 + 15 R^2 +
9 R^10 - 10 R^12)/(96 R^6)) Z^2 +
1/48 (-(5/R^6) + 5 R^4) Z^4) (0. +
10. Sin[5 \[Phi]]))/(Sqrt[((0. +
1/R + (5 ((-14 + 15 R^2 + 9 R^10 - 10 R^12)/(480 R^5) +
1/4 (1/R^5 + R^5) Z^2) (0. + 1.5 Cos[5 \[Phi]]))/
R + (5 (((-1 + R^2)^4 (18 + 23 R^2 + 19 R^4 +
10 R^6))/(26880 R^5) + ((-14 + 15 R^2 + 9 R^10 -
10 R^12) Z^2)/(960 R^5) +
1/48 (1/R^5 + R^5) Z^4) (0. + 10. Cos[5 \[Phi]]))/
R + ((-(1/R^5) + R^5) Z (0. -
1.5 Sin[5 \[Phi]]))/(2 R))^2 + (0. +
1/10 (-(1/R^5) + R^5) (0. + 1.5 Cos[5 \[Phi]]) +
1/2 (1/R^5 + R^5) Z (0. +
1.5 Sin[
5 \[Phi]]) + (((-14 + 15 R^2 + 9 R^10 -
10 R^12) Z)/(480 R^5) +
1/12 (1/R^5 + R^5) Z^3) (0. +
10. Sin[5 \[Phi]]))^2 + (0. +
1/10 (5/R^6 + 5 R^4) Z (0. +
1.5 Cos[
5 \[Phi]]) + ((30 R + 90 R^9 -
120 R^11)/(480 R^5) - (-14 + 15 R^2 + 9 R^10 -
10 R^12)/(96 R^6) + 1/4 (-(5/R^6) + 5 R^4) Z^2) (0. +
1.5 Sin[
5 \[Phi]]) + (((-1 + R^2)^4 (46 R + 76 R^3 +
60 R^5))/(26880 R^5) + ((-1 + R^2)^3 (18 + 23 R^2 +
19 R^4 + 10 R^6))/(3360 R^4) - ((-1 + R^2)^4 (18 +
23 R^2 + 19 R^4 + 10 R^6))/(5376 R^6) +
1/2 ((30 R + 90 R^9 - 120 R^11)/(480 R^5) - (-14 +
15 R^2 + 9 R^10 - 10 R^12)/(96 R^6)) Z^2 +
1/48 (-(5/R^6) + 5 R^4) Z^4) (0. +
10. Sin[5 \[Phi]]))^2)])]
result = ParallelTable[
cf[(i + 1/2)*5., (j + 1/2)*5, k*0.0785], {i, 0, 119}, {j, 0,
159}, {k, 0, 79}]; // AbsoluteTiming


It takes few seconds with 4 kernels.

• I didn't know this Compile function of Mathematica. Great! With two kernels it takes me now 3s. Thanks! – AJHC Apr 1 at 6:27
• @AJHC You are welcome! – Alex Trounev Apr 1 at 10:17