Esempio n. 1
0
        private Node getMaclaurinSerieRecursiveAccurate(int counter, int order, Node curNode)
        {
            Node   nodeTim   = new NodeTimes();
            Node   nodeDiv   = new NodeDivision();
            Node   nodeFac   = new NodeFactorial();
            Node   nodePow   = new NodePower();
            double zeroValue = curNode.GetValueForX(0);

            nodeTim.AddNode1(nodeDiv);
            nodeTim.AddNode2(nodePow);
            nodeDiv.AddNode1(new NodeNumber(false, false, zeroValue));
            nodeDiv.AddNode2(nodeFac);
            nodeFac.AddNode1(new NodeNumber(false, false, counter - 1));
            nodePow.AddNode1(new NodeNumber(true, false, 1));
            nodePow.AddNode2(new NodeNumber(false, false, counter - 1));

            if (counter == order)
            {
                return(nodeTim);
            }
            else if (counter < order && counter > 0)
            {
                Node nodePlus = new NodePlus();
                nodePlus.AddNode1(nodeTim);
                curNode = curNode.GetDerivative();
                Node receivedReplacementNode;
                curNode = curNode.MakeNodeClean(null, out receivedReplacementNode);
                nodePlus.AddNode2(getMaclaurinSerieRecursiveAccurate(counter + 1, order, curNode));
                return(nodePlus);
            }
            else
            {
                throw new Exception("Error while making Maclaurin serie: counter problem: " + counter);
            }
        }
Esempio n. 2
0
        public override Node GetDerivative() //O = original, D = derivative
        {
            Node nodeDiv     = new NodeDivision();
            Node nodeTimes1  = new NodeTimes();
            Node nodeTimes2  = new NodeTimes();
            Node nodeMin     = new NodeMinus();
            Node nodePow     = new NodePower();
            Node nodeNumber2 = new NodeNumber(false, false, 2);
            Node node1D      = node1.GetDerivative();
            Node node2D      = Node2.GetDerivative();
            Node node1O      = node1.Clone();
            Node node2O      = node2.Clone();
            Node node2O2     = node2.Clone();

            //divison (head node)
            nodeDiv.AddNode1(nodeMin);
            nodeDiv.AddNode2(nodePow);

            //creating upper part of division
            nodeMin.AddNode1(nodeTimes1);
            nodeMin.AddNode2(nodeTimes2);

            nodeTimes1.AddNode1(node1D);
            nodeTimes1.AddNode2(node2O);

            nodeTimes2.AddNode1(node1O);
            nodeTimes2.AddNode2(node2D);

            //creating lower part of division
            nodePow.AddNode1(node2O2);
            nodePow.AddNode2(nodeNumber2);

            return(nodeDiv);
        }
Esempio n. 3
0
        public override Node GetDerivative() //O = original, D = derivative
        {
            Node node1O1    = node1.Clone();
            Node node1D     = node1.GetDerivative();
            Node node2O1    = node2.Clone();
            Node nodeTimes1 = new NodeTimes();
            Node nodeTimes2 = new NodeTimes();

            if (node2.ContainsX())
            {
                Node thisNode   = this.Clone();
                Node node1O2    = node1.Clone();
                Node node2D     = node2.GetDerivative();
                Node nodePlus   = new NodePlus();
                Node nodeLN     = new NodeLN();
                Node nodeDiv    = new NodeDivision();
                Node nodeTimes3 = new NodeTimes();

                nodeTimes3.AddNode1(thisNode);
                nodeTimes3.AddNode2(nodePlus);

                nodePlus.AddNode1(nodeTimes1);
                nodePlus.AddNode2(nodeTimes2);

                nodeTimes1.AddNode1(node1D);
                nodeTimes1.AddNode2(nodeDiv);

                nodeDiv.AddNode1(node2O1);
                nodeDiv.AddNode2(node1O1);

                nodeTimes2.AddNode1(node2D);
                nodeTimes2.AddNode2(nodeLN);

                nodeLN.AddNode1(node1O2);

                return(nodeTimes3);
            }
            else
            {
                Node node2O2       = node2.Clone();
                Node nodeNumberOne = new NodeNumber(false, false, 1);
                Node nodeMin       = new NodeMinus();
                Node nodePow       = new NodePower();

                nodeTimes2.AddNode1(nodePow);
                nodeTimes2.AddNode2(nodeTimes1);

                nodeTimes1.AddNode1(node2O1);
                nodeTimes1.AddNode2(node1D);

                nodePow.AddNode1(node1O1);
                nodePow.AddNode2(nodeMin);

                nodeMin.AddNode1(node2O2);
                nodeMin.AddNode2(nodeNumberOne);

                return(nodeTimes2);
            }
        }
Esempio n. 4
0
        public override Node Clone()
        {
            Node newNode  = new NodeDivision();
            Node newNode1 = node1.Clone();
            Node newNode2 = node2.Clone();

            newNode.AddNode1(newNode1);
            newNode.AddNode2(newNode2);
            return(newNode);
        }
