@Override public Indices getIndices() { return content[0].getIndices().getFree(); }
private static void checkConsistence(Tensor from, Tensor to) { if (!TensorUtils.isZeroOrIndeterminate(to)) if (!from.getIndices().getFree().equalsRegardlessOrder(to.getIndices().getFree())) throw new IllegalArgumentException("Tensor from free indices not equal to tensor to free indices: " + from.getIndices().getFree() + " " + to.getIndices().getFree()); }
private static Tensor[] expandSamples(Tensor[] samples) { //select diff samples: HashSet<Wrapper> set = new HashSet<>(); for (Tensor sample : samples) set.add(new Wrapper(sample)); ArrayList<Tensor> r = new ArrayList<>(); for (Wrapper st : set) { r.ensureCapacity(ArithmeticUtils.pow(2, st.tensor.getIndices().getFree().size())); r.addAll(Arrays.asList(TensorGeneratorUtils.allStatesCombinations(st.tensor))); } return r.toArray(new Tensor[r.size()]); }
private static void checkConsistent(Tensor tensor, final int[] from) { int[] freeIndices = tensor.getIndices().getFree().getAllIndices().copy(); Arrays.sort(freeIndices); if (!Arrays.equals(freeIndices, from)) throw new IllegalArgumentException("From indices are not equal to free indices of tensor."); }
public static int hashWithIndices(final Tensor tensor) { return hashWithIndices(tensor, tensor.getIndices().getFree()); }
@Override public Expression create(Tensor... tensors) { if (tensors.length != 2) throw new IllegalArgumentException("Wrong number of arguments."); if (tensors[0] == null || tensors[1] == null) throw new NullPointerException(); if (!TensorUtils.isZero(tensors[1]) && !TensorUtils.isIndeterminate(tensors[1]) && !tensors[0].getIndices().getFree().equalsRegardlessOrder(tensors[1].getIndices().getFree())) throw new TensorException(String.format("Inconsistent indices in expression: %s != %s", tensors[0].getIndices().getFree(), tensors[1].getIndices().getFree())); return new Expression(IndicesFactory.create(tensors[0].getIndices().getFree()), tensors[0], tensors[1]); } }
private static boolean isScalar1(Tensor tensor) { return tensor.getIndices().getFree().size() == 0; }
@Override public void put(Tensor tensor) { if (pointer == data.length) throw new IllegalStateException("No more arguments in field."); if (tensor == null) throw new NullPointerException(); if (!tensor.getIndices().getFree().equalsRegardlessOrder(field.getArgIndices(pointer))) if (TensorUtils.isZeroOrIndeterminate(tensor)) this.changedSignature = true; else throw new IllegalArgumentException( "Free indices of putted tensor " + tensor.getIndices().getFree() + " differs from field argument binding indices " + field.getArgIndices(pointer) + "!"); data[pointer++] = tensor; }
private Wrapper(Tensor tensor) { this.tensor = tensor; this.freeIndices = StructureOfIndices.create( IndicesFactory.createSimple(null, tensor.getIndices().getFree())); }
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(); }
private static int hc(Tensor t, int[] inds) { Indices ind = t.getIndices().getFree(); int h = 31; int ii; for (int i = ind.size() - 1; i >= 0; --i) { ii = IndicesUtils.getNameWithType(ind.get(i)); if ((ii = Arrays.binarySearch(inds, ii)) >= 0) h ^= HashFunctions.JenkinWang32shift(ii); } return h; }
public Tensor nextTensorTree(TensorType head, int depth, Parameters parameters, Indices indices) { if (head == null) nextTensorTree(depth, parameters, indices); indices = indices.getFree(); if (depth == 0) return nextSimpleTensor(IndicesFactory.createSimple(null, indices)); if (head == TensorType.Product) return nextProductTree(depth, parameters, indices); if (head == TensorType.Sum) return nextSumTree(depth, parameters, indices); throw new RuntimeException(); }
private static TIntHashSet getAllDummyIndicesT(boolean includeScalarFunctions, Tensor tensor) { TIntHashSet set = new TIntHashSet(); appendAllIndicesNamesT(tensor, set, includeScalarFunctions); set.removeAll(IndicesUtils.getIndicesNames(tensor.getIndices().getFree())); return set; }
/** * @deprecated very unsafe method without checks */ @Deprecated public static Tensor multiplySumElementsOnFactors(Sum sum) { final Tensor[] newSumData = new Tensor[sum.size()]; for (int i = newSumData.length - 1; i >= 0; --i) newSumData[i] = multiply(CC.generateNewSymbol(), sum.get(i)); return new Sum(newSumData, IndicesFactory.create(newSumData[0].getIndices().getFree())); }
private static Tensor applyIndexMapping(Tensor tensor, IndexMapper indexMapper) { if (tensor instanceof SimpleTensor) return applyIndexMapping(tensor, indexMapper, false); if (tensor instanceof Complex || tensor instanceof ScalarFunction) return tensor; return applyIndexMapping(tensor, indexMapper, indexMapper.contract(getIndicesNames(tensor.getIndices().getFree()))); }
@Override public NameAndStructureOfIndices getIndicesTypeStructureAndName() { StructureOfIndices[] typeStructures = new StructureOfIndices[1 + argumentsIndices.length]; typeStructures[0] = StructureOfIndices.create(indices); for (int i = 0; i < argumentsIndices.length; ++i) { if (argumentsIndices[i] == null) argumentsIndices[i] = IndicesFactory.createSimple(null, content[i].getIndices().getFree()); typeStructures[i + 1] = StructureOfIndices.create(argumentsIndices[i]); } return new NameAndStructureOfIndices(name, typeStructures); }
private static Tensor invertIndices(Tensor t) { Indices free = t.getIndices().getFree(); Mapping mapping = new Mapping(free.toArray(), free.getInverted().toArray()); return ApplyIndexMapping.applyIndexMapping(t, mapping); }
/** * Inverts indices of tensor * * @param tensor tensor * @return tensor with inverted indices */ public static Tensor invertIndices(Tensor tensor) { Indices indices = tensor.getIndices().getFree(); if (indices.size() == 0) return tensor; return applyIndexMapping(tensor, new Mapping(indices.toArray(), indices.getInverted().toArray())); } }
/** * Puts together similar scalar subproducts in a given product. For example, tensor A_m*A^m*A_n*A^n * will be transformed to tensor (A_m*A^m)**2. * * @param product product * @return the result */ public static Tensor collectScalarFactorsInProduct(Product product) { if (TensorUtils.isSymbolic(product)) return product; ScalarsBackedProductBuilder builder = new ScalarsBackedProductBuilder(product.size(), 1, product.getIndices().getFree().size()); builder.put(product); return builder.build(); } }