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

cc.redberry.transformation.substitutions.AbstractSimpleSubstitution 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.substitutions;

import cc.redberry.concurrent.OutputPortUnsafe;
import cc.redberry.core.tensor.Derivative;
import cc.redberry.core.tensor.Product;
import cc.redberry.core.tensor.SimpleTensor;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.tensor.TensorNumber;
import cc.redberry.core.tensor.TensorWrapper;
import cc.redberry.core.indexmapping.IndexMappingUtils;
import cc.redberry.core.indexmapping.IndexMappingBuffer;
import cc.redberry.core.indexmapping.IndexMappings;
import cc.redberry.core.tensor.iterators.TensorFirstTreeIterator;
import cc.redberry.transformation.Transformation;
import cc.redberry.transformation.Transformations;
import cc.redberry.core.utils.TensorUtils;

/**
 *
 * @author Dmitry Bolotin
 * @author Stanislav Poslavsky
 */
public abstract class AbstractSimpleSubstitution extends AbstractSubstitution {
    private int level = 0;
    private int indicesLevel = -1;
    private Tensor firstProduct = null;
    private boolean derivativeVarChangedSignum = false;

    public AbstractSimpleSubstitution(T from, Tensor to, boolean allowDiffStates) {
        super(from, to, allowDiffStates);
    }

    public AbstractSimpleSubstitution(T from, Tensor to) {
        super(from, to);
    }

    @Override
    public Tensor transform(Tensor tensor) {
        //TODO review
        Tensor parent = tensor.getParent();
        TensorWrapper wrapper = new TensorWrapper(tensor);
        TensorFirstTreeIterator iterator = new TensorFirstTreeIterator(wrapper, new OnLeaving());
        Tensor current;
        while (iterator.hasNext()) {
            current = iterator.next();

            level++;
            if (indicesLevel == -1 && current instanceof Product) {
                firstProduct = current;
                indicesLevel = level;
            }

            if (current.getClass() != getFromClasss())
                continue;
            T _current = (T) current;
            if (_current.getName() != from.getName())
                continue;
            OutputPortUnsafe opu = IndexMappings.createPortForSimpleTensor(from, _current, allowDiffStates);


            IndexMappingBuffer buffer;
            //TODO refactor throw off if (i.e. spread this code in inheritors)
            if (allowDiffStates)
                buffer = IndexMappingUtils.tryGetPositiveWithoutDiffStates(opu);
            else
                buffer = IndexMappingUtils.tryGetPositive(opu);

            if (buffer == null)
                continue;

            Tensor newTo = getNewTo(_current, from, to);
            if (newTo == null)
                continue;


            if (allowDiffStates)
                if (!IndexMappingUtils.containsDiffStates(buffer))
                    newTo = ApplyIndexMappingUtils.applyIndexMappingWithoutDiffStates(newTo, buffer, getUsedIndices());
                else
                    newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, getUsedIndices());
            else
                newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, getUsedIndices());

            //TODO discuss with Dmitry
            if (buffer.getSignum())
                if (Derivative.onVarsIndicator.is(iterator)) {
                    derivativeVarChangedSignum ^= true;
                    iterator.set(Transformations.contractMetrics(newTo));
                } else
                    iterator.set(new Product(TensorNumber.createMINUSONE(), newTo));
            else if (Derivative.onVarsIndicator.is(iterator))
                iterator.set(Transformations.contractMetrics(newTo));
            else
                iterator.set(newTo);
        }
        Tensor result = wrapper.getInnerTensor();
        result.setParent(parent);
        return result;
    }

    private int[] getUsedIndices() {
        if (indicesLevel == -1)
            return new int[0];
        return TensorUtils.getAllIndicesNames(firstProduct);
    }

    public void derivativeVarCangeSignum() {
        derivativeVarChangedSignum ^= true;
    }

    private class OnLeaving implements Transformation {
        @Override
        public Tensor transform(Tensor tensor) {
            if (indicesLevel == level)
                indicesLevel = -1;
            level--;
            if (derivativeVarChangedSignum) {
                derivativeVarChangedSignum = false;
                return new Product(TensorNumber.createMINUSONE(), tensor);
            }
            return tensor;
        }
    }

    protected abstract Tensor getNewTo(T current, T from, Tensor to);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy