Beispiel #1
0
        /// <inheritdoc/>
        public override ExpNode Execute(PowOperNode node)
        {
            // TODO: Handle variable in exponent
            if (node.IsConstantBy(_variable))
            {
                return(QuickOpers.MakeNumericalNode(0));
            }

            var coefficient = node.RightChild;
            var @base       = node.LeftChild;
            var exponent    = QuickOpers.Add(-1, coefficient);

            return(QuickOpers.Multiply(coefficient, QuickOpers.Pow(@base, exponent)));
        }
Beispiel #2
0
        /// <inheritdoc/>
        public override ExpNode Execute(PowOperNode node)
        {
            // TODO: Handle variable in exponent
            if (node.IsConstantBy(_variable))
            {
                return(ConstantRule(node));
            }

            // Increment exponent, divide by exponent
            AdditionOperNode   exponent    = QuickOpers.Add(1, node.RightChild);
            RecipricalOperNode coefficient = QuickOpers.Reciprical(exponent.Clone());
            PowOperNode        @base       = QuickOpers.Pow(node.LeftChild, exponent);

            return(QuickOpers.Multiply(coefficient, @base));
        }
Beispiel #3
0
        /// <inheritdoc/>
        public override ExpNode Execute(PowOperNode node)
        {
            node.LeftChild  = node.LeftChild.Execute(this);
            node.RightChild = node.RightChild.Execute(this);

            if (node.LeftChild is NumericalValueNode lnvNode && node.RightChild is NumericalValueNode rnvNode)
            {
                return(QuickOpers.MakeNumericalNode(Math.Pow(lnvNode.DoubleValue, rnvNode.DoubleValue)));
            }

            if (node.RightChild is IntValueNode ivNode)
            {
                if (ivNode.DoubleValue == 0)
                {
                    return(QuickOpers.MakeNumericalNode(1));
                }
                if (ivNode.DoubleValue == 1)
                {
                    return(node.LeftChild);
                }

                if (node.LeftChild is ValueNode)
                {
                    // No good expanding
                    return(node);
                }

                int n = ivNode.Value;
                // Expand n times
                MultiplicationOperNode mNode = new();

                mNode.AddChild(node.LeftChild);
                for (int i = 1; i < n; i++)
                {
                    mNode.AddChild(node.LeftChild.Clone());
                }

                return(mNode.Execute(this));
            }

            return(PowerHelpers.Distribute(node));
        }
Beispiel #4
0
 /// <summary>
 /// Applies the power distributive property.
 /// </summary>
 /// <param name="node">The <see cref="PowOperNode"/> to simplify.</param>
 /// <returns>The resuling <see cref="ExpNode"/>.</returns>
 public static ExpNode Distribute(PowOperNode node)
 {
     if (node.LeftChild is ParenthesisOperNode parNode)
     {
         // Child is parenthesis
         // Check for multiplication
         if (parNode.Child is MultiplicationOperNode mNode)
         {
             // Grand child is multiplication
             // Distribute
             for (int i = 0; i < mNode.ChildCount; i++)
             {
                 PowOperNode pow = QuickOpers.Pow(mNode.GetChild(i), node.RightChild.Clone());
                 mNode.ReplaceChild(pow, i);
             }
             return(mNode);
         }
     }
     return(node);
 }
Beispiel #5
0
 /// <summary>
 /// Executes operation on a <see cref="PowOperNode"/>.
 /// </summary>
 /// <param name="node">The <see cref="PowOperNode"/> to execute operation on.</param>
 /// <returns>The result of the operation on a <see cref="PowOperNode"/>.</returns>
 public virtual ExpNode Execute(PowOperNode node) => Execute((BOperNode)node);
Beispiel #6
0
 /// <inheritdoc/>
 public override string Print(PowOperNode node)
 {
     return($"{node.LeftChild.Print(this)}^{node.RightChild.Print(this)}");
 }
Beispiel #7
0
 /// <summary>
 /// Prints a <see cref="PowOperNode"/>.
 /// </summary>
 /// <param name="node">The <see cref="PowOperNode"/> to print.</param>
 /// <returns>The <see cref="PowOperNode"/> printed to a string.</returns>
 public virtual string Print(PowOperNode node) => Print((BOperNode)node);