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

cc.redberry.transformation.integral.CollectIntegralFromSum 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.integral;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import cc.redberry.core.indices.IndicesTypeStructure;
import cc.redberry.core.indices.SimpleIndices;
import cc.redberry.core.tensor.Integral;
import cc.redberry.core.tensor.SimpleTensor;
import cc.redberry.core.tensor.Sum;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.tensor.TensorIterator;
import cc.redberry.core.indexmapping.IndexMappingDirect;
import cc.redberry.core.transformations.ApplyIndexMappingDirectTransformation;
import cc.redberry.transformation.Transformation;
import cc.redberry.transformation.substitutions.SubstitutionsFactory;

/**
 *
 * @author Stanislav Poslavsky
 */
public class CollectIntegralFromSum implements Transformation {
    public static CollectIntegralFromSum INSTANCE = new CollectIntegralFromSum();

    private CollectIntegralFromSum() {
    }

    @Override
    public Tensor transform(final Tensor tensor) {
        if (!(tensor instanceof Sum))
            return tensor;
        final IntegralIP iP = new IntegralIP();
        final TensorIterator iterator = tensor.iterator();
        Tensor current;
        while (iterator.hasNext()) {
            current = iterator.next();
            if (current instanceof Integral) {
                iP.put((Integral) current);
                iterator.remove();
            }
        }
        ((Sum) tensor).add(iP.result());
        return tensor.equivalent();
    }

    private static class IntegralIP {
        List integrals = new ArrayList<>();

        void put(final Integral integral) {
            for (Integral current : integrals) {
                final Transformation[] subs = getSubs(integral.vars(), current.vars());
                if (subs == null)
                    continue;
                Tensor target = integral.target();
                for (Transformation sub : subs)
                    target = sub.transform(target);
                current.setTarget(new Sum(current.target(), target));
                return;
            }
            integrals.add(integral);
        }

        Transformation[] getSubs(final SimpleTensor[] from, final SimpleTensor[] to) {
            int length;
            if ((length = from.length) != to.length)
                return null;
            Arrays.sort(from, IndicesComparator.INSTANCE);
            Arrays.sort(to, IndicesComparator.INSTANCE);
            final Transformation[] subs = new Transformation[length];
            SimpleIndices fromIndices, toIndices;
            for (int i = 0; i < length; ++i) {

                if (!(fromIndices = from[i].getIndices()).similarTypeStructure((toIndices = to[i].getIndices())))
                    return null;
                final IndexMappingDirect im = new IndexMappingDirect(fromIndices, toIndices);
                ApplyIndexMappingDirectTransformation.INSTANCE.perform(from[i], im);
                subs[i] = SubstitutionsFactory.createSubstitution(from[i], to[i]);
            }
            return subs;
        }

        Tensor result() {
            return new Sum(integrals);
        }

        private static class IndicesComparator implements Comparator {
            static IndicesComparator INSTANCE = new IndicesComparator();

            @Override
            public int compare(final SimpleTensor o1, final SimpleTensor o2) {
                final SimpleIndices i1 = o1.getIndices(), i2 = o2.getIndices();
                if (i1.size() < i2.size())
                    return -1;
                if (i1.size() > i2.size())
                    return 1;
                final IndicesTypeStructure s1 = new IndicesTypeStructure(i1), s2 = new IndicesTypeStructure(i2);
                return Integer.compare(s1.hashCode(), s2.hashCode());
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy