Example #1
0
        private Node GenerateMaclaurinSeriesPolynomial(double x, int order)
        {
            if (order == 0)
            {
                return(this);
            }
            if (order == 1)
            {
                return(new PlusSign(new MultiplicationSign(ReturnDerivative(x), new VariableNode()), this));
            }
            VariableNode      variableNode = new VariableNode();
            NaturalNumberNode orderNode    = new NaturalNumberNode(order);
            CaretSign         caretSign    = new CaretSign(variableNode, orderNode);
            Node f0 = ReturnDerivative(x);

            for (int i = 1; i < order; i++)
            {
                f0 = f0.ReturnDerivative(x);
            }
            MultiplicationSign multiplicationSign = new MultiplicationSign(caretSign, new RationalNumberNode(f0.Calculate(0)));
            FactorialFunction  factorialFunction  = new FactorialFunction(orderNode);
            DivisionSign       divisionSign       = new DivisionSign(multiplicationSign, factorialFunction);

            return(new PlusSign(divisionSign, ReturnMaclaurinSeriesPolynomial(x, order - 1)));
        }
Example #2
0
        public double ReturnMaclaurinSeries(double x, int order)
        {
            if (order == 0)
            {
                return(Calculate(0));
            }
            if (order == 1)
            {
                return(ReturnDerivative(x).Calculate(0) * x + Calculate(0));
            }
            VariableNode      variableNode = new VariableNode();
            NaturalNumberNode orderNode    = new NaturalNumberNode(order);
            CaretSign         caretSign    = new CaretSign(variableNode, orderNode);
            Node f0 = ReturnDerivative(x);

            for (int i = 1; i < order; i++)
            {
                f0 = f0.ReturnDerivative(x);
            }
            MultiplicationSign multiplicationSign = new MultiplicationSign(caretSign, new RationalNumberNode(f0.Calculate(0)));
            FactorialFunction  factorialFunction  = new FactorialFunction(orderNode);
            DivisionSign       divisionSign       = new DivisionSign(multiplicationSign, factorialFunction);

            return(divisionSign.Calculate(x) + ReturnMaclaurinSeries(x, order - 1));
        }
Example #3
0
 public override Node ReturnDerivative(double x)
 {
     if (leftNode is NumberNode && rightNode is NumberNode)
     {
         return(new NaturalNumberNode(0));
     }
     else if (leftNode is VariableNode && (rightNode is NaturalNumberNode || rightNode is RationalNumberNode))
     {
         RationalNumberNode rationalNumberNodeA = new RationalNumberNode(rightNode.Calculate(x));
         RationalNumberNode rationalNumberNodeB = new RationalNumberNode(rightNode.Calculate(x) - 1);
         CaretSign          caretSign           = new CaretSign(leftNode.Copy(), rationalNumberNodeB);
         MultiplicationSign multiplicationSign  = new MultiplicationSign(rationalNumberNodeA, caretSign);
         return(multiplicationSign);
     }
     else if (leftNode is NumberNode)
     {
         LogarithmFunction  logarithmFunction   = new LogarithmFunction(leftNode.Copy());
         CaretSign          caretSign           = new CaretSign(leftNode.Copy(), rightNode.Copy());
         MultiplicationSign multiplicationSignA = new MultiplicationSign(logarithmFunction, caretSign);
         MultiplicationSign multiplicationSignB = new MultiplicationSign(multiplicationSignA, caretSign.rightNode.ReturnDerivative(x));
         return(multiplicationSignB);
     }
     else
     {
         MinusSign          minusSign           = new MinusSign(rightNode.Copy(), new NaturalNumberNode(1));
         CaretSign          caretSign           = new CaretSign(leftNode.Copy(), minusSign);
         MultiplicationSign multiplicationSignA = new MultiplicationSign(rightNode.Copy(), caretSign);
         MultiplicationSign multiplicationSignB = new MultiplicationSign(multiplicationSignA, leftNode.ReturnDerivative(x));
         return(multiplicationSignB);
     }
 }
        public override Node ReturnDerivative(double x)
        {
            MultiplicationSign multiplicationSignA = new MultiplicationSign(leftNode.ReturnDerivative(x), rightNode.Copy());
            MultiplicationSign multiplicationSignB = new MultiplicationSign(leftNode.Copy(), rightNode.ReturnDerivative(x));
            MinusSign          minusSign           = new MinusSign(multiplicationSignA, multiplicationSignB);
            NaturalNumberNode  naturalNumberNode   = new NaturalNumberNode(2);
            CaretSign          caretSign           = new CaretSign(rightNode.Copy(), naturalNumberNode);
            DivisionSign       division            = new DivisionSign(minusSign, caretSign);

            return(division);
        }
