/*
* 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;
import cc.redberry.core.indices.Indices;
import cc.redberry.core.indices.IndicesUtils;
import cc.redberry.core.tensor.SimpleTensor;
import cc.redberry.core.tensor.Tensor;
import cc.redberry.core.tensor.TensorField;
import cc.redberry.core.indexmapping.IndexMappingDirect;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public class TensorFieldSubstitution extends AbstractSimpleSubstitution<TensorField> {
public TensorFieldSubstitution(TensorField from, Tensor to, boolean allowDiffStates) {
super(from, to, allowDiffStates);
for (Tensor arg : from)
if (!(arg instanceof SimpleTensor))
throw new SubstitutionException("Only simple tensors allowed as arguments of substituting fields");
}
public TensorFieldSubstitution(TensorField from, Tensor to) {
this(from, to, false);
}
@Override
protected Class getFromClasss() {
return TensorField.class;
}
@Override
protected Tensor getNewTo(TensorField current, TensorField from, Tensor to) {
SimpleTensorSubstitution[] ssts = getArgumentsSubs(from, current);
if (!allowDiffStates && ssts == null)
return null;
Tensor newTo = to.clone();
for (SimpleTensorSubstitution sst : ssts)
newTo = sst.transform(newTo);
return newTo;
}
private SimpleTensorSubstitution[] getArgumentsSubs(final TensorField from, final TensorField current) {
final SimpleTensorSubstitution[] argsSubstitutions =
new SimpleTensorSubstitution[from.getArgs().length];
Tensor argFrom, argCurrent;
for (int i = 0; i < from.getArgs().length; ++i) {
argFrom = from.getArgs()[i].clone();
argCurrent = current.getArgs()[i];
final Indices fromIndices = from.getArgIndices()[i];
final Indices currentIndices = current.getArgIndices()[i];
if (!allowDiffStates)
for (int j = 0; j < fromIndices.size(); ++j)
if (IndicesUtils.getRawStateInt(fromIndices.get(j))
!= IndicesUtils.getRawStateInt(currentIndices.get(j)))
return null;
final IndexMappingDirect im = new IndexMappingDirect(fromIndices,
currentIndices);
if (argFrom.getIndices().applyIndexMapping(im))
argFrom.update();
argsSubstitutions[i] =
new SimpleTensorSubstitution((SimpleTensor) argFrom, argCurrent, allowDiffStates);
}
return argsSubstitutions;
}
}