Ejemplo 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);
            }
        }
Ejemplo 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);
        }
Ejemplo 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);
            }
        }
Ejemplo n.º 4
0
        public override Node Clone()
        {
            Node newNode  = new NodePower();
            Node newNode1 = node1.Clone();
            Node newNode2 = node2.Clone();

            newNode.AddNode1(newNode1);
            newNode.AddNode2(newNode2);
            return(newNode);
        }
Ejemplo n.º 5
0
        private Node GetPolyNode(List <double> polyPoints)
        {
            //create polyfunction nodes
            List <NodePolyFunction> polyFunctions = new List <NodePolyFunction>();
            int totalPoints = polyPoints.Count / 2;
            int nOrder      = totalPoints - 1;

            for (int i = 0; i < polyPoints.Count; i += 2) //each point
            {
                double        ans     = polyPoints[i + 1];
                List <double> xValues = new List <double>();

                for (int j = nOrder; j >= 0; j--) //each x value
                {
                    double x = polyPoints[i];
                    if (x == 0)
                    {
                        x = 0.000000001;
                    }
                    xValues.Add(Math.Pow(x, j));
                }

                NodePolyFunction newNode = new NodePolyFunction(xValues, ans);
                polyFunctions.Add(newNode);
            }

            //transform polyfunctions into ideal functions (1a + 0 + 0 = ?, 0 + 1b + 0 = ? etc.)
            for (int i = 0; i < polyFunctions.Count; i++) //make current x value equal to 1 of this function
            {
                double divNumber;
                divNumber = polyFunctions[i].xValues[i];

                polyFunctions[i].divideBy(divNumber);

                for (int j = 0; j < polyFunctions.Count; j++)//make current x value equal to 0 of other functions
                {
                    if (j != i)
                    {
                        double timesNumber;
                        timesNumber = polyFunctions[j].xValues[i];

                        polyFunctions[j].minusBy(timesNumber, polyFunctions[i]);
                    }
                }
            }

            //make the return node out of the ideal (poly) functions
            Node headNode = new NodePlus();
            Node tempNode = headNode;

            for (int i = 0; i < polyFunctions.Count(); i++)
            {
                Node nodePow    = new NodePower();
                Node nodeTim    = new NodeTimes();
                Node nodeNum    = new NodeNumber(false, false, polyFunctions[i].answer);
                Node nodeNumLow = new NodeNumber(true, false, 1);
                Node nodeNumUp  = new NodeNumber(false, false, nOrder - i);

                nodeTim.AddNode1(nodeNum);
                nodeTim.AddNode2(nodePow);

                nodePow.AddNode1(nodeNumLow);
                nodePow.AddNode2(nodeNumUp);

                if (i <= polyFunctions.Count - 3) //normal
                {
                    tempNode.AddNode1(nodeTim);
                    Node nodePlus = new NodePlus();
                    tempNode.AddNode2(nodePlus);
                    tempNode = nodePlus;
                }
                else if (i <= polyFunctions.Count - 2) //one before last one
                {
                    tempNode.AddNode1(nodeTim);
                }
                else if (i <= polyFunctions.Count - 1) //last one
                {
                    tempNode.AddNode2(nodeTim);
                }
            }
            return(headNode);
        }
