All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.redberry.transformation.contractions.UncontractIndicesAndRename Maven / Gradle / Ivy

/*
 * Redberry: symbolic tensor computations.
 *
 * Copyright (c) 2010-2012:
 *   Stanislav Poslavsky   
 *   Bolotin Dmitriy       
 *
 * 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 .
 */
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 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;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy