Package cc.redberry.core.utils

Examples of cc.redberry.core.utils.IntArrayList


         * S1:
         * (a+b)*c + a*d + b*d -> (a+b)*c + (a+b)*d
         */
        Tensor temp = tensor;
        int i, j = temp.size();
        IntArrayList nonProductOfSumsPositions = new IntArrayList();
        for (i = 0; i < j; ++i)
            if (!isProductOfSums(temp.get(i)))
                nonProductOfSumsPositions.add(i);
//        if (nonProductOfSumsPositions.size() == tensor.size())
//            //when tensor = a*d + b*d + ... (no sums in products)
//            return JasFactor.factor(tensor);

         /*
         * S2:
         * finding product of sums in tensor with minimal number of multipliers
         * we call this term pivot
         */
        final Term[] terms;
        Int pivotPosition = new Int();
        if (nonProductOfSumsPositions.isEmpty() || nonProductOfSumsPositions.size() == temp.size()) {
            //if nonProductOfSumsPositions.isEmpty(), then tensor already of form (a+b)*c + (a+b)*d
            //or if no any sum in terms, then tensor has form a*c + b*c
            terms = sum2SplitArray((Sum) temp, pivotPosition);
        } else {//if no, we need to rebuild tensor
            SumBuilder sb = new SumBuilder();
            for (i = nonProductOfSumsPositions.size() - 1; i >= 0; --i) {
                assert temp instanceof Sum;
                sb.put(temp.get(nonProductOfSumsPositions.get(i)));
                temp = ((Sum) temp).remove(nonProductOfSumsPositions.get(i));
            }
            Tensor withoutSumsTerm = factor(sb.build());
            if (isProductOfSums(withoutSumsTerm)) {
                temp = Tensors.sum(temp, withoutSumsTerm);
                if (!(temp instanceof Sum))
View Full Code Here


        if (freeIndices1.length > freeIndices2.length) {
            int[] temp = freeIndices1;
            freeIndices1 = freeIndices2;
            freeIndices2 = temp;
        }
        IntArrayList contracted = new IntArrayList();
        for (int i = 0; i < freeIndices1.length; ++i)
            for (int j = 0; j < freeIndices2.length; ++j)
                if (freeIndices2[j] == inverseIndexState(freeIndices1[i]))
                    contracted.add(getNameWithType(freeIndices2[j]));
        return contracted.toArray();
    }
View Full Code Here

            Indices factorIndices = new IndicesBuilder().append(factors).getIndices();
            TIntHashSet dummies = new TIntHashSet(IndicesUtils.getIntersections(
                    factorIndices.getUpper().copy(), factorIndices.getLower().copy()));
            SimpleIndices currentFactorIndices;
            IntArrayList from = new IntArrayList(), to = new IntArrayList();
            ArrayList<Tensor> kroneckers = new ArrayList<>();
            int j, index, newIndex;
            IndexGenerator generator = new IndexGenerator(TensorUtils.getAllIndicesNamesT(tensor).toArray());
            for (int i = 0; i < factors.length; ++i) {
                from.clear();
                to.clear();
                currentFactorIndices = factors[i].getIndices();

                for (j = currentFactorIndices.size() - 1; j >= 0; --j) {
                    index = currentFactorIndices.get(j);
                    if (freeIndices.contains(getNameWithType(index))) {
                        newIndex = setRawState(getRawStateInt(index), generator.generate(getType(index)));
                        from.add(index);
                        to.add(newIndex);
                        kroneckers.add(Tensors.createKronecker(index, inverseIndexState(newIndex)));
                    } else if (IndicesUtils.getState(index) && dummies.contains(getNameWithType(index))) {
                        newIndex = setRawState(getRawStateInt(index), generator.generate(getType(index)));
                        from.add(index);
                        to.add(newIndex);
                        kroneckers.add(Tensors.createKronecker(index, inverseIndexState(newIndex)));
                    }
                }

                factors[i] = applyDirectMapping(factors[i],
                        new StateSensitiveMapping(from.toArray(), to.toArray()));
            }

            //temp check
//            factorIndices = new IndicesBuilder().append(factors).getIndices();
//            assert factorIndices.size() == factorIndices.getFree().size();
View Full Code Here

     *
     * @return {@inheritDoc}
     */
    @Override
    public SortedIndices getFreeIndices() {
        IntArrayList list = new IntArrayList();
        int u, l;
        int iLower = firstLower, iUpper = 0;
        for (; iUpper < firstLower && iLower < size; ++iLower, ++iUpper) {
            u = data[iUpper] & 0x7FFFFFFF; //taking name with type
            l = data[iLower];
            if (u < l) {
                list.add(data[iUpper]);
                --iLower;
            } else if (l < u) {
                list.add(l);
                --iUpper;
            }
        }
        list.add(data, iUpper, firstLower - iUpper);
        list.add(data, iLower, size - iLower);
        return new SortedIndices(list.toArray());
    }
View Full Code Here

     */
    @Override
    public IndicesBuilderAbstract append(IndicesBuilder ib) {
        if (!(ib instanceof IndicesBuilderSorted))
            append(ib.getIndices());
        IntArrayList newData = ((IndicesBuilderAbstract) ib).data;
        if (newData.size() == 0)
            return this;
        data.addAll(newData);
        changedFlag = true;
        return this;
    }
View Full Code Here

            //Very similar to Product case
            Derivative derivative = (Derivative) tensor;

            //Collecting all indices from derivative vars
            TensorIterator it = derivative.iterator();
            IntArrayList usedIndices = new IntArrayList();
            Tensor current;
            while (it.hasNext()) {
                current = it.next();
                if (Derivative.onVarsIndicator.is(it))
                    usedIndices.addAll(TensorUtils.getAllIndicesNames(current));
            }

            //Creating int set from collected indices
            int[] usedIndicesArray = MathUtils.getSortedDistinct(usedIndices.toArray());

            Tensor target = derivative.getTarget();

            //Calculating used indices for AIM transformation
            int[] targetIndices = IndicesUtils.getSortedDistinctIndicesNames(target.getIndices().getFreeIndices());
            int[] currentUsedIndices = MathUtils.intSetDifference(targetIndices, usedIndices.toArray());
            //Applying AIM transformation
            IndexMappingImpl indexMapping = new IndexMappingImpl(currentUsedIndices);
            ApplyIndexMappingTransformation.INSTANCE.perform(target, indexMapping);
        }
        return tensor;
