Package cc.redberry.transformation.contractions

Source Code of cc.redberry.transformation.contractions.UncontractIndices

/*
* 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.transformation.contractions;

import java.util.Map;
import cc.redberry.core.context.CC;
import cc.redberry.core.indices.Indices;
import static cc.redberry.core.indices.IndicesUtils.*;
import cc.redberry.core.indexgenerator.IndexGenerator;
import cc.redberry.core.tensor.Product;
import cc.redberry.core.tensor.SimpleTensor;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.indexmapping.IndexMappingDirectAllowingUnmapped;
import cc.redberry.core.transformations.ApplyIndexMappingDirectTransformation;
import cc.redberry.core.utils.TensorUtils;

/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class UncontractIndices extends UncontractIndicesAbstract {
    public UncontractIndices(int[] reservedSortedNames) {
        super(reservedSortedNames);
    }

    public UncontractIndices() {
    }

    public UncontractIndices(int[] forbidenSortedNames, Indices additionalIndices) {
        super(forbidenSortedNames);
        for (int i = 0; i < additionalIndices.size(); ++i)
            checked.add(additionalIndices.get(i));
    }

    public UncontractIndices(Indices additionalIndices) {
        for (int i = 0; i < additionalIndices.size(); ++i)
            checked.add(additionalIndices.get(i));
    }

    @Override
    protected IndexGenerator createIndexGenerator(Tensor tensor) {
        int[] forbidenIndices = TensorUtils.getAllIndicesBuilder(tensor).append(forbidenSortedNames).asArray();
        return new IndexGenerator(forbidenIndices);
    }

    @Override
    public Tensor renameIndicesAndBuidKroneckers(Tensor tensor) {
        if (tensor instanceof SimpleTensor || tensor instanceof Product) {
            Indices indices = tensor.getIndices();
            IndexMappingDirectAllowingUnmapped im = new IndexMappingDirectAllowingUnmapped();
            IndexGenerator ig = createIndexGenerator(tensor);
            for (int i = 0; i < indices.size(); ++i) {
                int index = indices.get(i);
                if (checked.contains(inverseIndexState(index))) {
                    int newIndex = getRawStateInt(index) | ig.generate(getType(index));
                    im.add(index, newIndex);
                } else
                    checked.add(index);
            }
            if (im.isEmpty())
                return tensor;
            //biulding kroneckers
            for (Map.Entry<Integer, Integer> entry : im.getMap().entrySet())
                kroneckers.add(CC.createKronecker(entry.getKey(), inverseIndexState(entry.getValue())));
            ApplyIndexMappingDirectTransformation.INSTANCE.perform(tensor, im);
        }
        return tensor;
    }
}
TOP

Related Classes of cc.redberry.transformation.contractions.UncontractIndices

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.