/** * Creates tester to test identity mapping {@code from} -> {@code from} * * @param from array of from indices * @param sign sign */ public IndexMappingBufferTester(int[] from, boolean sign) { from = IndicesUtils.getIndicesNames(from); Arrays.sort(from); this.from = this.to = from; this.signum = sign; innerBuffer = new IndexMappingBufferImpl(); }
private Split(Tensor[] factors, Tensor summand) { this.factors = factors; this.summands.add(summand); Arrays.sort(factors); this.hashCode = Arrays.hashCode(factors); this.forbidden = IndicesUtils.getIndicesNames(new IndicesBuilder().append(factors).getIndices()); }
public static List<Permutation> getSymmetriesFromMappings(final int[] indices, MappingsPort mappingsPort) { List<Permutation> symmetries = new ArrayList<>(); int[] sortedIndicesNames = IndicesUtils.getIndicesNames(indices); int[] sortPermutation = ArraysUtils.quickSortP(sortedIndicesNames); Mapping buffer; while ((buffer = mappingsPort.take()) != null) symmetries.add(getSymmetryFromMapping1(sortedIndicesNames, sortPermutation, buffer)); return symmetries; }
private static boolean containsSubIndices(Indices indices, Indices subIndices) { int[] indicesArray = IndicesUtils.getIndicesNames(indices); Arrays.sort(indicesArray); for (int i = 0, size = subIndices.size(); i < size; ++i) if (Arrays.binarySearch(indicesArray, getNameWithType(subIndices.get(i))) < 0) return false; return true; }
public static Permutation getSymmetryFromMapping(final int[] indices, Mapping mapping) { int[] sortedIndicesNames = IndicesUtils.getIndicesNames(indices); int[] _sortPermutation = ArraysUtils.quickSortP(sortedIndicesNames); return getSymmetryFromMapping1(sortedIndicesNames, _sortPermutation, mapping); }
public IndexMappingBufferTester(Mapping mapping) { this.from = mapping.fromNames; this.to = IndicesUtils.getIndicesNames(mapping.toData); this.signum = mapping.getSign(); this.innerBuffer = new IndexMappingBufferImpl(); }
/** * Creates transformation that makes tensors symmetric in specified indices, symmetry group of the result will be * guaranteed a super group of permutation group of specified indices. * * @param indices simple indices with symmetries * @param multiplyBySymmetryFactor if specified, then resulting sum will be divided by its size */ public SymmetrizeTransformation(SimpleIndices indices, boolean multiplyBySymmetryFactor) { this.indices = indices; this.indicesArray = indices.toArray(); this.sortedIndicesNames = IndicesUtils.getIndicesNames(indices); Arrays.sort(this.sortedIndicesNames); this.indicesGroup = indices.getSymmetries().getPermutationGroup(); this.multiplyBySymmetryFactor = multiplyBySymmetryFactor; }
private static TIntHashSet getAllDummyIndicesT(boolean includeScalarFunctions, Tensor tensor) { TIntHashSet set = new TIntHashSet(); appendAllIndicesNamesT(tensor, set, includeScalarFunctions); set.removeAll(IndicesUtils.getIndicesNames(tensor.getIndices().getFree())); return set; }
private static Tensor applyIndexMapping(Tensor tensor, IndexMapper indexMapper) { if (tensor instanceof SimpleTensor) return applyIndexMapping(tensor, indexMapper, false); if (tensor instanceof Complex || tensor instanceof ScalarFunction) return tensor; return applyIndexMapping(tensor, indexMapper, indexMapper.contract(getIndicesNames(tensor.getIndices().getFree()))); }
/** * Returns {@code true} if specified tensor is zero in consequence of its symmetries: is both symmetric and * asymmetric with respect to some permutation at the same time. * * @param t tensor * @return {@code true} if specified tensor is zero in consequence of its symmetries */ public static boolean isZeroDueToSymmetry(Tensor t) { int[] indices = IndicesUtils.getIndicesNames(t.getIndices().getFree()); IndexMappingBufferTester bufferTester = new IndexMappingBufferTester(indices, false); OutputPort<IndexMappingBuffer> mp = IndexMappings.createPortOfBuffers(bufferTester, t, t); IndexMappingBuffer buffer; while ((buffer = mp.take()) != null) if (buffer.getSign()) return true; return false; }
if ((f = factors[i]) instanceof MultiTensor || f.getIndices().getFree().size() == 0) { toResolve.add(f); forbidden.addAll(IndicesUtils.getIndicesNames(f.getIndices().getFree())); } else { forbidden.addAll(TensorUtils.getAllIndicesNamesT(f));
final int[] freeIndices = IndicesUtils.getIndicesNames(tensor.getIndices().getFree()); Arrays.sort(freeIndices);
/** * 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; }
@Override public Tensor toTensor() { SimpleTensor[] vars = new SimpleTensor[content.length - 1]; Tensor temp = content[0].toTensor(); TIntHashSet allowedDummies = TensorUtils.getAllIndicesNamesT(temp); IndicesBuilder free = new IndicesBuilder().append(temp.getIndices()); for (int i = 1; i < content.length; ++i) { temp = content[i].toTensor(); free.append(temp.getIndices().getInverted()); allowedDummies.addAll(IndicesUtils.getIndicesNames(temp.getIndices())); if (!(temp instanceof SimpleTensor) && !(temp instanceof TensorField)) throw new IllegalArgumentException("Derivative with respect to non simple argument: " + temp); vars[i - 1] = (SimpleTensor) temp; } allowedDummies.removeAll(IndicesUtils.getIndicesNames(free.getIndices().getFree())); Tensor result = new DifferentiateTransformation( vars, new Transformation[]{ExpandAndEliminateTransformation.EXPAND_AND_ELIMINATE} ).transform(content[0].toTensor()); result = ApplyIndexMapping.optimizeDummies(result); TIntHashSet generated = TensorUtils.getAllDummyIndicesT(result); generated.removeAll(allowedDummies); result = ApplyIndexMapping.renameDummy(result, generated.toArray(), allowedDummies.toArray()); return result; } }
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 Tensor differentiate(Tensor tensor, Transformation[] expandAndContract, SimpleTensor var, boolean useDeltaFunction) { if (var.getIndices().size() != 0) { TIntHashSet allTensorIndices = TensorUtils.getAllIndicesNamesT(tensor); TIntHashSet dummyTensorIndices = new TIntHashSet(allTensorIndices); dummyTensorIndices.removeAll(tensor.getIndices().getFree().getAllIndices().copy()); if (containsIndicesNames(allTensorIndices, var.getIndices().getNamesOfDummies()) || containsIndicesNames(dummyTensorIndices, var.getIndices())) { allTensorIndices.addAll(IndicesUtils.getIndicesNames(var.getIndices())); var = (SimpleTensor) renameDummy(var, TensorUtils.getAllIndicesNamesT(tensor).toArray()); tensor = renameDummy(tensor, TensorUtils.getAllIndicesNamesT(var).toArray(), allTensorIndices); } else allTensorIndices.addAll(IndicesUtils.getIndicesNames(var.getIndices())); tensor = renameIndicesOfFieldsArguments(tensor, allTensorIndices); } return differentiate1(tensor, createRule(var, useDeltaFunction), expandAndContract); }
return tensor; allIndicesNames.addAll(getIndicesNames(tensor.getIndices().getFree())); int[] from = fromL.toArray(), to = new int[fromL.size()]; Arrays.sort(from);
return tensor; allIndicesNames.addAll(getIndicesNames(tensor.getIndices().getFree())); IndexGeneratorImpl generator = new IndexGeneratorImpl(allIndicesNames.toArray()); int[] from = fromL.toArray(), to = new int[fromL.size()];
return tensor; allIndicesNames.addAll(getIndicesNames(tensor.getIndices().getFree())); IndexGeneratorImpl generator = new IndexGeneratorImpl(allIndicesNames.toArray()); int[] from = fromL.toArray(), to = new int[fromL.size()];
allTensorIndices.addAll(getIndicesNames(var.getIndices().getFree())); if (needRename) { resolvedVars = vars.clone(); if (resolvedVars[i].getIndices().size() != resolvedVars[i].getIndices().getFree().size()) resolvedVars[i] = (SimpleTensor) renameDummy(resolvedVars[i], allTensorIndices.toArray()); allTensorIndices.addAll(getIndicesNames(resolvedVars[i].getIndices()));