Tensor toCollect = split.term;
Tensor collectedTensor = term;
IntArrayList uncontractedIndicesToCollect = TensorUtils.getContractedIndicesNames(split.term, split.factoredOut);
IntArrayList uncontractedIndicesCollected = TensorUtils.getContractedIndicesNames(term, factors.getElements().get(0));
IndexMappingBuffer concurentBuffer = null;
if (factors.size() > 30)//TODO refacor in future
concurentBuffer = IndexMappings.getFirst(toCollect, collectedTensor, allowDiffStates);
else {
List<IndexMappingBuffer> buffers = IndexMappingUtils.createAllMappings(toCollect, collectedTensor, allowDiffStates);
//finding best mapping
//botleneck for very huge sums
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 (entry.getValue().diffStatesInitialized())
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(factorsIndicesNames);
usedIndices.addAll(termIndicesNames);
IndexGenerator ig = new IndexGenerator(usedIndices.toArray());
List<Tensor> mkCollectedList = new ArrayList<>();
for (Map.Entry<Integer, IndexMappingBufferRecord> entry : concurentBuffer.getMap().entrySet()) {