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);