Guest User

Untitled

a guest
Dec 17th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. pts = Partition[RandomReal[1, 10000], 2];
  2. ListPlot[pts]
  3.  
  4. pts2 = {pts[[1]]};
  5. Table[If[Min[Map[Norm[pts[[i]] - #] &, pts2]] > 0.05,
  6. AppendTo[pts2, pts[[i]]]], {i, 2, Length[pts],
  7. 1}]; // AbsoluteTiming (* -> 1.35 *)
  8. ListPlot[pts2]
  9.  
  10. pts = RandomReal[1, {10000, 2}];
  11. f = Nearest[pts];
  12.  
  13. k[{}, r_] := r
  14. k[ptsaux_, r_: {}] := Module[{x = RandomChoice[ptsaux]},
  15. k[Complement[ptsaux, f[x, {Infinity, .05}]], Append[r, x]]]
  16.  
  17. ListPlot@k[pts]
  18.  
  19. ops[pts_] := Module[{pts2},
  20. pts2 = {pts[[1]]};
  21. Table[If[Min[Map[Norm[pts[[i]] - #] &, pts2]] > 0.05,
  22. AppendTo[pts2, pts[[i]]]], {i, 2, Length[pts], 1}];
  23. pts2]
  24.  
  25. bobs[pts_] := Union[pts, SameTest -> (Norm[#1 - #2] < 0.05 &)]
  26.  
  27. belis[pts_] := Module[{f, k},
  28. f = Nearest[pts];
  29. k[{}, r_] := r;
  30. k[ptsaux_, r_: {}] := Module[{x = RandomChoice[ptsaux]},
  31. k[Complement[ptsaux, f[x, {Infinity, .05}]], Append[r, x]]];
  32. k[pts]]
  33.  
  34.  
  35. lens = {1000, 3000, 5000, 10000};
  36. pts = RandomReal[1, {#, 2}] & /@ lens;
  37. ls = First /@ {Timing[ops@#;], Timing[bobs@#;], Timing[belis@#;]} & /@ pts;
  38. ListLogLinePlot[ MapThread[List, {ConstantArray[lens, 3], Transpose@ls}, 2],
  39. PlotLegends -> {"OP", "BOB", "BELI"}, Joined ->True]
  40.  
  41. pts = Partition[RandomReal[1, 10000], 2];
  42.  
  43. ListPlot[pts]
  44.  
  45. pts2 = Union[pts, SameTest -> (Norm[#1 - #2] < 0.05 &)];
  46.  
  47. Length[pts2]
  48.  
  49. 326
  50.  
  51. ListPlot[pts2]
  52.  
  53. pts = Partition[RandomReal[1, 10000], 2];
  54. nearestOnGrid[points_, d_] := Nearest[points, Outer[List, Range[0, 1, d], Range[0, 1, d]]~Flatten~1]~Flatten~1
  55. testDistances[grid_, leastD_] := Min[EuclideanDistance @@@ grid~Subsets~{2}] < leastD
  56.  
  57. grid = nearestOnGrid[pts, 0.074]; // AbsoluteTiming
  58. testDistances[grid, 0.05] // AbsoluteTiming
  59. (* {0.000957, Null} *)
  60. (* {0.016401, True} *)
  61.  
  62. GraphicsRow[{ListPlot[pts], ListPlot[grid]}, ImageSize -> 600]
  63.  
  64. result = NestWhile[
  65. Nest[ Complement[#, Rest@Nearest[ # , RandomChoice[#] ,
  66. { Infinity, .05}]] & , #, Ceiling[(Length@#)/100] ] &, pts,
  67. Min[EuclideanDistance @@@ Nearest[#, #, 2]] < .05 & ];
  68.  
  69. pts = Last[NestWhile[{Complement[First[#],
  70. Nearest[First[#], x = RandomChoice[First[#]], {All, .1}]],
  71. Append[Last[#], x]} &, {RandomReal[1, {10000, 2}], {}},Length@First[‌​ #] != 0 &]];
  72. ListPlot[pts]
Add Comment
Please, Sign In to add comment