Ejemplo n.º 1
0
        public override IExpression Simplify()
        {
            IExpression newLeft  = left.Simplify();
            IExpression newRight = right.Simplify();

            ConstantExpression leftConst   = newLeft as ConstantExpression;
            ConstantExpression rightConst  = newRight as ConstantExpression;
            NegateExpression   leftNegate  = newLeft as NegateExpression;
            NegateExpression   rightNegate = newRight as NegateExpression;

            if (leftConst != null && rightConst != null)
            {
                // two constants;  just evaluate it;
                if (rightConst.Value == 0)
                {
                    throw new InvalidExpressionException("Divide by zero detected in function");
                }
                return(new ConstantExpression(leftConst.Value / rightConst.Value));
            }
            else if (leftConst != null && leftConst.Value == 0)
            {
                // 0 / y;  return 0;
                if (rightConst != null && rightConst.Value == 0)
                {
                    throw new InvalidExpressionException("Divide by zero detected in function");
                }
                return(new ConstantExpression(0));
            }
            else if (rightConst != null)
            {
                if (rightConst.Value == 0)
                {
                    // x / 0;
                    throw new InvalidExpressionException("Divide by zero detected in function");
                }
                if (rightConst.Value == 1)
                {
                    // x / 1;  return x;
                    return(newLeft);
                }
                if (rightConst.Value == -1)
                {
                    // x / -1;  return -x;
                    if (leftNegate != null)
                    {
                        // x = -u (-x = --u);  return u;
                        return(leftNegate.Child);
                    }
                    return(new NegateExpression(newLeft));
                }
            }
            else if (leftNegate != null && rightNegate != null)
            {
                // -x / -y;  return x / y;
                return(new DivExpression(leftNegate.Child, rightNegate.Child));
            }
            // x / y;  no simplification
            return(new DivExpression(newLeft, newRight));
        }
Ejemplo n.º 2
0
        public override IExpression Simplify()
        {
            IExpression newLeft  = left.Simplify();
            IExpression newRight = right.Simplify();

            ConstantExpression leftConst   = newLeft as ConstantExpression;
            ConstantExpression rightConst  = newRight as ConstantExpression;
            NegateExpression   leftNegate  = newLeft as NegateExpression;
            NegateExpression   rightNegate = newRight as NegateExpression;

            if (leftConst != null && rightConst != null)
            {
                // two constants;  just evaluate it;
                return(new ConstantExpression(leftConst.Value + rightConst.Value));
            }
            else if (leftConst != null && leftConst.Value == 0)
            {
                // 0 + y;  return y;
                return(newRight);
            }
            else if (rightConst != null && rightConst.Value == 0)
            {
                // x + 0;  return x;
                return(newLeft);
            }
            else if (rightNegate != null)
            {
                // x + -y;  return x - y;  (this covers -x + -y case too)
                return(new SubExpression(newLeft, rightNegate.Child));
            }
            else if (leftNegate != null)
            {
                // -x + y;  return y - x;
                return(new SubExpression(newRight, leftNegate.Child));
            }
            // x + y;  no simplification
            return(new AddExpression(newLeft, newRight));
        }
Ejemplo n.º 3
0
        public override IExpression Simplify()
        {
            IExpression newLeft  = left.Simplify();
            IExpression newRight = right.Simplify();

            ConstantExpression leftConst   = newLeft as ConstantExpression;
            ConstantExpression rightConst  = newRight as ConstantExpression;
            NegateExpression   rightNegate = newRight as NegateExpression;

            if (leftConst != null && rightConst != null)
            {
                // two constants;  just evaluate it;
                return(new ConstantExpression(leftConst.Value - rightConst.Value));
            }
            else if (leftConst != null && leftConst.Value == 0)
            {
                // 0 - y;  return -y;
                if (rightNegate != null)
                {
                    // y = -u (--u);  return u;
                    return(rightNegate.Child);
                }
                return(new NegateExpression(newRight));
            }
            else if (rightConst != null && rightConst.Value == 0)
            {
                // x - 0;  return x;
                return(newLeft);
            }
            else if (rightNegate != null)
            {
                // x - -y;  return x + y;
                return(new AddExpression(newLeft, rightNegate.Child));
            }
            // x - y;  no simplification
            return(new SubExpression(newLeft, newRight));
        }
Ejemplo n.º 4
0
        public override IExpression Simplify()
        {
            IExpression newLeft  = left.Simplify();
            IExpression newRight = right.Simplify();

            ConstantExpression leftConst   = newLeft as ConstantExpression;
            ConstantExpression rightConst  = newRight as ConstantExpression;
            NegateExpression   leftNegate  = newLeft as NegateExpression;
            NegateExpression   rightNegate = newRight as NegateExpression;

            if (leftConst != null && rightConst != null)
            {
                // two constants;  just evaluate it;
                return(new ConstantExpression(leftConst.Value * rightConst.Value));
            }
            else if (leftConst != null)
            {
                if (leftConst.Value == 0)
                {
                    // 0 * y;  return 0;
                    return(new ConstantExpression(0));
                }
                if (leftConst.Value == 1)
                {
                    // 1 * y;  return y;
                    return(newRight);
                }
                if (leftConst.Value == -1)
                {
                    // -1 * y;  return -y
                    if (rightNegate != null)
                    {
                        // y = -u (-y = --u);  return u;
                        return(rightNegate.Child);
                    }
                    return(new NegateExpression(newRight));
                }
            }
            else if (rightConst != null)
            {
                if (rightConst.Value == 0)
                {
                    // x * 0;  return 0;
                    return(new ConstantExpression(0));
                }
                if (rightConst.Value == 1)
                {
                    // x * 1;  return x;
                    return(newLeft);
                }
                if (rightConst.Value == -1)
                {
                    // x * -1;  return -x;
                    if (leftNegate != null)
                    {
                        // x = -u (-x = --u);  return u;
                        return(leftNegate.Child);
                    }
                    return(new NegateExpression(newLeft));
                }
            }
            else if (leftNegate != null && rightNegate != null)
            {
                // -x * -y;  return x * y;
                return(new MultExpression(leftNegate.Child, rightNegate.Child));
            }
            // x * y;  no simplification
            return(new MultExpression(newLeft, newRight));
        }