@Override
public Tensor transform(Tensor tensor) {
//TODO review
Tensor parent = tensor.getParent();
TensorWrapper wrapper = new TensorWrapper(tensor);
TensorFirstTreeIterator iterator = new TensorFirstTreeIterator(wrapper, new OnLeaving());
Tensor current;
while (iterator.hasNext()) {
current = iterator.next();
level++;
if (indicesLevel == -1 && current instanceof Product) {
firstProduct = current;
indicesLevel = level;
}
if (current.getClass() != getFromClasss())
continue;
T _current = (T) current;
if (_current.getName() != from.getName())
continue;
OutputPortUnsafe<IndexMappingBuffer> opu = IndexMappings.createPortForSimpleTensor(from, _current, allowDiffStates);
IndexMappingBuffer buffer;
//TODO refactor throw off if (i.e. spread this code in inheritors)
if (allowDiffStates)
buffer = IndexMappingUtils.tryGetPositiveWithoutDiffStates(opu);
else
buffer = IndexMappingUtils.tryGetPositive(opu);
if (buffer == null)
continue;
Tensor newTo = getNewTo(_current, from, to);
if (newTo == null)
continue;
if (allowDiffStates)
if (!IndexMappingUtils.containsDiffStates(buffer))
newTo = ApplyIndexMappingUtils.applyIndexMappingWithoutDiffStates(newTo, buffer, getUsedIndices());
else
newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, getUsedIndices());
else
newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, getUsedIndices());
//TODO discuss with Dmitry
if (buffer.getSignum())
if (Derivative.onVarsIndicator.is(iterator)) {
derivativeVarChangedSignum ^= true;
iterator.set(Transformations.contractMetrics(newTo));
} else
iterator.set(new Product(TensorNumber.createMINUSONE(), newTo));
else if (Derivative.onVarsIndicator.is(iterator))
iterator.set(Transformations.contractMetrics(newTo));
else
iterator.set(newTo);
}
Tensor result = wrapper.getInnerTensor();
result.setParent(parent);
return result;
}