Ejemplo n.º 6
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);
            }
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        private Node combineNodes(Node cleanNode1, Node cleanNode2, bool bothAreUpperNodes)
        {
            if (!bothAreUpperNodes)
            {
                if (!(cleanNode1 is NodeNumber) && cleanNode2 is NodeNumber)
                {
                    if (((NodeNumber)cleanNode2).Number == 0)
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(false, false, 1);
                        return(newNumberNode);
                    }
                }
                if (cleanNode1 is NodeNumber && cleanNode2 is NodeNumber)
                {
                    if (((NodeNumber)cleanNode2).Number == 0 && ((NodeNumber)cleanNode1).Number != 0)
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(false, false, 1);
                        return(newNumberNode);
                    }
                    else if (!cleanNode1.ContainsX() && !cleanNode2.ContainsX())
                    {
                        if (!cleanNode1.ContainsP() && !cleanNode2.ContainsP())
                        {
                            Node newNumberNode;
                            newNumberNode = new NodeNumber(false, false, Math.Pow(((NodeNumber)cleanNode1).Number, ((NodeNumber)cleanNode2).Number));
                            return(newNumberNode);
                        }
                    }
                    else if (((NodeNumber)cleanNode2).Number == 1)
                    {
                        return(cleanNode1);
                    }
                }

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

                if (cleanNode1 is NodeLN)
                {
                    Node nodePow = new NodePower();
                    nodePow.AddNode1(cleanNode1.Node1);
                    nodePow.AddNode2(cleanNode2);
                    cleanNode1.AddNode1(nodePow);
                    cleanNode1.MakeNodeClean(null, out trash);
                    return(cleanNode1);
                }
                if (cleanNode1 is NodeE)
                {
                    Node nodePlus = new NodePlus();
                    nodePlus.AddNode1(cleanNode1.Node1);
                    nodePlus.AddNode2(cleanNode2);
                    cleanNode1.AddNode1(nodePlus);
                    cleanNode1.MakeNodeClean(null, out trash);
                    return(cleanNode1);
                }
            }
            else //both are upper nodes, try to multiply time if possible (this is a copy of nodeTimes.combineNodes() method)
            {
                if (cleanNode1 is NodeNumber && cleanNode2 is NodeNumber)
                {
                    if (cleanNode1.ContainsX() != cleanNode2.ContainsX())     //one has X
                    {
                        if (cleanNode1.ContainsP() != cleanNode2.ContainsP()) //one has P
                        {
                            Node newNumberNode;
                            newNumberNode = new NodeNumber(true, true, ((NodeNumber)cleanNode1).Number * ((NodeNumber)cleanNode2).Number);
                            return(newNumberNode);
                        }
                        else if (!cleanNode1.ContainsP()) //both have not P
                        {
                            Node newNumberNode;
                            newNumberNode = new NodeNumber(true, false, ((NodeNumber)cleanNode1).Number * ((NodeNumber)cleanNode2).Number);
                            return(newNumberNode);
                        }
                    }
                    else if (!cleanNode1.ContainsX())                         //both have not X
                    {
                        if (cleanNode1.ContainsP() != cleanNode2.ContainsP()) //one has P
                        {
                            Node newNumberNode;
                            newNumberNode = new NodeNumber(false, true, ((NodeNumber)cleanNode1).Number * ((NodeNumber)cleanNode2).Number);
                            return(newNumberNode);
                        }
                        else if (!cleanNode1.ContainsP()) //both have not P
                        {
                            Node newNumberNode;
                            newNumberNode = new NodeNumber(false, false, ((NodeNumber)cleanNode1).Number * ((NodeNumber)cleanNode2).Number);
                            return(newNumberNode);
                        }
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 9
0
        private Node combineNodes(Node cleanNode1, Node cleanNode2)
        {
            if (cleanNode1 is NodeNumber && !(cleanNode2 is NodeNumber))
            {
                if (((NodeNumber)cleanNode1).Number == 0)
                {
                    Node newNumberNode;
                    newNumberNode = new NodeNumber(false, false, 0);
                    return(newNumberNode);
                }
                else if (((NodeNumber)cleanNode1).Number == 1 && !cleanNode1.ContainsX() && !cleanNode1.ContainsP())
                {
                    return(cleanNode2);
                }
                else if (cleanNode2 is NodePower)
                {
                    if (cleanNode2.Node1 is NodeNumber)
                    {
                        if (((NodeNumber)cleanNode1).ContainsX() == ((NodeNumber)cleanNode2.Node1).ContainsX())
                        {
                            //check if nodePower.Node1(lower) has only 1x and node has (somenumber)x
                        }
                    }
                }
            }
            if (cleanNode2 is NodeNumber && !(cleanNode1 is NodeNumber))
            {
                if (((NodeNumber)cleanNode2).Number == 0)
                {
                    Node newNumberNode;
                    newNumberNode = new NodeNumber(false, false, 0);
                    return(newNumberNode);
                }
                else if (((NodeNumber)cleanNode2).Number == 1 && !cleanNode2.ContainsX() && !cleanNode2.ContainsP())
                {
                    return(cleanNode1);
                }
            }
            if (cleanNode1 is NodeNumber && cleanNode2 is NodeNumber)
            {
                if (cleanNode1.ContainsX() != cleanNode2.ContainsX())     //one has X
                {
                    if (cleanNode1.ContainsP() != cleanNode2.ContainsP()) //one has P
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(true, true, ((NodeNumber)cleanNode1).Number * ((NodeNumber)cleanNode2).Number);
                        return(newNumberNode);
                    }
                    else if (!cleanNode1.ContainsP()) //both have not P
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(true, false, ((NodeNumber)cleanNode1).Number * ((NodeNumber)cleanNode2).Number);
                        return(newNumberNode);
                    }
                }
                else if (!cleanNode1.ContainsX())                         //both have not X
                {
                    if (cleanNode1.ContainsP() != cleanNode2.ContainsP()) //one has P
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(false, true, ((NodeNumber)cleanNode1).Number * ((NodeNumber)cleanNode2).Number);
                        return(newNumberNode);
                    }
                    else if (!cleanNode1.ContainsP()) //both have not P
                    {
                        Node newNumberNode;
                        newNumberNode = new NodeNumber(false, false, ((NodeNumber)cleanNode1).Number * ((NodeNumber)cleanNode2).Number);
                        return(newNumberNode);
                    }
                }
            }
            //none of the nodes are numbers, so they need to be cleaned!
            Node trash;

            if (cleanNode2.SameAs(cleanNode1))
            {
                Node nodePow = new NodePower();
                nodePow.AddNode1(cleanNode1);
                nodePow.AddNode2(new NodeNumber(false, false, 2));
                nodePow.MakeNodeClean(null, out trash);
                return(nodePow);
            }
            if ((cleanNode1 is NodePower && cleanNode2 is NodePower) || (cleanNode1 is NodeE && cleanNode2 is NodeE) || (cleanNode1 is NodeLN && cleanNode2 is NodeLN))
            {
                if (cleanNode1.Node1.SameAs(cleanNode2.Node1))
                {
                    if (cleanNode1 is NodePower)
                    {
                        Node nodePow  = new NodePower();
                        Node nodePlus = new NodePlus();
                        nodePow.AddNode1(cleanNode1.Node1);
                        nodePow.AddNode2(nodePlus);
                        nodePlus.AddNode1(cleanNode1.Node2);
                        nodePlus.AddNode2(cleanNode2.Node2);
                        nodePow.MakeNodeClean(null, out trash);
                        return(nodePow);
                    }
                }
                if (cleanNode1 is NodeE)
                {
                    Node nodeE    = new NodeE();
                    Node nodePlus = new NodePlus();
                    nodeE.AddNode1(nodePlus);
                    nodePlus.AddNode1(cleanNode1.Node1);
                    nodePlus.AddNode2(cleanNode2.Node1);
                    nodeE.MakeNodeClean(null, out trash);
                    return(nodeE);
                }
            }

            return(null);
        }