# How can I speed up processing with DeleteCases?

I would like to get points that are only inside a region. Here are the region and unfiltered points:

reg = Polygon[{{-(π/3), -(π/Sqrt)}, {-((2 π)/3),
0}, {-(π/3), π/Sqrt}, {π/3, π/Sqrt}, {(
2 π)/3, 0}, {π/3, -(π/Sqrt)}, {π/
3, -(π/Sqrt)}, {-(π/3), -(π/Sqrt)}}];
allPoints= Flatten[
ParallelTable[{x, y}, {x, -2.5, 2.5, 0.01}, {y, -2.5, 2.5, 0.01}], 1];


Now, to get the points inside the Polygon I used

npont = DeleteCases[allPoints, ({x_, y_} /; {x, y} ∉ reg)];


but it takes too long. I waited for 5 minutes without output. How can I speed this up?

• try Select[RegionMember@reg]@allPoints?
– kglr
Jan 8 at 1:01

As kglr says, you should use RegionMember. However, instead of mapping RegionMember (which is basically what his Select code does), you should provide all the points at once to the RegionMemberFunction. For example:

rmf = RegionMember[reg];

r1 = Select[rmf] @ allPoints; //AbsoluteTiming (* kglr *)
r2 = Pick[allPoints, rmf[allPoints]]; //AbsoluteTiming

r1 === r2


{3.96721, Null}

{0.083231, Null}

True