}
@Override
public Tensor transform(Tensor tensor) {
Tensor parent = tensor.getParent();
TensorWrapper tensorWrapper = new TensorWrapper(tensor);
TreeIteratorSubs iterator = new TreeIteratorSubs(tensorWrapper);
TraverseState state;
Tensor current;
OUT:
while ((state = iterator.next()) != null) {
if (state != TraverseState.Entering)
continue;
current = iterator.tensor();
if (!(current instanceof TensorField))
continue;
TensorField currentField = (TensorField) current;
IndexMappingBuffer buffer =
IndexMappingUtils.tryGetPositiveWithoutDiffStates(IndexMappings.createPortForSimpleTensor(from, currentField, allowDiffStates));
if (buffer == null)
continue;
assert from.getArgs().length == currentField.getArgs().length;
Tensor[] fromArgs = from.getArgs(), currentArgs = currentField.getArgs();
Indices[] fromIndices = from.getArgIndices(), currentIndices = currentField.getArgIndices();
List<Transformation> transformations = new ArrayList<>();
Tensor fArg;
int[] cIndices, fIndices;
int i, j;
for (i = 0; i < fromArgs.length; ++i) {
if (IndexMappings.mappingExists(currentArgs[i], fromArgs[i], allowDiffStates))
continue;
fArg = fromArgs[i].clone();
fIndices = fromIndices[i].getAllIndices().copy();
cIndices = currentIndices[i].getAllIndices().copy();
if (cIndices.length != fIndices.length)
throw new InconsistentSubstitutionException(from, to, current);
boolean diffStates = false;
for (j = 0; j < cIndices.length; ++j)
if (IndicesUtils.getRawStateInt(cIndices[j]) != IndicesUtils.getRawStateInt(fIndices[j])) {
diffStates = true;
break;
}
if (diffStates && !allowDiffStates)
continue OUT;
if (diffStates) {
fArg = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(fArg, fIndices, cIndices, new int[0]);
fArg = Transformations.contractMetrics(fArg);
fromArgs[i] = Transformations.contractMetrics(fromArgs[i]);
} else {
for (j = 0; j < cIndices.length; ++j) {
cIndices[j] = IndicesUtils.getNameWithType(cIndices[j]);
fIndices[j] = IndicesUtils.getNameWithType(fIndices[j]);
}
IndexMappingImpl im = new IndexMappingImpl(new int[0], fIndices, cIndices);
fArg = ApplyIndexMappingTransformation.INSTANCE.perform(fArg, im);
}
if (!IndexMappings.mappingExists(fromArgs[i], fArg, allowDiffStates))
throw new InconsistentSubstitutionException(from, to, current);
transformations.add(Substitutions.createSubstitution(fArg, currentArgs[i], allowDiffStates));
}
Tensor newTo = to.clone();
if (!allowDiffStates || !IndexMappingUtils.containsDiffStates(buffer))
newTo = ApplyIndexMappingTransformation.INSTANCE.perform(newTo, new IndexMappingImpl(iterator.usedIndices(), buffer));
else
newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, iterator.usedIndices());
if (!transformations.isEmpty()) {
for (Transformation transformation : transformations)
newTo = transformation.transform(newTo);
int[] indices = newTo.getIndices().getFreeIndices().getAllIndices().copy();
for (i = 0; i < indices.length; ++i)
indices[i] = IndicesUtils.getNameWithType(indices[i]);
newTo = ApplyIndexMappingTransformation.INSTANCE.perform(newTo, new IndexMappingImpl(iterator.usedIndices(), indices, indices));
}
iterator.set(newTo);
}
Tensor result = tensorWrapper.getInnerTensor();
result.setParent(parent);
return result;
}