IntArray _toIndices = mapping.getToData();
to = mapping.getToData().copy();
private static Tensor _applyIndexMapping(Tensor tensor, Mapping mapping, int[] forbidden) { final int mappingSize = mapping.size(); int[] allForbidden = new int[mappingSize + forbidden.length]; IntArray toData = mapping.getToData(), fromNames = mapping.getFromNames(); ArraysUtils.arraycopy(toData, 0, allForbidden, 0, mappingSize); System.arraycopy(forbidden, 0, allForbidden, mappingSize, forbidden.length); int i; for (i = allForbidden.length - 1; i >= 0; --i) allForbidden[i] = IndicesUtils.getNameWithType(allForbidden[i]); IntArrayList fromL = new IntArrayList(mappingSize), toL = new IntArrayList(mappingSize); fromL.addAll(fromNames); toL.addAll(toData); Arrays.sort(allForbidden); final int[] dummyIndices = TensorUtils.getAllDummyIndicesT(tensor).toArray(); final int[] forbiddenGeneratorIndices = new int[allForbidden.length + dummyIndices.length]; System.arraycopy(allForbidden, 0, forbiddenGeneratorIndices, 0, allForbidden.length); System.arraycopy(dummyIndices, 0, forbiddenGeneratorIndices, allForbidden.length, dummyIndices.length); IndexGeneratorImpl generator = new IndexGeneratorImpl(forbiddenGeneratorIndices); for (int index : dummyIndices) if (Arrays.binarySearch(allForbidden, index) >= 0) { //if index is dummy it cannot be free, so from (which is equal to free) //cannot contain it assert ArraysUtils.binarySearch(fromNames, index) < 0; fromL.add(index); toL.add(generator.generate(IndicesUtils.getType(index))); } int[] _from = fromL.toArray(), _to = toL.toArray(); ArraysUtils.quickSort(_from, _to); return applyIndexMapping(tensor, new IndexMapper(_from, _to)); }
public static Tensor applyIndexMappingAndRenameAllDummies(Tensor tensor, Mapping mapping, int[] allowedDummies) { if (TensorUtils.isZero(tensor)) return tensor; int[] freeIndicesNames = IndicesUtils.getIndicesNames(tensor.getIndices().getFree()); Arrays.sort(freeIndicesNames); if (!mapping.getFromNames().equalsToArray(freeIndicesNames)) throw new IllegalArgumentException("From indices names does not match free indices names of tensor. Tensor: " + tensor + " mapping: " + mapping); final int[] dummies = TensorUtils.getAllDummyIndicesT(tensor).toArray(); int[] from = new int[mapping.size() + dummies.length]; int[] to = new int[mapping.size() + dummies.length]; ArraysUtils.arraycopy(mapping.getFromNames(), 0, from, 0, mapping.size()); ArraysUtils.arraycopy(mapping.getToData(), 0, to, 0, mapping.size()); System.arraycopy(dummies, 0, from, mapping.size(), dummies.length); IndexGeneratorFromData generator = new IndexGeneratorFromData(allowedDummies); for (int i = mapping.size() + dummies.length - 1, mappingSize = mapping.size(); i >= mappingSize; --i) to[i] = generator.generate(IndicesUtils.getType(from[i])); ArraysUtils.quickSort(from, to); tensor = applyIndexMapping(tensor, new IndexMapper(from, to)); if (mapping.getSign()) tensor = Tensors.negate(tensor); return tensor; }
allowed.removeAll(IndicesUtils.getIndicesNames(mapping.getToData())); newTo = applyIndexMappingAndRenameAllDummies(to, mapping, allowed.toArray());