Example #5
0
        public double ReturnNewtonMaclaurinSeries(double x, int order)
        {
            if (order == 0)
            {
                return(Calculate(0));
            }
            if (order == 1)
            {
                return(ReturnNewtonDerivative(0) * x + Calculate(0));
            }
            VariableNode       variableNode       = new VariableNode();
            NaturalNumberNode  orderNode          = new NaturalNumberNode(order);
            CaretSign          caretSign          = new CaretSign(variableNode, orderNode);
            RationalNumberNode f0                 = new RationalNumberNode(CalculatePolynomialDerivative(0, order));
            MultiplicationSign multiplicationSign = new MultiplicationSign(caretSign, f0);
            FactorialFunction  factorialFunction  = new FactorialFunction(orderNode);
            DivisionSign       divisionSign       = new DivisionSign(multiplicationSign, factorialFunction);

            return(divisionSign.Calculate(x) + ReturnNewtonMaclaurinSeries(x, order - 1));
        }
        public override Node Simplify()
        {
            Node simplifiedLeftNode  = leftNode.Simplify();
            Node simplifiedRightNode = rightNode.Simplify();

            if (simplifiedLeftNode.GetType() == simplifiedRightNode.GetType() && simplifiedLeftNode.Calculate(2) == simplifiedRightNode.Calculate(2))
            {
                return(new NaturalNumberNode(1)); //5 / 5 = 1, x / x = 1
            }
            else if (simplifiedRightNode is NaturalNumberNode && simplifiedRightNode.Calculate(0) == 1)
            {
                return(simplifiedLeftNode);                                                                                        //5 / 1 = 5, x / 1 = 1
            }
            else if (simplifiedLeftNode is NaturalNumberNode && simplifiedLeftNode.Calculate(0) == 0)
            {
                return(new NaturalNumberNode(0));                                                                                      //0 / 5 = 0
            }
            else if (simplifiedLeftNode is SineFunction && simplifiedRightNode is CosineFunction || (simplifiedLeftNode is CosineFunction && simplifiedRightNode is SineFunction))
            {
                Operation functionB = (Operation)simplifiedRightNode;
                if (simplifiedLeftNode is Operation && simplifiedRightNode is Operation)
                {
                    Operation functionA   = (Operation)simplifiedLeftNode;
                    double    childValueA = functionA.ReturnChild().Calculate(5);
                    double    childValueB = functionB.ReturnChild().Calculate(5);
                    if (simplifiedLeftNode is SineFunction && simplifiedRightNode is CosineFunction)
                    {
                        if (childValueA == childValueB)
                        {
                            return(new TangentFunction(functionA.ReturnChild()));                            //sin(x) / cos(x) = tan(x)
                        }
                    }
                    else if (simplifiedLeftNode is CosineFunction && simplifiedRightNode is SineFunction)
                    {
                        if (childValueA == childValueB)
                        {
                            return(new CotangentFunction(functionA.ReturnChild()));                            //cos(x) / sin(x) = cotg(x)
                        }
                    }
                }
                return(new DivisionSign(simplifiedLeftNode, simplifiedRightNode));
            }
            //else if (simplifiedLeftNode is NaturalNumberNode && simplifiedLeftNode.Calculate(5) == 1 && simplifiedRightNode is TangentFunction) //1 / tan(x) = cotg(x)
            //    return new CotangentFunction(functionB.ReturnChild());
            //else if (simplifiedLeftNode is NaturalNumberNode && simplifiedLeftNode.Calculate(5) == 1 && simplifiedRightNode is CotangentFunction) // 1 / cotg(x) = tan(x)
            //    return new TangentFunction(functionB.ReturnChild());
            else if (simplifiedLeftNode is NumberNode && simplifiedRightNode is NumberNode) // 10 / 2 = 5
            {
                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^5 / e^3 = e^2
            {
                return(new ExponentialFunction(new MinusSign(((Operation)simplifiedLeftNode).ReturnChild().Simplify(),
                                                             ((Operation)simplifiedRightNode).ReturnChild().Simplify()).Simplify()));
            }
            else if (simplifiedLeftNode is VariableNode && simplifiedRightNode is CaretSign)
            {
                CaretSign caretSign = (CaretSign)simplifiedRightNode;
                if (caretSign.ReturnLeftNode() is VariableNode && caretSign.ReturnRightNode() is NaturalNumberNode) //x / x^5 = 1 / x^4
                {
                    return(new DivisionSign(new NaturalNumberNode(1), new CaretSign(new VariableNode(),
                                                                                    new NaturalNumberNode(Convert.ToInt32(caretSign.ReturnRightNode().Calculate(1)) - 1))));
                }
                else
                {
                    return(new CaretSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else if (simplifiedRightNode is VariableNode && simplifiedLeftNode is CaretSign)
            {
                CaretSign caretSign = (CaretSign)simplifiedLeftNode;
                if (caretSign.ReturnLeftNode() is VariableNode && caretSign.ReturnRightNode() is NaturalNumberNode)
                {
                    return(new CaretSign(new VariableNode(), new NaturalNumberNode(Convert.ToInt32(caretSign.ReturnRightNode().Calculate(1)) - 1))); //x^5 / x = x^4
                }
                else
                {
                    return(new CaretSign(simplifiedLeftNode, simplifiedRightNode));
                }
            }
            else
            {
                return(new DivisionSign(simplifiedLeftNode, simplifiedRightNode));
            }
        }