/** * Creates tester with for specified from and to indices. */ public IndexMappingBufferTester(FromToHolder holder) { this.from = holder.from; this.to = holder.to; this.signum = holder.sign; this.innerBuffer = new IndexMappingBufferImpl(); }
@Override public void addSign(boolean sign) { innerBuffer.addSign(sign); }
@Override public TIntObjectHashMap<IndexMappingBufferRecord> getMap() { return innerBuffer.getMap(); }
@Override public String toString() { return "inner: " + innerBuffer.toString(); } }
@Override public void removeContracted() { innerBuffer.removeContracted(); }
@Override public boolean isEmpty() { return innerBuffer.isEmpty(); }
@Override public IndexMappingBufferTester clone() { return new IndexMappingBufferTester(innerBuffer.clone(), from, to, signum); }
@Override public boolean tryMap(int from, int to) { int fromName = IndicesUtils.getNameWithType(from), toName = IndicesUtils.getNameWithType(to); int position; if ((position = Arrays.binarySearch(this.from, fromName)) < 0) return innerBuffer.tryMap(from, to); return this.to[position] == toName; }
/** * String representation in specified output format. * * @param format output format * @return string representation in specified output format */ public String toString(OutputFormat format) { StringBuilder sb = new StringBuilder(); sb.append(sign ? '-' : '+').append('{'); if (map.isEmpty()) return sb.append('}').toString(); String from, to; TIntObjectIterator<IndexMappingBufferRecord> iterator = map.iterator(); while (iterator.hasNext()) { iterator.advance(); if (iterator.value().isContracted()) { from = toStringIndex(iterator.key(), format).substring(1); to = toStringIndex(iterator.value().getIndexName(), format).substring(1); sb.append(','); } else { from = toStringIndex(setRawState(iterator.value().getFromRawState(), iterator.key()), format); to = toStringIndex(setRawState(iterator.value().getToRawState(), iterator.value().getIndexName()), format); } sb.append(from).append(" -> ").append(to).append(", "); } sb.deleteCharAt(sb.length() - 1).deleteCharAt(sb.length() - 1).append('}'); return sb.toString(); } // public String toStringWithStates() {
@Override public String toString() { return toString(CC.getDefaultOutputFormat()); }
/** * Resets tester */ public void reset() { innerBuffer = new IndexMappingBufferImpl(); }
/** * Creates output port of mappings from tensor <i>{@code from}</i> onto tensor <i>{@code to}</i>. * * @param from from tensor * @param to to tensor * @return output port of mappings */ public static MappingsPort createPort(Tensor from, Tensor to) { return new MappingsPort(createPortOfBuffers(new IndexMappingBufferImpl(), from, to)); }
/** * Creates output port of mappings of tensor {@code from} on tensor {@code to}. * * @param from from tensor * @param to to tensor * @return output port of mappings */ static OutputPort<IndexMappingBuffer> createPortOfBuffers(Tensor from, Tensor to) { return createPortOfBuffers(new IndexMappingBufferImpl(), from, to); }
/** * Creates tester to test identity mapping {@code from} -> {@code from} * * @param from array of from indices * @param sign sign */ public IndexMappingBufferTester(int[] from, boolean sign) { from = IndicesUtils.getIndicesNames(from); Arrays.sort(from); this.from = this.to = from; this.signum = sign; innerBuffer = new IndexMappingBufferImpl(); }
public IndexMappingBufferTester(Mapping mapping) { this.from = mapping.fromNames; this.to = IndicesUtils.getIndicesNames(mapping.toData); this.signum = mapping.getSign(); this.innerBuffer = new IndexMappingBufferImpl(); }
/** * Creates output port of mappings of two products of tensors represented as arrays of multipliers, where * each multiplier of {@code from} will be mapped on the multiplier of {@code to} at the same * position. Such ordering can be obtained via {@link cc.redberry.core.transformations.substitutions.ProductsBijectionsPort}. * In contrast to {@link #createPortOfBuffers(cc.redberry.core.tensor.Tensor, cc.redberry.core.tensor.Tensor)}, this method * will fully handles mappings of free indices on contracted ones (like e.g. _i^j -> _k^k). * * @param from from tensor * @param to to tensor * @return port of mappings of indices */ public static MappingsPort createBijectiveProductPort(Tensor[] from, Tensor[] to) { if (from.length != to.length) throw new IllegalArgumentException("From length != to length."); if (from.length == 0) return new MappingsPort(IndexMappingProvider.Util.singleton(new IndexMappingBufferImpl())); if (from.length == 1) return new MappingsPort(createPortOfBuffers(from[0], to[0])); return new MappingsPort( new MappingsPortRemovingContracted( new SimpleProductMappingsPort(IndexMappingProvider.Util.singleton(new IndexMappingBufferImpl()), from, to))); }
private static boolean mappingExists(Tensor from, Tensor to) { final IndexMappingProvider pp = IndexMappings.createPort( IndexMappingProvider.Util.singleton(new IndexMappingBufferImpl()), from, to); pp.tick(); return pp.take() != null; }
/** * Creates output port of mappings of two simple tensors and does not take into account the arguments of fields. * * @param from from tensor * @param to to tensor * @return port of mappings of indices */ public static MappingsPort simpleTensorsPort(SimpleTensor from, SimpleTensor to) { final IndexMappingProvider provider = ProviderSimpleTensor.FACTORY_SIMPLETENSOR.create(IndexMappingProvider.Util.singleton(new IndexMappingBufferImpl()), from, to); provider.tick(); return new MappingsPort(new MappingsPortRemovingContracted(provider)); }
@Override public IndexMappingBufferImpl clone() { TIntObjectHashMap<IndexMappingBufferRecord> newMap = new TIntObjectHashMap<>(map); TIntObjectIterator<IndexMappingBufferRecord> iterator = newMap.iterator(); while (iterator.hasNext()) { iterator.advance(); iterator.setValue(iterator.value().clone()); } return new IndexMappingBufferImpl(newMap, sign); }