/** * Creates iterator over tensor elements. * * @param tensor tensor */ public BasicTensorIterator(Tensor tensor) { this.tensor = tensor; this.size = tensor.size(); }
@SuppressWarnings("unchecked") public SumPort(Tensor tensor, boolean expandSymbolic) { this.tensor = tensor; this.expandSymbolic = expandSymbolic; this.ports = new OutputPort[tensor.size()]; reset(); }
@Override public Tensor take() { Tensor t = null; while (pointer < tensor.size()) { t = ports[pointer].take(); if (t == null) ++pointer; else return t; } return t; } }
public Tensor[] toArray() { return getRange(0, size()); }
/** * Retrieves several sub-tensors from current tensor. This function is * faster than sequential invocations of {@link #get(int)} method. * * @param from index of first sub-tensor to be retrieved (inclusive) * @param to next index after last sub-tensor to be retrieved (exclusive) * @return array with retrieved tensors */ public Tensor[] getRange(int from, final int to) { int size = size(); if (from < 0 || from > to || to > size) throw new IndexOutOfBoundsException(); Tensor[] range = new Tensor[to - from]; for (size = 0; from < to; ++size, ++from) range[size] = get(from); return range; }
@Override public IndexMappingProvider create(IndexMappingProvider opu, Tensor from, Tensor to) { if (from.size() != to.size()) return IndexMappingProvider.Util.EMPTY_PROVIDER; for (int i = 0; i < from.size(); ++i) if (from.get(i).hashCode() != to.get(i).hashCode()) return IndexMappingProvider.Util.EMPTY_PROVIDER; return new ProviderSum(opu, from, to); } };
@Override public void reset() { pointer = 0; for (int i = tensor.size() - 1; i >= 0; --i) ports[i] = createPort(tensor.get(i), expandSymbolic); }
/** * Consequently returns terms of the resulting expanded expression, * calculating the next term on invocation. * * @return next term in the resulting expanded expression */ @Override public Tensor take() { if (index >= sum1.size() * sum2.size()) return null; int i1 = (int) (index / sum2.size()); int i2 = (int) (index % sum2.size()); ++index; if (factors.length == 0) return Tensors.multiply(sum1.get(i1), sum2.get(i2)); else return Tensors.multiply(ArraysUtils.addAll(factors, sum1.get(i1), sum2.get(i2))); } }
private static List<Tensor> expandPower(Tensor power) { List<Tensor> powers = new ArrayList<>(power.get(1).size()); for (Tensor exponent : power.get(1)) powers.add(Tensors.pow(power.get(0), exponent)); return powers; }
private static Monomial getFromProduct(Tensor t, SimpleTensor[] coefficients) { if (!(t instanceof Product)) return null; for (int j = 0; j < t.size(); j++) { if (!(t.get(j) instanceof SimpleTensor)) continue; for (int i = 0; i < coefficients.length; i++) if (t.get(j).equals(coefficients[i])) return new Monomial(((Product) t).remove(j), i); } return null; }
private static Term tensor2term(Tensor tensor) { if (tensor instanceof Product) { FactorNode[] factors = new FactorNode[tensor.size()]; int i = -1; for (Tensor t : tensor) factors[++i] = createNode(t); return new Term(factors); } return new Term(new FactorNode[]{createNode(tensor)}); }
public PowerPort(Tensor tensor, int[] initialForbidden, boolean expandSymbolic) { this.expandSymbolic = expandSymbolic; base = tensor.get(0); power = ((Complex) tensor.get(1)).getReal().intValue(); int[] upperBounds = new int[power]; Arrays.fill(upperBounds, base.size()); tuplesPort = new IntTuplesPort(upperBounds); this.initialForbidden = initialForbidden; currentPort = nextPort(); }
private static boolean matchSimpleTensors(Tensor a, Tensor b) { if (a.getClass() != b.getClass()) return false; if (a.hashCode() != b.hashCode()) return false; if (TensorUtils.isPositiveIntegerPowerOfSimpleTensor(a)) return TensorUtils.isPositiveIntegerPowerOfSimpleTensor(b) && a.get(1).equals(b.get(1)) && matchSimpleTensors(a.get(0), b.get(0)); if (a instanceof TensorField) for (int i = a.size() - 1; i >= 0; --i) if (!IndexMappings.positiveMappingExists(a.get(i), b.get(i))) return false; return true; }
@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; }
@Override public IndexMappingProvider create(IndexMappingProvider opu, Tensor from, Tensor to) { if (((TensorField) from).getName() != ((TensorField) to).getName()) return IndexMappingProvider.Util.EMPTY_PROVIDER; for (int i = 0; i < from.size(); ++i) { if (!IndexMappings.positiveMappingExists(from.get(i), to.get(i))) return IndexMappingProvider.Util.EMPTY_PROVIDER; } return new ProviderSimpleTensor(opu, (SimpleTensor) from, (SimpleTensor) to); } };
public static Tensor collectNonScalars(Tensor t) { FromChildToParentIterator iterator = new FromChildToParentIterator(t); Tensor c; while ((c = iterator.next()) != null) if (c instanceof Sum) { //TODO add check whether we need to do this transformation SumBuilderSplitingScalars sbss = new SumBuilderSplitingScalars(c.size()); for (Tensor tt : c) sbss.put(tt); iterator.set(sbss.build()); } return iterator.result(); } }
private static Tensor[] reIm(Tensor sum) { IntArrayList im = new IntArrayList(sum.size()); for (int i = sum.size() - 1; i >= 0; --i) { if (sum.get(i) instanceof Complex && !((Complex) sum.get(i)).getImaginary().isZero()) im.add(i); else if (sum.get(i) instanceof Product && !((Product) sum.get(i)).getFactor().getImaginary().isZero()) im.add(i); } Tensor[] parts = new Tensor[2]; int[] positions = im.toArray(); parts[0] = ((Sum) sum).select(positions); parts[1] = ((Sum) sum).remove(positions); return parts; }
private static void appendAllIndicesT(Tensor tensor, TIntHashSet set) { if (tensor instanceof SimpleTensor) { Indices ind = tensor.getIndices(); final int size = ind.size(); for (int i = 0; i < size; ++i) set.add(ind.get(i)); } else if (tensor instanceof Power) { appendAllIndicesT(tensor.get(0), set); } else { Tensor t; for (int i = tensor.size() - 1; i >= 0; --i) { t = tensor.get(i); if (t instanceof ScalarFunction) continue; appendAllIndicesT(t, set); } } }
private static boolean isSymbolicPoly(Tensor t) { if (t.getIndices().size() != 0 || t instanceof ScalarFunction) return false; if (t instanceof SimpleTensor) return t.size() == 0;//not a field if (t instanceof Power) { if (!isSymbolicPoly(t.get(0))) return false; if (!TensorUtils.isInteger(t.get(1))) return false; Complex e = (Complex) t.get(1); return e.isReal() && !e.isNumeric(); } for (Tensor tt : t) if (!isSymbolicPoly(tt)) return false; return true; }
private static void appendAllIndicesNamesT(Tensor tensor, TIntHashSet set, boolean includeScalarFunctions) { if (tensor instanceof SimpleTensor) { Indices ind = tensor.getIndices(); set.ensureCapacity(ind.size()); final int size = ind.size(); for (int i = 0; i < size; ++i) set.add(IndicesUtils.getNameWithType(ind.get(i))); } else if (tensor instanceof Power) { appendAllIndicesNamesT(tensor.get(0), set); } else if (tensor instanceof ScalarFunction && !includeScalarFunctions) return; else { Tensor t; for (int i = tensor.size() - 1; i >= 0; --i) { t = tensor.get(i); appendAllIndicesNamesT(t, set); } } }