private static Tensor differentiate(Tensor tensor, Transformation[] expandAndContract, SimpleTensor var) {
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);