How to sum an ndarray over ranges bounded by other indexes