private static Tensor renameDummyWithSign(Tensor tensor, int[] forbidden, boolean sign) { Tensor result = renameDummy(tensor, forbidden); return sign ? Tensors.negate(result) : result; }
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); }
@Override SimpleTensorDifferentiationRule newRuleForTensor(Tensor tensor) { return new SymmetricDifferentiationRule(this.var, renameDummy(derivative, TensorUtils.getAllIndicesNamesT(tensor).toArray()), allFreeFrom, freeVarIndices, useDeltaFunction); }
public void safeSet(Tensor tensor) { if (innerIterator.current() != tensor) set(ApplyIndexMapping.renameDummy(tensor, getForbidden())); }
void put(Tensor summand, Tensor factor) { TIntHashSet allowed = TensorUtils.getAllDummyIndicesT(factor); allowed.removeAll(factorForbiddenIndices); //old variant //IndexGenerator ig = new IndexGeneratorFromData(TensorUtils.getAllDummyIndicesT(factor).toArray()); summand = ApplyIndexMapping.renameDummy(summand, factorForbiddenIndices, allowed.toArray()); builder.put(summand); }
else for (j = 0; j < exponent; ++j) { temp = ApplyIndexMapping.renameDummy(scalars[i], allForbidden.toArray()); allForbidden.addAll(TensorUtils.getAllIndicesNamesT(temp)); factorOut.add(temp);
public static void resolveAllDummies(Tensor[] factors) { TIntHashSet forbidden = new TIntHashSet(); int i; for (i = factors.length - 1; i >= 0; --i) forbidden.addAll(TensorUtils.getAllIndicesNamesT(factors[i])); for (i = factors.length - 1; i >= 0; --i) { factors[i] = ApplyIndexMapping.renameDummy(factors[i], forbidden.toArray()); forbidden.addAll(TensorUtils.getAllIndicesNamesT(factors[i])); } }
if (result[i] == null) { factor = toResolve.get(--toResolvePosition); newFactor = ApplyIndexMapping.renameDummy(factor, forbidden.toArray()); forbidden.addAll(TensorUtils.getAllIndicesNamesT(newFactor)); result[i] = newFactor;
@Override public Tensor onLeaving(StackPosition<ForbiddenContainer> stackPosition) { if (!stackPosition.isModified()) return null; StackPosition<ForbiddenContainer> prev = stackPosition.previous(); if (prev == null) return null; Tensor tensor = stackPosition.getTensor(); tensor = ApplyIndexMapping.renameDummy(tensor, prev.getPayload().getForbidden().toArray()); prev.getPayload().submit(EMPTY_INT_SET, TensorUtils.getAllIndicesNamesT(tensor)); return tensor; } };
OutputPort<Tensor> nextPort() { final int[] tuple = tuplesPort.take(); if (tuple == null) return null; TIntHashSet added = new TIntHashSet(initialForbidden); ProductBuilder builder = new ProductBuilder(); builder.put(base.get(tuple[0])); for (int i = 1; i < tuple.length; ++i) builder.put(ApplyIndexMapping.renameDummy(base.get(tuple[i]), added.toArray(), added)); return createPort(builder.build(), expandSymbolic); }
public static Tensor expandPower(Sum argument, int power, int[] forbiddenIndices, Transformation[] transformations) { //TODO improve algorithm using Newton formula!!! int i; Tensor temp = argument; TIntHashSet forbidden = new TIntHashSet(forbiddenIndices); TIntHashSet argIndices = TensorUtils.getAllIndicesNamesT(argument); forbidden.ensureCapacity(argIndices.size() * power); forbidden.addAll(argIndices); for (i = power - 1; i >= 1; --i) { temp = expandPairOfSums((Sum) temp, (Sum) ApplyIndexMapping.renameDummy(argument, forbidden.toArray(), forbidden), transformations); if (!(temp instanceof Sum)) { temp = multiply(temp, apply(transformations, pow(argument, i - 1))); break; } } return temp; }
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); }
bases[1] = ApplyIndexMapping.renameDummy(bases[1], TensorUtils.getAllIndicesNamesT(tensor).toArray()); flag = false; for (Tensor base : bases) {
@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; } }
if (j != i) TensorUtils.appendAllIndicesNamesT(get(j), forbidden); tensor = ApplyIndexMapping.renameDummy(tensor, forbidden.toArray());
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);
descriptor = ApplyIndexMapping.renameDummy(descriptor, forbidden.toArray()); forbidden.addAll(TensorUtils.getAllIndicesNamesT(descriptor));