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

cc.redberry.transformation.collect.RenameContractedIndices 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.collect;

import java.util.Arrays;
import cc.redberry.core.indices.Indices;
import cc.redberry.core.indices.IndicesBuilderSorted;
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.transformation.Transformation;
import cc.redberry.core.utils.IntArrayList;

/**
 *
 * @author Dmitry Bolotin
 * @author Stanislav Poslavsky
 */
public class RenameContractedIndices implements Transformation {
    IntArrayList checked = new IntArrayList();
    final int[] usedNames;

    public RenameContractedIndices(int[] usedNames) {
        this.usedNames = usedNames;
        Arrays.sort(this.usedNames);
    }

    public RenameContractedIndices(int[] usedNames, IntArrayList checked) {
        this.usedNames = usedNames;
        this.checked.addAll(checked);
        Arrays.sort(this.usedNames);
    }

    @Override
    public Tensor transform(Tensor tensor) {
        if (tensor instanceof SimpleTensor || tensor instanceof Product) {
            Indices indices = tensor.getIndices();
            checked.ensureCapacity(indices.size());
            int index;
            IndexMappingDirectAllowingUnmapped im = new IndexMappingDirectAllowingUnmapped();
            //TODO discover case to put allIndices in ig
            IndexGenerator ig = new IndexGenerator(new IndicesBuilderSorted().append(indices).append(usedNames).asArray());
            for (int i = 0; i < indices.size(); i++) {
                index = indices.get(i);
                if (checked.contains(getNameWithType(index))) {
                    if (Arrays.binarySearch(usedNames, getNameWithType(index)) >= 0) {
                        int newIndex = getRawStateInt(index) | ig.generate(getType(index));
                        im.add(index, newIndex);
                        im.add(inverseIndexState(index), inverseIndexState(newIndex));
                    }
                } else
                    checked.add(getNameWithType(index));
            }
            ApplyIndexMappingDirectTransformation.INSTANCE.perform(tensor, im);
        }
        return tensor;
    }

    public void transform(Tensor tensor1, Tensor tensor2) {
        Indices indices = new IndicesBuilderSorted().append(tensor1.getIndices()).append(tensor2.getIndices()).getIndices();
        checked.ensureCapacity(indices.size());
        int index;
        IndexMappingDirectAllowingUnmapped im = new IndexMappingDirectAllowingUnmapped();
        //TODO discover case to put allIndices in ig
        IndexGenerator ig = new IndexGenerator(new IndicesBuilderSorted().append(indices).append(usedNames).asArray());
        for (int i = 0; i < indices.size(); i++) {
            index = indices.get(i);
            if (checked.contains(getNameWithType(index))) {
                if (Arrays.binarySearch(usedNames, getNameWithType(index)) >= 0) {
                    int newIndex = getRawStateInt(index) | ig.generate(getType(index));
                    im.add(index, newIndex);
                    im.add(inverseIndexState(index), inverseIndexState(newIndex));
                }
            } else
                checked.add(getNameWithType(index));
        }
        ApplyIndexMappingDirectTransformation.INSTANCE.perform(tensor1, im);
        ApplyIndexMappingDirectTransformation.INSTANCE.perform(tensor2, im);

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy