Package cc.redberry.transformation.contractions

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

/*
* 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 static cc.redberry.core.indices.IndicesUtils.*;
import java.util.Arrays;
import java.util.Map;
import cc.redberry.core.context.CC;
import cc.redberry.core.indices.Indices;
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 UncontractIndicesAndRename extends UncontractIndicesAbstract {
    final int[] indicesToRenameNames;

    public UncontractIndicesAndRename(int[] indicesToRename) {
        super();
        if (indicesToRename.length == 0)
            throw new RuntimeException("use UncontractIndices class");
        this.indicesToRenameNames = indicesToRename;
        Arrays.sort(this.indicesToRenameNames);
    }

    public UncontractIndicesAndRename(int[] forbidenSortedNames, int[] indicesToRename) {
        super(forbidenSortedNames);
//        if (indicesToRename.length == 0)
//            throw new RuntimeException("use UncontractIndices class");
        this.indicesToRenameNames = indicesToRename;
        Arrays.sort(this.indicesToRenameNames);
    }

    @Override
    protected IndexGenerator createIndexGenerator(Tensor tensor) {
        int[] forbidenIndices = TensorUtils.getAllIndicesBuilder(tensor).append(forbidenSortedNames).append(indicesToRenameNames).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();
            IndexMappingDirectAllowingUnmapped renameUsedIndices = null;
            IndexGenerator ig = createIndexGenerator(tensor);
            for (int i = 0; i < indices.size(); ++i) {
                int index = indices.get(i);
                if (checked.contains(inverseIndexState(index)))
                    if (Arrays.binarySearch(indicesToRenameNames, getNameWithType(index)) < 0) {
//                        uncontracted.addAll(inverseIndexState(index));
                        int newIndex = getRawStateInt(index) | ig.generate(getType(index));
                        im.add(index, newIndex);
                    } else {
                        if (renameUsedIndices == null)
                            renameUsedIndices = new IndexMappingDirectAllowingUnmapped();
                        int newIndex = getRawStateInt(index) | ig.generate(getType(index));
                        int kroneckerIndex = getRawStateInt(index) | ig.generate(getType(index));
                        renameUsedIndices.add(index, kroneckerIndex);
                        renameUsedIndices.add(inverseIndexState(index), inverseIndexState(newIndex));
                        im.add(newIndex, kroneckerIndex);
          
                    }
                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())));
            if (renameUsedIndices != null)
                im.add(renameUsedIndices);
//                ApplyIndexMappingDirectTransformation.INSTANCE.perform(tensor, renameUsedIndices);
            ApplyIndexMappingDirectTransformation.INSTANCE.perform(tensor, im);
        }
        return tensor;
    }
}
TOP

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

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.