@Override
    public Tensor evaluate(EvaluationContext<NAMETYPE> context) {
        Tensor tensor = argument.evaluate(context);

        TensorType renamedType = TypeResolver.rename(tensor.type(), fromDimensions, toDimensions);

        // an array which lists the index of each label in the renamed type
        int[] toIndexes = new int[tensor.type().dimensions().size()];
        for (int i = 0; i < tensor.type().dimensions().size(); i++) {
            String dimensionName = tensor.type().dimensions().get(i).name();
            String newDimensionName = fromToMap.getOrDefault(dimensionName, dimensionName);
            toIndexes[renamedType.indexOfDimension(newDimensionName).get()] = i;
        }

        // avoid building a new tensor if dimensions can simply be renamed
        if (simpleRenameIsPossible(toIndexes)) {
            return tensor.withType(renamedType);
        }

        Tensor.Builder builder = Tensor.Builder.of(renamedType);
        for (Iterator<Tensor.Cell> i = tensor.cellIterator(); i.hasNext(); ) {
            Map.Entry<TensorAddress, Double> cell = i.next();
            TensorAddress renamedAddress = cell.getKey().partialCopy(toIndexes);
            builder.cell(renamedAddress, cell.getValue());
        }
        return builder.build();
    }
The <i>rename</i> tensor function returns a tensor where some dimensions are assigned new names.

@author bratseth
vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java