* @return the array of tensors with renamed dummy indices
*/
public static Tensor[] resolveDummy(Tensor[] factors) {
//TODO preserve ordering //?
Tensor[] result = new Tensor[factors.length];
TIntHashSet forbidden = new TIntHashSet();
ArrayList<Tensor> toResolve = new ArrayList<>();
int position = -1;
for (Tensor f : factors) {
if (f instanceof Sum || f.getIndices().getFree().size() == 0) {
toResolve.add(f);
forbidden.addAll(f.getIndices().getFree().getAllIndices().copy());
} else {
forbidden.addAll(TensorUtils.getAllIndicesNamesT(f));
result[++position] = f;
}
}
Tensor factor, newFactor;
for (int i = toResolve.size() - 1; i >= 0; --i) {
factor = toResolve.get(i);
newFactor = ApplyIndexMapping.renameDummy(factor, forbidden.toArray());
forbidden.addAll(TensorUtils.getAllIndicesNamesT(newFactor));
result[++position] = newFactor;
}
return result;
}