Package cc.redberry.transformation.substitutions.n

Source Code of cc.redberry.transformation.substitutions.n.SimpleSubstitution

/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
*   Stanislav Poslavsky   <stvlpos@mail.ru>
*   Bolotin Dmitriy       <bolotin.dmitriy@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package cc.redberry.transformation.substitutions.n;

import cc.redberry.core.tensor.*;
import cc.redberry.core.indexmapping.IndexMappingBuffer;
import cc.redberry.core.indexmapping.IndexMappingImpl;
import cc.redberry.core.indexmapping.IndexMappingUtils;
import cc.redberry.core.indexmapping.IndexMappings;
import cc.redberry.core.tensor.iterators.TraverseState;
import cc.redberry.core.transformations.ApplyIndexMappingTransformation;
import cc.redberry.transformation.Transformation;
import cc.redberry.transformation.substitutions.ApplyIndexMappingUtils;
import cc.redberry.core.utils.TensorUtils;

/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
class SimpleSubstitution implements Transformation {
    static final SubstitutionProvider SIMPLE_SUBSTITUTION_PROVIDER = new SubstitutionProvider() {
        @Override
        public SimpleSubstitution createSubstitution(Tensor from, Tensor to, boolean allowDiffStates) {
            return new SimpleSubstitution(from, to, allowDiffStates);
        }
    };
    private final Tensor from, to;
    private final boolean allowDiffStates;

    private SimpleSubstitution(Tensor from, Tensor to, boolean allowDiffStates) {
        this.from = from;
        this.to = to;
        this.allowDiffStates = allowDiffStates;
    }

    @Override
    public Tensor transform(Tensor tensor) {
        Tensor parent = tensor.getParent();
        TensorWrapper tensorWrapper = new TensorWrapper(tensor);
        TreeIteratorSubs iterator = new TreeIteratorSubs(tensorWrapper);

        Tensor current;
        TraverseState state;
        int i;
        OUT:
        while ((state = iterator.next()) != null) {
            if (state != TraverseState.Entering)
                continue;
            current = iterator.tensor();
            IndexMappingBuffer buffer =
                    IndexMappingUtils.tryGetPositiveWithoutDiffStates(IndexMappings.createPort(from, current, allowDiffStates));
            if (buffer == null)
                continue;
            for (Derivative derivative : iterator.derivativeStack)
                for (i = 0; i < derivative.getDerivativeOrder(); ++i)
                    if (IndexMappings.mappingExists(from, derivative.getVariation(i), true) //TODO Simple tensor optimization
                            || TensorUtils.contains(to, derivative.getVariation(i)))
                        throw new InconsistentSubstitutionException("TODO message");
            Tensor newTo = to.clone();
            if (!allowDiffStates || !IndexMappingUtils.containsDiffStates(buffer))
                newTo = ApplyIndexMappingTransformation.INSTANCE.perform(newTo, new IndexMappingImpl(iterator.usedIndices(), buffer));
            else
                newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, iterator.usedIndices());
            if (buffer.getSignum() == true)
                newTo = new Product(TensorNumber.createMINUSONE(), newTo);
            iterator.set(newTo);
        }
        Tensor result = tensorWrapper.getInnerTensor();
        result.setParent(parent);
        return result;
    }
}
TOP

Related Classes of cc.redberry.transformation.substitutions.n.SimpleSubstitution

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.