
cc.redberry.transformation.IndicesInsertion 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;
import cc.redberry.core.context.CC;
import cc.redberry.core.indices.Indices;
import cc.redberry.core.indices.IndicesBuilderSimple;
import cc.redberry.core.indices.IndicesBuilderSorted;
import cc.redberry.core.indices.IndicesFactory;
import cc.redberry.core.indices.IndicesUtils;
import cc.redberry.core.indices.SimpleIndices;
import cc.redberry.core.indexgenerator.IndexGenerator;
import cc.redberry.core.indexgenerator.IndexGeneratorWrapper;
import cc.redberry.core.tensor.Expression;
import cc.redberry.core.tensor.Product;
import cc.redberry.core.tensor.SimpleTensor;
import cc.redberry.core.tensor.Sum;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.tensor.TensorIterator;
import cc.redberry.core.utils.Indicator;
import cc.redberry.core.utils.TensorUtils;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
* @author Konstantin Kiselev
*/
public class IndicesInsertion implements Transformation {
private final Indicator indicator;
private final Indices indices;
private final int length;
public IndicesInsertion(Indicator indicator, Indices indices) {
if (indices.getUpper().length() != indices.getLower().length())
//TODO more effective check
throw new IllegalArgumentException();
this.indicator = indicator;
this.indices = indices;
this.length = indices.size() / 2;
}
//TODO Indices -> int[]
private Tensor transform(Tensor tensor, IndexGenerator indexGenerator, Indices ind) {
if (tensor instanceof SimpleTensor) {
if (!indicator.is(tensor))
return tensor.clone();
IndicesBuilderSimple ib = new IndicesBuilderSimple();
ib.append(tensor.getIndices()).append(ind);
return CC.createSimpleTensor(
CC.getNameDescriptor(((SimpleTensor) tensor).getName()).getName(), (SimpleIndices) ib.getIndices());
} else if (tensor instanceof Sum) {
Sum newSum = new Sum();
IndexGeneratorWrapper wrapper = new IndexGeneratorWrapper(indexGenerator);
for (Tensor t : tensor) {
//TODO review !!!!!!!!!!!!
//FIXME review !!!!!!!!!!!!
// newSum.add(transform(t, wrapper, ind));
// BUG bug was detected in cc.redberry physics
// we used generator instead of wrapper
newSum.add(transform(t, indexGenerator, ind));
wrapper.dump();
}
wrapper.write();
return newSum;
} else if (tensor instanceof Product) {
Product p = new Product();
TensorIterator iterator = tensor.iterator();
Tensor current;
Tensor temp = null;
int[] lower = ind.getLower().copy();
int[] upper = ind.getUpper().copy();
int[] total = new int[length * 2];
while (iterator.hasNext()) {
current = iterator.next();
if (!indicator.is(current)) {
p.add(current.clone());
continue;
}
if (temp == null) {
System.arraycopy(upper, 0, total, 0, length);
for (int i = length; i < length * 2; ++i)
total[i] = indexGenerator.generate(
IndicesUtils.getType(lower[i - length]));
temp = current;
continue;
} else {
p.add(transform(temp, indexGenerator, IndicesFactory.createSimple(total.clone())));
for (int i = 0; i < length; ++i)
total[i] = 0x80000000 ^ total[i + length];
for (int i = length; i < length * 2; ++i)
total[i] = indexGenerator.generate(
IndicesUtils.getType(total[i]));
temp = current;
}
}
if (temp != null) {
System.arraycopy(lower, 0, total, length, length);
p.add(transform(temp, indexGenerator, IndicesFactory.createSimple(total)));
}
return p;
} else
return tensor.clone();
}
private IndexGenerator createIndexGenerator(Tensor tensor) {
return new IndexGenerator(new IndicesBuilderSorted().append(indices).append(TensorUtils.getAllIndices(tensor)).asArray());
}
@Override
public Tensor transform(Tensor tensor) {
if (tensor instanceof Expression) {
Expression e = (Expression) tensor;
return new Expression(transform(e.left()), e.right());
}
return transform(tensor, createIndexGenerator(tensor), indices);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy