Example #1
0
        /// <summary>
        /// Multiplies a tensor by scalars.
        /// </summary>
        /// <param name="node">The <see cref="MultiplicationOperNode"/> to simplify.</param>
        /// <param name="simplifier">The <see cref="Simplifier"/> calling.</param>
        /// <returns>The resuling <see cref="MultiplicationOperNode"/>.</returns>
        public static MultiplicationOperNode MultiplyScalarTensor(MultiplicationOperNode node, Simplifier simplifier)
        {
            TensorNode tensor1 = null;
            int        ti      = -1;

            for (int i = 0; i < node.ChildCount; i++)
            {
                if (i == ti)
                {
                    continue;
                }

                if (node.GetChild(i) is TensorNode tensor2)
                {
                    if (tensor1 != null)
                    {
                        if (i < ti)
                        {
                            TensorNode swap = tensor1;
                            tensor1 = tensor2;
                            tensor2 = swap;
                        }

                        if (!tensor1.CanMatrixMultiply(tensor2))
                        {
                            return((MultiplicationOperNode)simplifier.HandleError(new CannotMultiplyTensors(simplifier, node, $"Tensor nodes of size {tensor1.SizeIdentity} and {tensor2.SizeIdentity} could not be multiplied.")));
                        }
                    }
                    else
                    {
                        tensor1 = tensor2;
                        ti      = i;
                        i       = -1;
                    }
                }
                else
                {
                    if (tensor1 != null)
                    {
                        for (int j = 0; j < tensor1.ChildCount; j++)
                        {
                            ExpNode simpleChild = QuickOpers.Multiply(tensor1.GetChild(j), node.GetChild(i)).Execute(simplifier);
                            tensor1.ReplaceChild(simpleChild, j);
                        }

                        node.RemoveChild(i);
                        if (i < ti)
                        {
                            ti--;
                        }
                        i--;
                    }
                }
            }

            return(node);
        }
Example #2
0
        /// <inheritdoc/>
        public override ExpNode Execute(TensorNode node)
        {
            for (int i = 0; i < node.ChildCount; i++)
            {
                ExpNode simpleChild = node.GetChild(i).Execute(this);
                node.ReplaceChild(simpleChild, i);
            }

            return(node);
        }
Example #3
0
        /// <inheritdoc/>
        public override string Print(TensorNode node)
        {
            string cache = string.Empty;

            switch (node.TensorType)
            {
            case TensorType.Vector:
            {
                // Print each child seperated by ',' wrapped in "<>";
                cache += "<";
                for (int i = 0; i < node.ChildCount; i++)
                {
                    cache += node.GetChild(i).Print(this);
                    if (i < node.ChildCount - 1)
                    {
                        cache += ",";
                    }
                }
                cache += ">";
                return(cache);
            }

            case TensorType.Matrix:
            {
                cache = $"\\matrix{node.SizeIdentity}{{";
                for (int i = 0; i < node.ChildCount; i++)
                {
                    cache += node.GetChild(i).Print(this);
                    if (i < node.ChildCount - 1)
                    {
                        cache += ",";
                    }
                }
                cache += "}";
                return(cache);
            }

            default:
                // TODO: Print matricies and tensors
                return(string.Empty);
            }
        }