/*
* 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.core.indexmapping;
import java.util.ArrayList;
import java.util.List;
import cc.redberry.concurrent.OutputPortUnsafe;
import cc.redberry.core.number.ComplexElement;
import cc.redberry.core.tensor.ProductContent;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.combinatorics.IntPermutationsGenerator;
import cc.redberry.core.utils.stretces.PrecalculatedStretches;
import cc.redberry.core.utils.stretces.Stretch;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
final class ProviderProductAndDerivative implements IndexMappingProvider {
static final IndexMappingProviderFactory FACTORY = new IndexMappingProviderFactory() {
@Override
public IndexMappingProvider create(IndexMappingProvider opu, Tensor from, Tensor to, boolean allowDiffStates) {
ProductContent fromC = (ProductContent) from.getContent(),
toC = (ProductContent) to.getContent();
if (fromC.size() != toC.size())
return IndexMappingProvider.Util.EMPTY_PROVIDER;
Boolean sign = compareFactors(fromC.getFactor(), toC.getFactor());
if (sign == null)
return IndexMappingProvider.Util.EMPTY_PROVIDER;
for (int i = 0; i < fromC.size(); ++i)
if (fromC.get(i).hashCode() != toC.get(i).hashCode())
return IndexMappingProvider.Util.EMPTY_PROVIDER;
if (!fromC.getContractionStructure().equals(toC.getContractionStructure()))
return IndexMappingProvider.Util.EMPTY_PROVIDER;
if (!testScalars(fromC.getScalarContents(), toC.getScalarContents(), allowDiffStates))
return IndexMappingProvider.Util.EMPTY_PROVIDER;
if (sign.booleanValue() == true)
return new MinusIndexMappingProvider(new ProviderProductAndDerivative(opu, fromC.getNonScalarContent(), toC.getNonScalarContent(), allowDiffStates));
return new ProviderProductAndDerivative(opu, fromC.getNonScalarContent(), toC.getNonScalarContent(), allowDiffStates);
}
};
private static Boolean compareFactors(ComplexElement c1, ComplexElement c2) {
if (c1.isEquals(c2))
return false;
if (c1.isEquals(c2.negotiate()))
return true;
return null;
}
private static boolean testScalars(ProductContent[] from, ProductContent[] to, boolean allowDiffStates) {
if (from.length != to.length)
return false;
int i;
int[] hashes = new int[from.length];
for (i = 0; i < from.length; ++i)
if ((hashes[i] = from[i].hashCode()) != to[i].hashCode())
return false;
PrecalculatedStretches precalculatedStretches = new PrecalculatedStretches(hashes);
for (Stretch stretch : precalculatedStretches)
if (stretch.length == 1)
if (!mappingExists(from[stretch.from], to[stretch.from], allowDiffStates))
return false;
OUTER:
for (Stretch stretch : precalculatedStretches)
if (stretch.length > 1) {
SEMIOUTER:
for (int[] permutation : new IntPermutationsGenerator(stretch.length)) {
for (i = 0; i < stretch.length; ++i)
if (!mappingExists(from[stretch.from + i], to[stretch.from + permutation[i]], allowDiffStates))
continue SEMIOUTER; // This permutation is bad
continue OUTER; //Good permutation has been found
}
return false; //No good combinatorics found
}
return true;
}
private static boolean mappingExists(ProductContent from, ProductContent to, boolean allowDiffStates) {
if (from.isDerivativeContent() != to.isDerivativeContent())
return false;
final ProviderProductAndDerivative pp = new ProviderProductAndDerivative(IndexMappingItUtils.EMPTY_BUFFERS_PORT, from, to, allowDiffStates);
pp.tick();
return pp.take() != null;
}
//private final ProductContent from, to;
//private PermutationsProvider permutationsProvider;
private final DummyIndexMappingProvider dummyProvider;
private final OutputPortUnsafe<IndexMappingBuffer> op;
private ProviderProductAndDerivative(final OutputPortUnsafe<? extends IndexMappingBuffer> opu,
final ProductContent from, final ProductContent to, boolean allowDiffStates) {
this.dummyProvider = new DummyIndexMappingProvider(opu);
//this.from = from;
//this.to = to;
int begin = 0;
int i;
//List<PermutationsProvider> disjointProviders = new ArrayList<>();
List<Pair> stretches = new ArrayList<>();
List<Tensor> npFrom = new ArrayList<>(), npTo = new ArrayList<>();
for (i = 1; i <= from.size(); ++i)
if (i == from.size() || !from.getContractionStructure().get(i).equals(from.getContractionStructure().get(i - 1))) {
if (i - 1 != begin)
stretches.add(new Pair(from.getRange(begin, i), to.getRange(begin, i)));
else {
npFrom.add(from.get(i - 1));
npTo.add(to.get(i - 1));
}
begin = i;
}
//TODO sort stretches by length
OutputPortUnsafe<IndexMappingBuffer> lastOutput = dummyProvider;
if (!npFrom.isEmpty())
lastOutput = new SimpleProductProvider(dummyProvider, npFrom.toArray(new Tensor[npFrom.size()]), npTo.toArray(new Tensor[npTo.size()]), allowDiffStates);
if (stretches.isEmpty())
this.op = lastOutput;
else {
PermutatorProvider[] pProviders = new PermutatorProvider[stretches.size()];
i = 0;
for (Pair p : stretches)
lastOutput = pProviders[i++] = new PermutatorProvider(lastOutput, p.from, p.to, allowDiffStates);
this.op = new SimpleProductProvider(pProviders);
}
}
@Override
public boolean tick() {
return dummyProvider.tick();
}
@Override
public IndexMappingBuffer take() {
return op.take();
}
protected static class Pair {
public final Tensor[] from, to;
public Pair(final Tensor[] from, final Tensor[] to) {
this.from = from;
this.to = to;
}
}
}