Пример #1
0
        public override Function Derivatives()
        {
            Function DerivativeNode = new Divide();

            Function TopDerivative = new Substract();

            DerivativeNode.Left = TopDerivative;

            Function TopLeftDerivative  = new Multiply();
            Function TopRightDerivative = new Multiply();

            TopDerivative.Left       = TopLeftDerivative;
            TopDerivative.Right      = TopRightDerivative;
            TopLeftDerivative.Left   = Left.Derivatives();
            TopLeftDerivative.Right  = Right;
            TopRightDerivative.Left  = Left;
            TopRightDerivative.Right = Right.Derivatives();

            Function BottomDerivative = new Multiply();

            DerivativeNode.Right   = BottomDerivative;
            BottomDerivative.Left  = Right;
            BottomDerivative.Right = Right;

            return(DerivativeNode);
        }
Пример #2
0
        public override Function Derivatives()
        {
            Function DerivativeNode = new Substract();

            DerivativeNode.Left  = Left.Derivatives();
            DerivativeNode.Right = Right.Derivatives();
            return(DerivativeNode);
        }
        public override Function GetDerivativeAnalytically()
        {
            Substract derivative = new Substract();

            if (this.LeftFunc != null)
            {
                derivative.LeftFunc = this.LeftFunc.GetDerivativeAnalytically();
            }
            derivative.RightFunc = this.RightFunc.GetDerivativeAnalytically();
            return(derivative);
        }
