
cc.redberry.transformation.substitutions.ApplyIndexMappingUtils Maven / Gradle / Ivy
/*
* Redberry: symbolic tensor computations.
*
* Copyright (c) 2010-2012:
* Stanislav Poslavsky
* Bolotin Dmitriy
*
* 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 .
*/
package cc.redberry.transformation.substitutions;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import cc.redberry.core.context.CC;
import cc.redberry.core.indices.IndicesUtils;
import cc.redberry.core.indexgenerator.IndexGenerator;
import cc.redberry.core.tensor.Product;
import cc.redberry.core.tensor.SimpleTensor;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.indexmapping.IndexMappingBufferRecord;
import cc.redberry.core.indexmapping.IndexMappingImpl;
import cc.redberry.core.indexmapping.IndexMappingBuffer;
import cc.redberry.core.transformations.ApplyIndexMappingTransformation;
import cc.redberry.core.utils.TensorUtils;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class ApplyIndexMappingUtils {
public static Tensor applyIndexMappingWithoutDiffStates(Tensor target,
IndexMappingBuffer indexMappingBuffer, int[] usedIndices) {
return ApplyIndexMappingTransformation.INSTANCE.perform(target,
new IndexMappingImpl(usedIndices, indexMappingBuffer));
}
public static Tensor applyIndexMappingWithoutDiffStates(Tensor target,
IndexMappingBuffer indexMappingBuffer) {
return applyIndexMappingWithoutDiffStates(target, indexMappingBuffer, new int[0]);
}
public static Tensor applyIndexMappingWithDiffStates(Tensor target,
int[] from, int[] to, int[] usedIndices) {
if (from.length != to.length)
throw new IllegalArgumentException();
IndexMappingImpl preprocess = new IndexMappingImpl();
//creating metrics for rising-lowing indices
int fromIndex, toIndex, fromState, toState;
IndexGenerator ig = new IndexGenerator(TensorUtils.getAllIndices(target));
List metrics = new ArrayList<>();
int i = 0;
for (; i < from.length; ++i) {
//diff states mapping detected
if ((fromState = IndicesUtils.getRawStateInt(from[i])) != (toState = IndicesUtils.getRawStateInt(to[i]))) {
fromIndex = IndicesUtils.getNameWithType(from[i]);
toIndex = ig.generate(IndicesUtils.getType(fromIndex));
preprocess.add(fromIndex, toIndex);
if (fromState != 0)
metrics.add(CC.createMetric(fromIndex, toIndex));
else
metrics.add(CC.createMetric(0x80000000 | fromIndex, 0x80000000 | toIndex));
}
from[i] = IndicesUtils.getNameWithType(from[i]);
to[i] = IndicesUtils.getNameWithType(to[i]);
}
//preprocessing conflicting indices
target = ApplyIndexMappingTransformation.INSTANCE.perform(target, preprocess);
//adding metrics
if (!metrics.isEmpty()) {
Product p = new Product();
p.add(target);
p.add(metrics);
target = p;
}
IndexMappingImpl im = new IndexMappingImpl(usedIndices, from, to);
return ApplyIndexMappingTransformation.INSTANCE.perform(target, im);
}
public static Tensor applyIndexMappingWithDiffStates(Tensor target,
IndexMappingBuffer indexMappingBuffer, int[] usedIndices) {
IndexMappingImpl preprocess = new IndexMappingImpl();
//creating metrics for rising-lowing indices
int fromIndex, toIndex;
IndexGenerator ig = new IndexGenerator(TensorUtils.getAllIndices(target));
List metrics = new ArrayList<>();
for (Map.Entry entry : indexMappingBuffer.getMap().entrySet()) {
IndexMappingBufferRecord record = entry.getValue();
//diff states mapping detected
if (record.diffStatesInitialized() && !record.isContracted()) {
fromIndex = entry.getKey().intValue();
toIndex = ig.generate(IndicesUtils.getType(fromIndex));
preprocess.add(fromIndex, toIndex);
byte states = record.getStates();
if ((states & 1) == 1)
metrics.add(CC.createMetric(fromIndex, toIndex));
else
metrics.add(CC.createMetric(0x80000000 | fromIndex, 0x80000000 | toIndex));
}
}
//preprocessing conflicting indices
target = ApplyIndexMappingTransformation.INSTANCE.perform(target, preprocess);
//adding metrics
if (!metrics.isEmpty()) {
Product p = new Product();
p.add(target);
p.add(metrics);
target = p;
}
IndexMappingImpl im = new IndexMappingImpl(usedIndices, indexMappingBuffer);
return ApplyIndexMappingTransformation.INSTANCE.perform(target, im);
}
public static Tensor applyIndexMappingWithDiffStates(Tensor target,
IndexMappingBuffer indexMappingBuffer) {
return applyIndexMappingWithDiffStates(target, indexMappingBuffer, new int[0]);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy