/** * Applies specified mapping of indices to tensor. * * @param tensor tensor * @param mapping mapping of indices * @return tensor with renamed indices */ public static Tensor applyIndexMapping(Tensor tensor, Mapping mapping) { return applyIndexMapping(tensor, mapping, new int[0]); }
private static Tensor renameDummyWithSign(Tensor tensor, int[] forbidden, boolean sign) { Tensor result = renameDummy(tensor, forbidden); return sign ? Tensors.negate(result) : result; }
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()); }
TByteObjectHashMap<MaxType> maxTypeCounts = new TByteObjectHashMap<>(); for (int i = oldData.length - 1; i >= 0; --i) { c = optimizeDummies(oldData[i]); if (c != oldData[i]) { if (newData == null) current = binarySearchAbs(current); from[count++] = index; newData = oldData.clone(); newData[i] = applyIndexMapping(newData[i], new IndexMapper(from, to, count), false); return __unsafe_mapping_apply__(t, new Transformation() { @Override public Tensor transform(Tensor t) {
descriptor = ApplyIndexMapping.applyIndexMapping(descriptor, new Mapping(free.getAllIndices().copy(), factorIndices), forbidden.toArray()); descriptor = ApplyIndexMapping.renameDummy(descriptor, forbidden.toArray()); forbidden.addAll(TensorUtils.getAllIndicesNamesT(descriptor));
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 renameDummyWithSign(tensor, forbidden, mapping.getSign()); return renameDummyWithSign(tensor, forbidden, mapping.getSign()); return applyIndexMapping(tensor, new Mapping(from, to, mapping.getSign()), forbidden);
arg = applyIndexMapping(arg, mapping = new IndexMapper(_from, _to)); if (arg != field.args[i]) { if (args == null) { int i; for (i = p.data.length - 1; i >= 0; --i) { temp = renameIndicesOfFieldsArguments(p.data[i], forbidden); if (temp != p.data[i]) { if (data == null) temp = renameIndicesOfFieldsArguments(p.indexlessData[i], forbidden); if (temp != p.indexlessData[i]) { if (indexless == null) Tensor temp, data[] = null; for (int i = s.size() - 1; i >= 0; --i) { temp = renameIndicesOfFieldsArguments(s.data[i], forbidden); if (temp != s.data[i]) { if (data == null) Tensor a = renameIndicesOfFieldsArguments(tensor.get(0), forbidden), b = renameIndicesOfFieldsArguments(tensor.get(1), forbidden); if (a == tensor.get(0) && b == tensor.get(1)) return tensor; Tensor arg = renameIndicesOfFieldsArguments(tensor.get(0), forbidden); if (arg == tensor.get(0)) return tensor; return tensor.getFactory().create(arg);
@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; } }
/** * 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 transform(Tensor t) { return ApplyIndexMapping.applyIndexMappingAutomatically(t, this); }
@Override public Tensor transform(Tensor t) { return optimizeDummies(t); } });
Tensor[] rhs = new Tensor[params.length]; for (int j = 0; j < params.length; ++j) { Tensor inverted = ApplyIndexMapping.invertIndices(coeffs[j]); for (int i = 0; i < params.length; ++i) { matrix[i][j] = expandAndEliminate(multiplyAndRenameConflictingDummies(coeffs[i], inverted), simplifications);
if (!allTensorIndices.isEmpty() && resolvedVars[i].getIndices().size() != 0) { if (resolvedVars[i].getIndices().size() != resolvedVars[i].getIndices().getFree().size()) resolvedVars[i] = (SimpleTensor) renameDummy(resolvedVars[i], allTensorIndices.toArray()); allTensorIndices.addAll(getIndicesNames(resolvedVars[i].getIndices())); tensor = renameDummy(tensor, TensorUtils.getAllIndicesNamesT(resolvedVars).toArray(), allTensorIndices); tensor = renameIndicesOfFieldsArguments(tensor, allTensorIndices);
/** * Applies given mapping of indices to tensor. In contrast to {@link #applyIndexMapping(Tensor, Mapping, int[])} * this method does not assumes that {@code from} indices matches exactly free indices of tensor: * if some free index of specified tensor does not present in map, then it will be mapped on itself; * if some index present in map, but does not present in free indices of tensor, then this mapping rule will * be ignored. * * @param tensor tensor * @param mapping mapping of indices * @return tensor with renamed indices * @throws IllegalArgumentException if {@code from.length != to.length} */ public static Tensor applyIndexMappingAutomatically(Tensor tensor, Mapping mapping) { return applyIndexMappingAutomatically(tensor, mapping, new int[0]); }
FactorNode(Tensor factor, TensorBuilder builder) { this.factor = ApplyIndexMapping.optimizeDummies(factor); this.builder = builder; factorForbiddenIndices = TensorUtils.getAllIndicesNamesT(this.factor).toArray(); }
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()))); }
private static Tensor differentiateWithRenaming(Tensor tensor, SimpleTensorDifferentiationRule rule, Transformation[] expandAndEliminate) { SimpleTensorDifferentiationRule newRule = rule.newRuleForTensor(tensor); tensor = renameDummy(tensor, newRule.getForbidden()); return differentiate1(tensor, newRule, expandAndEliminate); }
remainderIndices.addAll(getAllIndicesNamesT(indexlessRemainder)); remainderIndices.addAll(getAllIndicesNamesT(dataRemainderT)); newTo = applyIndexMapping(to, mapping, remainderIndices.toArray()); forbidden.forbidden.addAll(getAllIndicesNamesT(newTo)); } else { newTo = applyIndexMappingAndRenameAllDummies(to, mapping, allowed.toArray());
base.summands.add(ApplyIndexMapping.applyIndexMappingAutomatically(toAdd.summands.get(0), mapping, base.forbidden)); continue out;