Package cc.redberry.transformation.contractions

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

/*
* 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.ArrayList;
import java.util.Arrays;
import java.util.List;
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.transformation.Transformation;
import cc.redberry.core.utils.TensorUtils;
import static cc.redberry.core.indices.IndicesUtils.*;

/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class UncontractIndicesTransformation implements Transformation {
    final List<Tensor> kroneckers = new ArrayList<>();
    final List<Integer> checked = new ArrayList<>();
    final List<Integer> uncontracted = new ArrayList<>();
    final int[] usedIndicesNames;

    public UncontractIndicesTransformation() {
        usedIndicesNames = new int[0];
    }

    public UncontractIndicesTransformation(Indices toUncontracct) {
        for (int i = 0; i < toUncontracct.size(); ++i)
            checked.add(inverseIndexState(toUncontracct.get(i)));
        usedIndicesNames = new int[0];
    }

    public UncontractIndicesTransformation(int[] usedIndicesNames) {
        this.usedIndicesNames = usedIndicesNames;
        Arrays.sort(this.usedIndicesNames);
    }

    public UncontractIndicesTransformation(Indices toUncontracct, int[] usedIndicesNames) {
        for (int i = 0; i < toUncontracct.size(); ++i)
            checked.add(inverseIndexState(toUncontracct.get(i)));
        this.usedIndicesNames = usedIndicesNames;
        Arrays.sort(this.usedIndicesNames);
    }

    public List<Tensor> getKroneckers() {
        return kroneckers;
    }

    public List<Integer> getUncontracted() {
        return uncontracted;
    }

    @Override
    public Tensor transform(Tensor tensor) {
        Product result = new Product();
        result.add(renameIndicesAndBuidKroneckers(tensor));
        result.add(kroneckers);
        return result.equivalent();
    }

    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;
           
            ig = new IndexGenerator(TensorUtils.getAllIndicesBuilder(tensor).append(usedIndicesNames).asArray());
            for (int i = 0; i < indices.size(); ++i) {
                int index = indices.get(i);
                if (checked.contains(inverseIndexState(index)))
                    if (usedIndicesNames.length == 0 || Arrays.binarySearch(usedIndicesNames, getNameWithType(index)) < 0) {
//                        uncontracted.add(inverseIndexState(index));
                        int newIndex = getRawStateInt(index) | ig.generate(getType(index));
                        uncontracted.add(getNameWithType(index));
                        uncontracted.add(getNameWithType(newIndex));
                        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);
                        uncontracted.add(getNameWithType(newIndex));                       
                        uncontracted.add(getNameWithType(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.UncontractIndicesTransformation

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.