/** * Returns mapping, formed from this mapping by multiplying by specified sign. * * @param sign false - plus, true - minus one * @return a new mapping, formed from this mapping by multiplying by specified sign */ public Mapping addSign(boolean sign) { return new Mapping(fromNames, toData, sign ^ this.sign, true); }
if (mapping.isEmpty() || tensor.getIndices().getFree().size() == 0) return renameDummyWithSign(tensor, forbidden, mapping.getSign()); int[] from = mapping.getFromNames().copy(), to = mapping.getToData().copy(); return renameDummyWithSign(tensor, forbidden, mapping.getSign()); return applyIndexMapping(tensor, new Mapping(from, to, mapping.getSign()), forbidden);
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; }
private static Permutation getSymmetryFromMapping1(final int[] sortedIndicesNames, final int[] _sortPermutation, Mapping mapping) { final int dimension = sortedIndicesNames.length; IntArray _fromIndices = mapping.getFromNames(); IntArray _toIndices = mapping.getToData(); permutation[i] = i; return Permutations.createPermutation(mapping.getSign(), permutation); //this is inverse permutation
/** * Applies specified mapping of indices to tensor preventing some dummy index to be equal to one of the specified * <i>forbidden</i> indices. * * @param tensor tensor * @param mapping mapping of indices * @param forbidden forbidden indices * @return tensor with renamed indices */ public static Tensor applyIndexMapping(Tensor tensor, Mapping mapping, int[] forbidden) { if (mapping.isEmpty()) { if (tensor.getIndices().getFree().size() != 0) throw new IllegalArgumentException("From length does not match free indices size."); return renameDummyWithSign(tensor, forbidden, mapping.getSign()); } int[] freeIndicesNames = IndicesUtils.getIndicesNames(tensor.getIndices().getFree()); Arrays.sort(freeIndicesNames); if (!mapping.getFromNames().equalsToArray(freeIndicesNames)) { String fromIndices; try { fromIndices = IndicesUtils.toString(mapping.getFromNames().copy()); } catch (Exception e) { fromIndices = "error"; } throw new IllegalArgumentException("From indices names (" + fromIndices + ") does not match free indices names of tensor (" + IndicesUtils.toString(freeIndicesNames) + ")."); } Tensor result = _applyIndexMapping(tensor, mapping, forbidden); return mapping.getSign() ? Tensors.negate(result) : result; }
return null; boolean sign = mapping.getSign(); mapping = null; return null; mapping = mapping.addSign(sign); Arrays.sort(indexlessBijection); Arrays.sort(dataBijection); newTo = mapping.getSign() ? Tensors.negate(to) : to; else { if (possiblyAddsDummies) { allowed.removeAll(IndicesUtils.getIndicesNames(mapping.getToData())); newTo = applyIndexMappingAndRenameAllDummies(to, mapping, allowed.toArray());
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)); }
private static boolean testMapping(Mapping0 mapping0, Tensor eps, Tensor part) { if (!(part instanceof Product)) return false; Product p = (Product) part; Complex factor = p.getFactor(); Tensor ds = p.getDataSubProduct(); if (IndexMappings.testMapping(mapping0.mapping, eps, ds)) return factor.equals(mapping0.factor); else if (IndexMappings.testMapping(mapping0.mapping.addSign(true), eps, ds)) return factor.equals(mapping0.factor.negate()); return false; }
public IndexMappingBufferTester(Mapping mapping) { this.from = mapping.fromNames; this.to = IndicesUtils.getIndicesNames(mapping.toData); this.signum = mapping.getSign(); this.innerBuffer = new IndexMappingBufferImpl(); }
@Override public String toString() { return Arrays.toString(bijection) + "\n" + mapping.toString(); } }
Tensor applyIndexMappingToTo(Tensor oldFrom, Tensor to, Mapping mapping, SubstitutionIterator iterator) { if (toIsSymbolic) return mapping.getSign() ? Tensors.negate(to) : to; if (possiblyAddsDummies) return ApplyIndexMapping.applyIndexMapping(to, mapping, iterator.getForbidden()); return ApplyIndexMapping.applyIndexMappingAndRenameAllDummies(to, mapping, TensorUtils.getAllDummyIndicesT(oldFrom).toArray()); }
@Override public Mapping take() { IndexMappingBuffer temp = innerPort.take(); if (temp == null) return null; return new Mapping(temp); } }
match = IndexMappings.getFirst(transformed[0], oldCoefficient); if (match != null) { newCoefficient = match.getSign() ? Tensors.negate(transformed[1]) : transformed[1]; break;
/** * Returns the first mapping of tensor {@code from} on tensor {@code to}. * * @param from from tensor * @param to to tensor * @return mapping of indices of tensor {@code from} on tensor {@code to} */ public static Mapping getFirst(Tensor from, Tensor to) { IndexMappingBuffer buffer = createPortOfBuffers(from, to).take(); if (buffer == null) return null; return new Mapping(buffer); }
private static Tensor permute(Tensor tensor, int[] indicesArray, int[] upperPermutation, int[] lowerPermutation, List<int[]> generatedPermutations, List<Permutation> symmetries) { //creating resulting permutation upper indices are first, //because initial indices are sorted int[] permutation = new int[upperPermutation.length + lowerPermutation.length]; System.arraycopy(upperPermutation, 0, permutation, 0, upperPermutation.length); System.arraycopy(lowerPermutation, 0, permutation, upperPermutation.length, lowerPermutation.length); //TODO discover better algorithm (possible using stretches of symmetries) //checking wheather the way beetween current permutation and already //generated combinatorics exists throw any possible combination of symmetries for (int[] p : generatedPermutations) for (Permutation symmetry : symmetries) if (Arrays.equals(permutation, symmetry.permute(p))) return null; generatedPermutations.add(permutation); //processing new indices from permutation final int[] newIndices = new int[indicesArray.length]; for (int i = 0; i < indicesArray.length; ++i) newIndices[i] = indicesArray[permutation[i]]; //processing new tensor return ApplyIndexMapping.applyIndexMapping(tensor, new Mapping(indicesArray, newIndices), new int[0]); }
to[i] = IndicesUtils.getRawStateInt(fromIndex) ^ IndicesUtils.parseIndex(fromTo[1]); return new Mapping(from, to, sign);
@Override Tensor differentiateSimpleTensorWithoutCheck(SimpleTensor simpleTensor) { int[] to = simpleTensor.getIndices().getAllIndices().copy(); to = addAll(to, freeVarIndices); return applyIndexMapping(derivative, new Mapping(allFreeFrom, to), new int[0]); }
private static Tensor setIndices(Tensor t, Indices from, Indices to) { return ApplyIndexMapping.applyIndexMapping(t, new Mapping(from.getAllIndices().copy(), to.getAllIndices().copy()), new int[0]); }
private static Tensor invertIndices(Tensor t) { Indices free = t.getIndices().getFree(); Mapping mapping = new Mapping(free.toArray(), free.getInverted().toArray()); return ApplyIndexMapping.applyIndexMapping(t, mapping); }
private Tensor __newTo(DFromTo fromTo, TensorField currentField, Tensor currentNode, SubstitutionIterator iterator) { TensorField from = fromTo.from; Mapping mapping = IndexMappings.simpleTensorsPort(from, currentField).take(); if (mapping == null) return currentNode; Indices[] fromIndices = from.getArgIndices(), currentIndices = currentField.getArgIndices(); List<Tensor> argFrom = new ArrayList<>(), argTo = new ArrayList<>(); Tensor fArg; int[] cIndices, fIndices; int i; for (i = from.size() - 1; i >= 0; --i) { if (IndexMappings.positiveMappingExists(currentNode.get(i), from.get(i))) continue; fIndices = fromIndices[i].getAllIndices().copy(); cIndices = currentIndices[i].getAllIndices().copy(); assert cIndices.length == fIndices.length; fArg = ApplyIndexMapping.applyIndexMapping(from.get(i), new Mapping(fIndices, cIndices), new int[0]); argFrom.add(fArg); argTo.add(currentNode.get(i)); } Tensor newTo = fromTo.to; newTo = new SubstitutionTransformation( argFrom.toArray(new Tensor[argFrom.size()]), argTo.toArray(new Tensor[argTo.size()]), false).transform(newTo); return applyIndexMappingToTo(currentNode, newTo, mapping, iterator); }