public IExpressionNode Simplify()
        {
            LeftOperand  = LeftOperand.Simplify();
            RightOperand = RightOperand.Simplify();
            if (!LeftOperand.ContainsVariable() && !RightOperand.ContainsVariable())
            {
                return(new Constant(this.Evaluate(null)));
            }
            else if (!LeftOperand.ContainsVariable() && RightOperand.ContainsVariable())
            {
                if (LeftOperand.Evaluate(null) == 0)
                {
                    return(new Constant(0));
                }
            }
            else if (LeftOperand.ContainsVariable() && !RightOperand.ContainsVariable())
            {
                if (RightOperand.Evaluate(null) == 0)
                {
                    throw new DivideByZeroException();
                }

                if (RightOperand.Evaluate(null) == 1)
                {
                    return(LeftOperand.DeepCopy());
                }
            }

            return(this);
        }
Exemple #2
0
 public IExpressionNode Derivate()
 {
     if (LeftOperand.ContainsVariable() && RightOperand.ContainsVariable())
     {
         return(new OperationMultiplication(
                    this.DeepCopy(),
                    new OperationMultiplication(
                        new FunctionLn(LeftOperand.DeepCopy()),
                        RightOperand.DeepCopy()).Derivate()));
     }
     else if (LeftOperand.ContainsVariable())
     {
         return(new OperationMultiplication(
                    new OperationMultiplication(
                        RightOperand.DeepCopy(),
                        new OperationPower(
                            LeftOperand.DeepCopy(),
                            new OperationSubtraction(RightOperand.DeepCopy(), new Constant(1))
                            )
                        ),
                    LeftOperand.Derivate()
                    ));
     }
     else if (RightOperand.ContainsVariable())
     {
         return(new OperationMultiplication(
                    new OperationMultiplication(
                        this.DeepCopy(),
                        new FunctionLn(LeftOperand.DeepCopy())
                        ),
                    RightOperand.Derivate()
                    ));
     }
     else
     {
         return(new Constant(0));
     }
 }
 public bool ContainsVariable() => LeftOperand.ContainsVariable() || RightOperand.ContainsVariable();