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); }
/** * Inverts indices of tensor * * @param tensor tensor * @return tensor with inverted indices */ public static Tensor invertIndices(Tensor tensor) { Indices indices = tensor.getIndices().getFree(); if (indices.size() == 0) return tensor; return applyIndexMapping(tensor, new Mapping(indices.toArray(), indices.getInverted().toArray())); } }
public ParseTokenDerivative(TokenType tokenType, ParseToken... content) { super(tokenType, content); IndicesBuilder ib = new IndicesBuilder(); ib.append(content[0].getIndices().getFree()); for (int i = content.length - 1; i >= 1; --i) ib.append(content[i].getIndices().getInverted().getFree()); indices = ib.getIndices(); }
@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; } }