Package cc.redberry.core.tensor

Examples of cc.redberry.core.tensor.TensorWrapper


    }

    @Override
    public Tensor transform(Tensor tensor) {
        Tensor parent = tensor.getParent();
        TensorWrapper tensorWrapper = new TensorWrapper(tensor);
        TreeIteratorSubs iterator = new TreeIteratorSubs(tensorWrapper);
        TraverseState state;
        Tensor current;
        OUT:
        while ((state = iterator.next()) != null) {
            if (state != TraverseState.Entering)
                continue;
            current = iterator.tensor();
            if (!(current instanceof TensorField))
                continue;
            TensorField currentField = (TensorField) current;
            IndexMappingBuffer buffer =
                    IndexMappingUtils.tryGetPositiveWithoutDiffStates(IndexMappings.createPortForSimpleTensor(from, currentField, allowDiffStates));
            if (buffer == null)
                continue;

            assert from.getArgs().length == currentField.getArgs().length;

            Tensor[] fromArgs = from.getArgs(), currentArgs = currentField.getArgs();
            Indices[] fromIndices = from.getArgIndices(), currentIndices = currentField.getArgIndices();

            List<Transformation> transformations = new ArrayList<>();
            Tensor fArg;
            int[] cIndices, fIndices;
            int i, j;
            for (i = 0; i < fromArgs.length; ++i) {
                if (IndexMappings.mappingExists(currentArgs[i], fromArgs[i], allowDiffStates))
                    continue;
                fArg = fromArgs[i].clone();
                fIndices = fromIndices[i].getAllIndices().copy();
                cIndices = currentIndices[i].getAllIndices().copy();
                if (cIndices.length != fIndices.length)
                    throw new InconsistentSubstitutionException(from, to, current);
                boolean diffStates = false;
                for (j = 0; j < cIndices.length; ++j)
                    if (IndicesUtils.getRawStateInt(cIndices[j]) != IndicesUtils.getRawStateInt(fIndices[j])) {
                        diffStates = true;
                        break;
                    }
                if (diffStates && !allowDiffStates)
                    continue OUT;

                if (diffStates) {
                    fArg = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(fArg, fIndices, cIndices, new int[0]);
                    fArg = Transformations.contractMetrics(fArg);
                    fromArgs[i] = Transformations.contractMetrics(fromArgs[i]);
                } else {
                    for (j = 0; j < cIndices.length; ++j) {
                        cIndices[j] = IndicesUtils.getNameWithType(cIndices[j]);
                        fIndices[j] = IndicesUtils.getNameWithType(fIndices[j]);
                    }
                    IndexMappingImpl im = new IndexMappingImpl(new int[0], fIndices, cIndices);
                    fArg = ApplyIndexMappingTransformation.INSTANCE.perform(fArg, im);
                }

                if (!IndexMappings.mappingExists(fromArgs[i], fArg, allowDiffStates))
                    throw new InconsistentSubstitutionException(from, to, current);
                transformations.add(Substitutions.createSubstitution(fArg, currentArgs[i], allowDiffStates));
            }

            Tensor newTo = to.clone();
            if (!allowDiffStates || !IndexMappingUtils.containsDiffStates(buffer))
                newTo = ApplyIndexMappingTransformation.INSTANCE.perform(newTo, new IndexMappingImpl(iterator.usedIndices(), buffer));
            else
                newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, iterator.usedIndices());


            if (!transformations.isEmpty()) {
                for (Transformation transformation : transformations)
                    newTo = transformation.transform(newTo);
                int[] indices = newTo.getIndices().getFreeIndices().getAllIndices().copy();
                for (i = 0; i < indices.length; ++i)
                    indices[i] = IndicesUtils.getNameWithType(indices[i]);
                newTo = ApplyIndexMappingTransformation.INSTANCE.perform(newTo, new IndexMappingImpl(iterator.usedIndices(), indices, indices));
            }
            iterator.set(newTo);
        }
        Tensor result = tensorWrapper.getInnerTensor();
        result.setParent(parent);
        return result;
    }
View Full Code Here


    public Tensor transform(Tensor tensor, boolean tensorFirst) {
        //TODO review
        //Remembing parent
        Tensor parent = tensor.getParent();
        TensorWrapper wrapper = new TensorWrapper(tensor);
        TensorTreeIterator it = TensorTreeIteratorFactory.create(tensorFirst, wrapper);
        Tensor current, old;
        while (it.hasNext()) {
            old = current = it.next();
            for (Transformation transformation : transformations)
                if ((current = transformation.transform(current)) == null)
                    break;
            if (current == null)
                if (AbstractTensorWrapper.onInnerTensorIndicator.is(it))
                    it.set(TensorNumber.createZERO());
                else
                    it.remove();
            else if (old != current)
                it.set(current);
        }
        Tensor result = wrapper.getInnerTensor();
        result.setParent(parent);
        return result;
    }
View Full Code Here

    @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;
    }
