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

cc.redberry.transformation.symmetrize.Symmetrize 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.symmetrize;

import java.util.ArrayList;
import java.util.List;
import cc.redberry.core.context.CC;
import cc.redberry.core.indices.Indices;
import cc.redberry.core.indices.IndicesUtils;
import cc.redberry.core.number.ComplexElement;
import cc.redberry.core.number.NumberFraction;
import cc.redberry.core.number.RationalElement;
import cc.redberry.core.tensor.Product;
import cc.redberry.core.tensor.Sum;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.tensor.TensorNumber;
import cc.redberry.core.indexmapping.IndexMappingDirectAllowingUnmapped;
import cc.redberry.core.indexmapping.IndexMappingUtils;
import cc.redberry.core.combinatorics.Permutation;
import cc.redberry.core.combinatorics.Symmetries;
import cc.redberry.core.combinatorics.Symmetry;
import cc.redberry.core.transformations.ApplyIndexMappingDirectTransformation;
import cc.redberry.transformation.Transformation;

/**
 *
 * @author Dmitry Bolotin
 * @author Stanislav Poslavsky
 */
public class Symmetrize implements Transformation {
    private final Indices indices;
    private final int[] indicesNames;
    private final Symmetries symmetries;
    private final boolean allowDiffStates;
    private final boolean multiplyFactorial;

    public Symmetrize(Indices indices, Symmetries symmetries, boolean multiplyFactorial) {
        this.indices = indices.getFreeIndices();
        this.multiplyFactorial = multiplyFactorial;
        this.indicesNames = indices.getFreeIndices().getAllIndices().copy();
//        for (int i = 0; i < indicesNames.length; ++i)
//            indicesNames[i] = IndicesUtils.getNameWithType(indicesNames[i]);
        this.symmetries = symmetries;
      
        boolean allowDiffStates = false;
        for (Symmetry symmetry : symmetries.getBaseSymmetries())
            for (int i = 0; i < symmetries.dimension(); ++i)
                if (IndicesUtils.getRawStateInt(indices.get(i))
                        != IndicesUtils.getRawStateInt(indices.get(symmetry.newIndexOf(i)))) {
                    allowDiffStates = true;
                    break;
                }
        if(allowDiffStates && !CC.withMetric())
            throw new IllegalArgumentException("Diff states in non metrix regim");
        this.allowDiffStates = allowDiffStates;
    }

    @Override
    public Tensor transform(Tensor tensor) {
        if (!tensor.getIndices().getFreeIndices().equalsIgnoreOrder(indices))
            return tensor;
        if (symmetries.isEmpty())
            return TensorNumber.createZERO();
        Symmetries tensorSymmetries = IndexMappingUtils.getSymmetriesFromMappings(indices, tensor, allowDiffStates);

        List generatedTensors = new ArrayList<>();
        List generatedPermutations = new ArrayList<>();
        OUT:
        for (Permutation permutation : symmetries) {
            for (Permutation generatedPermutation : generatedPermutations)
                for (Permutation tensorSymmetry : tensorSymmetries)
                    if (permutation.equals(generatedPermutation.composition(tensorSymmetry)))
                        continue OUT;
            generatedPermutations.add(permutation);

            int[] newIndicesNames = permutation.permute(indicesNames);
            IndexMappingDirectAllowingUnmapped im = new IndexMappingDirectAllowingUnmapped(indicesNames, newIndicesNames);
            generatedTensors.add(ApplyIndexMappingDirectTransformation.INSTANCE.perform(tensor.clone(), im));
        }
        if (generatedTensors.isEmpty())
            return TensorNumber.createZERO();
        if (generatedTensors.size() == 1)
            return generatedTensors.get(0);
        if (multiplyFactorial) {
            TensorNumber num = new TensorNumber(new ComplexElement(new NumberFraction(1, generatedTensors.size()), RationalElement.ZERO));
            return new Product(num, new Sum(generatedTensors).equivalent());
        } else
            return new Sum(generatedTensors).equivalent();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy