Package cc.redberry.core.indexmapping

Examples of cc.redberry.core.indexmapping.IndexMappingBuffer


                    IntArrayList uncontractedIndicesToCollect = TensorUtils.getContractedIndicesNames(split.collectedTerm, split.factoredOut);
                    IntArrayList uncontractedIndicesCollected = TensorUtils.getContractedIndicesNames(collectedTerm.collectedItem, collectedTerm.collectedFactors.getElements().get(0));

                    //finding best mapping
                    //botleneck for very huge sums
                    IndexMappingBuffer concurentBuffer = null;
                    int concurrence = 0, currentConcurrence;
                    boolean sign = false;
                    for (IndexMappingBuffer buffer : buffers) {
                        currentConcurrence = 0;
                        for (Map.Entry<Integer, IndexMappingBufferRecord> entry : buffer.getMap().entrySet()) {
                            int indexToCollect = entry.getKey();
                            int indexCollected = entry.getValue().getIndexName();
                            if (indexToCollect == indexCollected || (uncontractedIndicesToCollect.contains(indexToCollect) && uncontractedIndicesCollected.contains(indexCollected)))
                                currentConcurrence++;
                        }
                        if (concurentBuffer == null
                                || currentConcurrence > concurrence
                                || (sign && !buffer.getSignum()
                                && (currentConcurrence >= concurrence))) {
                            concurentBuffer = buffer;
                            concurrence = currentConcurrence;
                        }
                    }

                    IndexMappingDirectAllowingUnmapped mappingToCollect = new IndexMappingDirectAllowingUnmapped();
                    IndexMappingDirectAllowingUnmapped mappingCollectedTensor = new IndexMappingDirectAllowingUnmapped();

                    //building index generator
                    IntArrayList usedIndices = new IntArrayList(TensorUtils.getAllIndicesNames(split.factoredOut));
                    usedIndices.addAll(collectedTerm.getCollectedFactorsIndicesNames());
                    usedIndices.addAll(collectedTerm.getCollectedTermIndicesNames());
                    IndexGenerator ig = new IndexGenerator(usedIndices.toArray());

                    List<Tensor> kroneckersCollected = new ArrayList<>();

                    for (Map.Entry<Integer, IndexMappingBufferRecord> entry : concurentBuffer.getMap().entrySet()) {
                        int indexCollected, indexToCollect, rawState = ((entry.getValue().getStates() & 1) ^ 1) << 31;
                        indexCollected = rawState | entry.getValue().getIndexName();
                        indexToCollect = rawState | entry.getKey();

                        SimpleTensor kroneckerCollected;
                        SimpleTensor kroneckerToCollect;
                       
                        if (indexToCollect == indexCollected)
                            continue;
                        else if (uncontractedIndicesToCollect.contains(getNameWithType(indexToCollect))
                                && uncontractedIndicesCollected.contains(getNameWithType(indexCollected))) {
                            mappingToCollect.add(inverseIndexState(indexToCollect),
                                    inverseIndexState(indexCollected));
//                            continue;
                        }

                        else if (uncontractedIndicesToCollect.contains(getNameWithType(indexToCollect))) {
                            kroneckerCollected = CC.createKronecker(inverseIndexState(indexToCollect), indexCollected);

                            collectedTerm.getCollectedFactorsIndicesNames().add(getNameWithType(indexCollected));
                            collectedTerm.getCollectedFactorsIndicesNames().add(getNameWithType(indexToCollect));

                            collectedTerm.getCollectedTermIndicesNames().replaceFirst(getNameWithType(indexCollected), getNameWithType(indexToCollect));
                            mappingCollectedTensor.add(indexCollected, indexToCollect);
                            kroneckersCollected.add(kroneckerCollected);
//                            continue;
                        } else if (uncontractedIndicesCollected.contains(getNameWithType(indexCollected))) {
                            kroneckerToCollect = CC.createKronecker(indexToCollect, inverseIndexState(indexCollected));
                            split.factoredOut.add(kroneckerToCollect);
//                            continue;
                        } else {
                            int newIndex = ig.generate(getType(indexToCollect));
                            collectedTerm.getCollectedFactorsIndicesNames().add(getNameWithType(indexCollected));
                            collectedTerm.getCollectedFactorsIndicesNames().add(newIndex);
                            collectedTerm.getCollectedTermIndicesNames().replaceFirst(getNameWithType(indexCollected), newIndex);
//                        uncontractedIndicesCollected.addAll(newIndex);
//                        uncontractedIndicesToCollect.addAll(newIndex);
//                        uncontractedIndicesCollected.addAll(getNameWithType(indexCollected));
//                        uncontractedIndicesToCollect.addAll(getNameWithType(indexToCollect));


                            newIndex = getRawStateInt(indexToCollect) | newIndex;

                            kroneckerCollected = CC.createKronecker(indexCollected, inverseIndexState(newIndex));
                            kroneckerToCollect = CC.createKronecker(indexToCollect, inverseIndexState(newIndex));
                            mappingCollectedTensor.add(indexCollected, newIndex);

                            kroneckersCollected.add(kroneckerCollected);
                            split.factoredOut.add(kroneckerToCollect);
                        }
                    }
                    if (!kroneckersCollected.isEmpty())
                        for (Tensor p : collectedTerm.collectedFactors)
                            ((Product) p).add(kroneckersCollected);
                    if (concurentBuffer.getSignum())
                        split.factoredOut.addFirst(TensorNumber.createMINUSONE());
                    if (!mappingToCollect.isEmpty())
                        ApplyIndexMappingDirectTransformation.INSTANCE.perform(split.factoredOut, mappingToCollect);
                    collectedTerm.collectedFactors.add(split.factoredOut);
                    collectedTerm.getCollectedFactorsIndicesNames().addAll(TensorUtils.getAllIndicesNames(split.factoredOut));
