Exemplo n.º 1
0
        public override Operand Simplify()
        {
            NaturalLog simplifiedExpression = new NaturalLog();

            simplifiedExpression.LeftSuccessor = LeftSuccessor.Simplify();
            return(simplifiedExpression);
        }
Exemplo n.º 2
0
        public override Operand Simplify()
        {
            Cosine simplifiedExpression = new Cosine();

            simplifiedExpression.LeftSuccessor = LeftSuccessor.Simplify();
            return(simplifiedExpression);
        }
Exemplo n.º 3
0
        public override Operand Simplify()
        {
            // Try to simplify as much as possible.
            Division simplifiedExpression = new Division();

            simplifiedExpression.LeftSuccessor  = LeftSuccessor.Simplify();
            simplifiedExpression.RightSuccessor = RightSuccessor.Simplify();
            // When we divide by 1, we can just return
            // the numerator of the division.
            if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // If a number but not PI.
            {
                // The denominator = 1, thus we can return the numerator.
                if (Convert.ToDouble(simplifiedExpression.RightSuccessor.Data) == 1) // If the number is 1, we simplify it to just the simplification of the left branch.
                {
                    return(simplifiedExpression.LeftSuccessor.Simplify());
                }
            }
            else if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // The right was either not a number or PI.
            {                                                                                                      // We ensured that the left is not PI
                // Check if the numerator is 0
                // if so we can return a number 0.
                if (Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data) == 0.0)
                {
                    return(new RealNumber(0.0));
                }
            }
            return(simplifiedExpression);
        }
Exemplo n.º 4
0
        public override Operand Simplify()
        {
            Addition simplifiedExpression = new Addition();

            simplifiedExpression.LeftSuccessor  = LeftSuccessor.Simplify();
            simplifiedExpression.RightSuccessor = RightSuccessor.Simplify();
            if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // Must not be PI or crash.
            {
                // If the right is also a number, we can simplify the two by calculating the result.
                // and return a new operand object holding the result as data up to the previous call.
                if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Must not be PI or crash, or loss of info if we simplify perhaps.
                {
                    // These will always be non PI numbers either int or real.
                    double addition = Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data) + Convert.ToDouble(simplifiedExpression.RightSuccessor.Data);
                    return(new RealNumber(addition));
                }
                else if (Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data) == 0.0) // This left is never PI, if it is 0 we can simplify right safely.
                {
                    return(simplifiedExpression.RightSuccessor.Simplify());
                }
            }
            else if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Right must not be PI or it'll crash.
            {
                if (Convert.ToDouble(simplifiedExpression.RightSuccessor.Data) == 0)
                {
                    return(simplifiedExpression.LeftSuccessor.Simplify());
                }
            }
            return(simplifiedExpression);
        }
Exemplo n.º 5
0
        public override Operand Simplify()
        {
            Subtraction simplifiedExpression = new Subtraction();

            simplifiedExpression.LeftSuccessor  = LeftSuccessor.Simplify();
            simplifiedExpression.RightSuccessor = RightSuccessor.Simplify();
            if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // Left is not PI and a number.
            {
                // In this scenario the right sub tree can be an expression still.
                if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Right not PI and a number.
                {
                    // We have 2 numbers so we can safely calculate.
                    double difference = Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data) - Convert.ToDouble(simplifiedExpression.RightSuccessor.Data);
                    return(new RealNumber(difference));
                }
                // Left can be 0 and that means the right is negative and we leave the expression tree as 0 - right expression
                // to not have to worry about unary or binary operator of subtraction.
            }
            else if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Left was either an expression or it is PI.
            {                                                                                                        // Thus we have to ensure that the Right is not PI or crash.
                // This case means that left is not a number. And we thus should be
                // able to return the negative of the right expression.
                if (Convert.ToDouble(simplifiedExpression.RightSuccessor.Data) == 0.0)
                {
                    return(simplifiedExpression.LeftSuccessor.Simplify());
                }
            }
            return(simplifiedExpression);
        }
Exemplo n.º 6
0
 public override Operand Simplify()
 {
     if (!(RightSuccessor is PI))                          // Ensure the power is not PI or it would crash.
     {
         if (Convert.ToDouble(RightSuccessor.Data) == 1.0) // If it's 1 we return the simplified version of left.
         {
             return(LeftSuccessor.Simplify());
         }
         return(Copy()); // Otherwise just the copy.
     }
     else // All other cases, just return the copy.
     {
         return(Copy());
     }
 }
Exemplo n.º 7
0
        public override Operand Simplify()
        {
            Multiplication simplifiedExpression = (Multiplication)Copy();

            simplifiedExpression.LeftSuccessor  = LeftSuccessor.Simplify();
            simplifiedExpression.RightSuccessor = RightSuccessor.Simplify();
            // If either of the operands equals 0 or 1 then we want to simplify specifically.
            if (simplifiedExpression.RightSuccessor is Integer && !(simplifiedExpression.RightSuccessor is PI)) // Right is a number but not PI.
            {
                // If Left is also an integer we can create a new real number operand for the result and return it up.
                if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // As long as the left is not PI or crash.
                {
                    double result = Convert.ToDouble(simplifiedExpression.RightSuccessor.Data) * Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data);
                    return(new RealNumber(result));
                }

                // If both are numbers we could multiply and return the result.
                // But this might be iffy with double values and rounding errors?
                switch (Convert.ToDouble(simplifiedExpression.RightSuccessor.Data)) // Safe as Right is not PI in this case.
                {
                case 0.0:                                                           // Multiply by zero means return 0.
                    return(new Integer(0.0));

                case 1.0:     // Multiply by one means return the other part of the expression.
                    return(simplifiedExpression.LeftSuccessor.Simplify());
                }
            }
            else if (simplifiedExpression.LeftSuccessor is Integer && !(simplifiedExpression.LeftSuccessor is PI)) // This means the simplified right expression was not a number.
            {                                                                                                      // Since the Right is not a number or it was PI.
                switch (Convert.ToDouble(simplifiedExpression.LeftSuccessor.Data))                                 // The left is not PI so we can convert the data to a number
                {
                case 0.0:
                    return(new Integer(0.0));

                case 1.0:
                    return(simplifiedExpression.RightSuccessor.Simplify());
                }
            }
            return(simplifiedExpression);
        }