While solving my study problem (I need to programme an algorithm called "slab decomposition") I faced some troubles connected with sorting an array. So, I have an association array, where a key is a number of a slab and a value is a list of cells that slab consists of.
I've written a function that searches a minimal y-value of the cell in the slab, here it is:
MyMin[cell_, slab_] :=
Module[{pair = {}, y = {}}, For[i = 1, i <= Length@cell, ++i,
If[Min[cell[[i, 1]], cell[[Mod[i, Length@cell] + 1, 1]]] <
slab[[1]] && (Max[cell[[i, 1]],
cell[[Mod[i, Length@cell] + 1, 1]]] > slab[[2]] ||
Abs[Max[cell[[i, 1]], cell[[Mod[i, Length@cell] + 1, 1]]] -
slab[[2]]] < 10^-10) ||
Max[cell[[i, 1]], cell[[Mod[i, Length@cell] + 1, 1]]] >
slab[[2]] && (Min[cell[[i, 1]],
cell[[Mod[i, Length@cell] + 1, 1]]] < slab[[1]] ||
Abs[Min[cell[[i, 1]], cell[[Mod[i, Length@cell] + 1, 1]]] -
slab[[1]]] < 10^-10),
AppendTo[pair, {i, Mod[i, Length@cell] + 1}]]];
If[pair === {}, Return[Infinity]];
For[i = 1, i <= Length[pair], ++i,
For[j = 1, j <= 2, ++j,
AppendTo[
y, ((slab[[j]] -
cell[[pair[[i, 1]], 1]]) (cell[[pair[[i, 2]], 2]] -
cell[[pair[[i, 1]], 2]]))/(cell[[pair[[i, 2]], 1]] -
cell[[pair[[i, 1]], 1]]) + cell[[pair[[i, 1]], 2]]]]];
Return[Min[y]]]
I try to create an association array where the lists as the values will be sorted by the cell's minimal coordinate in this slab.
<|Table[i ->
SortBy[cellsInSlabs[i], MyMin[cells[[#]], slabs[[i]]] &], {i,
Length@slabs}]|>;
Unfortunately, this command gives the right answer only for 8 or 9 first slabs, then the arrays are just sorted in ascending order. But when I apply this command for the random slab outside the Table, it works correct. E.g.:
In[42]:= SortBy[cellsInSlabs[93], MyMin[cells[[#]], slabs[[93]]] &]
Out[42]= {26, 61, 47, 71, 134, 136, 167, 148, 149, 115, 112, 50, 32}
If you managed to understand me and my problem, please, write down here, cause I just can't understand what's wrong
Some explanations.
cellsInSlabs - the association array with the structure: "the number of the slab" -> "the list of numbers of cells in this slab". E.g. (firts 10 elements):
<|1 -> {15, 16, 40}, 2 -> {13, 15, 16, 17, 18, 40},
3 -> {12, 13, 14, 15, 16, 17, 18, 19, 40},
4 -> {12, 14, 16, 17, 18, 19, 40, 93, 95},
5 -> {12, 14, 17, 18, 19, 91, 93, 95, 122},
6 -> {12, 14, 17, 18, 19, 20, 38, 39, 66, 91, 93, 95, 122},
7 -> {12, 14, 20, 38, 39, 66, 90, 91, 93, 95, 122},
8 -> {20, 38, 39, 66, 88, 90, 91, 93, 94, 95, 122},
9 -> {20, 38, 39, 66, 88, 90, 91, 94, 122, 145, 146},
10 -> {11, 20, 38, 39, 41, 55, 56, 66, 88, 90, 91, 94, 122, 145,
146}|>
cells is a list of lists of points of verices, so an element of this list looks like {{x1,y1},{x2,y2},{x3,y3}} (I have only triangles in my task). E.g. (from 1st to the 5th)
{{{1.5, 0}, {1.47106, 0.195487}, {1.32664, 0.0868638}}, {{1.47106,
0.195487}, {1.39, 0.375888}, {1.2832, 0.287133}}, {{1.32664,
0.0868638}, {1.47106, 0.195487}, {1.2832, 0.287133}}, {{1.39,
0.375888}, {1.26934, 0.53282}, {1.2832, 0.287133}}, {{1.26934,
0.53282}, {1.1212, 0.664302}, {1.12445, 0.42743}}}
So if I write cells[[cellsInSlabs[1][[1]]]], I get {{-1.47106, 0.195487}, {-1.5, 0}, {-1.33672, 0.147156}}, which is the list of points of the vertices of the 15th cell.
slabs is a list consisting of lists that contain an initial and a final x-coordinate of each slab. E.g. (first 5 slabs)
{{-1.5, -1.47106}, {-1.47106, -1.39}, {-1.39, -1.33672}, {-1.33672, \
-1.30743}, {-1.30743, -1.26934}}
As I already mentioned, I have some troubles while making a Table. If I create it, I get these results (from 1st to 15th slab):
<|1 -> {16, 40, 15}, 2 -> {17, 18, 16, 40, 15, 13},
3 -> {19, 17, 18, 16, 40, 15, 13, 14, 12},
4 -> {19, 17, 18, 16, 40, 95, 14, 12, 93},
5 -> {19, 17, 18, 93, 14, 12, 91, 95, 122},
6 -> {19, 17, 18, 93, 14, 12, 20, 38, 39, 66, 91, 95, 122},
7 -> {93, 14, 12, 20, 38, 39, 66, 90, 91, 95, 122},
8 -> {20, 93, 95, 38, 39, 66, 88, 90, 91, 94, 122},
9 -> {20, 38, 39, 66, 88, 90, 91, 94, 122, 145, 146},
10 -> {11, 20, 38, 39, 41, 55, 56, 66, 88, 90, 91, 94, 122, 145,
146}, 11 -> {11, 20, 39, 41, 55, 56, 88, 90, 91, 94, 119, 121, 122,
145, 146},
12 -> {11, 41, 55, 56, 88, 89, 90, 91, 92, 94, 119, 121, 122, 145,
146}, 13 -> {11, 41, 55, 56, 88, 89, 92, 94, 119, 121, 145, 146,
147}, 14 -> {11, 41, 55, 56, 89, 92, 119, 121, 145, 146, 147, 196},
15 -> {11, 21, 37, 41, 55, 56, 65, 75, 89, 92, 119, 121, 145, 146,
147, 196}|>
The output is correct for 1-8 slabs (the numbers of cells are sorted there by their minimal y-coordinate in this very slab, the way of sorting you can see above). But then the numbers of slabs are just sorted in the ascending way. If I sort any of these arrays outside the table, the output is correct. Here is the e.g. for 12th slab:
In[47]:= SortBy[cellsInSlabs[12], MyMin[cells[[#]], slabs[[12]]] &]
Out[47]= {41, 56, 89, 92, 90, 91, 122, 145, 146, 94, 88, 121, 119, 55, 11}
I check it with the following command:
In[48]:=
SortBy[cellsInSlabs[12], MyMin[cells[[#]], slabs[[12]]] &] /.
y_Integer :> MyMin[cells[[y]], slabs[[12]]]
Out[48]= {-0.693474, -0.644355, -0.421685, -0.410052, -0.154418, \
-0.154418, -0.14279, -0.14279, 0.0683472, 0.170552, 0.259021, \
0.377257, 0.442765, 0.45263, 0.641756}

cellsInSlabsorslabsorcellsto look at. We have no idea what the output{26, 61, 47, 71, 134, 136, 167, 148, 149, 115, 112, 50, 32}means. And you're expecting us to reverse engineer code that you've already said is wrong but that we have no way of knowing how it might be wrong. $\endgroup$