Пример #4
0
 public override Function SimplifyFunction()
 {
     Left  = Left.SimplifyFunction();
     Right = Right.SimplifyFunction();
     if (Left.IsConstant() && Right.IsConstant())
     {
         return(new RealNumber(CalculateValue(0)));
     }
     if (Right.ToString() == "0")
     {
         return(Left);
     }
     if (Left is Substract && Right.IsConstant())
     {
         if (Left.Left.IsConstant())
         {
             Function function = new Substract();
             function.Left  = new RealNumber(Left.Left.CalculateValue(0) - Right.CalculateValue(0));
             function.Right = Left.Right;
             return(function.SimplifyFunction());
         }
         if (Left.Right.IsConstant())
         {
             Function function = new Substract();
             function.Left  = Left.Left;
             function.Right = new RealNumber(Left.Right.CalculateValue(0) + Right.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     if (Right is Substract && Left.IsConstant())
     {
         if (Right.Left.IsConstant())
         {
             Function function = new Plus();
             function.Left  = new RealNumber(Left.CalculateValue(0) - Right.Left.CalculateValue(0));
             function.Right = Right.Right;
             return(function.SimplifyFunction());
         }
         if (Right.Right.IsConstant())
         {
             Function function = new Substract();
             function.Left  = new RealNumber(Left.CalculateValue(0) + Right.Right.CalculateValue(0));
             function.Right = Right.Left;
             return(function.SimplifyFunction());
         }
     }
     if (Left is Plus && Right.IsConstant())
     {
         if (Left.Left.IsConstant())
         {
             Function function = new Plus();
             function.Left  = new RealNumber(Left.Left.CalculateValue(0) - Right.CalculateValue(0));
             function.Right = Left.Right;
             return(function.SimplifyFunction());
         }
         if (Left.Right.IsConstant())
         {
             Function function = new Plus();
             function.Left  = Left.Left;
             function.Right = new RealNumber(Left.Right.CalculateValue(0) - Right.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     if (Right is Plus && Left.IsConstant())
     {
         if (Right.Left.IsConstant())
         {
             Function function = new Substract();
             function.Left  = new RealNumber(Left.CalculateValue(0) - Right.Left.CalculateValue(0));
             function.Right = Right.Right;
             return(function.SimplifyFunction());
         }
         if (Right.Right.IsConstant())
         {
             Function function = new Substract();
             function.Left  = new RealNumber(Left.CalculateValue(0) - Right.Right.CalculateValue(0));
             function.Right = Right.Left;
             return(function.SimplifyFunction());
         }
     }
     if (Left is NaturalLogarithm && Right is NaturalLogarithm)
     {
         if (Left.Left.IsConstant() && Right.Left.IsConstant())
         {
             Function function = new NaturalLogarithm();
             function.Left = new RealNumber(Left.Left.CalculateValue(0) / Right.Left.CalculateValue(0));
             return(function.SimplifyFunction());
         }
     }
     return(this);
 }
Пример #5
0
        private Function CreateTree()
        {
            Function currentNode = null;
            string   token       = listNotations.First();

            switch (token)
            {
            case "c":
                currentNode = new Cosine();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "/":
                currentNode = new Divide();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            case "e":
                currentNode = new Exp();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "!":
                currentNode = new Factorial();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "*":
                currentNode = new Multiply();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            case "l":
                currentNode = new NaturalLogarithm();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "n":
                listNotations.Remove(token);
                string valueN = listNotations.First();
                currentNode = new NaturalNumber(Convert.ToInt32(valueN));
                listNotations.Remove(valueN);
                break;

            case "x":
                currentNode = new ParameterX();
                listNotations.Remove(token);
                break;

            case "p":
                currentNode = new Pi();
                listNotations.Remove(token);
                break;

            case "+":
                currentNode = new Plus();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            case "^":
                currentNode = new Power();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            case "r":
                listNotations.Remove(token);
                string valueR = listNotations.First();
                currentNode = new RealNumber(Convert.ToDouble(valueR));
                listNotations.Remove(listNotations.First());
                break;

            case "s":
                currentNode = new Sine();
                listNotations.Remove(token);
                currentNode.Left = CreateTree();
                break;

            case "-":
                currentNode = new Substract();
                listNotations.Remove(token);
                currentNode.Left  = CreateTree();
                currentNode.Right = CreateTree();
                break;

            default:
                currentNode = new NaturalNumber(Convert.ToInt32(token));
                listNotations.Remove(token);
                break;
            }
            return(currentNode);
        }
Пример #6
0
        //Method

        //Insert function to the binary tree
        public Function Insert(ref string s)
        {
            Function root = null;

            switch (s[0])
            {
            case '+':
            {
                root = new Plus();
                s    = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case '-':
            {
                root = new Substract();
                if (s[1] != '(')
                {
                    break;
                }
                s = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case '*':
            {
                root = new Multiply();
                s    = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case '/':
            {
                root = new Divide();
                s    = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case '^':
            {
                root = new Power();
                s    = s.Substring(2);
                while (s[0] != ',')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                s = s.Substring(1);
                while (s[0] != ')')
                {
                    root.RightFunc = Insert(ref s);
                    s = s.Substring(1);
                }
                break;
            }

            case 'n':
            {
                root = new n();
                s    = s.Substring(2);
                string[] parts = s.Split(')');
                (root as n).Data = Convert.ToInt32(parts[0].ToString());
                while (s[0] != ')')
                {
                    s = s.Substring(1);
                }
                break;
            }

            case 'r':
            {
                root = new r();
                s    = s.Substring(2);
                string[] parts = s.Split(')');
                (root as r).Data = Convert.ToDouble(parts[0].ToString());
                while (s[0] != ')')
                {
                    s = s.Substring(1);
                }
                break;
            }

            case 's':
            {
                root = new S();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case 'c':
            {
                root = new c();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case 'e':
            {
                root = new e();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case 'l':
            {
                root = new l();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case '!':
            {
                root = new Factorial();
                s    = s.Substring(2);
                while (s[0] != ')')
                {
                    root.LeftFunc = Insert(ref s);
                    s             = s.Substring(1);
                }
                break;
            }

            case 'p':
            {
                root = new pi();
                break;
            }

            case 'x':
            {
                root = new x();
                break;
            }

            default:
            {
                root = new Digit();
                (root as Digit).Data = Convert.ToInt32(s[0].ToString());
                break;
            }
            }
            return(root);
        }