View Full Code Here

        UncontractIndices uncontractIndicesTransformation =
                new UncontractIndices(TensorUtils.getAllIndicesNames(factoredOut));
        term = uncontractIndicesTransformation.renameIndicesAndBuidKroneckers(term);
        factoredOut.add(uncontractIndicesTransformation.getKroneckers());
        factorsIndicesNames = new IntArrayBuffer(TensorUtils.getAllIndicesNames(factoredOut));
        termIndicesNames = new IntArrayList(TensorUtils.getAllIndicesNames(term));
        //TODO investigate cloning
        //factors.addAll(factoredOut.clone());
        factors.add(factoredOut);
        initialized = true;
    }
View Full Code Here

        Tensor toCollect = split.term;
        Tensor collectedTensor = term;


        IntArrayList uncontractedIndicesToCollect = TensorUtils.getContractedIndicesNames(split.term, split.factoredOut);
        IntArrayList uncontractedIndicesCollected = TensorUtils.getContractedIndicesNames(term, factors.getElements().get(0));

        IndexMappingBuffer concurentBuffer = null;
        if (factors.size() > 30)//TODO refacor in future
            concurentBuffer = IndexMappings.getFirst(toCollect, collectedTensor, allowDiffStates);
        else {
            List<IndexMappingBuffer> buffers = IndexMappingUtils.createAllMappings(toCollect, collectedTensor, allowDiffStates);
            //finding best mapping
            //botleneck for very huge sums
            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 (entry.getValue().diffStatesInitialized())
                        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(factorsIndicesNames);
        usedIndices.addAll(termIndicesNames);
        IndexGenerator ig = new IndexGenerator(usedIndices.toArray());

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

        for (Map.Entry<Integer, IndexMappingBufferRecord> entry : concurentBuffer.getMap().entrySet()) {
View Full Code Here

            super.add(num);
        }

        @Override
        public void addAll(int[] arr) {
            IntArrayList temp = new IntArrayList();
            for (int i : arr)
                if (contains(i))
                    continue;
                else
                    temp.add(i);
            super.addAll(temp.toArray());
        }
View Full Code Here

                    closed = true;
                    return null;
                }

                int[] bijectionNew = bijection.clone();
                IntArrayList addedBijectionsNew = addedBijections.clone();

                int j, fromTensorIndex, targetTensorIndex;
                long fromContraction, targetContraction;
                PermutationInfo currentInfo = firstInfo;
                do
                    for (j = 0; j < currentInfo.fromContractions.length; ++j) {
                        //Vse tut cherez jopu mi napisali, no vrode pravil'no
                        fromContraction = currentInfo.fromContractions[j];
                        targetContraction = currentInfo.targetContractions[currentInfo.permutation[j]];

                        assert getFromIndexId(fromContraction) == getFromIndexId(targetContraction);

                        fromTensorIndex = getToTensorIndex(fromContraction);
                        targetTensorIndex = getToTensorIndex(targetContraction);

                        if (getToIndexId(fromContraction) != getToIndexId(targetContraction)) {
                            if (!currentInfo.nextAndResetRightChain()) {
                                closed = true;
                                return null;
                            }
                            continue MAIN;
                        }

                        assert fromTensorIndex != -1;

                        if (targetTensorIndex == -1) { //Not contracted index of target (but from is contracted with some tensor)
                            if (!currentInfo.nextAndResetRightChain()) {
                                closed = true;
                                return null;
                            }
                            continue MAIN;
                        }

                        if (!weakMatch(fromData[fromTensorIndex], targetData[targetTensorIndex])) {
                            if (!currentInfo.nextAndResetRightChain()) {
                                closed = true;
                                return null;
                            }
                            continue MAIN;
                        }

                        if (bijectionNew[fromTensorIndex] == -1) {
                            //Try addAll new bijection
                            if (alreadyContains(bijectionNew, targetTensorIndex)) {
                                if (!currentInfo.nextAndResetRightChain()) {
                                    closed = true;
                                    return null;
                                }
                                continue MAIN;
                            }
                            bijectionNew[fromTensorIndex] = targetTensorIndex;
                            addedBijectionsNew.add(fromTensorIndex);
                        } else if (bijectionNew[fromTensorIndex] != targetTensorIndex) { //Testing weather new bijection is consistent with already exists
                            if (!currentInfo.nextAndResetRightChain()) {
                                closed = true;
                                return null;
                            }
                            continue MAIN;
                        }
                    }
                while ((currentInfo = currentInfo.next) != null);

                innerPort = new InnerPort(bijectionNew, addedBijectionsNew.toArray());
            } while (true);
        }
View Full Code Here

TOP

Related Classes of cc.redberry.core.utils.IntArrayList

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.