示例#1
0
        public override Node Simplify()
        {
            Node simplifiedLeftNode  = leftNode.Simplify();
            Node simplifiedRightNode = rightNode.Simplify();

            if (simplifiedLeftNode is NumberNode && simplifiedRightNode is NumberNode &&
                (!(simplifiedLeftNode is IrrationalNumberNode) && !(simplifiedRightNode is IrrationalNumberNode)))    //5 - 3 = 2
            {
                double valueA = 0;
                double valueB = 0;
                if (simplifiedLeftNode is RationalNumberNode)
                {
                    valueA = ((RationalNumberNode)simplifiedLeftNode).Value;
                }
                if (simplifiedLeftNode is NaturalNumberNode)
                {
                    valueA = ((NaturalNumberNode)simplifiedLeftNode).Value;
                }
                if (simplifiedRightNode is RationalNumberNode)
                {
                    valueB = ((RationalNumberNode)simplifiedRightNode).Value;
                }
                if (simplifiedRightNode is NaturalNumberNode)
                {
                    valueB = ((NaturalNumberNode)simplifiedRightNode).Value;
                }
                return(new RationalNumberNode(valueA - valueB));
            }
            else if (simplifiedRightNode is NumberNode && simplifiedRightNode.Calculate(0) == 0)
            {
                return(simplifiedLeftNode);                                                     //5 - 0 = 5
            }
            else if (simplifiedLeftNode is VariableNode && simplifiedRightNode is VariableNode) //x - x = 0
            {
                return(new NaturalNumberNode(0));
            }
            else if ((simplifiedLeftNode is VariableNode || simplifiedLeftNode is IrrationalNumberNode) && simplifiedRightNode is MultiplicationSign) //x - 5x = -4x, p - 5p  = -4p
            {
                MultiplicationSign simplifiedMultiplicationRightNode = (MultiplicationSign)simplifiedRightNode;
                double             valueA = 0;
                if (simplifiedMultiplicationRightNode.ReturnRightNode() is VariableNode || simplifiedMultiplicationRightNode.ReturnRightNode() is IrrationalNumberNode)
                {
                    if (simplifiedMultiplicationRightNode.ReturnRightNode().GetType() == simplifiedLeftNode.GetType())
                    {
                        if (simplifiedMultiplicationRightNode.ReturnLeftNode() is RationalNumberNode)
                        {
                            valueA = ((RationalNumberNode)simplifiedMultiplicationRightNode.ReturnLeftNode()).Value;
                        }
                        if (simplifiedMultiplicationRightNode.ReturnLeftNode() is NaturalNumberNode)
                        {
                            valueA = ((NaturalNumberNode)simplifiedMultiplicationRightNode.ReturnLeftNode()).Value;
                        }
                        return(new MultiplicationSign(new RationalNumberNode(1 - valueA), simplifiedMultiplicationRightNode.ReturnRightNode()));
                    }
                    return(new MinusSign(simplifiedLeftNode, simplifiedRightNode));
                }
                else
                {
                    if (simplifiedMultiplicationRightNode.ReturnLeftNode().GetType() == simplifiedLeftNode.GetType())
                    {
                        if (simplifiedMultiplicationRightNode.ReturnRightNode() is RationalNumberNode)
                        {
                            valueA = ((RationalNumberNode)simplifiedMultiplicationRightNode.ReturnRightNode()).Value;
                        }
                        if (simplifiedMultiplicationRightNode.ReturnRightNode() is NaturalNumberNode)
                        {
                            valueA = ((NaturalNumberNode)simplifiedMultiplicationRightNode.ReturnRightNode()).Value;
                        }
                        return(new MultiplicationSign(new RationalNumberNode(1 - valueA), simplifiedMultiplicationRightNode.ReturnLeftNode()));
                    }
                    return(new MinusSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else if (simplifiedLeftNode is MultiplicationSign && (simplifiedRightNode is VariableNode || simplifiedRightNode is IrrationalNumberNode)) //5x - x = 4x, 5p - p = 4p
            {
                MultiplicationSign simplifiedMultiplicationLeftNode = (MultiplicationSign)simplifiedLeftNode;
                double             valueA = 0;
                if (simplifiedMultiplicationLeftNode.ReturnRightNode() is VariableNode || simplifiedMultiplicationLeftNode.ReturnRightNode() is IrrationalNumberNode)
                {
                    if (simplifiedMultiplicationLeftNode.ReturnRightNode().GetType() == simplifiedRightNode.GetType())
                    {
                        if (simplifiedMultiplicationLeftNode.ReturnLeftNode() is RationalNumberNode)
                        {
                            valueA = ((RationalNumberNode)simplifiedMultiplicationLeftNode.ReturnLeftNode()).Value;
                        }
                        if (simplifiedMultiplicationLeftNode.ReturnLeftNode() is NaturalNumberNode)
                        {
                            valueA = ((NaturalNumberNode)simplifiedMultiplicationLeftNode.ReturnLeftNode()).Value;
                        }
                        return(new MultiplicationSign(new RationalNumberNode(valueA - 1), simplifiedMultiplicationLeftNode.ReturnRightNode()));
                    }
                    return(new MinusSign(simplifiedLeftNode, simplifiedRightNode));
                }
                else
                {
                    if (simplifiedMultiplicationLeftNode.ReturnLeftNode().GetType() == simplifiedRightNode.GetType())
                    {
                        if (simplifiedMultiplicationLeftNode.ReturnRightNode() is RationalNumberNode)
                        {
                            valueA = ((RationalNumberNode)simplifiedMultiplicationLeftNode.ReturnRightNode()).Value;
                        }
                        if (simplifiedMultiplicationLeftNode.ReturnRightNode() is NaturalNumberNode)
                        {
                            valueA = ((NaturalNumberNode)simplifiedMultiplicationLeftNode.ReturnRightNode()).Value;
                        }
                        return(new MultiplicationSign(new RationalNumberNode(valueA - 1), simplifiedMultiplicationLeftNode.ReturnLeftNode()));
                    }
                    return(new MinusSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else if (simplifiedLeftNode is IrrationalNumberNode && simplifiedRightNode is IrrationalNumberNode &&
                     simplifiedLeftNode.Calculate(0) == simplifiedRightNode.Calculate(0)) //p - p = 0
            {
                return(new NaturalNumberNode(0));
            }
            else if (simplifiedLeftNode is LogarithmFunction && simplifiedRightNode is LogarithmFunction) //ln(10) - ln(5) = ln(2)
            {
                return(new LogarithmFunction(new DivisionSign(((Operation)simplifiedLeftNode).ReturnChild().Simplify(),
                                                              ((Operation)simplifiedRightNode).ReturnChild().Simplify())).Simplify());
            }
            else if (simplifiedLeftNode is MultiplicationSign && simplifiedRightNode is MultiplicationSign && //5x - 3x = 2x, 5p - 3p = 2p
                     ((((Operator)simplifiedLeftNode).ReturnRightNode() is VariableNode && ((Operator)simplifiedRightNode).ReturnRightNode() is VariableNode) ||
                      (((Operator)simplifiedLeftNode).ReturnRightNode() is PiNode && ((Operator)simplifiedRightNode).ReturnRightNode() is PiNode)))
            {
                if (((Operator)simplifiedLeftNode).ReturnLeftNode() is NumberNode &&
                    Math.Abs(((Operator)simplifiedLeftNode).ReturnLeftNode().Calculate(1) % 1) <= (Double.Epsilon * 100) &&
                    ((Operator)simplifiedRightNode).ReturnLeftNode() is NumberNode &&
                    Math.Abs(((Operator)simplifiedRightNode).ReturnLeftNode().Calculate(1) % 1) <= (Double.Epsilon * 100))
                {
                    return(new MultiplicationSign(new NaturalNumberNode(
                                                      Convert.ToInt32(((Operator)simplifiedLeftNode).ReturnLeftNode().Calculate(1) - ((Operator)simplifiedRightNode).ReturnLeftNode().Calculate(1))),
                                                  ((Operator)simplifiedLeftNode).ReturnRightNode()).Simplify());
                }
                else
                {
                    return(new MinusSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else
            {
                return(new MinusSign(simplifiedLeftNode, simplifiedRightNode));
            }
        }
示例#2
0
        public override Node Simplify()
        {
            Node simplifiedLeftNode  = leftNode.Simplify();
            Node simplifiedRightNode = rightNode.Simplify();

            if (simplifiedLeftNode is NaturalNumberNode && simplifiedLeftNode.Calculate(0) == 1)
            {
                return(simplifiedRightNode);                                                                                 //1 * 5 = 5
            }
            else if (simplifiedRightNode is NaturalNumberNode && simplifiedRightNode.Calculate(0) == 1)
            {
                return(simplifiedLeftNode);                                                                                        //5 * 1 = 5
            }
            else if (simplifiedLeftNode is NaturalNumberNode && simplifiedLeftNode.Calculate(0) == 0 ||
                     simplifiedRightNode is NaturalNumberNode && simplifiedRightNode.Calculate(0) == 0)
            {
                return(new NaturalNumberNode(0));                                                                                      //0 * 5 = 0, 5 * 0 = 0
            }
            else if ((simplifiedLeftNode is TangentFunction && simplifiedRightNode is CotangentFunction) ||
                     (simplifiedLeftNode is CotangentFunction && simplifiedRightNode is TangentFunction)) // tan(x) * cotg(x) = 1
            {
                double childValueA = ((Operation)simplifiedLeftNode).ReturnChild().Calculate(5);
                double childValueB = ((Operation)simplifiedRightNode).ReturnChild().Calculate(5);
                if (childValueA == childValueB)
                {
                    return(new NaturalNumberNode(1));
                }
                return(new MultiplicationSign(simplifiedLeftNode, simplifiedRightNode));
            }
            else if ((simplifiedLeftNode is TangentFunction && simplifiedRightNode is CosineFunction) ||
                     (simplifiedLeftNode is CosineFunction && simplifiedRightNode is TangentFunction) &&
                     ((Operation)simplifiedLeftNode).ReturnChild().Calculate(5) == ((Operation)simplifiedRightNode).ReturnChild().Calculate(5)) //tan(x) * cos(x) = sin(x)
            {
                Node child = ((Operation)simplifiedLeftNode).ReturnChild();
                return(new SineFunction(child));
            }
            else if ((simplifiedLeftNode is CotangentFunction && simplifiedRightNode is SineFunction) ||
                     (simplifiedLeftNode is SineFunction && simplifiedRightNode is CotangentFunction) &&
                     ((Operation)simplifiedLeftNode).ReturnChild().Calculate(5) == ((Operation)simplifiedRightNode).ReturnChild().Calculate(5)) //cotg(x) * sin(x) = cos(x)
            {
                Node child = ((Operation)simplifiedLeftNode).ReturnChild();
                return(new CosineFunction(child));
            }
            else if (simplifiedLeftNode is NumberNode && simplifiedRightNode is NumberNode &&
                     (!(simplifiedLeftNode is IrrationalNumberNode) && !(simplifiedRightNode is IrrationalNumberNode))) //5 * 2 = 10
            {
                double valueA = 0;
                double valueB = 0;
                if (simplifiedLeftNode is RationalNumberNode)
                {
                    valueA = ((RationalNumberNode)simplifiedLeftNode).Value;
                }
                if (simplifiedLeftNode is NaturalNumberNode)
                {
                    valueA = ((NaturalNumberNode)simplifiedLeftNode).Value;
                }
                if (simplifiedRightNode is RationalNumberNode)
                {
                    valueB = ((RationalNumberNode)simplifiedRightNode).Value;
                }
                if (simplifiedRightNode is NaturalNumberNode)
                {
                    valueB = ((NaturalNumberNode)simplifiedRightNode).Value;
                }
                return(new RationalNumberNode(valueA * valueB));
            }
            else if (simplifiedLeftNode is ExponentialFunction && simplifiedRightNode is ExponentialFunction) //e^3 * e^2 = e^5
            {
                return(new ExponentialFunction(new PlusSign(((Operation)simplifiedLeftNode).ReturnChild().Simplify(),
                                                            ((Operation)simplifiedRightNode).ReturnChild().Simplify()).Simplify()));
            }
            else if (simplifiedLeftNode is VariableNode && simplifiedRightNode is NumberNode) //x * 5 = 5 * x
            {
                return(new MultiplicationSign(simplifiedRightNode, simplifiedLeftNode).Simplify());
            }
            else if (simplifiedLeftNode is PiNode && simplifiedRightNode is NumberNode && !(simplifiedRightNode is IrrationalNumberNode)) //p * 5 = 5 * p
            {
                return(new MultiplicationSign(simplifiedRightNode, simplifiedLeftNode).Simplify());
            }
            else if (simplifiedLeftNode is NumberNode && simplifiedRightNode is MultiplicationSign &&
                     ((MultiplicationSign)simplifiedRightNode).ReturnLeftNode().Simplify() is NaturalNumberNode) //5 * (2 * x) = 10 * x
            {
                return(new MultiplicationSign(
                           new RationalNumberNode(simplifiedLeftNode.Calculate(1) * ((MultiplicationSign)simplifiedRightNode).ReturnLeftNode().Simplify().Calculate(1)),
                           ((MultiplicationSign)simplifiedRightNode).ReturnRightNode().Simplify()));
            }
            else if (simplifiedLeftNode is MultiplicationSign && simplifiedRightNode is MultiplicationSign &&
                     ((MultiplicationSign)simplifiedRightNode).ReturnLeftNode().Simplify() is NaturalNumberNode) //(5 * x) * (2 * p) = 10 * p * x
            {
                return(new MultiplicationSign(
                           new RationalNumberNode(((MultiplicationSign)simplifiedLeftNode).ReturnLeftNode().Simplify().Calculate(1)
                                                  * ((MultiplicationSign)simplifiedRightNode).ReturnLeftNode().Simplify().Calculate(1)),
                           new MultiplicationSign(((MultiplicationSign)simplifiedLeftNode).ReturnRightNode().Simplify(),
                                                  ((MultiplicationSign)simplifiedRightNode).ReturnRightNode().Simplify())).Simplify());
            }
            else if (simplifiedLeftNode is VariableNode && simplifiedRightNode is VariableNode) //x * x = x^2
            {
                return(new CaretSign(simplifiedLeftNode, new NaturalNumberNode(2)));
            }
            else if (simplifiedLeftNode is MultiplicationSign && simplifiedRightNode is VariableNode) //(5 * x) * x = 5 * x^2
            {
                MultiplicationSign multiplicationSign = (MultiplicationSign)simplifiedLeftNode;
                if (multiplicationSign.ReturnLeftNode() is NaturalNumberNode && multiplicationSign.ReturnRightNode() is VariableNode)
                {
                    return(new MultiplicationSign(multiplicationSign.ReturnLeftNode(), new CaretSign(simplifiedRightNode, new NaturalNumberNode(2))));
                }
                else
                {
                    return(new MultiplicationSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else
            {
                return(new MultiplicationSign(simplifiedLeftNode, simplifiedRightNode));
            }
        }
示例#3
0
        public override Node Simplify()
        {
            Node simplifiedLeftNode  = leftNode.Simplify();
            Node simplifiedRightNode = rightNode.Simplify();

            if (simplifiedLeftNode is NumberNode && simplifiedLeftNode.Calculate(0) == 0)
            {
                return(simplifiedRightNode);                                                                          //0 + 5 = 5
            }
            else if (simplifiedRightNode is NumberNode && simplifiedRightNode.Calculate(0) == 0)
            {
                return(simplifiedLeftNode);                                                     //5 + 0 = 0
            }
            else if (simplifiedLeftNode is VariableNode && simplifiedRightNode is VariableNode) //x + x = 2 * x
            {
                return(new MultiplicationSign(new NaturalNumberNode(2), simplifiedLeftNode));
            }
            else if (simplifiedLeftNode is NumberNode && simplifiedRightNode is NumberNode &&
                     (!(simplifiedLeftNode is IrrationalNumberNode) && !(simplifiedRightNode is IrrationalNumberNode))) //2 + 5 = 7
            {
                double valueA = 0;
                double valueB = 0;
                if (simplifiedLeftNode is RationalNumberNode)
                {
                    valueA = ((RationalNumberNode)simplifiedLeftNode).Value;
                }
                if (simplifiedLeftNode is NaturalNumberNode)
                {
                    valueA = ((NaturalNumberNode)simplifiedLeftNode).Value;
                }
                if (simplifiedRightNode is RationalNumberNode)
                {
                    valueB = ((RationalNumberNode)simplifiedRightNode).Value;
                }
                if (simplifiedRightNode is NaturalNumberNode)
                {
                    valueB = ((NaturalNumberNode)simplifiedRightNode).Value;
                }
                return(new RationalNumberNode(valueA + valueB));
            }
            else if ((simplifiedLeftNode is VariableNode || simplifiedLeftNode is IrrationalNumberNode) &&
                     simplifiedRightNode is MultiplicationSign) //x + 4x = 5x, p + 4p = 5p
            {
                MultiplicationSign simplifiedMultiplicationRightNode = (MultiplicationSign)simplifiedRightNode;
                double             valueA = 0;
                if (simplifiedMultiplicationRightNode.ReturnRightNode() is VariableNode || simplifiedMultiplicationRightNode.ReturnRightNode() is IrrationalNumberNode)
                {
                    if (simplifiedMultiplicationRightNode.ReturnRightNode().GetType() == simplifiedLeftNode.GetType())
                    {
                        if (simplifiedMultiplicationRightNode.ReturnLeftNode() is RationalNumberNode)
                        {
                            valueA = ((RationalNumberNode)simplifiedMultiplicationRightNode.ReturnLeftNode()).Value;
                        }
                        if (simplifiedMultiplicationRightNode.ReturnLeftNode() is NaturalNumberNode)
                        {
                            valueA = ((NaturalNumberNode)simplifiedMultiplicationRightNode.ReturnLeftNode()).Value;
                        }
                        return(new MultiplicationSign(new RationalNumberNode(valueA + 1), simplifiedMultiplicationRightNode.ReturnRightNode()));
                    }
                    return(new PlusSign(simplifiedLeftNode, simplifiedRightNode));
                }
                else
                {
                    if (simplifiedMultiplicationRightNode.ReturnLeftNode().GetType() == simplifiedLeftNode.GetType())
                    {
                        if (simplifiedMultiplicationRightNode.ReturnRightNode() is RationalNumberNode)
                        {
                            valueA = ((RationalNumberNode)simplifiedMultiplicationRightNode.ReturnRightNode()).Value;
                        }
                        if (simplifiedMultiplicationRightNode.ReturnRightNode() is NaturalNumberNode)
                        {
                            valueA = ((NaturalNumberNode)simplifiedMultiplicationRightNode.ReturnRightNode()).Value;
                        }
                        return(new MultiplicationSign(new RationalNumberNode(valueA + 1), simplifiedMultiplicationRightNode.ReturnLeftNode()));
                    }
                    return(new PlusSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else if (simplifiedLeftNode is MultiplicationSign &&
                     (simplifiedRightNode is VariableNode || simplifiedRightNode is IrrationalNumberNode)) //4x + x = 5x, 4p + p = 5p
            {
                MultiplicationSign simplifiedMultiplicationLeftNode = (MultiplicationSign)simplifiedLeftNode;
                double             valueA = 0;
                if (simplifiedMultiplicationLeftNode.ReturnRightNode() is VariableNode || simplifiedMultiplicationLeftNode.ReturnRightNode() is IrrationalNumberNode)
                {
                    if (simplifiedMultiplicationLeftNode.ReturnRightNode().GetType() == simplifiedRightNode.GetType())
                    {
                        if (simplifiedMultiplicationLeftNode.ReturnLeftNode() is RationalNumberNode)
                        {
                            valueA = ((RationalNumberNode)simplifiedMultiplicationLeftNode.ReturnLeftNode()).Value;
                        }
                        if (simplifiedMultiplicationLeftNode.ReturnLeftNode() is NaturalNumberNode)
                        {
                            valueA = ((NaturalNumberNode)simplifiedMultiplicationLeftNode.ReturnLeftNode()).Value;
                        }
                        return(new MultiplicationSign(new RationalNumberNode(valueA + 1), simplifiedMultiplicationLeftNode.ReturnRightNode()));
                    }
                    return(new PlusSign(simplifiedLeftNode, simplifiedRightNode));
                }
                else
                {
                    if (simplifiedMultiplicationLeftNode.ReturnLeftNode().GetType() == simplifiedRightNode.GetType())
                    {
                        if (simplifiedMultiplicationLeftNode.ReturnRightNode() is RationalNumberNode)
                        {
                            valueA = ((RationalNumberNode)simplifiedMultiplicationLeftNode.ReturnRightNode()).Value;
                        }
                        if (simplifiedMultiplicationLeftNode.ReturnRightNode() is NaturalNumberNode)
                        {
                            valueA = ((NaturalNumberNode)simplifiedMultiplicationLeftNode.ReturnRightNode()).Value;
                        }
                        return(new MultiplicationSign(new RationalNumberNode(valueA + 1), simplifiedMultiplicationLeftNode.ReturnLeftNode()));
                    }
                    return(new PlusSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else if (simplifiedLeftNode is IrrationalNumberNode &&
                     simplifiedRightNode is IrrationalNumberNode && simplifiedLeftNode.Calculate(0) == simplifiedRightNode.Calculate(0)) //p + p = 2 * p
            {
                return(new MultiplicationSign(new NaturalNumberNode(2), simplifiedLeftNode));
            }
            else if (simplifiedLeftNode is LogarithmFunction && simplifiedRightNode is LogarithmFunction) //ln(5) + ln(2) = ln(10)
            {
                return(new LogarithmFunction(new MultiplicationSign(((Operation)simplifiedLeftNode).ReturnChild().Simplify(),
                                                                    ((Operation)simplifiedRightNode).ReturnChild().Simplify())).Simplify());
            }
            else if (simplifiedLeftNode is MultiplicationSign && simplifiedRightNode is MultiplicationSign &&
                     ((((Operator)simplifiedLeftNode).ReturnRightNode() is VariableNode && ((Operator)simplifiedRightNode).ReturnRightNode() is VariableNode) ||
                      (((Operator)simplifiedLeftNode).ReturnRightNode() is PiNode && ((Operator)simplifiedRightNode).ReturnRightNode() is PiNode))) //2x + 3x = 5x, 2p + 3p = 5p
            {
                if (((Operator)simplifiedLeftNode).ReturnLeftNode() is NumberNode &&
                    Math.Abs(((Operator)simplifiedLeftNode).ReturnLeftNode().Calculate(1) % 1) <= (Double.Epsilon * 100) &&
                    ((Operator)simplifiedRightNode).ReturnLeftNode() is NumberNode &&
                    Math.Abs(((Operator)simplifiedRightNode).ReturnLeftNode().Calculate(1) % 1) <= (Double.Epsilon * 100))
                {
                    return(new MultiplicationSign(new NaturalNumberNode(
                                                      Convert.ToInt32(((Operator)simplifiedLeftNode).ReturnLeftNode().Calculate(1) + ((Operator)simplifiedRightNode).ReturnLeftNode().Calculate(1))),
                                                  ((Operator)simplifiedLeftNode).ReturnRightNode()).Simplify());
                }
                else
                {
                    return(new PlusSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else
            {
                return(new PlusSign(simplifiedLeftNode, simplifiedRightNode));
            }
        }