@Override public TensorBuilder getBuilder() { return new SumBuilder(data.length); }
PowerNode(Tensor base, Tensor exponent) { this.base = base; if (exponent == Complex.ONE) this.exponent = null; else { this.exponent = new SumBuilder(); this.exponent.put(exponent); } }
/** * Returns a kind of sum builder. * * @return a kind of sum builder */ public TensorBuilder getBuilder() { TensorBuilder builder = new SumBuilder(); builder.put(summand); return builder; }
void putExponent(Tensor exp) { if (exponent == null) { exponent = new SumBuilder(); exponent.put(Complex.ONE); } exponent.put(exp); }
@Override public Tensor create(Tensor... tensors) { if (tensors.length == 0) return Complex.ZERO; if (tensors.length == 1) return tensors[0]; TensorBuilder builder = new SumBuilder(tensors.length); for (Tensor t : tensors) builder.put(t); return builder.build(); } }
@Override public TensorBuilder clone() { TIntObjectHashMap<List<FactorNode>> summands = new TIntObjectHashMap<>(this.summands); for (List<FactorNode> vals : summands.valueCollection()) for (int i = vals.size() - 1; i >= 0; --i) vals.set(i, vals.get(i).clone()); return new SumBuilder(summands, complex, indices, sortedFreeIndices.clone()); } }
private static Tensor average(final int[] indices) { if (indices.length == 0) return Complex.ONE; if (indices.length == 2) return Tensors.createMetricOrKronecker(indices[0], indices[1]); SumBuilder sb = new SumBuilder(); for (int i = 1; i < indices.length; ++i) { int[] suffix = new int[indices.length - 2]; System.arraycopy(indices, 1, suffix, 0, i - 1); System.arraycopy(indices, i + 1, suffix, i - 1, indices.length - i - 1); sb.put(Tensors.multiply(Tensors.createMetricOrKronecker(indices[0], indices[i]), average(suffix))); } return sb.build(); }
public Tensor nextSumTree(int depth, Parameters parameters, Indices indices) { int sumSize = getRandomValue(parameters.minSumSize, parameters.maxSumSize); TensorBuilder sum = new SumBuilder(); for (int i = 0; i < sumSize; ++i) sum.put(nextTensorTree(TensorType.Product, depth - 1, parameters, indices)); return sum.build(); }
public static Tensor expandUsingPort(Tensor t, boolean expandSymbolic) { OutputPort<Tensor> port = createPort(t, expandSymbolic); if (port instanceof OutputPort.Singleton) return port.take(); SumBuilder sb = new SumBuilder(); Tensor n; while ((n = port.take()) != null) sb.put(n); return sb.build(); }
private static Tensor multiplyWithBuilder(Sum sum, Tensor factor, Transformation... transformations) { SumBuilder sb = new SumBuilder(sum.size()); for (Tensor t : sum) sb.put(Transformation.Util.applySequentially(multiply(t, factor), transformations)); return sb.build(); }
@Override public Tensor transform(Tensor t) { //expand applies only on the top level if (t instanceof Product || t instanceof Power) return expandProduct(t); if (t instanceof Sum) { SumBuilder sb = new SumBuilder(t.size()); for (Tensor s : t) sb.put(transform(s)); return sb.build(); } return t; }
private static Tensor det1(Tensor[][] matrix) { if (matrix.length == 1) return matrix[0][0]; SumBuilder sum = new SumBuilder(); Tensor temp; for (int i = 0; i < matrix.length; ++i) { temp = multiplyAndRenameConflictingDummies(matrix[0][i], det(deleteFromMatrix(matrix, 0, i))); if (i % 2 == 1) temp = negate(temp); sum.put(temp); } return sum.build(); }
@Override Tensor newTo_(Tensor current, SubstitutionIterator iterator) { Tensor old = null; while (old != current) { old = current; SumBijectionPort.BijectionContainer bc = new SumBijectionPort(from, current).take(); if (bc == null) return current; Mapping mapping = bc.mapping; Tensor newTo = applyIndexMappingToTo(current, to, mapping, iterator); SumBuilder builder = new SumBuilder(); int[] bijection = bc.bijection; builder.put(newTo); Arrays.sort(bijection); int pivot = 0; for (int i = 0, size = current.size(); i < size; ++i) { if (pivot >= bijection.length || i != bijection[pivot]) builder.put(current.get(i)); else ++pivot; } current = builder.build(); } return current; } }
/** * Expands out the product of two sums. * * @param s1 first sum * @param s2 second sum * @param transformations additional transformations to be * consequently applied on each term * in the resulting expression. * @param factors if specified, then each resulting term will be multiplied on this factor * @return the resulting expanded tensor */ public static Tensor expandPairOfSums(Sum s1, Sum s2, Tensor[] factors, Transformation[] transformations) { ExpandPairPort epp = new ExpandPairPort(s1, s2, factors); TensorBuilder sum = new SumBuilder(s1.size() * s2.size()); Tensor t; while ((t = epp.take()) != null) sum.put(apply(transformations, t)); return sum.build(); }
public static Tensor multiplySumElementsOnFactorAndExpand(Sum sum, Tensor factor, Transformation[] transformations) { if (TensorUtils.isZero(factor)) return Complex.ZERO; if (TensorUtils.isOne(factor)) return sum; if (factor instanceof Sum && factor.getIndices().size() != 0) throw new IllegalArgumentException(); if (TensorUtils.haveIndicesIntersections(sum, factor)) { SumBuilder sb = new SumBuilder(sum.size()); for (Tensor t : sum) sb.put(apply(transformations, ExpandUtils.expandIndexlessSubproduct.transform(multiply(t, factor)))); return sb.build(); } else return apply(transformations, FastTensors.multiplySumElementsOnFactorAndExpand(sum, factor)); }
public static Tensor multiplySumElementsOnFactor(Sum sum, Tensor factor, Transformation[] transformations) { if (TensorUtils.isZero(factor)) return Complex.ZERO; if (TensorUtils.isOne(factor)) return sum; if (TensorUtils.haveIndicesIntersections(sum, factor)) { SumBuilder sb = new SumBuilder(sum.size()); for (Tensor t : sum) sb.put(apply(transformations, multiply(t, factor))); return sb.build(); } else return apply(transformations, FastTensors.multiplySumElementsOnFactor(sum, factor)); }
private Tensor traceOfArray(Tensor[] data) { //calculates trace using recursive algorithm if (data.length == 1) return Complex.ZERO; if (data.length == 2) return multiply(traceOfOne.get(1), createMetricOrKronecker(data[0].getIndices().get(metricType, 0), data[1].getIndices().get(metricType, 0))); if (data.length % 2 != 0) return Complex.ZERO; SumBuilder sb = new SumBuilder(); Tensor temp; for (int i = 0; i < data.length - 1; ++i) { temp = multiply(Complex.TWO, createMetricOrKronecker(data[i].getIndices().get(metricType, 0), data[i + 1].getIndices().get(metricType, 0)), traceOfArray(subArray(data, i, i + 1))); if (i % 2 != 0) temp = negate(temp); sb.put(temp); swap(data, i, i + 1); } return multiply(Complex.ONE_HALF, sb.build()); }
private Tensor factorSymbolicTerms(Tensor tensor) { 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); if (remainder instanceof Sum) { SumBuilder sb = new SumBuilder(remainder.size()); for (Tensor tt : remainder) sb.put(factorSymbolicTerms(tt)); remainder = sb.build(); } else remainder = factorSymbolicTerms(remainder); iterator.set(Tensors.sum(symbolicPart, remainder)); } return iterator.result(); }
private Tensor orderArray0(Tensor[] gammas) { SumBuilder sb = new SumBuilder(); int swaps = 0; for (int i = 0; i < gammas.length - 1; i++)
return negate(toLeft0(gammas, index - 1)); } else { SumBuilder sb = new SumBuilder();