/*
* 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.concurrent.OutputPortUnsafe;
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.*;
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;
import java.util.Arrays;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
class ProductSubstitution implements Transformation {
static final SubstitutionProvider PROVIDER_SUBSTITUTION_PRODUCT = new SubstitutionProvider() {
@Override
public Transformation createSubstitution(Tensor from, Tensor to, boolean allowDiffStates) {
return new ProductSubstitution((Product) from, to, allowDiffStates);
}
};
private final Product from;
private final Tensor to;
private final boolean allowDiffStates;
private final Tensor[] data;
private final FullContractionsStructure fromContractions;
ProductSubstitution(Product from, Tensor to, boolean allowDiffStates) {
this.from = from;
this.to = to;
this.allowDiffStates = allowDiffStates;
this.fromContractions = from.getContent().getFullContractionStructure();
int[] seeds = new int[fromContractions.componentCount];
Arrays.fill(seeds, -1);
for (int i = 0; i < fromContractions.components.length; ++i)
if (seeds[fromContractions.components[i]] == -1)
seeds[fromContractions.components[i]] = i;
this.data = from.getContent().getDataCopy();
}
@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();
if (!(current instanceof Product))
continue;
final ProductContent currentContent = (ProductContent) current.getContent();
ProductsBijectionsPort bijectionsPort = new ProductsBijectionsPort(from.getContent(), currentContent);
int[] bijection;
IndexMappingBuffer buffer = null;
while ((bijection = bijectionsPort.take()) != null) {
OutputPortUnsafe<IndexMappingBuffer> opu = IndexMappings.createBijectiveProductPort(data, dataTo(currentContent.getDataCopy(), bijection), allowDiffStates);
buffer = opu.take();
if (buffer != null)
break;
}
if (buffer == null)
continue;
//Processing new Product
Arrays.sort(bijection);
Product resulting = new Product();
for (i = 0; i < currentContent.size(); ++i)
if (Arrays.binarySearch(bijection, i) < 0)
resulting.add(currentContent.get(i));
if (!currentContent.getFactor().isOne())
resulting.add(new TensorNumber(currentContent.getFactor()));
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())
newTo = new Product(TensorNumber.createMINUSONE(), newTo);
resulting.add(newTo);
//CHECKSTYLE a*a = 2 in a*a*a*a -> 2*a*a
iterator.set(Transformations.calculateNumbers(resulting.equivalent()));
}
Tensor result = tensorWrapper.getInnerTensor();
result.setParent(parent);
return result;
}
private static Tensor[] dataTo(final Tensor[] data, final int[] bijection) {
Tensor[] to = new Tensor[bijection.length];
for (int i = 0; i < bijection.length; ++i)
to[i] = data[bijection[i]];
return to;
}
}