View Full Code Here

    @Override
    public Tensor transform(Tensor tensor) {
        //TODO review
        Tensor parent = tensor.getParent();
        TensorWrapper wrapper = new TensorWrapper(tensor);
        TensorFirstTreeIterator iterator = new TensorFirstTreeIterator(wrapper, new OnLeaving());
        Tensor current;
        while (iterator.hasNext()) {
            current = iterator.next();

            level++;
            if (indicesLevel == -1 && current instanceof Product) {
                firstProduct = current;
                indicesLevel = level;
            }

            if (current.getClass() != getFromClasss())
                continue;
            T _current = (T) current;
            if (_current.getName() != from.getName())
                continue;
            OutputPortUnsafe<IndexMappingBuffer> opu = IndexMappings.createPortForSimpleTensor(from, _current, allowDiffStates);


            IndexMappingBuffer buffer;
            //TODO refactor throw off if (i.e. spread this code in inheritors)
            if (allowDiffStates)
                buffer = IndexMappingUtils.tryGetPositiveWithoutDiffStates(opu);
            else
                buffer = IndexMappingUtils.tryGetPositive(opu);

            if (buffer == null)
                continue;

            Tensor newTo = getNewTo(_current, from, to);
            if (newTo == null)
                continue;


            if (allowDiffStates)
                if (!IndexMappingUtils.containsDiffStates(buffer))
                    newTo = ApplyIndexMappingUtils.applyIndexMappingWithoutDiffStates(newTo, buffer, getUsedIndices());
                else
                    newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, getUsedIndices());
            else
                newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, getUsedIndices());

            //TODO discuss with Dmitry
            if (buffer.getSignum())
                if (Derivative.onVarsIndicator.is(iterator)) {
                    derivativeVarChangedSignum ^= true;
                    iterator.set(Transformations.contractMetrics(newTo));
                } else
                    iterator.set(new Product(TensorNumber.createMINUSONE(), newTo));
            else if (Derivative.onVarsIndicator.is(iterator))
                iterator.set(Transformations.contractMetrics(newTo));
            else
                iterator.set(newTo);
        }
        Tensor result = wrapper.getInnerTensor();
        result.setParent(parent);
        return result;
    }
View Full Code Here

    }

    @Override
    public Tensor transform(Tensor tensor) {
        Tensor parent = tensor.getParent();
        TensorWrapper tensorWrapper = new TensorWrapper(tensor);
        TreeIteratorSubs iterator = new TreeIteratorSubs(tensorWrapper);
        TraverseState state;
        Tensor current;
        OUT:
        while ((state = iterator.next()) != null) {
            if (state != TraverseState.Leaving)
                continue;
            current = iterator.tensor();
            if (!(current instanceof TensorField))
                continue;
            TensorField currentField = (TensorField) current;
            IndexMappingBuffer buffer =
                    IndexMappingUtils.tryGetPositiveWithoutDiffStates(IndexMappings.createPortForSimpleTensor(from, currentField, allowDiffStates));
            if (buffer == null)
                continue;

            assert from.getArgs().length == currentField.getArgs().length;

            Tensor[] fromArgs = from.getArgs(), currentArgs = currentField.getArgs();
            Indices[] fromIndices = from.getArgIndices(), currentIndices = currentField.getArgIndices();

            List<Transformation> transformations = new ArrayList<>();
            Tensor fArg;
            int[] cIndices, fIndices;
            int i, j;
            for (i = 0; i < fromArgs.length; ++i) {
                if (IndexMappings.mappingExists(currentArgs[i], fromArgs[i], allowDiffStates))
                    continue;
                fArg = fromArgs[i].clone();
                fIndices = fromIndices[i].getAllIndices().copy();
                cIndices = currentIndices[i].getAllIndices().copy();
                if (cIndices.length != fIndices.length)
                    throw new InconsistentSubstitutionException(from, to, current);
                boolean diffStates = false;
                for (j = 0; j < cIndices.length; ++j)
                    if (IndicesUtils.getRawStateInt(cIndices[j]) != IndicesUtils.getRawStateInt(fIndices[j])) {
                        diffStates = true;
                        break;
                    }
                if (diffStates && !allowDiffStates)
                    continue OUT;

                if (diffStates) {
                    fArg = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(fArg, fIndices, cIndices, new int[0]);
                    fArg = Transformations.contractMetrics(fArg);
                    fromArgs[i] = Transformations.contractMetrics(fromArgs[i]);
                } else {
                    for (j = 0; j < cIndices.length; ++j) {
                        cIndices[j] = IndicesUtils.getNameWithType(cIndices[j]);
                        fIndices[j] = IndicesUtils.getNameWithType(fIndices[j]);
                    }
                    IndexMappingImpl im = new IndexMappingImpl(new int[0], fIndices, cIndices);
                    fArg = ApplyIndexMappingTransformation.INSTANCE.perform(fArg, im);
                }

                if (!IndexMappings.mappingExists(fromArgs[i], fArg, allowDiffStates))
                    throw new InconsistentSubstitutionException(from, to, current);
                transformations.add(Substitutions.createSubstitution(fArg, currentArgs[i], allowDiffStates));
            }

            Tensor newTo = to.clone();
            if (!allowDiffStates || !IndexMappingUtils.containsDiffStates(buffer))
                newTo = ApplyIndexMappingTransformation.INSTANCE.perform(newTo, new IndexMappingImpl(iterator.usedIndices(), buffer));
            else
                newTo = ApplyIndexMappingUtils.applyIndexMappingWithDiffStates(newTo, buffer, iterator.usedIndices());


            if (!transformations.isEmpty()) {
                for (Transformation transformation : transformations)
                    newTo = transformation.transform(newTo);
                int[] indices = newTo.getIndices().getFreeIndices().getAllIndices().copy();
                for (i = 0; i < indices.length; ++i)
                    indices[i] = IndicesUtils.getNameWithType(indices[i]);
                newTo = ApplyIndexMappingTransformation.INSTANCE.perform(newTo, new IndexMappingImpl(iterator.usedIndices(), indices, indices));
            }
            iterator.set(newTo);
        }
        Tensor result = tensorWrapper.getInnerTensor();
        result.setParent(parent);
        return result;
    }
View Full Code Here

TOP

Related Classes of cc.redberry.core.tensor.TensorWrapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.