Package cc.redberry.transformation.substitutions.n

Source Code of cc.redberry.transformation.substitutions.n.TensorFieldSubstitution$DummyTransformation

/*
* 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 java.util.ArrayList;
import java.util.List;
import cc.redberry.core.indices.Indices;
import cc.redberry.core.indices.IndicesUtils;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.tensor.TensorField;
import cc.redberry.core.tensor.TensorWrapper;
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.Transformations;
import cc.redberry.transformation.substitutions.ApplyIndexMappingUtils;

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

    private TensorFieldSubstitution(TensorField 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);
        TraverseState state;
        Tensor current;
        OUT:
        while ((state = iterator.next()) != null) {
            if (state != TraverseState.Entering)
                continue;
            current = iterator.tensor();
            if (!(current instanceof TensorField))
                continue;
            TensorField currentField = (TensorField) current;
            IndexMappingBuffer buffer =
                    IndexMappingUtils.tryGetPositiveWithoutDiffStates(IndexMappings.createPortForSimpleTensor(from, currentField, allowDiffStates));
            if (buffer == null)
                continue;

            assert from.getArgs().length == currentField.getArgs().length;

            Tensor[] fromArgs = from.getArgs(), currentArgs = currentField.getArgs();
            Indices[] fromIndices = from.getArgIndices(), currentIndices = currentField.getArgIndices();

            List<Transformation> transformations = new ArrayList<>();
            Tensor fArg;
            int[] cIndices, fIndices;
            int i, j;
            for (i = 0; i < fromArgs.length; ++i) {
                if (IndexMappings.mappingExists(currentArgs[i], fromArgs[i], allowDiffStates))
                    continue;
                fArg = fromArgs[i].clone();
                fIndices = fromIndices[i].getAllIndices().copy();
                cIndices = currentIndices[i].getAllIndices().copy();
                if (cIndices.length != fIndices.length)
                    throw new InconsistentSubstitutionException(from, to, current);
                boolean diffStates = false;
                for (j = 0; j < cIndices.length; ++j)
                    if (IndicesUtils.getRawStateInt(cIndices[j]) != IndicesUtils.getRawStateInt(fIndices[j])) {
                        diffStates = true;
                        break;
                    }
                if (diffStates && !allowDiffStates)
                    continue OUT;

                if (diffStates) {
                    fArg = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(fArg, fIndices, cIndices, new int[0]);
                    fArg = Transformations.contractMetrics(fArg);
                    fromArgs[i] = Transformations.contractMetrics(fromArgs[i]);
                } else {
                    for (j = 0; j < cIndices.length; ++j) {
                        cIndices[j] = IndicesUtils.getNameWithType(cIndices[j]);
                        fIndices[j] = IndicesUtils.getNameWithType(fIndices[j]);
                    }
                    IndexMappingImpl im = new IndexMappingImpl(new int[0], fIndices, cIndices);
                    fArg = ApplyIndexMappingTransformation.INSTANCE.perform(fArg, im);
                }

                if (!IndexMappings.mappingExists(fromArgs[i], fArg, allowDiffStates))
                    throw new InconsistentSubstitutionException(from, to, current);
                transformations.add(Substitutions.createSubstitution(fArg, currentArgs[i], allowDiffStates));
            }

            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 (!transformations.isEmpty()) {
                for (Transformation transformation : transformations)
                    newTo = transformation.transform(newTo);
                int[] indices = newTo.getIndices().getFreeIndices().getAllIndices().copy();
                for (i = 0; i < indices.length; ++i)
                    indices[i] = IndicesUtils.getNameWithType(indices[i]);
                newTo = ApplyIndexMappingTransformation.INSTANCE.perform(newTo, new IndexMappingImpl(iterator.usedIndices(), indices, indices));
            }
            iterator.set(newTo);
        }
        Tensor result = tensorWrapper.getInnerTensor();
        result.setParent(parent);
        return result;
    }

    private static class DummyTransformation implements Transformation {
        static final DummyTransformation INSTANCE = new DummyTransformation();

        private DummyTransformation() {
        }

        @Override
        public Tensor transform(Tensor tensor) {
            return tensor;
        }
    }
}
TOP

Related Classes of cc.redberry.transformation.substitutions.n.TensorFieldSubstitution$DummyTransformation

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.