Tensor build() { return builder.build(); }
@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(); } }
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(); }
/** * Returns new tensor instance with i-th sub-tensor replaced by provided * tensor. * * @param i index of sub-tensor to be replaced * @param tensor tensor to replace i-th sub-tensor * @return new instance of tensor */ public Tensor set(int i, Tensor tensor) { int size = size(); if (i >= size || i < 0) throw new IndexOutOfBoundsException(); if (tensor == null) throw new NullPointerException(); TensorBuilder builder = getBuilder(); for (int j = 0; j < size; ++j) if (j == i) builder.put(tensor); else builder.put(get(j)); return builder.build(); }
/** * 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(); }
/** * Applies transformation to all first descendants (but not to the tensor itself) * * @param tensor * @param transformation * @return result */ public static Tensor applyToEachChild(Tensor tensor, final Transformation transformation) { TensorBuilder builder = null; Tensor c, oc; for (int i = 0, size = tensor.size(); i < size; ++i) { c = transformation.transform(oc = tensor.get(i)); if (builder != null || c != oc) { if (builder == null) { builder = tensor.getBuilder(); for (int j = 0; j < i; ++j) builder.put(tensor.get(j)); } builder.put(c); } } if (builder == null) return tensor; return builder.build(); }
return rebuild.build();
return pb.build();
pb.put(c.get(i)); iterator.set(pb == null ? c : pb.build()); } else iterator.set(c); } else