public static Tensor applyIndexMapping(Tensor tensor, IndexMapper mapper) {
TreeTraverseIterator iterator = new TreeTraverseIterator(tensor, TraverseGuide.EXCEPT_FUNCTIONS_AND_FIELDS);
TraverseState state;
SimpleIndices oldIndices, newIndices;
SimpleTensor simpleTensor;
while ((state = iterator.next()) != null) {
if (state == TraverseState.Leaving)
continue;
if (!(iterator.current() instanceof SimpleTensor))
continue;
simpleTensor = (SimpleTensor) iterator.current();
oldIndices = simpleTensor.getIndices();
newIndices = oldIndices.applyIndexMapping(mapper);
if (oldIndices != newIndices)
if (simpleTensor instanceof TensorField)
iterator.set(Tensors.setIndicesToField((TensorField) simpleTensor, newIndices));
else