/*
* 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 java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import cc.redberry.core.tensor.Derivative;
import cc.redberry.core.tensor.Fraction;
import cc.redberry.core.tensor.MultiTensor;
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.tensor.TensorNumber;
import cc.redberry.core.tensor.TensorWrapper;
import cc.redberry.core.indexmapping.IndexMappings;
import cc.redberry.core.tensor.iterators.GuidePermit;
import cc.redberry.core.tensor.iterators.IterationGuide;
import cc.redberry.core.tensor.iterators.TensorLastTreeIterator;
import cc.redberry.core.transformations.EquivalentTransformation;
/**
*
* @author Dmitry Bolotin
* @author Stanislav Poslavsky
*/
public abstract class AbstractZeroSimpleTensorSubstitution<T extends SimpleTensor> extends AbstractZeroSubstitution<T> {
Deque<Tensor[]> derivativesVars;
public AbstractZeroSimpleTensorSubstitution(T from, boolean allowDiffStates) {
super(from, allowDiffStates);
}
public abstract boolean canMatch(T from, T current);
private void subsZero(TensorLastTreeIterator iterator) {
if (Fraction.onDenominatorIndicator.is(iterator))
throw new ArithmeticException("Divide by zero");
else if (MultiTensor.onSummandIndicator.is(iterator))
iterator.remove();
else if (MultiTensor.onMultiplierIndicator.is(iterator) || Derivative.onTargetIndicator.is(iterator))
if (iterator.isUnderIterator(TensorWrapper.onInnerTensorIndicator, 2)) {
iterator.levelUp();
iterator.set(TensorNumber.createZERO());
} else if (iterator.isUnderIterator(MultiTensor.onSummandIndicator, 3)) {
iterator.levelUp();
iterator.remove();
} else
iterator.set(TensorNumber.createZERO());
else
iterator.set(TensorNumber.createZERO());
}
@Override
public Tensor transform(Tensor tensor) {
//TODO review
Tensor parent = tensor.getParent();
TensorWrapper wrapper = new TensorWrapper(tensor);
TensorLastTreeIterator iterator = new TensorLastTreeIterator(wrapper, new Guide(), EquivalentTransformation.INSTANCE);
Tensor current;
OUT_FOR:
while (iterator.hasNext()) {
current = iterator.next();
if (current instanceof Sum && ((Sum) current).isEmpty()) {
subsZero(iterator);
continue;
}
if (current.getClass() != getFromClasss())
continue;
T _current = (T) current;
if (_current.getName() != from.getName())
continue;
if (IndexMappings.createPortForSimpleTensor(from, _current, allowDiffStates).take() == null)
continue;
if (!canMatch(from, _current))
continue;
if (iterator.isUnderIterator(Derivative.onTargetIndicator, Integer.MAX_VALUE)) {
for (Tensor[] vars : derivativesVars) {
int i;
if ((i = Arrays.binarySearch(vars, current)) >= 0) {
if (!allowDiffStates) {
//TODO discover all possibiliyies
if (!IndexMappings.mappingExists(from, (SimpleTensor) vars[i], true)) {
subsZero(iterator);
continue OUT_FOR;
}
}
continue OUT_FOR;
}
}
subsZero(iterator);
} else
subsZero(iterator);
}
Tensor result = wrapper.getInnerTensor().equivalent();
result.setParent(parent);
return result;
}
private class Guide implements IterationGuide {
@Override
public GuidePermit letInside(TensorIterator iterator, Tensor tensor) {
if (tensor instanceof Derivative) {
Tensor[] vars = ((Derivative) tensor).getVars();
Arrays.sort(vars);
if (derivativesVars == null)
derivativesVars = new ArrayDeque<>();
derivativesVars.push(vars);
}
if (Derivative.onVarsIndicator.is(iterator))
return GuidePermit.DontShow;
return GuidePermit.Enter;
}
}
}