def vectorized_app1(volume, roi, radius):
    m, n, r = volume.shape
x, y, z = np.mgrid[0:m, 0:n, 0:r]
X = x - roi[0]
Y = y - roi[1]
Z = z - roi[2]
return X ** 2 + Y ** 2 + Z ** 2 < radius ** 2

def vectorized_app1_improved(volume, roi, radius):
    m, n, r = volume.shape
x, y, z = np.mgrid[0:m, 0:n, 0:r]
X = x - roi[0]
Y = y - roi[1]
Z = z - roi[2]
return ne.evaluate('X**2 + Y**2 + Z**2 < radius**2')

def vectorized_app2(volume, roi, radius):
    m, n, r = volume.shape
vals = ((np.arange(m) - roi[0]) ** 2)[:, (None), (None)] + ((np.arange(n) -
    roi[1]) ** 2)[:, (None)] + (np.arange(r) - roi[2]) ** 2
return vals < radius ** 2

def vectorized_app2_simplified(volume, roi, radius):
    m, n, r = volume.shape
x, y, z = np.ogrid[0:m, 0:n, 0:r] - roi
return x ** 2 + y ** 2 + z ** 2 < radius ** 2
Python vectorizing nested for loops