Tensor build() { if (exponent == null) return base; return Tensors.pow(base, exponent.build()); }
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 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; } }
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)); }
return sb.build();
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(); }
ordered = negate(ordered); sb.put(ordered); return sb.build();
return sb.build();
return sb.build();
return sb.build();