Пример #1
0
        /// <summary>
        /// Splits an <see cref="AdditionOperNode"/> into terms and simplifies by common terms.
        /// </summary>
        /// <param name="node">The <see cref="AdditionOperNode"/> to simplify.</param>
        /// <returns>The resulting <see cref="ExpNode"/>.</returns>
        public static AdditionOperNode SimplfiyATerms(AdditionOperNode node)
        {
            SortedSet <AdditiveTerm> aTerms = new();

            for (int i = 0; i < node.ChildCount; i++)
            {
                AdditiveTerm aTerm = new(node.GetChild(i));

                if (aTerms.TryGetValue(aTerm, out AdditiveTerm existingATerm))
                {
                    existingATerm.AddToCoefficient(aTerm);
                }
                else
                {
                    aTerms.Add(aTerm);
                }
            }

            node.ClearChildren();
            foreach (var term in aTerms)
            {
                node.AddChild(term.AsExpNode());
            }

            return(node);
        }
Пример #2
0
        /// <inheritdoc/>
        public override ExpNode Execute(AdditionOperNode node)
        {
            double valueProg = 0;

            for (int i = 0; i < node.ChildCount; i++)
            {
                ExpNode simpleChild = node.GetChild(i).Execute(this);

                if (simpleChild is NumericalValueNode nvNode)
                {
                    valueProg += nvNode.DoubleValue;
                    node.RemoveChild(i);
                    i--;
                }
                else if (simpleChild is AdditionOperNode aNode)
                {
                    aNode.TransferChildren(node);
                    node.RemoveChild(i);
                    i--;
                }
                else
                {
                    node.ReplaceChild(simpleChild, i);
                }
            }

            if (node.ChildCount == 0 || valueProg != 0)
            {
                node.AddChild(QuickOpers.MakeNumericalNode(valueProg));
            }

            AdditionHelpers.SimplfiyATerms(node);

            if (node.ChildCount == 0)
            {
                return(QuickOpers.MakeNumericalNode(0));
            }
            else if (node.ChildCount == 1)
            {
                return(node.GetChild(0));
            }

            return(AdditionHelpers.SumTensors(node, this));
        }