示例#1
0
        static void Main(string[] args)
        {
            string a = "+(r(32.43), *(32, x))";

            if (a.Contains('r'))
            {
                a = Plotter.DeleteCharFromString(a, 'r');
            }

            Plotter p = new Plotter();

            p.ProcessString(a);

            var v = p.Root;

            Console.WriteLine();

            Console.ReadKey();
        }
示例#2
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);
        }
示例#3
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);
        }
示例#4
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);
        }
示例#5
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);
        }
示例#6
0
 public LnNode(string input, BaseNode parentNode)
 {
     value  = Plotter.GetStringFromIndex(input, 1);
     parent = parentNode;
 }
示例#7
0
 /// <summary>
 /// Sets the derivative (read the methods to understand)
 /// </summary>
 /// <param name="node"></param>
 protected void SetDerivativeRoot(BaseNode node)
 {
     Plotter.SetDerivativeRoot(node, ref derivativeRoot);
 }
示例#8
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);
        }
示例#9
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;
                }
            }
        }
示例#10
0
        /// <summary>
        /// Creates tree based on an input string and root node
        /// </summary>
        /// <param name="s"></param>
        /// <param name="baseNode"></param>
        public void CreateTree(string s, BaseNode baseNode)
        {
            // if the string is empty, we don't do anything. This is the base case to leave the recursion
            if (s == string.Empty)
            {
                return;
            }

            // if it's 's', or '+', or whatever, we create a dedicated class (watch first case to see the logic)
            if (s[0] == 's')
            {
                SinNode node = new SinNode(s, baseNode); // dedicated class
                baseNode.Insert(node);                   // we insert it to the current head node
                CreateTree(node.value, node);            // we change the head node to the newly created one
            }
            else if (s[0] == 'c')
            {
                CosNode node = new CosNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == '*')
            {
                // same as in the first 'if'
                MultiplicationNode node = new MultiplicationNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == '+')
            {
                // same as in the first 'if'
                SumNode node = new SumNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == '/')
            {
                DivisionNode node = new DivisionNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == '-' && !(s[1] >= '0' && s[1] <= '9'))
            {
                SubstractionNode node = new SubstractionNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == 'l')
            {
                LnNode node = new LnNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == '^')
            {
                PowerNode node = new PowerNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == '!')
            {
                FactorialNode node = new FactorialNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == 'p' || (s[0] >= '0' && s[0] <= '9'))
            {
                // stuff below just parses number
                string toParseIntoNumber = string.Empty;
                int    counter           = 0;

                if (s[0] == 'p')
                {
                    toParseIntoNumber = "p";
                }
                else
                {
                    while ((s[counter] >= '0' && s[counter] <= '9') || s[counter] == '.')
                    {
                        toParseIntoNumber += s[counter];
                        counter++;
                    }
                }

                if (toParseIntoNumber.Contains('.'))
                {
                    toParseIntoNumber = toParseIntoNumber.Replace('.', ',');
                }

                string @newS = string.Empty;

                for (int i = (s[0] == 'p' ? 1 : counter); i < s.Length; i++)
                {
                    newS += s[i];
                }

                // same stuff as in the first 'if'
                NumberNode node = new NumberNode(newS, baseNode, toParseIntoNumber);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == '-' && (s[1] >= '0' && s[1] <= '9'))
            {
                // negative number
                s = Plotter.GetStringFromIndex(s, 1);

                string toParseIntoNumber = string.Empty;
                int    counter           = 0;

                if (s[0] == 'p')
                {
                    toParseIntoNumber = "p";
                }
                else
                {
                    do
                    {
                        toParseIntoNumber += s[counter];
                        counter++;
                    } while (counter < s.Length && ((s[counter] >= '0' && s[counter] <= '9') || s[counter] == '.'));
                }

                string @newS = string.Empty;

                for (int i = (s[0] == 'p' ? 1 : counter); i < s.Length; i++)
                {
                    newS += s[i];
                }

                NumberNode node = new NumberNode(newS, baseNode, "-" + toParseIntoNumber);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == 'x')
            {
                // same as in the first 'if'
                BasicFunctionXNode node = new BasicFunctionXNode(s, baseNode);
                baseNode.Insert(node);
                CreateTree(node.value, node);
            }
            else if (s[0] == '(' || s[0] == ' ')
            {
                s = GetStringFromIndex(s, 1);  // practically delete that ( or ' '
                CreateTree(s, baseNode);
            }
            else if (s[0] == ')')
            {
                // count how many times ')' appears, let this number be 'i', then our head node is gonna go 'i' levels up

                int i = 0;

                while (s[i] == ')' && (s[i] != ',' || s[i] != ' '))
                {
                    i++;
                    if (i == s.Length)
                    {
                        break;
                    }
                }

                for (int j = 0; j < i; j++)
                {
                    if (baseNode.parent != null)
                    {
                        baseNode = baseNode.parent;
                    }
                    else
                    {
                        throw new Exception("Eror in your input");
                    }
                }


                s = GetStringFromIndex(s, i);
                CreateTree(s, baseNode);
            }
            else if (s[0] == ',')
            {
                if (baseNode.parent == null)
                {
                    throw new Exception("Error in your input");
                }

                // go one level up
                baseNode = baseNode.parent;
                s        = GetStringFromIndex(s, 1);
                CreateTree(s, baseNode);
            }
        }
