/*
* 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.Arrays;
import static cc.redberry.core.tensor.FullContractionsStructure.*;
import cc.redberry.core.tensor.*;
import cc.redberry.core.tensor.iterators.TraverseState;
import cc.redberry.transformation.Transformation;
import cc.redberry.core.utils.ArraysUtils;
import cc.redberry.core.utils.stretces.Stretch;
import cc.redberry.core.utils.stretces.StretchIteratorS;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
class ProductSubstitution implements Transformation {
static final SubstitutionProvider PROVIDER_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 int[] seeds;
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.seeds = seeds;
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;
go(((Product) current).getContent());
}
Tensor result = tensorWrapper.getInnerTensor();
result.setParent(parent);
return result;
}
private void go(ProductContent productContent) {
FullContractionsStructure productContructions = productContent.getFullContractionStructure();
final Tensor[] productData = productContent.getDataCopy();
int seed = seeds[0];
Tensor headFrom = data[seed];
long[] contractions = fromContractions.contractions[seed];
int i;
//Search for first match
for (i = 0; i < productData.length; ++i)
if (weakMatch(headFrom, productData[i]))
break;
if (i == productData.length)
return; //No match found
int matchedIndex = i;
short[] diffIds = headFrom.getIndices().getDiffIds().clone();
int[] diffIdsPermutation = ArraysUtils.quickSortP(diffIds);
int[] bijection = new int[data.length];
Arrays.fill(bijection, -1);
bijection[seed] = matchedIndex;
for (Stretch stretch : new StretchIteratorS(diffIds))
if (stretch.length == 1) {
long fromIndexContraction = contractions[diffIdsPermutation[stretch.from]];
long productIndexContraction = productContructions.contractions[matchedIndex][diffIdsPermutation[stretch.from]];
if (getToIndexId(fromIndexContraction) != getToIndexId(productIndexContraction))
return; //Early search termination
int fromTensorIndex = getToTensorIndex(fromIndexContraction);
int productTensorIndex = getToTensorIndex(productIndexContraction);
if (!weakMatch(data[fromTensorIndex], productData[productTensorIndex]))
return; //early termination
bijection[fromTensorIndex] = productTensorIndex;
}
System.out.println(Arrays.toString(data));
System.out.println(Arrays.toString(дергаем(productData, bijection)));
}
private Tensor[] дергаем(Tensor[] product, int[] bijection) {
Tensor[] r = new Tensor[bijection.length];
for (int i = 0; i < bijection.length; ++i)
if (bijection[i] == -1)
r[i] = null;
else
r[i] = product[bijection[i]];
return r;
}
private void foundBijection(int[] bijection, final Tensor[] productData) {
}
private boolean weakMatch(Tensor t0, Tensor t1) {
if (t0.hashCode() != t1.hashCode())
return false;
if (t0.getClass() != t1.getClass())
return false;
if (t0.getIndices().getClass() != t1.getIndices().getClass())
return false;
if (t0.getIndices().size() != t1.getIndices().size())
return false;
return true;
}
}