Esempio n. 5
0
        public override Node GetDerivative() //O = original, D = derivative
        {
            Node nodeDiv = new NodeDivision();
            Node node1O  = node1.Clone();
            Node node1D  = node1.GetDerivative();

            nodeDiv.AddNode1(node1D);
            nodeDiv.AddNode2(node1O);

            return(nodeDiv);
        }
Esempio n. 6
0
        public override Node MakeNodeClean(Node prevNodeNumber, out Node replacementNode)
        {
            Node garbage;

            if (node1 is NodeLN && node2 is NodeLN) //exception
            {
                Node nodeDiv = new NodeDivision();
                Node nodeln  = new NodeLN();
                nodeln.AddNode1(nodeDiv);
                nodeDiv.AddNode1(Node1.Node1);
                nodeDiv.AddNode2(node2.Node1);
                nodeln.MakeNodeClean(null, out garbage);
                replacementNode = null;
                return(nodeln);
            }
            else
            {
                Node nodePlus = new NodePlus();
                nodePlus.AddNode1(node1);
                if (node2 is NodeNumber)
                {
                    ((NodeNumber)node2).SwapPositivity();
                    nodePlus.AddNode2(node2);
                }
                else if (node2 is NodeMinus)
                {
                    Node newNode2 = node2.MakeNodeClean(null, out garbage); //node2 is now nodeplus
                    ((NodePlus)newNode2).SwapPositivity();
                    nodePlus.AddNode2(newNode2);
                }
                else
                {
                    Node nodeTimes  = new NodeTimes();
                    Node nodeNumber = new NodeNumber(false, false, -1);
                    nodeTimes.AddNode1(nodeNumber);
                    nodeTimes.AddNode2(node2);
                    nodePlus.AddNode2(nodeTimes);
                }
                replacementNode = null;
                nodePlus.MakeNodeClean(null, out garbage);
                return(nodePlus);
            }
        }
Esempio n. 7
0
        private Node drawMaclaurinSerieFastRecursive(int counter, int order, double[] maclaurinValues, GraphDrawer drawer, Node prevCompletedNode)
        {
            NodeHolder nh;
            Node       nodeTim   = new NodeTimes();
            Node       nodeDiv   = new NodeDivision();
            Node       nodeFac   = new NodeFactorial();
            Node       nodePow   = new NodePower();
            double     zeroValue = maclaurinValues[counter - 1];

            nodeTim.AddNode1(nodeDiv);
            nodeTim.AddNode2(nodePow);
            nodeDiv.AddNode1(new NodeNumber(false, false, zeroValue));
            nodeDiv.AddNode2(nodeFac);
            nodeFac.AddNode1(new NodeNumber(false, false, counter - 1));
            nodePow.AddNode1(new NodeNumber(true, false, 1)); //X
            nodePow.AddNode2(new NodeNumber(false, false, counter - 1));

            if (counter == order)
            {
                Node nodePlus = new NodePlus();
                if (prevCompletedNode != null)
                {
                    nodePlus.AddNode1(nodeTim);
                    nodePlus.AddNode2(prevCompletedNode);
                    prevCompletedNode = nodePlus.Clone();
                    nh = new NodeHolder(prevCompletedNode);
                }
                else
                {
                    prevCompletedNode = nodeTim;
                    nh = new NodeHolder(prevCompletedNode);
                }
                if (counter == 1)
                {
                    drawer.drawFunction(nh, true);
                }
                else
                {
                    drawer.drawFunction(nh, false);
                }
                return(nodeTim);
            }
            else if (counter < order && counter > 0)
            {
                Node nodePlus = new NodePlus();
                if (prevCompletedNode != null)
                {
                    nodePlus.AddNode1(nodeTim);
                    nodePlus.AddNode2(prevCompletedNode);
                    prevCompletedNode = nodePlus.Clone();
                    nh = new NodeHolder(prevCompletedNode);
                }
                else
                {
                    prevCompletedNode = nodeTim;
                    nh = new NodeHolder(prevCompletedNode);
                }
                if (counter == 1)
                {
                    drawer.drawFunction(nh, true);
                }
                else
                {
                    drawer.drawFunction(nh, false);
                }
                nodePlus.AddNode1(nodeTim);
                nodePlus.AddNode2(drawMaclaurinSerieFastRecursive(counter + 1, order, maclaurinValues, drawer, prevCompletedNode));
                return(nodePlus);
            }
            else
            {
                throw new Exception("Error while making Maclaurin serie: counter problem: " + counter);
            }
        }