示例#11
0
        /// <summary>
        /// To be called when a tree needs to be built upon an input string
        /// </summary>
        /// <param name="s"></param>
        public void ProcessString(string s)
        {
            if (s[0] == 's')
            {
                root = new SinNode(s, null);
            }
            else if (s[0] == '*')
            {
                root = new MultiplicationNode(s, null);
            }
            else if (s[0] == '+')
            {
                root = new SumNode(s, null);
            }
            else if (s[0] == '/')
            {
                root = new DivisionNode(s, null);
            }
            else if (s[0] == '-' && !(s[1] >= '0' && s[1] <= '9'))
            {
                root = new SubstractionNode(s, null);
            }
            else if (s[0] == 'c')
            {
                root = new CosNode(s, null);
            }
            else if (s[0] == 'l')
            {
                root = new LnNode(s, null);
            }
            else if (s[0] == '^')
            {
                root = new PowerNode(s, null);
            }
            else if (s[0] == '!')
            {
                root = new FactorialNode(s, null);
            }
            else if (s[0] == 'x')
            {
                root = new BasicFunctionXNode(s, null);
            }
            else if (s[0] >= '0' && s[0] <= '9')
            {
                string toParseIntoNumber = string.Empty;
                int    counter           = 0;

                if (s[0] == 'p')
                {
                    toParseIntoNumber = "p";
                }
                else
                {
                    do
                    {
                        toParseIntoNumber += s[counter];
                        counter++;
                    } while (counter < s.Length && (s[counter] >= '0' && s[counter] <= '9') || s[counter] == '.');
                }

                string @newS = string.Empty;

                for (int i = (s[0] == 'p' ? 1 : counter); i < s.Length; i++)
                {
                    newS += s[i];
                }

                // same stuff as in the first 'if'
                root = new NumberNode(newS, null, toParseIntoNumber);
            }
            else if (s[0] == '-' && (s[1] >= '0' && s[1] <= '9'))
            {
                // negative number
                s = Plotter.GetStringFromIndex(s, 1);

                string toParseIntoNumber = string.Empty;
                int    counter           = 0;

                if (s[0] == 'p')
                {
                    toParseIntoNumber = "p";
                }
                else
                {
                    do
                    {
                        toParseIntoNumber += s[counter];
                        counter++;
                    } while (counter < s.Length && (s[counter] >= '0' && s[counter] <= '9') || s[counter] == '.');
                }

                string @newS = string.Empty;

                for (int i = (s[0] == 'p' ? 1 : counter); i < s.Length; i++)
                {
                    newS += s[i];
                }

                // same stuff as in the first 'if'
                root = new NumberNode(newS, null, "-" + toParseIntoNumber);
            }

            CreateTree(root.value, root);
        }