Skip to main content
added 25 characters in body
Source Link
Michael E2
  • 244.7k
  • 18
  • 350
  • 774

A couple of numerical ways, which should be faster on larger problems:

(* auxiliary function: returns 1/0 if arg is in/out the interval {a, b} *)
intervalMember[a_, b_] := UnitStep[# - a] UnitStep[b - #] &;

Pick[list, Norm /@ N@list // intervalMember[3 - 0.1, 3 + 0.1], 1]

Pick[list, #.# & /@ N@list // intervalMember[(3 - 0.1)^2, (3 + 0.1)^2], 1]

(*  {{-2, -2, -1},..., {2, 2, 1}} -- outputs centers  *)

Yet another way, which is pretty fast (Nearest returns a list sorted by distance):

nf = Nearest[list];Nearest[N@list];
Complement[Drop[
 nf[{0, 0, 0}, {All, 3 + 0.1}], 
 nf[Length@nf[{0, 0, 0}, {All, 3 - 0.1}]]
 

For the specific problem in the OP (large integer radius 3 sphere, centers of small spheres on integer grid, only integer coordinates within small spheres are at the centers):

Pick[list, #.# & /@ list, 3^2]

A couple of numerical ways, which should be faster on larger problems:

(* auxiliary function: returns 1/0 if arg is in/out the interval {a, b} *)
intervalMember[a_, b_] := UnitStep[# - a] UnitStep[b - #] &;

Pick[list, Norm /@ N@list // intervalMember[3 - 0.1, 3 + 0.1], 1]

Pick[list, #.# & /@ N@list // intervalMember[(3 - 0.1)^2, (3 + 0.1)^2], 1]

(*  {{-2, -2, -1},..., {2, 2, 1}} -- outputs centers  *)

Yet another way:

nf = Nearest[list];
Complement[
 nf[{0, 0, 0}, {All, 3 + 0.1}], 
 nf[{0, 0, 0}, {All, 3 - 0.1}]]
 

For the specific problem in the OP (large integer radius 3 sphere, centers of small spheres on integer grid, only integer coordinates within small spheres are at the centers):

Pick[list, #.# & /@ list, 3^2]

A couple of numerical ways, which should be faster on larger problems:

(* auxiliary function: returns 1/0 if arg is in/out the interval {a, b} *)
intervalMember[a_, b_] := UnitStep[# - a] UnitStep[b - #] &;

Pick[list, Norm /@ N@list // intervalMember[3 - 0.1, 3 + 0.1], 1]

Pick[list, #.# & /@ N@list // intervalMember[(3 - 0.1)^2, (3 + 0.1)^2], 1]

(*  {{-2, -2, -1},..., {2, 2, 1}} -- outputs centers  *)

Yet another way, which is pretty fast (Nearest returns a list sorted by distance):

nf = Nearest[N@list];
Drop[
 nf[{0, 0, 0}, {All, 3 + 0.1}], 
 Length@nf[{0, 0, 0}, {All, 3 - 0.1}]]
 

For the specific problem in the OP (large integer radius 3 sphere, centers of small spheres on integer grid, only integer coordinates within small spheres are at the centers):

Pick[list, #.# & /@ list, 3^2]
Source Link
Michael E2
  • 244.7k
  • 18
  • 350
  • 774

A couple of numerical ways, which should be faster on larger problems:

(* auxiliary function: returns 1/0 if arg is in/out the interval {a, b} *)
intervalMember[a_, b_] := UnitStep[# - a] UnitStep[b - #] &;

Pick[list, Norm /@ N@list // intervalMember[3 - 0.1, 3 + 0.1], 1]

Pick[list, #.# & /@ N@list // intervalMember[(3 - 0.1)^2, (3 + 0.1)^2], 1]

(*  {{-2, -2, -1},..., {2, 2, 1}} -- outputs centers  *)

Yet another way:

nf = Nearest[list];
Complement[
 nf[{0, 0, 0}, {All, 3 + 0.1}], 
 nf[{0, 0, 0}, {All, 3 - 0.1}]]
 

For the specific problem in the OP (large integer radius 3 sphere, centers of small spheres on integer grid, only integer coordinates within small spheres are at the centers):

Pick[list, #.# & /@ list, 3^2]