This function is very inefficient: you allocate a huge array only to get the extrema. Also since you take all combinations anyhow, you can extremize each axis by itself. Here is my suggestion which should take a fraction of the original runtime:
function bound(xe_all, ye_all, ze_all)
xmin = Inf; xmax = -Inf
ymin = Inf; ymax = -Inf
zmin = Inf; zmax = -Inf
for block in axes(xe_all, 2)
@views r, theta, phi = xe_all[:, b], ye_all[:, b], ze_all[:, b]
rvals = extrema(r)
theta_sin_vals = extrema(sin, theta)
theta_cos_vals = extrema(cos, theta)
phi_sin_vals = extrema(sin, phi)
phi_cos_vals = extrema(cos, phi)
new_zmin, new_zmax = extrema(r*cos_theta for r in rvals for cos_theta in theta_cos_vals)
zmax = max(zmax, new_zmax)
zmin = min(zmin, new_zmin)
# do the others yourself
end
return xmin, xmax, ymin, ymax, zmin, zmax
end
Note that the xmin, ymin, zmin parameters are globals again. In this inner loop that will cost a lot of performance.