private static void reduce(IndexedTensor.Builder builder, DirectIndexedAddress destAddress, IndexedTensor argument, Aggregator aggregator, DirectIndexedAddress address, int[] toKeep, int keepIndex, int[] toReduce) {
        if (keepIndex < toKeep.length) {
            int currentIndex = toKeep[keepIndex];
            int dimSize = Convert.safe2Int(argument.dimensionSizes().size(currentIndex));

            int nextKeep = keepIndex + 1;
            for (int i = 0; i < dimSize; i++) {
                address.setIndex(currentIndex, i);
                destAddress.setIndex(keepIndex, i);
                reduce(builder, destAddress, argument, aggregator, address, toKeep, nextKeep, toReduce);
            }
        } else {
            ValueAggregator valueAggregator = ValueAggregator.ofType(aggregator);
            reduce(argument, valueAggregator, address, toReduce, 0);
            builder.cell(valueAggregator.aggregatedValue(), destAddress.getIndexes());
        }

    }
Creates a reduce function.

@param argument the tensor to reduce
@param aggregator the aggregator function to use
@param dimensions the list of dimensions to remove. If an empty list is given, all dimensions are reduced,
                  producing a dimensionless tensor (a scalar).
@throws IllegalArgumentException if any of the tensor dimensions are not present in the input tensor
vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java