Esempio n. 8
0
        private Node combineNodes(Node cleanNode1, Node cleanNode2)
        {
            if (cleanNode1 is NodeNumber && cleanNode2 is NodeNumber)
            {
                int negOrPos = 0;
                if (((NodeNumber)cleanNode1).Number == ((NodeNumber)cleanNode2).Number)
                {
                    negOrPos = 1;
                }
                else if (((NodeNumber)cleanNode1).Number == -1 * ((NodeNumber)cleanNode2).Number)
                {
                    negOrPos = -1;
                }
                if (negOrPos != 0)
                {
                    if (cleanNode1.ContainsX() == cleanNode2.ContainsX() && cleanNode1.ContainsP() == cleanNode2.ContainsP())
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(false, false, negOrPos);
                        return(newNumberNode);
                    }
                    else if (cleanNode1.ContainsX() == cleanNode2.ContainsX() && !cleanNode2.ContainsP())
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(false, true, negOrPos);
                        return(newNumberNode);
                    }
                    else if (cleanNode1.ContainsP() == cleanNode2.ContainsP() && !cleanNode2.ContainsX())
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(true, false, negOrPos);
                        return(newNumberNode);
                    }
                }
            }
            if (cleanNode1 is NodeNumber)
            {
                if (((NodeNumber)cleanNode1).Number == 0)
                {
                    Node newNumberNode;
                    newNumberNode = new NodeNumber(false, false, 0);
                    return(newNumberNode);
                }
                if (cleanNode2 is NodeFactorial)
                {
                    if (((NodeNumber)cleanNode1).Number == ((NodeFactorial)cleanNode2).GetValueForX(0))
                    {
                        return(new NodeNumber(cleanNode1.ContainsX(), cleanNode1.ContainsP(), 1));
                    }
                }
            }
            if (cleanNode2 is NodeNumber)
            {
                if (cleanNode1 is NodeFactorial)
                {
                    if (((NodeNumber)cleanNode2).Number == ((NodeFactorial)cleanNode1).GetValueForX(0))
                    {
                        Node nodeDiv = new NodeDivision();
                        nodeDiv.AddNode1(new NodeNumber(false, false, 1));
                        nodeDiv.AddNode2(new NodeNumber(cleanNode2.ContainsX(), cleanNode2.ContainsP(), 1));
                        return(nodeDiv);
                    }
                }
                if (((NodeNumber)cleanNode2).Number == 1 && !cleanNode2.ContainsP() && !cleanNode2.ContainsX())
                {
                    return(cleanNode1);
                }
            }

            //none of the nodes are numbers, so they need to be cleaned!
            Node trash;

            if (cleanNode2.SameAs(cleanNode1))
            {
                Node nodeNum = new NodeNumber(false, false, 1);
                return(nodeNum);
            }
            if ((cleanNode1 is NodePower && cleanNode2 is NodePower) || (cleanNode1 is NodeE && cleanNode2 is NodeE) || (cleanNode1 is NodeTimes && cleanNode2 is NodeTimes))
            {
                if (!(cleanNode1 is NodeE))
                {
                    if (cleanNode1.Node1.SameAs(cleanNode2.Node1))
                    {
                        if (cleanNode1 is NodePower)
                        {
                            NodePower nodePow = new NodePower();
                            NodeMinus nodeMin = new NodeMinus();
                            nodePow.AddNode1(cleanNode1.Node1);
                            nodePow.AddNode2(nodeMin);
                            nodeMin.AddNode1(cleanNode1.Node2);
                            nodeMin.AddNode2(cleanNode2.Node2);
                            nodePow.MakeNodeClean(null, out trash);
                            return(nodePow);
                        }
                        if (cleanNode1 is NodeTimes)
                        {
                            NodeDivision nodeDiv = new NodeDivision();
                            nodeDiv.AddNode1(cleanNode1.Node2);
                            nodeDiv.AddNode2(cleanNode2.Node2);
                            nodeDiv.MakeNodeClean(null, out trash);
                            return(nodeDiv);
                        }
                    }
                    else if (cleanNode1.Node1.SameAs(cleanNode2.Node2))
                    {
                        if (cleanNode1 is NodeTimes)
                        {
                            NodeDivision nodeDiv = new NodeDivision();
                            nodeDiv.AddNode1(cleanNode1.Node2);
                            nodeDiv.AddNode2(cleanNode2.Node1);
                            nodeDiv.MakeNodeClean(null, out trash);
                            return(nodeDiv);
                        }
                    }
                    else if (cleanNode1.Node2.SameAs(cleanNode2.Node1))
                    {
                        if (cleanNode1 is NodeTimes)
                        {
                            NodeDivision nodeDiv = new NodeDivision();
                            nodeDiv.AddNode1(cleanNode1.Node1);
                            nodeDiv.AddNode2(cleanNode2.Node2);
                            nodeDiv.MakeNodeClean(null, out trash);
                            return(nodeDiv);
                        }
                    }
                    else if (cleanNode1.Node2.SameAs(cleanNode2.Node2))
                    {
                        if (cleanNode1 is NodeTimes)
                        {
                            NodeDivision nodeDiv = new NodeDivision();
                            nodeDiv.AddNode1(cleanNode1.Node1);
                            nodeDiv.AddNode2(cleanNode2.Node1);
                            nodeDiv.MakeNodeClean(null, out trash);
                            return(nodeDiv);
                        }
                    }
                }
                else
                {
                    NodeE     nodeE   = new NodeE();
                    NodeMinus nodeMin = new NodeMinus();
                    nodeE.AddNode1(nodeMin);
                    nodeMin.AddNode1(cleanNode1.Node1);
                    nodeMin.AddNode2(cleanNode2.Node1);
                    nodeE.MakeNodeClean(null, out trash);
                    return(nodeE);
                }
            }

            return(null);
        }