public static Tensor factor(Tensor tensor) {
return factor(tensor, true, JasFactor.ENGINE);
}
private static Tensor factorSymbolicTerms(Tensor tensor, FactorizationEngine factorizationEngine) {
FromParentToChildIterator iterator = new FromParentToChildIterator(tensor);
Tensor c;
while ((c = iterator.next()) != null) {
if (!(c instanceof Sum))
continue;
Tensor remainder = c, temp;
IntArrayList symbolicPositions = new IntArrayList();
for (int i = c.size() - 1; i >= 0; --i) {
temp = c.get(i);
if (isSymbolic(temp)) {
symbolicPositions.add(i);
if (remainder instanceof Sum)
remainder = ((Sum) remainder).remove(i);
else remainder = Complex.ZERO;
}
}
Tensor symbolicPart = ((Sum) c).select(symbolicPositions.toArray());
symbolicPart = factorSymbolicTerm(symbolicPart, factorizationEngine);
if (remainder instanceof Sum) {
SumBuilder sb = new SumBuilder(remainder.size());
for (Tensor tt : remainder)
sb.put(factorSymbolicTerms(tt, factorizationEngine));
remainder = sb.build();
} else
remainder = factorSymbolicTerms(remainder, factorizationEngine);
iterator.set(Tensors.sum(symbolicPart, remainder));
}
return iterator.result();
}