private static Tensor multiplyWithFactory(Sum sum, Tensor factor, Transformation... transformations) { final ArrayList<Tensor> newSumData = new ArrayList<>(sum.size()); Tensor temp; boolean reduced = false; for (int i = sum.size() - 1; i >= 0; --i) { temp = Transformation.Util.applySequentially(multiply(factor, sum.get(i)), transformations); if (!TensorUtils.isZero(temp)) { newSumData.add(temp); if (!reduced && isReduced(sum.get(i), factor, temp)) reduced = true; } } if (newSumData.size() == 0) return Complex.ZERO; if (newSumData.size() == 1) return newSumData.get(0); final Tensor[] data = newSumData.toArray(new Tensor[newSumData.size()]); if (reduced) return SumFactory.FACTORY.create(data); return new Sum(data, IndicesFactory.create(newSumData.get(0).getIndices().getFree())); }
/** * @deprecated very unsafe method without checks */ @Deprecated public static Tensor multiplySumElementsOnFactors(Sum sum) { final Tensor[] newSumData = new Tensor[sum.size()]; for (int i = newSumData.length - 1; i >= 0; --i) newSumData[i] = multiply(CC.generateNewSymbol(), sum.get(i)); return new Sum(newSumData, IndicesFactory.create(newSumData[0].getIndices().getFree())); }
@Override public Expression create(Tensor... tensors) { if (tensors.length != 2) throw new IllegalArgumentException("Wrong number of arguments."); if (tensors[0] == null || tensors[1] == null) throw new NullPointerException(); if (!TensorUtils.isZero(tensors[1]) && !TensorUtils.isIndeterminate(tensors[1]) && !tensors[0].getIndices().getFree().equalsRegardlessOrder(tensors[1].getIndices().getFree())) throw new TensorException(String.format("Inconsistent indices in expression: %s != %s", tensors[0].getIndices().getFree(), tensors[1].getIndices().getFree())); return new Expression(IndicesFactory.create(tensors[0].getIndices().getFree()), tensors[0], tensors[1]); } }
@Override public void put(Tensor tensor) { if (tensor == null) throw new NullPointerException(); else if (left == null) { left = tensor; indices = IndicesFactory.create(left.getIndices().getFree()); } else if (right == null) { right = tensor; if (!TensorUtils.isZero(right) && !TensorUtils.isIndeterminate(right) && !indices.equalsRegardlessOrder(right.getIndices().getFree())) throw new TensorException("Inconsistent indices in expression.", tensor); } else throw new TensorException("Expression have only two parts."); }
@Override public Indices getFree() { IntArrayList list = new IntArrayList(); int u, l; int iLower = firstLower, iUpper = 0; for (; iUpper < firstLower && iLower < data.length; ++iLower, ++iUpper) { u = data[iUpper] & 0x7FFFFFFF; //taking name with type l = data[iLower]; if (u < l) { list.add(data[iUpper]); --iLower; } else if (l < u) { list.add(l); --iUpper; } } list.add(data, iUpper, firstLower - iUpper); list.add(data, iLower, data.length - iLower); return IndicesFactory.create(list.toArray()); }
/** * Returns {@link Indices} of the corresponding mathematical expression. * * @return {@link Indices} of the corresponding mathematical expression */ public Indices getIndices() { switch (tokenType) { case Product: case Trace: IndicesBuilder builder = new IndicesBuilder(); for (ParseToken node : content) builder.append(node.getIndices()); return builder.getIndices(); case Sum: return IndicesFactory.create(content[0].getIndices()); case Power: return IndicesFactory.EMPTY_INDICES; } throw new ParserException("Unknown tensor type: " + tokenType); }
if (indices == null) { assert dataContainer.list.size() == 1; indices = IndicesFactory.create(dataContainer.list.get(0).getIndices());
indices = IndicesFactory.create(tensor.getIndices().getFree()); sortedFreeIndices = indices.getAllIndices().copy(); Arrays.sort(sortedFreeIndices);
public static Tensor symmetrizeUpperLowerIndices(Tensor tensor, boolean multiplyOnSymmetryFactor) { Indices indices = IndicesFactory.create(tensor.getIndices().getFree()); int[] indicesArray = indices.getAllIndices().copy(); List<Permutation> symmetries = TensorUtils.getIndicesSymmetriesForIndicesWithSameStates(indicesArray, tensor);
return new Sum(newData, IndicesFactory.create(newData[0].getIndices().getFree()));
return new Sum(newData, IndicesFactory.create(newData[0].getIndices().getFree()));