/** * Appends consequentially indices of {@code tensors} in specified array. * * @param tensors an array of tensors * @return a reference to this object */ public IndicesBuilder append(Tensor... tensors) { for (Tensor t : tensors) append(t); return this; }
/** * Appends consequentially indices of {@code tensors} in specified array. * * @param tensors a collection of tensors * @return a reference to this object */ public IndicesBuilder append(Collection<? extends Tensor> tensors) { for (Tensor t : tensors) append(t); return this; }
/** * Appends specified {@code Indices}. * * @param indices indices to be appended * @return a reference to this object */ public IndicesBuilder append(Indices indices) { return append(indices.getAllIndices()); }
/** * Appends specified {@code IndicesBuilder}. * * @param ib IndicesBuilder * @return a reference to this object */ public IndicesBuilder append(IndicesBuilder ib) { return append(ib.toArray()); }
/** * Appends indices of specified {@code Tensor}. * * @param tensor a tensor * @return a reference to this object */ public IndicesBuilder append(Tensor tensor) { return append(tensor.getIndices()); }
private Split(Tensor[] factors, Tensor summand) { this.factors = factors; this.summands.add(summand); Arrays.sort(factors); this.hashCode = Arrays.hashCode(factors); this.forbidden = IndicesUtils.getIndicesNames(new IndicesBuilder().append(factors).getIndices()); }
@Override protected Tensor select1(int[] positions) { int add = factor == Complex.ONE ? 0 : 1; Complex newFactor = Complex.ONE; List<Tensor> newIndexless = new ArrayList<>(), newData = new ArrayList<>(); for (int position : positions) { position -= add; if (position == -1) newFactor = factor; else if (position < indexlessData.length) newIndexless.add(indexlessData[position]); else newData.add(data[position - indexlessData.length]); } return new Product(new IndicesBuilder().append(newData).getIndices(), newFactor, newIndexless.toArray(new Tensor[newIndexless.size()]), newData.toArray(new Tensor[newData.size()])); }
@Override protected Tensor remove1(int[] positions) { Complex newFactor = factor; if (factor != Complex.ONE) { if (positions[0] == 0) { newFactor = Complex.ONE; positions = Arrays.copyOfRange(positions, 1, positions.length); } for (int i = positions.length - 1; i >= 0; --i) --positions[i]; } int dataFrom = Arrays.binarySearch(positions, indexlessData.length - 1); if (dataFrom < 0) dataFrom = ~dataFrom - 1; final int[] indexlessPositions = Arrays.copyOfRange(positions, 0, dataFrom + 1); final int[] dataPositions = Arrays.copyOfRange(positions, dataFrom + 1, positions.length); for (int i = 0; i < dataPositions.length; ++i) dataPositions[i] -= indexlessData.length; Tensor[] newIndexless = ArraysUtils.remove(indexlessData, indexlessPositions); Tensor[] newData = ArraysUtils.remove(data, dataPositions); return createProduct(new IndicesBuilder().append(newData).getIndices(), newFactor, newIndexless, newData); }
public ParseTokenDerivative(TokenType tokenType, ParseToken... content) { super(tokenType, content); IndicesBuilder ib = new IndicesBuilder(); ib.append(content[0].getIndices().getFree()); for (int i = content.length - 1; i >= 1; --i) ib.append(content[i].getIndices().getInverted().getFree()); indices = ib.getIndices(); }
public Indices getIndices() { IndicesBuilder indices = new IndicesBuilder(); for (int i = 0; i < length; i++) indices.append(pc.get(gPositions.get(i))); return indices.getIndices(); }
return new Product(new IndicesBuilder().append(data).getIndices(), factor, indexLess.toArray(new Tensor[indexLess.size()]),
ibs.append(m); return new Product(ibs.getIndices(), factor, indexLess.toArray(new Tensor[indexLess.size()]),
/** * 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); }
@Override public Tensor toTensor() { SimpleTensor[] vars = new SimpleTensor[content.length - 1]; Tensor temp = content[0].toTensor(); TIntHashSet allowedDummies = TensorUtils.getAllIndicesNamesT(temp); IndicesBuilder free = new IndicesBuilder().append(temp.getIndices()); for (int i = 1; i < content.length; ++i) { temp = content[i].toTensor(); free.append(temp.getIndices().getInverted()); allowedDummies.addAll(IndicesUtils.getIndicesNames(temp.getIndices())); if (!(temp instanceof SimpleTensor) && !(temp instanceof TensorField)) throw new IllegalArgumentException("Derivative with respect to non simple argument: " + temp); vars[i - 1] = (SimpleTensor) temp; } allowedDummies.removeAll(IndicesUtils.getIndicesNames(free.getIndices().getFree())); Tensor result = new DifferentiateTransformation( vars, new Transformation[]{ExpandAndEliminateTransformation.EXPAND_AND_ELIMINATE} ).transform(content[0].toTensor()); result = ApplyIndexMapping.optimizeDummies(result); TIntHashSet generated = TensorUtils.getAllDummyIndicesT(result); generated.removeAll(allowedDummies); result = ApplyIndexMapping.renameDummy(result, generated.toArray(), allowedDummies.toArray()); return result; } }
} else { Tensor[] newData = ArraysUtils.remove(data, i - indexlessData.length); return new Product(new IndicesBuilder().append(newData).getIndices(), complex, indexlessData, newData);
Indices factorIndices = new IndicesBuilder().append(factors).getIndices(); TIntHashSet dummies = new TIntHashSet(IndicesUtils.getIntersections( factorIndices.getUpper().toArray(), factorIndices.getLower().toArray()));
Tensor[] newData = data.clone(); newData[i - indexlessData.length] = tensor; return new Product(new IndicesBuilder().append(newData).getIndices(), newFactor, indexlessData, newData);
return new Product(new IndicesBuilder().append(newData).getIndices(), product.factor, newIndexless, newData);