Ejemplo n.º 1
0
        public override void CreateDerivativeTree(BaseNode parent, bool isLeft = true)
        {
            MultiplicationNode multiplicationNode1 = new MultiplicationNode(Plotter.CloneTree(this.left), Plotter.CloneTree(this.right), null);
            MultiplicationNode multiplicationNode2 = new MultiplicationNode(Plotter.CloneTree(this.left), Plotter.CloneTree(this.right), null);
            SubstractionNode   substraction        = new SubstractionNode(multiplicationNode1, multiplicationNode2, null);
            PowerNode          power = new PowerNode(Plotter.CloneTree(this.right), new NumberNode(null, 2), null);
            DivisionNode       node  = new DivisionNode(substraction, power, null);

            if (parent != null)
            {
                if (isLeft)
                {
                    parent.left = node;
                }
                else
                {
                    parent.right = node;
                }
            }

            node.left.left.left.CreateDerivativeTree(node.left.left);
            node.left.right.right.CreateDerivativeTree(node.left.right, false);

            //Plotter.SetDerivativeRoot (node);
            SetDerivativeRoot(node);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a MacLaurien series based off the function
        /// </summary>
        /// <param name="mcLaurienRoot">Where the McLaurien Series will be outputted</param>
        /// <param name="order">Nth order of a series</param>
        public void CreateMcLaurienSeries(out BaseNode mcLaurienRoot, int order = 5)
        {
            if (derivativeRoot == null)
            {
                CreateDerivativeTree(); derivativeRoot.Simplify();
            }

            // we made sure that there is a derivative

            BaseNode myDerivative = Plotter.CloneTree(derivativeRoot);

            myDerivative.derivativeRoot = myDerivative;
            double[] values = new double[order + 1]; // values for functions (f(0), derivative of f(0), second derivative of f(0), etc..)

            values[0] = root.Calculate(0);           // set up a value for f(0)
            if (values.Length >= 2)
            {
                values[1] = derivativeRoot.Calculate(0);                      // set up a value of the first derivative of f(0)
            }
            if (values.Length >= 3)
            {
                for (int i = 2; i < values.Length; i++)
                {
                    myDerivative.CreateDerivativeTree(null);
                    values[i]    = myDerivative.derivativeRoot.Calculate(0);
                    myDerivative = myDerivative.derivativeRoot;
                }
            }

            List <BaseNode> mcLaurienItems = new List <BaseNode> ();

            SumNode result = new SumNode(null, null, null);

            result.left = new NumberNode(null, values[0]);

            for (int i = 1; i < values.Length; i++)
            {
                DivisionNode       item        = new DivisionNode(null, null, null);
                FactorialNode      denominator = new FactorialNode(new NumberNode(null, i), null); // not sure about this line
                MultiplicationNode numerator   = new MultiplicationNode(
                    new NumberNode(null, values[i]),
                    new PowerNode(new BasicFunctionXNode("", null), new NumberNode(null, i), null), null
                    );
                item.left  = numerator;
                item.right = denominator;
                mcLaurienItems.Add(item);
            }

            foreach (var item in mcLaurienItems)
            {
                result.PutToRightNode(item);
            }

            mcLaurienRoot = result;
        }
Ejemplo n.º 3
0
        public override void CreateDerivativeTree(BaseNode parent, bool isLeft = true)
        {
            SumNode node = new SumNode(Plotter.CloneTree(this.left), Plotter.CloneTree(this.right), parent);

            if (parent != null)
            {
                if (isLeft)
                {
                    parent.left = node;
                }
                else
                {
                    parent.right = node;
                }
            }
            node.left.CreateDerivativeTree(node);
            node.right.CreateDerivativeTree(node, false);

            //Plotter.SetDerivativeRoot (node);
            SetDerivativeRoot(node);
        }
Ejemplo n.º 4
0
        public override void CreateDerivativeTree(BaseNode parent, bool isLeft = true)
        {
            CosNode            cosNode = new CosNode(Plotter.CloneTree(this.left), null);
            MultiplicationNode node    = new MultiplicationNode(Plotter.CloneTree(this.left), cosNode, null);

            if (parent != null)
            {
                if (isLeft)
                {
                    parent.left = node;
                }
                else
                {
                    parent.right = node;
                }
            }

            node.left.CreateDerivativeTree(node);

            //Plotter.SetDerivativeRoot (node);
            SetDerivativeRoot(node);
        }
Ejemplo n.º 5
0
        public override void CreateDerivativeTree(BaseNode parent, bool isLeft = true)
        {
            SumNode sum = new SumNode(Plotter.CloneTree(this), Plotter.CloneTree(this), this.parent);

            if (parent != null)
            {
                if (isLeft)
                {
                    parent.left = sum;
                }
                else
                {
                    parent.right = sum;
                }
            }

            sum.left.left.CreateDerivativeTree(sum.left);
            sum.right.right.CreateDerivativeTree(sum.right, false);

            //Plotter.SetDerivativeRoot (sum);
            SetDerivativeRoot(sum);
        }
Ejemplo n.º 6
0
        public override void CreateDerivativeTree(BaseNode parent, bool isLeft = true)
        {
            // derivative of ln(x) = 1/x

            DivisionNode       division       = new DivisionNode(new NumberNode(null, 1), Plotter.CloneTree(this.left), null);
            MultiplicationNode multiplication = new MultiplicationNode(division, Plotter.CloneTree(this.left), null);

            if (parent != null)
            {
                if (isLeft)
                {
                    parent.left = multiplication;
                }
                else
                {
                    parent.right = multiplication;
                }
            }

            multiplication.right.CreateDerivativeTree(multiplication, false);

            //Plotter.SetDerivativeRoot (multiplication);
            SetDerivativeRoot(multiplication);
        }
Ejemplo n.º 7
0
        public override void CreateDerivativeTree(BaseNode parent, bool isLeft = true)
        {
            if (this.right is NumberNode && this.left is BasicFunctionXNode)
            {
                var lesser           = (right as NumberNode).RealValue - 1;
                BasicFunctionXNode x = new BasicFunctionXNode("", null);
                MultiplicationNode multiplication = new MultiplicationNode(new NumberNode(null, (right as NumberNode).RealValue),
                                                                           new PowerNode(x, new NumberNode(null, lesser), null), null);

                if (parent != null)
                {
                    if (isLeft)
                    {
                        parent.left = multiplication;
                    }
                    else
                    {
                        parent.right = multiplication;
                    }
                }

                SetDerivativeRoot(multiplication);
                return;
            }
            else
            {
                if (this.right is NumberNode && this.left is NumberNode)
                {
                    // if both this.left and this.right are numbers, return 0 for its just a number and it's anyway gon be 0
                    NumberNode node = new NumberNode(parent, 0);

                    if (parent != null)
                    {
                        if (isLeft)
                        {
                            parent.left = node;
                        }
                        else
                        {
                            parent.right = node;
                        }
                    }

                    //Plotter.SetDerivativeRoot (node);
                    SetDerivativeRoot(node);
                    return;
                }
                else if (this.right is NumberNode && !(this.left is NumberNode))
                {
                    // f(x) ^ (some number)
                    // if left one some function
                    double nMinus1 = ((NumberNode)this.right).RealValue - 1;
                    var    value   = ((NumberNode)this.right).RealValue;

                    if (value == 1)
                    {
                        var node = Plotter.CloneTree(this);

                        if (parent != null)
                        {
                            if (isLeft)
                            {
                                parent.left = node;
                            }
                            else
                            {
                                parent.right = node;
                            }
                        }

                        node.left.CreateDerivativeTree(node);
                        SetDerivativeRoot(node);
                        return;
                    }

                    PowerNode          power          = new PowerNode(Plotter.CloneTree(this.left), new NumberNode(null, nMinus1), null);
                    MultiplicationNode multiplication = new MultiplicationNode(new NumberNode(null, value), Plotter.CloneTree(this.left), null);

                    // if the f(x) is more complicated than just 'x', we do additional calculation
                    if (!(multiplication.right is BasicFunctionXNode))
                    {
                        MultiplicationNode node = new MultiplicationNode(multiplication, Plotter.CloneTree(this.left), parent);
                        node.right.CreateDerivativeTree(multiplication, false);

                        if (parent != null)
                        {
                            if (isLeft)
                            {
                                parent.left = node;
                            }
                            else
                            {
                                parent.right = node;
                            }
                        }

                        //Plotter.SetDerivativeRoot (node);
                        SetDerivativeRoot(node);
                        return;
                    }

                    multiplication.parent = parent;

                    if (parent != null)
                    {
                        if (isLeft)
                        {
                            parent.left = multiplication;
                        }
                        else
                        {
                            parent.right = multiplication;
                        }
                    }

                    //Plotter.SetDerivativeRoot (multiplication);
                    SetDerivativeRoot(multiplication);
                    return;
                }
                else if (!(this.right is NumberNode) && (this.left is NumberNode))
                {
                    // (some number) ^ f(x)

                    var value = ((NumberNode)this.left).RealValue;

                    if (this.right is BasicFunctionXNode)
                    {
                        // simple function
                        PowerNode          power = new PowerNode(new NumberNode(null, value), new BasicFunctionXNode(""), null);
                        LnNode             ln    = new LnNode(new NumberNode(null, value), null);
                        MultiplicationNode node  = new MultiplicationNode(power, ln, parent);

                        if (parent != null)
                        {
                            if (isLeft)
                            {
                                parent.left = node;
                            }
                            else
                            {
                                parent.right = node;
                            }
                        }

                        //Plotter.SetDerivativeRoot (node);
                        SetDerivativeRoot(node);
                        return;
                    }
                    else
                    {
                        // function is more complicated
                        PowerNode          power          = new PowerNode(new NumberNode(null, value), this.right, null);
                        LnNode             ln             = new LnNode(new NumberNode(null, value), null);
                        MultiplicationNode multiplication = new MultiplicationNode(power, ln, parent);
                        MultiplicationNode node           = new MultiplicationNode(multiplication, Plotter.CloneTree(this.right), parent);
                        node.right.CreateDerivativeTree(node, false);

                        if (parent != null)
                        {
                            if (isLeft)
                            {
                                parent.left = node;
                            }
                            else
                            {
                                parent.right = node;
                            }
                        }

                        //Plotter.SetDerivativeRoot (node);
                        SetDerivativeRoot(node);
                        return;
                    }
                }
                else if (!(this.right is NumberNode) && !(this.left is NumberNode))
                {
                    // neither is a number
                    // CASE: f(x) ^ g(x)
                    // d(f(x) ^ g(x))/dx = e^(g(x)*ln(f(x)) * d((g(x)*f(x)))/dx )
                    // this.left = f(x), this.right = g(x)

                    LnNode             lnFx                       = new LnNode(Plotter.CloneTree(this.left), null);                                                                // create ln(f(x))
                    MultiplicationNode multiplication             = new MultiplicationNode(Plotter.CloneTree(this.right), lnFx, null);                                             // create g(x)*ln(f(x))
                    PowerNode          ePower                     = new PowerNode(new NumberNode(null, Math.E), multiplication, null);                                             // create e^(g(x)*ln(f(x)))
                    MultiplicationNode derivativeOfMultiplication = new MultiplicationNode(Plotter.CloneTree(multiplication.left), Plotter.CloneTree(multiplication.right), null); // do the derivative of g(x)*ln(f(x))
                    MultiplicationNode node                       = new MultiplicationNode(ePower, derivativeOfMultiplication, parent);                                            // put it all together

                    node.right.CreateDerivativeTree(node, false);                                                                                                                  // take a derivative

                    if (parent != null)
                    {
                        if (isLeft)
                        {
                            parent.left = node;
                        }
                        else
                        {
                            parent.right = node;
                        }
                    }

                    //Plotter.SetDerivativeRoot (node);
                    SetDerivativeRoot(node);
                    return;
                }
            }
        }