if (!(tensor instanceof Product))
return tensor;
//remember integrals
final List<Integral> integrals = new ArrayList<>();
TensorIterator iterator = tensor.iterator();
Tensor current;
while (iterator.hasNext()) {
current = iterator.next();
if (current instanceof Integral) {
integrals.add((Integral) current);
iterator.remove();
}
}
//no any integrals in the expression
if (integrals.isEmpty())
return tensor;
//only integrals
if (((Product) tensor).isEmpty())
return new Product(integrals);
//remember integration vars for each integral
final List<Map<Integer, SimpleTensor>> vars = new ArrayList<>();
for (final Integral i : integrals) {
final Map<Integer, SimpleTensor> map = new HashMap<>();
for (final SimpleTensor s : i.vars())
map.put(s.getName(), s);
vars.add(map);
}
//main routine
iterator = tensor.iterator();
Collection<SimpleTensor> tempVars;
while (iterator.hasNext()) {
current = iterator.next();
//vars in current
tempVars = TensorUtils.getDiffSimpleTensorContent(current);
out_for:
for (int i = 0; i < integrals.size(); ++i) {
final Map<Integer, SimpleTensor> map = vars.get(i);
for (SimpleTensor st : tempVars)
if (map.containsKey(st.getName()))
continue out_for;
//current can be placed under current integral
final Integral integral = integrals.get(i);
integrals.set(i, new Integral(new Product(current, integral.target()), integral.vars()));
iterator.remove();
}
}
Product result = new Product();
result.add(tensor);