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

cc.redberry.transformation.contractions.UncontractIndices 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 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 entry : im.getMap().entrySet())
                kroneckers.add(CC.createKronecker(entry.getKey(), inverseIndexState(entry.getValue())));
            ApplyIndexMappingDirectTransformation.INSTANCE.perform(tensor, im);
        }
        return tensor;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy