private static Tensor applyIndexMapping(Tensor tensor, final IndexMapper indexMapper, boolean contractIndices) {
if (tensor instanceof SimpleTensor) {
SimpleTensor simpleTensor = (SimpleTensor) tensor;
SimpleIndices oldIndices = simpleTensor.getIndices(),
newIndices = oldIndices.applyIndexMapping(indexMapper);
if (oldIndices == newIndices)
return tensor;
if (tensor instanceof TensorField) {
TensorField field = (TensorField) simpleTensor;
return Tensors.field(field.name, newIndices, field.argIndices, field.args);