for (Tensor current : tensor) {
Split split = split(current);
for (CollectedTerm collectedTerm : collectedTerms)
if (TTest.testEqualstensorStructure(collectedTerm.collectedItem, split.collectedTerm)) {
//main rountine
UncontractIndicesAndRename uncontractIndicesTransformation =
new UncontractIndicesAndRename(TensorUtils.getAllIndicesNames(split.factoredOut),
collectedTerm.collectedTermIndicesNames.toArray());
split.collectedTerm = uncontractIndicesTransformation.renameIndicesAndBuidKroneckers(split.collectedTerm);
List<Tensor> generatedKroneckers = uncontractIndicesTransformation.getKroneckers();
split.factoredOut.add(generatedKroneckers);
//renaming indices of toCollect tensor
new RenameContractedIndices(collectedTerm.getCollectedFactorsIndicesNames().toArray()).transform(split.collectedTerm, split.factoredOut);
Tensor toCollect = split.collectedTerm;
Tensor collectedTensor = collectedTerm.collectedItem;
List<IndexMappingBuffer> buffers = IndexMappingUtils.createAllMappings(toCollect, collectedTensor, false);
IntArrayList uncontractedIndicesToCollect = TensorUtils.getContractedIndicesNames(split.collectedTerm, split.factoredOut);
IntArrayList uncontractedIndicesCollected = TensorUtils.getContractedIndicesNames(collectedTerm.collectedItem, collectedTerm.collectedFactors.getElements().get(0));
//finding best mapping
//botleneck for very huge sums
IndexMappingBuffer concurentBuffer = null;
int concurrence = 0, currentConcurrence;
boolean sign = false;
for (IndexMappingBuffer buffer : buffers) {
currentConcurrence = 0;
for (Map.Entry<Integer, IndexMappingBufferRecord> entry : buffer.getMap().entrySet()) {
int indexToCollect = entry.getKey();
int indexCollected = entry.getValue().getIndexName();
if (indexToCollect == indexCollected || (uncontractedIndicesToCollect.contains(indexToCollect) && uncontractedIndicesCollected.contains(indexCollected)))
currentConcurrence++;
}
if (concurentBuffer == null
|| currentConcurrence > concurrence
|| (sign && !buffer.getSignum()
&& (currentConcurrence >= concurrence))) {
concurentBuffer = buffer;
concurrence = currentConcurrence;
}
}
IndexMappingDirectAllowingUnmapped mappingToCollect = new IndexMappingDirectAllowingUnmapped();
IndexMappingDirectAllowingUnmapped mappingCollectedTensor = new IndexMappingDirectAllowingUnmapped();
//building index generator
IntArrayList usedIndices = new IntArrayList(TensorUtils.getAllIndicesNames(split.factoredOut));
usedIndices.addAll(collectedTerm.getCollectedFactorsIndicesNames());
usedIndices.addAll(collectedTerm.getCollectedTermIndicesNames());
IndexGenerator ig = new IndexGenerator(usedIndices.toArray());
List<Tensor> kroneckersCollected = new ArrayList<>();
for (Map.Entry<Integer, IndexMappingBufferRecord> entry : concurentBuffer.getMap().entrySet()) {
int indexCollected, indexToCollect, rawState = ((entry.getValue().getStates() & 1) ^ 1) << 31;
indexCollected = rawState | entry.getValue().getIndexName();
indexToCollect = rawState | entry.getKey();
SimpleTensor kroneckerCollected;
SimpleTensor kroneckerToCollect;
if (indexToCollect == indexCollected)
continue;
else if (uncontractedIndicesToCollect.contains(getNameWithType(indexToCollect))
&& uncontractedIndicesCollected.contains(getNameWithType(indexCollected))) {
mappingToCollect.add(inverseIndexState(indexToCollect),
inverseIndexState(indexCollected));
// continue;
}
else if (uncontractedIndicesToCollect.contains(getNameWithType(indexToCollect))) {
kroneckerCollected = CC.createKronecker(inverseIndexState(indexToCollect), indexCollected);
collectedTerm.getCollectedFactorsIndicesNames().add(getNameWithType(indexCollected));
collectedTerm.getCollectedFactorsIndicesNames().add(getNameWithType(indexToCollect));
collectedTerm.getCollectedTermIndicesNames().replaceFirst(getNameWithType(indexCollected), getNameWithType(indexToCollect));
mappingCollectedTensor.add(indexCollected, indexToCollect);
kroneckersCollected.add(kroneckerCollected);
// continue;
} else if (uncontractedIndicesCollected.contains(getNameWithType(indexCollected))) {
kroneckerToCollect = CC.createKronecker(indexToCollect, inverseIndexState(indexCollected));
split.factoredOut.add(kroneckerToCollect);
// continue;
} else {
int newIndex = ig.generate(getType(indexToCollect));
collectedTerm.getCollectedFactorsIndicesNames().add(getNameWithType(indexCollected));
collectedTerm.getCollectedFactorsIndicesNames().add(newIndex);
collectedTerm.getCollectedTermIndicesNames().replaceFirst(getNameWithType(indexCollected), newIndex);
// uncontractedIndicesCollected.addAll(newIndex);
// uncontractedIndicesToCollect.addAll(newIndex);
// uncontractedIndicesCollected.addAll(getNameWithType(indexCollected));
// uncontractedIndicesToCollect.addAll(getNameWithType(indexToCollect));
newIndex = getRawStateInt(indexToCollect) | newIndex;
kroneckerCollected = CC.createKronecker(indexCollected, inverseIndexState(newIndex));
kroneckerToCollect = CC.createKronecker(indexToCollect, inverseIndexState(newIndex));
mappingCollectedTensor.add(indexCollected, newIndex);
kroneckersCollected.add(kroneckerCollected);
split.factoredOut.add(kroneckerToCollect);
}
}
if (!kroneckersCollected.isEmpty())
for (Tensor p : collectedTerm.collectedFactors)
((Product) p).add(kroneckersCollected);
if (concurentBuffer.getSignum())
split.factoredOut.addFirst(TensorNumber.createMINUSONE());
if (!mappingToCollect.isEmpty())
ApplyIndexMappingDirectTransformation.INSTANCE.perform(split.factoredOut, mappingToCollect);
collectedTerm.collectedFactors.add(split.factoredOut);
collectedTerm.getCollectedFactorsIndicesNames().addAll(TensorUtils.getAllIndicesNames(split.factoredOut));
if (!mappingCollectedTensor.isEmpty())
ApplyIndexMappingDirectTransformation.INSTANCE.perform(collectedTensor, mappingCollectedTensor);
continue OUT_FOR;
}
UncontractIndices uncontractIndicesTransformation =
new UncontractIndices(TensorUtils.getAllIndicesNames(split.factoredOut));
split.collectedTerm = uncontractIndicesTransformation.renameIndicesAndBuidKroneckers(split.collectedTerm);
split.factoredOut.add(uncontractIndicesTransformation.getKroneckers());
CollectedTerm newTerm = new CollectedTerm(split.collectedTerm, split.factoredOut);
collectedTerms.add(newTerm);
}
}