docs: private SparseTensor reduceDimensions(Set<Integer> dimensionsToEliminate,
        boolean useSum, Backpointers backpointers) {
      // TODO(jayantk): This method can be generalized to support a
      // generic reduce
      // operation (as a function), but it's unclear what the effect on
      // performance will be.
  
      // Rotate all of the dimensions which are being eliminated to the
      // end of the keyNums array.
      int[] newLabels = new int[numDimensions()];
      int[] inversionPermutation = new int[numDimensions()];
      int[] newDimensions = new int[numDimensions()];
      int[] newDimensionSizes = new int[numDimensions()];
      int numEliminated = 0;
      int[] dimensionNums = getDimensionNumbers();
      int[] dimensionSizes = getDimensionSizes();
      for (int i = 0; i < dimensionNums.length; i++) {
        if (dimensionsToEliminate.contains(dimensionNums[i])) {
          // Dimension labels must be unique, hence numEliminated.
          newLabels[i] = Integer.MAX_VALUE - numEliminated;
          inversionPermutation[dimensionNums.length - (numEliminated + 1)] = i;
          numEliminated++;
        } else {
          newLabels[i] = dimensionNums[i];
          inversionPermutation[i - numEliminated] = i;
          newDimensions[i - numEliminated] = dimensionNums[i];
          newDimensionSizes[i - numEliminated] = dimensionSizes[i];
        }
      }
  
      if (numEliminated == 0) {
        // If none of the dimensions being eliminated are actually part of
        // this tensor, then there's no need to do any more work.
        if (backpointers != null) {
          backpointers.setBackpointers(keyNums, keyNums, keyNums.length, this);
        }
        return this;
      } else if (numEliminated == dimensionNums.length && backpointers == null && useSum) {
        // Faster implementation for summing up all values in the tensor,
        // a common operation.
        return sumOutAllDimensions();
      }
  
      SparseTensor relabeled = relabelDimensions(newLabels);
      int resultNumDimensions = dimensionNums.length - numEliminated;
  
      // Get a number which we can divide each key by to map it to a key
      // in the reduced dimensional tensor.
      long keyNumDenominator = (resultNumDimensions > 0) ? relabeled.indexOffsets[resultNumDimensions - 1] :
          relabeled.indexOffsets[0] * relabeled.getDimensionSizes()[0];
  
      long[] resultKeyInts = new long[relabeled.values.length];
      long[] backpointerKeyInts = new long[relabeled.values.length];
      double[] resultValues = new double[relabeled.values.length];
  
      int resultInd = 0;
      long[] relabeledKeyNums = relabeled.keyNums;
      double[] relabeledValues = relabeled.values;
      int relabeledValuesLength = relabeled.values.length;
      for (int i = 0; i < relabeledValuesLength; i++) {
        long relabeledKeyNumI = relabeledKeyNums[i];
        if (i != 0 && resultInd > 0 &&
            (relabeledKeyNumI / keyNumDenominator) == resultKeyInts[resultInd - 1]) {
          // This key maps to the same entry as the previous key.
          if (useSum) {
            resultValues[resultInd - 1] += relabeledValues[i];
          } else {
            double resultVal = resultValues[resultInd - 1];
            double relabeledVal = relabeledValues[i];
            if (relabeledVal > resultVal) {
              resultValues[resultInd - 1] = relabeledVal;
              backpointerKeyInts[resultInd - 1] = relabeledKeyNumI;
            }
          }
        } else {
          if (resultInd > 0 && resultValues[resultInd - 1] == 0.0) {
            // Make sure the result tensor contains no zero-valued
            // entries.
            resultInd--;
          }
  
          resultKeyInts[resultInd] = relabeledKeyNumI / keyNumDenominator;
          backpointerKeyInts[resultInd] = relabeledKeyNumI;
          resultValues[resultInd] = relabeledValues[i];
          resultInd++;
        }
  
        int prevIndex = resultInd - 1;
        if (!useSum && resultValues[prevIndex] < 0.0) {
          // Ensure that, if values is negative, we include missing keys
          // in the maximization.
          long prevKeyNum = relabeledKeyNumI - 1;
          long nextKeyNum = relabeledKeyNumI + 1;
  
          if (i > 0 && relabeledKeyNums[i - 1] != prevKeyNum
              && prevKeyNum / keyNumDenominator == resultKeyInts[prevIndex]) {
            // prevKeyNum is not in relabeled, but has a higher value
            // than the current key.
            resultValues[prevIndex] = 0.0;
            backpointerKeyInts[prevIndex] = prevKeyNum;
          } else if (i + 1 < relabeledValuesLength && relabeledKeyNums[i + 1] != nextKeyNum
              && nextKeyNum / keyNumDenominator == resultKeyInts[prevIndex]) {
            // nextKeyNum is not in relabeled, but has a higher value
            // than the current key. Delete the current key from the tensor.
            resultValues[prevIndex] = 0.0;
            backpointerKeyInts[prevIndex] = nextKeyNum;
          }
        }
      }
  
      if (backpointers != null) {
        // backpointerKeyInts needs to have the inverse dimension
        // relabeling applied to it.
        long[] transformedBackpointers = transformKeyNums(backpointerKeyInts, relabeled.indexOffsets,
            this.indexOffsets, inversionPermutation);
        backpointers.setBackpointers(resultKeyInts, transformedBackpointers, resultInd, this);
      }
  
      return resizeIntoTable(ArrayUtils.copyOf(newDimensions, resultNumDimensions),
          ArrayUtils.copyOf(newDimensionSizes, resultNumDimensions),
          resultKeyInts, resultValues, resultInd);
    }