/** * Returns an instance of specified simple tensor with specified indices * * @param tensor simple tensor * @param indices indices * @return instance of specified simple tensor with specified indices */ public static SimpleTensor setIndices(SimpleTensor tensor, int[] indices) { return setIndices(tensor, IndicesFactory.createSimple(null, indices)); }
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())); }
/** * Parses string expression and returns indices. * * @param expression string representation of indices * @return integer indices * @throws IllegalArgumentException if string does not represent correct indices object. */ public static SimpleIndices parseSimple(String expression) { return IndicesFactory.createSimple(null, parse(expression)); }
/** * @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())); }
public static String toString(int[] indices, OutputFormat mode) { //todo refactor using StringBuilder since InconsistensIndicesException can be thrown return IndicesFactory.createSimple(null, indices).toString(mode); }
@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]); } }
private ParseToken addDeltas(OuterIndices inserted, ParseToken node, OuterIndices expected, int[][] upper, int[][] lower) { List<ParseToken> multipliers = new ArrayList<>(); for (byte i = 0; i < TYPES_COUNT; ++i) { if (!inserted.initialized[i] && expected.initialized[i]) { if (expected.lower[i] == 0 && expected.upper[i] == 0) continue; if (expected.lower[i] != 1 || expected.upper[i] != 1) throw new IllegalArgumentException("Deltas insertion is only supported for one upper and " + "one lower omitted indices; node: " + node); multipliers.add(new ParseTokenSimpleTensor(IndicesFactory.createSimple(null, upper[i][0], lower[i][0]), CC.current().getKroneckerName())); } } if (multipliers.isEmpty()) return node; multipliers.add(node); return new ParseToken(TokenType.Product, multipliers.toArray(new ParseToken[multipliers.size()])); } }
@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."); }
/** * Parses string expression and returns indices allowing repeated indices with same states (like T_ii): appropriate * raising or lowering will be performed automatically. * * @param expression string representation of indices * @return integer indices * @throws IllegalArgumentException if string does not represent correct indices object. */ public static SimpleIndices parseSimpleIgnoringVariance(String expression) { int[] indices = parse(expression); out: for (int i = 0; i < indices.length - 1; ++i) for (int j = i + 1; j < indices.length; ++j) if (indices[i] == indices[j]) { indices[i] = IndicesUtils.inverseIndexState(indices[i]); continue out; } return IndicesFactory.createSimple(null, indices); }
@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()); }
private Wrapper(Tensor tensor) { this.tensor = tensor; this.freeIndices = StructureOfIndices.create( IndicesFactory.createSimple(null, tensor.getIndices().getFree())); }
/** * 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); }
/** * Returns an array of free indices only * * @param indices * @return array of free indices only */ public static int[] getFree(int[] indices) { return IndicesFactory.createSimple(null, indices).getFree().getAllIndices().copy(); }
if (indices == null) { assert dataContainer.list.size() == 1; indices = IndicesFactory.create(dataContainer.list.get(0).getIndices());
protected Tensor[] createLine(final int length) { Tensor[] gammas = new Tensor[length]; int matrixIndex, u = matrixIndex = setType(matrixType, 0); for (int i = 0; i < length; ++i) gammas[i] = Tensors.simpleTensor(gammaName, createSimple(null, u | 0x80000000, u = ++matrixIndex, setType(metricType, i))); return gammas; }
indices = IndicesFactory.create(tensor.getIndices().getFree()); sortedFreeIndices = indices.getAllIndices().copy(); Arrays.sort(sortedFreeIndices);
/** * Returns new tensor field with specified string name, indices and * arguments list. Free indices of arguments assumed as arguments indices * bindings of this field bindings. * * @param name int name of the field * @param indices indices * @param arguments arguments list * @return new instance of {@link TensorField} object */ public static TensorField field(String name, SimpleIndices indices, Tensor[] arguments) { SimpleIndices[] argIndices = new SimpleIndices[arguments.length]; for (int i = 0; i < argIndices.length; ++i) argIndices[i] = IndicesFactory.createSimple(null, arguments[i].getIndices().getFree()); return field(name, indices, argIndices, arguments); }
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);
public Tensor nextTensorTree(TensorType head, int depth, Parameters parameters, Indices indices) { if (head == null) nextTensorTree(depth, parameters, indices); indices = indices.getFree(); if (depth == 0) return nextSimpleTensor(IndicesFactory.createSimple(null, indices)); if (head == TensorType.Product) return nextProductTree(depth, parameters, indices); if (head == TensorType.Sum) return nextSumTree(depth, parameters, indices); throw new RuntimeException(); }
return new Sum(newData, IndicesFactory.create(newData[0].getIndices().getFree()));