Ejemplo n.º 1
0
        /// <summary>
        /// Evaluates the operator on scalar operands.
        /// </summary>
        /// <param name="Left">Left value.</param>
        /// <param name="Right">Right value.</param>
        /// <param name="Variables">Variables collection.</param>
        /// <returns>Result</returns>
        public override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
        {
            DoubleNumber DL = Left as DoubleNumber;
            DoubleNumber DR = Right as DoubleNumber;

            if (DL != null && DR != null)
            {
                return(new DoubleNumber(DL.Value - DR.Value));
            }
            else
            {
                return(Subtract.EvaluateSubtraction(Left, Right, this));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Differentiates a script node, if possible.
        /// </summary>
        /// <param name="VariableName">Name of variable to differentiate on.</param>
        /// <param name="Variables">Collection of variables.</param>
        /// <returns>Differentiated node.</returns>
        public ScriptNode Differentiate(string VariableName, Variables Variables)
        {
            int        Start      = this.Start;
            int        Len        = this.Length;
            Expression Expression = this.Expression;

            if (this.left is ConstantElement ConstantBase)
            {
                return(this.DifferentiationChainRule(VariableName, Variables, this.right,
                                                     new Multiply(
                                                         this,
                                                         new Ln(ConstantBase, Start, Len, Expression),
                                                         Start, Len, Expression)));
            }
            else if (this.left is IDifferentiable Left)
            {
                if (this.right is ConstantElement ConstantExp)
                {
                    if (ConstantExp.Constant.Equals(DoubleNumber.OneElement))
                    {
                        return(Left.Differentiate(VariableName, Variables));
                    }
                    else if (ConstantExp.Constant.Equals(DoubleNumber.TwoElement))
                    {
                        return(this.DifferentiationChainRule(VariableName, Variables, this.left,
                                                             new Multiply(
                                                                 ConstantExp,
                                                                 this.left,
                                                                 Start, Len, Expression)));
                    }
                    else
                    {
                        return(this.DifferentiationChainRule(VariableName, Variables, this.left,
                                                             new Multiply(
                                                                 ConstantExp,
                                                                 new Power(
                                                                     this.left,
                                                                     new ConstantElement(Subtract.EvaluateSubtraction(ConstantExp.Constant, DoubleNumber.OneElement, this), Start, Len, Expression),
                                                                     Start, Len, Expression),
                                                                 Start, Len, Expression)));
                    }
                }
                else if (this.right is IDifferentiable Right)
                {
                    return(new Multiply(
                               this,
                               new Add(
                                   new Multiply(
                                       Left.Differentiate(VariableName, Variables),
                                       new Divide(this.right, this.left, Start, Len, Expression),
                                       Start, Len, Expression),
                                   new Multiply(
                                       Right.Differentiate(VariableName, Variables),
                                       new Ln(this.left, Start, Len, Expression),
                                       Start, Len, Expression),
                                   Start, Len, Expression),
                               Start, Len, Expression));
                }
                else
                {
                    throw new ScriptRuntimeException("Operands not differentiable.", this);
                }
            }
            else
            {
                throw new ScriptRuntimeException("Operands not differentiable.", this);
            }
        }