View Full Code Here


    private boolean compare(Tensor from, Tensor to) {
        int[] fromIndices = from.getIndices().getFreeIndices().getAllIndices().copy();
        for (int i = 0; i < fromIndices.length; ++i)
            fromIndices[i] = IndicesUtils.getNameWithType(fromIndices[i]);
        OutputPortUnsafe<IndexMappingBuffer> provider = IndexMappings.createPositivePort(new IndexMappingBufferTester(fromIndices, CC.withMetric()), from, to);
        IndexMappingBuffer buffer;
        if ((buffer = provider.take()) != null) {
            assert buffer.isEmpty();
            return true;
        }
        return false;
    }
View Full Code Here

    private boolean compare(Tensor from, Tensor to) {
        int[] fromIndices = from.getIndices().getFreeIndices().getAllIndices().copy();
        for (int i = 0; i < fromIndices.length; ++i)
            fromIndices[i] = IndicesUtils.getNameWithType(fromIndices[i]);
        OutputPortUnsafe<IndexMappingBuffer> provider = IndexMappings.createPort(new IndexMappingBufferTester(fromIndices, CC.withMetric()), from, to);
        IndexMappingBuffer buffer;
        if ((buffer = provider.take()) != null) {
            buffer.removeContracted();
            assert buffer.isEmpty();
            return buffer.getSignum() == true;
        }
        return false;
    }
View Full Code Here

            Tensor riman2 = Tensors.parse(e1);

            long start = System.currentTimeMillis();
            for (int i = 0; i < 1000; ++i) {
                MappingsPort mp = IndexMappings.createPort(riman1, riman2);
                IndexMappingBuffer buffera;
                while ((buffera = mp.take()) != null) ;
            }
            time += System.currentTimeMillis() - start;
        }
        return time;
