Package cc.redberry.core.tensor

Examples of cc.redberry.core.tensor.Tensor


        int i;
        for (i = 0; i < equations.length; ++i) {
            Expression eq = equations[i];
            //iterating over the whole equation
            FromChildToParentIterator iterator = new FromChildToParentIterator(eq);
            Tensor t;
            while ((t = iterator.next()) != null) {
                if (!(t instanceof Product) || t.getIndices().size() == 0)
                    continue;
                //scalars content
                Tensor[] scalars = ((Product) t).getContent().getScalars();
                for (Tensor scalar : scalars) {
                    if (!tensorSubstitutions.containsKey(scalar)) {
View Full Code Here


    }

    @Override
    public Tensor toTensor() {
        SimpleTensor[] vars = new SimpleTensor[content.length - 1];
        Tensor temp;
        for (int i = 1; i < content.length; ++i) {
            temp = content[i].toTensor();
            if (!(temp instanceof SimpleTensor) && !(temp instanceof TensorField))
                throw new IllegalArgumentException("Derivative with respect to non simple argument: " + temp);
            vars[i - 1] = (SimpleTensor) temp;
View Full Code Here

     */
    public TraverseState next() {

        if (lastState == TraverseState.Leaving) {

            Tensor cur = null;
            if (currentPointer.payload != null)
                cur = currentPointer.payload.onLeaving(currentPointer);

            if (cur != null)
                current = cur;

            currentPointer = currentPointer.previous;
            currentPointer.set(current);
        }

        Tensor next;
        while (true) {
            next = currentPointer.next();
            if (next == null) {
                if (currentPointer.previous == null)
                    return lastState = null;
View Full Code Here

        this.allowDiffStates = allowDiffStates;
    }

    @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

            builder.append("F").append(i).append(" [label=\"-\" style=\"filled\" fillcolor=\"#CCFFFF\"];");
        for (i = 0; i < data.length; ++i)
            builder.append("T").append(i).append(" [label=\"[").append(i).append("]  ").append(data[i].toString(mode)).append("\"").append(seeds.contains(i) ? " style=\"filled\" fillcolor=\"#B6FA50\"" : "").append("];");
        long contraction;
        for (i = 0; i < contractions.length; ++i) {
            Tensor from = data[i];
            for (int j = 0; j < contractions[i].length; ++j) {
                contraction = contractions[i][j];
                int toIndex = FullContractionsStructure.getToTensorIndex(contraction);
                //Tensor to = null;
                //if (toIndex != -1)
                //    to = data[toIndex];
                int index = from.getIndices().get(j);
                String sIndex = CC.getIndexConverterManager().getSymbol(IndicesUtils.getNameWithType(index), ToStringMode.LaTeX);
                if (toIndex != -1 && IndicesUtils.getState(index))
                    builder.append("T").append(i).append("->T").append(toIndex).append(" [label=\"").append(sIndex).append("\"]");
                if (toIndex == -1)
                    if (IndicesUtils.getState(index))
View Full Code Here

    public Tensor transform(Tensor tensor) {
        if (!(tensor instanceof Derivative))
            return tensor;
        Derivative derivative = (Derivative) tensor;
        Tensor target = derivative.getTarget().clone();
        for (int i = 0; i < derivative.getDerivativeOrder(); ++i) {
            target = getDerivative(target, derivative.getVariation(i));
            if (target == null)
                return TensorNumber.createZERO();
        }
View Full Code Here

    private Tensor getDerivative(Tensor target, SimpleTensor var) {
        if (target instanceof Sum) {
            Sum sum = (Sum) target;
            TensorIterator it = sum.iterator();
            Tensor current, derivative;
            Sum res = new Sum();
            while (it.hasNext()) {
                current = it.next();
                derivative = getDerivative(current, var);
                if (derivative == null)
                    continue;// it.remove();
                else
                    res.add(derivative);//if (derivative != current)
                //it.set(derivative);
            }
            if (res.isEmpty())
                return null;
            return res.equivalent();
        } else if (target instanceof Product) {
            Product product = (Product) target;
            Tensor derivative;
            List<Tensor> resultProducts = new ArrayList<>();
            for (int i = 0; i < product.size(); ++i) {
                derivative = getDerivative(product.getElements().get(i), var);
                if (derivative == null)
                    continue;
                Product clone = (Product) product.clone();
                clone.getElements().remove(i);
                if (!isOne(derivative))
                    clone.add(derivative);
                resultProducts.add(clone.equivalent());
            }
            if (resultProducts.isEmpty())
                return null;
            if (resultProducts.size() == 1)
                return resultProducts.get(0);
            return new Sum(resultProducts);
        } else if (target.getClass() == SimpleTensor.class) {
            SimpleTensor sp = (SimpleTensor) target;
            if (sp.getName() != var.getName())
                return null;
            if (sp.getIndices().size() == 0)
                return TensorNumber.createONE();
            Product kroneckers = new Product();
            Indices targetIndices = sp.getIndices();
            Indices varIndices = var.getIndices();
            for (int i = 0; i < sp.getIndices().size(); ++i)
                kroneckers.add(CC.createMetricOrKronecker(targetIndices.get(i), varIndices.get(i)));
            return kroneckers.equivalent();
        } else if (target.getClass() == TensorField.class) {
            TensorField field = (TensorField) target;
            Tensor[] args = field.getArgs();
            for (int i = 0; i < args.length; ++i)
                if (getDerivative(args[i], var) != null)
                    return Derivative.createFromInversed(target,var);
            return null;
        } else if (target instanceof AbstractScalarFunction) {
            AbstractScalarFunction func = (AbstractScalarFunction) target;
            Tensor der = getDerivative(func.getInnerTensor(), var);
            if (der == null)
                return null;
            if (isOne(der))
                return func.derivative();
            return new Product(func.derivative(), der);
View Full Code Here


        //renaming indices of toCollect tensor
        new RenameContractedIndices(factorsIndicesNames.toArray()).transform(split.term, split.factoredOut);

        Tensor toCollect = split.term;
        Tensor collectedTensor = term;


        IntArrayList uncontractedIndicesToCollect = TensorUtils.getContractedIndicesNames(split.term, split.factoredOut);
        IntArrayList uncontractedIndicesCollected = TensorUtils.getContractedIndicesNames(term, factors.getElements().get(0));
View Full Code Here

    @Override
    public Tensor tensorEquvivalent() {
        if (!initialized)
            return pairProduct(factoredOut, term);
        TensorIterator it = factors.iterator();
        Tensor equivalent;
        Tensor current;
        while (it.hasNext()) {
            current = it.next();
            equivalent = current.equivalent();
            if (current != equivalent)
                it.set(equivalent);
        }
        return pairProduct(Transformations.calculateNumbers(factors), term.equivalent());
    }
View Full Code Here

        equals.put(null);
        if (!symbols.initialized())
            return equals.result();
        if (!equals.initialized())
            return symbols.result();
        Tensor s1 = symbols.result();
        Tensor s2 = equals.result();
        return new Sum(s1, s2);
    }
View Full Code Here

TOP

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

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.