docs: private DenseTensor reduceDimensions(Collection<Integer> dimensionsToEliminate,
        boolean useSum, Backpointers backpointers) {
      SortedSet<Integer> dimensionsToKeep = Sets.newTreeSet(Ints.asList(getDimensionNumbers()));
      dimensionsToKeep.removeAll(dimensionsToEliminate);
  
      int[] myDimensionNumbers = getDimensionNumbers();
      int[] myDimensionSizes = getDimensionSizes();
      List<Integer> dimensionNumsToKeep = Lists.newArrayList();
      List<Integer> dimensionSizesToKeep = Lists.newArrayList();
      List<Integer> dimensionNumsToEliminate = Lists.newArrayList();
      List<Integer> dimensionSizesToEliminate = Lists.newArrayList();
      for (int i = 0; i < myDimensionNumbers.length; i++) {
        if (!dimensionsToEliminate.contains(myDimensionNumbers[i])) {
          dimensionNumsToKeep.add(myDimensionNumbers[i]);
          dimensionSizesToKeep.add(myDimensionSizes[i]);
        } else {
          dimensionNumsToEliminate.add(myDimensionNumbers[i]);
          dimensionSizesToEliminate.add(myDimensionSizes[i]);
        }
      }
  
      DenseTensorBuilder outputBuilder = new DenseTensorBuilder(Ints.toArray(dimensionNumsToKeep),
          Ints.toArray(dimensionSizesToKeep));
      // Optionally return the list of keynums which determined the values
      // in the returned tensor.
      long[] newBackpointerNums = null;
      long[] oldBackpointerNums = null;
      if (backpointers != null) {
        newBackpointerNums = new long[outputBuilder.values.length];
        oldBackpointerNums = new long[outputBuilder.values.length];
      }
  
      // Maps a key of the reduced tensor into a partial key of this.
      int[] dimensionMapping = getDimensionMapping(outputBuilder.getDimensionNumbers());
      int[] partialKey = new int[getDimensionNumbers().length];
      Arrays.fill(partialKey, -1);
      Iterator<KeyValue> otherKeyValues = outputBuilder.keyValueIterator();
  
      int backpointerIndex = 0;
      while (otherKeyValues.hasNext()) {
        KeyValue otherKeyValue = otherKeyValues.next();
        for (int i = 0; i < otherKeyValue.getKey().length; i++) {
          partialKey[dimensionMapping[i]] = otherKeyValue.getKey()[i];
        }
  
        Iterator<int[]> myKeyIterator = new SliceIndexIterator(getDimensionSizes(), partialKey);
        double resultVal = (useSum) ? 0.0 : Double.NEGATIVE_INFINITY;
        long backpointerKeyNum = -1;
        while (myKeyIterator.hasNext()) {
          if (useSum) {
            resultVal += getByDimKey(myKeyIterator.next());
          } else {
            int[] nextKey = myKeyIterator.next();
            double keyVal = getByDimKey(nextKey);
            if (keyVal > resultVal) {
              resultVal = keyVal;
              backpointerKeyNum = dimKeyToKeyNum(nextKey);
            }
          }
        }
        outputBuilder.put(otherKeyValue.getKey(), resultVal);
  
        if (backpointers != null) {
          newBackpointerNums[backpointerIndex] = outputBuilder.dimKeyToKeyNum(otherKeyValue.getKey());
          oldBackpointerNums[backpointerIndex] = backpointerKeyNum;
          backpointerIndex++;
        }
      }
  
      if (backpointers != null) {
        backpointers.setBackpointers(newBackpointerNums, oldBackpointerNums, backpointerIndex, this);
      }
  
      return outputBuilder.buildNoCopy();
    }