View Full Code Here

        result[result.length - 1] = factor;
        return Tensors.multiply(result);
    }

    SubsResult atomicSubstitute(PContent content, TIntHashSet forbidden) {
        IndexMappingBuffer buffer = null;
        int[] indexlessBijection, dataBijection;

        IndexlessBijectionsPort indexlessPort
                = new IndexlessBijectionsPort(fromIndexless, content.indexless);

        while ((indexlessBijection = indexlessPort.take()) != null) {
            buffer = createBijectiveProductPort(fromIndexless, extract(content.indexless, indexlessBijection)).take();
            if (buffer != null)
                break;
        }

        if (buffer == null)
            return null;

        boolean sign = buffer.getSign();
        buffer = null;

        Tensor[] currentData;
        if (content.data instanceof Product) {
            ProductContent currentContent = ((Product) content.data).getContent();
            currentData = currentContent.getDataCopy();
            ProductsBijectionsPort dataPort = new ProductsBijectionsPort(fromContent, currentContent);
            while ((dataBijection = dataPort.take()) != null) {
                buffer = createBijectiveProductPort(fromData, extract(currentData, dataBijection)).take();
                if (buffer != null)
                    break;
            }
        } else {
            if (TensorUtils.isOne(content.data)) {
                if (fromContent.size() != 0)
                    return null;
                dataBijection = new int[0];
                currentData = new Tensor[0];
                buffer = new IndexMappingBufferImpl();
            } else {
                if (fromContent.size() != 1)
                    return null;
                dataBijection = new int[1];
                currentData = new Tensor[]{content.data};
                buffer = getFirst(fromContent.get(0), content.data);
            }
        }

        if (buffer == null)
            return null;

        buffer.addSign(sign);
        Arrays.sort(indexlessBijection);
        Arrays.sort(dataBijection);

        Tensor[] indexlessRemainder = new Tensor[content.indexless.length - fromIndexless.length];
        ProductBuilder dataRemainder = new ProductBuilder(0,
                (content.data instanceof Product)
                        ? content.data.size()
                        : 1 - fromContent.size());
        int pivot = 0;
        int i, j = 0;
        for (i = 0; i < content.indexless.length; ++i) {
            if (pivot < indexlessBijection.length && i == indexlessBijection[pivot])
                ++pivot;
            else
                indexlessRemainder[j++] = content.indexless[i];
        }
        pivot = 0;
        for (i = 0; i < currentData.length; ++i) {
            if (pivot < dataBijection.length && i == dataBijection[pivot])
                ++pivot;
            else
                dataRemainder.put(currentData[i]);
        }
        Tensor dataRemainderT = dataRemainder.build();

        PContent remainder = new PContent(indexlessRemainder, dataRemainderT);

        Tensor newTo;
        if (toIsSymbolic)
            newTo = buffer.getSign() ? Tensors.negate(to) : to;
        else {
            TIntHashSet remainderIndices = new TIntHashSet(forbidden);
            remainderIndices.addAll(getAllIndicesNamesT(indexlessRemainder));
            remainderIndices.addAll(getAllIndicesNamesT(dataRemainderT));
            newTo = applyIndexMapping(to, buffer, remainderIndices.toArray());
View Full Code Here

        super(from, to);
    }

    @Override
    Tensor newTo_(Tensor currentNode, SubstitutionIterator iterator) {
        IndexMappingBuffer buffer =
                IndexMappings.getFirst(from, currentNode);
        if (buffer == null)
            return currentNode;
        Tensor newTo;
        if (toIsSymbolic)
            newTo = buffer.getSign() ? Tensors.negate(to) : to;
        else
            newTo = ApplyIndexMapping.applyIndexMapping(to, buffer, iterator.getForbidden());

        return newTo;
    }
View Full Code Here

    @Override
    public BijectionContainer take() {
        if (finished)
            return null;
        List<int[]> bijections = new ArrayList<>();
        IndexMappingBuffer buffer;
        int i, b[];
        final int mappersSize = mappers.size();
        OUT:
        while (true) {
            buffer = source.take();
View Full Code Here

            return mapping;
        }

        @Override
        public IndexMappingBuffer take() {
            IndexMappingBuffer buf;
            while (true) {
                if (currentSource != null && (buf = currentSource.take()) != null)
                    return buf;
                if (!permutationGenerator.hasNext())
                    return null;
View Full Code Here

    Tensor newTo_(Tensor currentNode, SubstitutionIterator iterator) {
        BijectionContainer bc = new SumBijectionPort(from, currentNode).take();
        if (bc == null)
            return currentNode;

        IndexMappingBuffer buffer = bc.buffer;
        Tensor newTo;
        if (toIsSymbolic)
            newTo = buffer.getSign() ? Tensors.negate(to) : to;
        else
            newTo = ApplyIndexMapping.applyIndexMapping(to, buffer, iterator.getForbidden());

        SumBuilder builder = new SumBuilder();
        int[] bijection = bc.bijection;
View Full Code Here

    @Override
    Tensor newTo_(Tensor currentNode, SubstitutionIterator iterator) {
        TensorField currentField = (TensorField) currentNode;
        TensorField from = (TensorField) this.from;
        IndexMappingBuffer buffer = IndexMappings.simpleTensorsPort(from, currentField).take();
        if (buffer == null)
            return currentNode;

        Indices[] fromIndices = from.getArgIndices(),
                currentIndices = currentField.getArgIndices();

        List<Tensor> argFrom = new ArrayList<>(), argTo = new ArrayList<>();
        Tensor fArg;
        int[] cIndices, fIndices;
        int i;
        for (i = from.size() - 1; i >= 0; --i) {
            if (IndexMappings.positiveMappingExists(currentNode.get(i), from.get(i)))
                continue;

            fIndices = fromIndices[i].getAllIndices().copy();
            cIndices = currentIndices[i].getAllIndices().copy();

            assert cIndices.length == fIndices.length;

            fArg = ApplyIndexMapping.applyIndexMapping(from.get(i), fIndices, cIndices, new int[0]);

            argFrom.add(fArg);
            argTo.add(currentNode.get(i));
        }

        Tensor newTo = to;
        newTo = new SubstitutionTransformation(
                argFrom.toArray(new Tensor[argFrom.size()]),
                argTo.toArray(new Tensor[argTo.size()]),
                false).transform(newTo);
        if (!TensorUtils.isSymbolic(newTo))
            newTo = ApplyIndexMapping.applyIndexMapping(newTo, buffer, iterator.getForbidden());
        else if (buffer.getSign())
            newTo = Tensors.negate(newTo);
        return newTo;
    }
View Full Code Here

TOP

Related Classes of cc.redberry.core.indexmapping.IndexMappingBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.