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); }
public override Function Simplify() { if (RightOperand.Simplify() is NaturalNumber n2 && n2.Evaluate(0) == 0) { return(LeftOperand.Simplify()); } if (LeftOperand.Simplify() is NaturalNumber n1 && n1.Evaluate(0) == 0) { return(new Negation(RightOperand.Simplify())); } return(new Subtraction(LeftOperand.Simplify(), RightOperand.Simplify())); }
public override Function Simplify() { if (LeftOperand.Simplify() is NaturalNumber n1 && n1.Evaluate(0) == 0 || RightOperand.Simplify() is NaturalNumber n2 && n2.Evaluate(0) == 0) { return(new NaturalNumber(0)); } if (LeftOperand.Simplify() is NaturalNumber n3 && n3.Evaluate(0) == 1) { return(RightOperand.Simplify()); } if (RightOperand.Simplify() is NaturalNumber n4 && n4.Evaluate(0) == 1) { return(LeftOperand.Simplify()); } return(new Multiplication(LeftOperand.Simplify(), RightOperand.Simplify())); }