# Improve code style

I want to calculate r, theta and phi.

My code is very ugly. How would you propose to obtain the same results?

rStep = 2;
nCells = 3;
dTheta = 4;
dPhi = 5;

repmap = Table[Table[Range[nCells]*rStep, nCells], nCells]

{{{2, 4, 6}, {2, 4, 6}, {2, 4, 6}}, {{2, 4, 6}, {2, 4, 6}, {2, 4,
6}}, {{2, 4, 6}, {2, 4, 6}, {2, 4, 6}}}

flatten =
repmap[[All, #1, #]] & /@ Range[Length[repmap]] & /@
Range[Length[repmap]]

{{{2, 2, 2}, {4, 4, 4}, {6, 6, 6}}, {{2, 2, 2}, {4, 4, 4}, {6, 6,
6}}, {{2, 2, 2}, {4, 4, 4}, {6, 6, 6}}}

r = Flatten[flatten]

{2, 2, 2, 4, 4, 4, 6, 6, 6, 2, 2, 2, 4, 4, 4, 6, 6, 6, 2, 2, 2, 4, 4, \
4, 6, 6, 6}

repmap2 =
Table[Table[(Range[nCells] - 1)*dTheta - Pi/2, nCells], nCells] // N

{{{-1.5708, 2.4292, 6.4292}, {-1.5708, 2.4292, 6.4292}, {-1.5708,
2.4292, 6.4292}}, {{-1.5708, 2.4292, 6.4292}, {-1.5708, 2.4292,
6.4292}, {-1.5708, 2.4292, 6.4292}}, {{-1.5708, 2.4292,
6.4292}, {-1.5708, 2.4292, 6.4292}, {-1.5708, 2.4292, 6.4292}}}

flatten2 = repmap2[[All, All, #]] & /@ Range[Length[repmap2]]

{{{-1.5708, -1.5708, -1.5708}, {-1.5708, -1.5708, -1.5708}, {-1.5708, \
-1.5708, -1.5708}}, {{2.4292, 2.4292, 2.4292}, {2.4292, 2.4292,
2.4292}, {2.4292, 2.4292, 2.4292}}, {{6.4292, 6.4292,
6.4292}, {6.4292, 6.4292, 6.4292}, {6.4292, 6.4292, 6.4292}}}

theta = Flatten[flatten2]

{-1.5708, -1.5708, -1.5708, -1.5708, -1.5708, -1.5708, -1.5708, \
-1.5708, -1.5708, 2.4292, 2.4292, 2.4292, 2.4292, 2.4292, 2.4292, \
2.4292, 2.4292, 2.4292, 6.4292, 6.4292, 6.4292, 6.4292, 6.4292, \
6.4292, 6.4292, 6.4292, 6.4292}

repmap3 = Table[Table[(Range[nCells] - 1)*dPhi, nCells], nCells]

{{{0, 5, 10}, {0, 5, 10}, {0, 5, 10}}, {{0, 5, 10}, {0, 5, 10}, {0, 5,
10}}, {{0, 5, 10}, {0, 5, 10}, {0, 5, 10}}}

flatten3 = repmap3[[All, #, All]] & /@ Range[Length[repmap3]]

{{{0, 5, 10}, {0, 5, 10}, {0, 5, 10}}, {{0, 5, 10}, {0, 5, 10}, {0, 5,
10}}, {{0, 5, 10}, {0, 5, 10}, {0, 5, 10}}}

Phi = Flatten[flatten3]

{0, 5, 10, 0, 5, 10, 0, 5, 10, 0, 5, 10, 0, 5, 10, 0, 5, 10, 0, 5, \
10, 0, 5, 10, 0, 5, 10}

• Besides the example (thanks for that), can you explain what are you trying to achieve in general? Commented Jul 11, 2019 at 15:31
• Finally I want to calculate the so called pair correlation function of 3d data points using spherical coordinates. I have uploaded a matlab solution of a colleague, which I am trying to convert to Mathematica. This part now produces the same results as in matlab and is the basis for the rest of his code. Please see: drive.google.com/file/d/1KII7-EbuTd_eBI_4I0EKH-eKW8enCN7M/…
– lio
Commented Jul 11, 2019 at 15:58
• Just as a quick thing, any time you use Table[Table[..., nCells], nCells] this may be more compactly expressed as ConstantArray[..., {nCells, nCells}] and likely more quickly constructed as such. Commented Jul 11, 2019 at 16:11

This answer doesn't make any attempt to understand the problem being solved, but instead to generically replace the array constructs with ones that are (hopefully) easier to comprehend. The results should be identical to the code provided in the question.

repmap = ConstantArray[Range[nCells] rStep, {nCells, nCells}];
r2 = Flatten[Transpose[repmap, {3, 1, 2}]];
repmap2 =
ConstantArray[(Range[nCells] - 1) dTheta - Pi/2, {nCells, nCells}];
theta2 = Flatten[Transpose[repmap2, {3, 2, 1}]] // N;
repmap3 = ConstantArray[(Range[nCells] - 1) dPhi, {nCells, nCells}];
phi2 = Flatten[repmap3, {1, 2, 3}];


I used r2, theta2, and phi2 so that the results could be directly compared via:

{r == r2, theta == theta2, Phi == phi2}


Which should return {True, True, True}.

Looking at this bit by bit:

I used ConstantArray[..., {nCell, nCell}] instead of Table[Table[..., nCell], nCell], because there is no actual iteration over a variable here. Instead, this is just constructing several copies of the same list, which ConstantArray is designed for. The use of ConstantArray also signals that the core elements of this construct are all the same semantically, which should help in remembering what's going on.

It turns out that the complicated expressions in the repmap terms are all various tensor transposes. I found these transpose matches by brute force, but practically speaking it is just swapping the position of various dimensions within the tensors. It also turns out that repmap3 does not need any transposition, and can be Flattened directly.

Because the Transpose expressions are so much shorter, I omitted the flatten intermediates entirely and simply included the Flatten outside of the Transpose.