Package cc.redberry.core.indexmapping

Source Code of cc.redberry.core.indexmapping.ProviderIntegralFactory

/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
*   Stanislav Poslavsky   <stvlpos@mail.ru>
*   Bolotin Dmitriy       <bolotin.dmitriy@gmail.com>
*
* This file is part of Redberry.
*
* Redberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Redberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Redberry. If not, see <http://www.gnu.org/licenses/>.
*/
package cc.redberry.core.indexmapping;

import java.util.Arrays;
import cc.redberry.core.tensor.Integral;
import cc.redberry.core.tensor.SimpleTensor;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.combinatorics.IntPermutationsGenerator;
import cc.redberry.core.utils.stretces.PrecalculatedStretches;
import cc.redberry.core.utils.stretces.Stretch;

/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
final class ProviderIntegralFactory implements IndexMappingProviderFactory {
    static final ProviderIntegralFactory INSTANCE = new ProviderIntegralFactory();

    private ProviderIntegralFactory() {
    }

    @Override
    public IndexMappingProvider create(IndexMappingProvider opu, Tensor from, Tensor to, boolean allowDiffStates) {
        final Integral intFrom = (Integral) from, intTo = (Integral) to;
        final SimpleTensor[] varsFrom = intFrom.vars(), varsTo = intTo.vars();
        if (varsFrom.length != varsTo.length)
            return IndexMappingProvider.Util.EMPTY_PROVIDER;
        Arrays.sort(varsFrom); //TODO change to quick Sort
        Arrays.sort(varsTo); //TODO change to quick Sort
        int i;
        int[] hashes = new int[varsFrom.length];
        for (i = 0; i < varsFrom.length; ++i)
            if ((hashes[i] = varsFrom[i].hashCode()) != varsTo[i].hashCode())
                return IndexMappingProvider.Util.EMPTY_PROVIDER;
        PrecalculatedStretches stretches = new PrecalculatedStretches(hashes);
        for (Stretch stretch : stretches)
            if (stretch.length == 1)
                if (!IndexMappings.mappingExists(varsFrom[stretch.from], varsTo[stretch.from], allowDiffStates))
                    return IndexMappingProvider.Util.EMPTY_PROVIDER;
        STRETCHES:
        for (Stretch stretch : stretches)
            if (stretch.length != 1) {
                PERMUTATIONS:
                for (int[] permutation : new IntPermutationsGenerator(stretch.length)) {
                    for (i = 0; i < stretch.length; ++i)
                        if (!IndexMappings.mappingExists(varsFrom[stretch.from + i], varsTo[stretch.from + permutation[i]], allowDiffStates))
                            continue PERMUTATIONS;
                    continue STRETCHES;
                }
                return IndexMappingProvider.Util.EMPTY_PROVIDER;
            }
        return IndexMappings.createPort(opu, intFrom.target(), intTo.target(), allowDiffStates);
    }
}
TOP

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

TOP
Copyright © 2018 www.massapi.com. 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.