Given a spherical shell volume (i.e. one sphere subtracted by a smaller sphere of same center) I need to calculate all integral positions within this volume.
I can do this easily by calculating all integral points within the outer radius (by a cartesian product of all integers up to the outer diameter) and subsequently removing the points within the inner radius.
However, in some situations the radii can be rather large, such that generating all the points within the outer radius can be more time consuming than I would like. So I would like to eliminate the processing time wasted calculating the points within the inner radius (or at least minimize the number necessary), if possible.
Here is some current code, however the radii can be larger.
import numpy as n
import numpy.linalg as la
def cartesian_product(*arrays):
la = len(arrays)
dtype = n.result_type(*arrays)
arr = n.empty([len(a) for a in arrays] + [la], dtype=dtype)
for i, a in enumerate(n.ix_(*arrays)):
arr[..., i] = a
return arr.reshape(-1, la)
def points_in_shell(inner_radius, outer_radius):
cutoff = int(n.ceil(outer_radius))
int_range = n.arange(-cutoff, cutoff+1)
pos_initial = cartesian_product(int_range, int_range, int_range)
pos_norm = la.norm(pos_initial, axis=1)
valid_idx = n.where((pos_norm >= inner_radius) & (pos_norm <= outer_radius))
pos_final = pos_initial[valid_idx]
return pos_final
vals = points_in_shell(89.634, 95.254)
Any thoughts?