if (tensor instanceof Product) {
int i;
int count = 0;
Tensor current, old;
IndicesBuilder ib = new IndicesBuilder();
List<Tensor> newProductElements = new ArrayList<>();
for (i = tensor.size() - 1; i >= 0; --i) {
current = tensor.get(i);
if (isN(current)) {
ib.append(current);
++count;
} else {
if (TensorUtils.isScalar(current)) {
FromChildToParentIterator iterator = new FromChildToParentIterator(current);
Tensor temp;
boolean flag = false;
while ((temp = iterator.next()) != null) {
if (isN(temp)) {
flag = true;
break;
}
}
if (!flag) {
newProductElements.add(current);
continue;
}
if (!(current instanceof Power) || !TensorUtils.isInteger(current.get(1)) || ((Complex) current.get(1)).intValue() != 2)
throw new IllegalArgumentException();
Tensor[] bases = {current.get(0), current.get(0)};
bases[1] = ApplyIndexMapping.renameDummy(bases[1], TensorUtils.getAllIndicesNamesT(tensor).toArray());
flag = false;
for (Tensor base : bases) {
for (Tensor t : base) {
if (isN(t)) {
ib.append(t);
++count;
flag = true;
} else
newProductElements.add(t);
}
}
if (!flag)
throw new IllegalArgumentException("Expand first");
} else
newProductElements.add(current);
}
}
if (count == 0)
return tensor;
if (count % 2 != 0)
return Complex.ZERO;
// System.out.println(count);
count = count / 2;
Tensor result = average(ib.getIndices().getAllIndices().copy());
long factor = ArithmeticUtils.pow((long) 2, count) * ArithmeticUtils.factorial(count + 1);//may be BigInteger?
Complex number = new Complex((long) factor).reciprocal();
result = ExpandTransformation.expand(result);
newProductElements.add(number);
newProductElements.add(result);