Exemplo n.º 1
0
        // numbes, constants, variables and brakets
        private ExpressionTreeNode ExprLevel6()
        {
            ExpressionTreeNode temp = null;

            switch (CurrentLexeme.Lexeme)
            {
            case Lexeme.Number:
            case Lexeme.Pi:
            case Lexeme.E:
            case Lexeme.Var:
                temp = new ExpressionTreeNode(CurrentLexeme, null, null);
                NextLexeme();
                break;

            case Lexeme.LeftBracket:
                NextLexeme();
                temp = ExprLevel1();
                if (CurrentLexeme.Lexeme != Lexeme.RightBracket)
                {
                    throw new ExpressionSyntaxException(") excepted", _expression, _parsedExpression);
                }
                NextLexeme();
                break;
            }

            return(temp);
        }
Exemplo n.º 2
0
        private SortedList FindVariables(ExpressionTreeNode node)
        {
            SortedList list = new SortedList();

            if (node.ExpressionLexeme.Lexeme == Lexeme.Var)
            {
                list.Add(node.ExpressionLexeme.LexemeName.ToLower(), "");
            }

            if (node.Left != null)
            {
                foreach (DictionaryEntry e in FindVariables(node.Left))
                {
                    if (!list.ContainsKey(e.Key))
                    {
                        list.Add(e.Key, e.Value);
                    }
                }
            }

            if (node.Right != null)
            {
                foreach (DictionaryEntry e in FindVariables(node.Right))
                {
                    if (!list.ContainsKey(e.Key))
                    {
                        list.Add(e.Key, e.Value);
                    }
                }
            }

            return(list);
        }
Exemplo n.º 3
0
        // ^ operator
        private ExpressionTreeNode ExprLevel3()
        {
            ExpressionTreeNode temp = ExprLevel4();
            ExpressionLexeme   lex;

            while (true)
            {
                switch (CurrentLexeme.Lexeme)
                {
                case Lexeme.Power:
                    if (temp == null)
                    {
                        throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);
                    }
                    lex = CurrentLexeme;
                    NextLexeme();
                    ExpressionTreeNode right = ExprLevel3();
                    if (right == null)
                    {
                        throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);
                    }
                    temp = new ExpressionTreeNode(lex, temp, right);
                    break;

                default:
                    return(temp);
                }
            }
        }
Exemplo n.º 4
0
        // unary + - operators
        private ExpressionTreeNode ExprLevel4()
        {
            ExpressionTreeNode temp = null;
            ExpressionLexeme   lex;

            switch (CurrentLexeme.Lexeme)
            {
            case Lexeme.Plus:
                NextLexeme();
                temp = ExprLevel5();
                break;

            case Lexeme.Minus:
                lex = new ExpressionLexeme("", Lexeme.UMinus);
                NextLexeme();
                ExpressionTreeNode right = ExprLevel5();
                if (right == null)
                {
                    throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);
                }
                temp = new ExpressionTreeNode(lex, right);
                break;

            default:
                temp = ExprLevel5();
                break;
            }

            return(temp);
        }
Exemplo n.º 5
0
        // math functions
        private ExpressionTreeNode ExprLevel5()
        {
            ExpressionTreeNode temp = null;

            switch (CurrentLexeme.LexemeType)
            {
            case LexemeType.ltFunction:
                temp = new ExpressionTreeNode(CurrentLexeme, null, null);
                NextLexeme();
                if (CurrentLexeme.Lexeme != Lexeme.LeftBracket)
                {
                    throw new ExpressionSyntaxException("( excepted", _expression, _parsedExpression);
                }
                NextLexeme();

                ExpressionTreeNode left = ExprLevel1();
                if (left == null)
                {
                    throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);
                }
                temp.Left = left;
                if (CurrentLexeme.Lexeme != Lexeme.RightBracket)
                {
                    throw new ExpressionSyntaxException(") excepted", _expression, _parsedExpression);
                }
                NextLexeme();
                break;

            default:
                temp = ExprLevel6();
                break;
            }

            return(temp);
        }
Exemplo n.º 6
0
		private void CreateTree(ExpressionLexeme[] lexemes)
		{
			_root = null;
			_lexemesQueue = new Queue (lexemes);
			NextLexeme();

			_root = ExprLevel1();
			if (CurrentLexeme.Lexeme != ExpressionLexeme.EndLexeme.Lexeme)
				throw new ExpressionSyntaxException("operator expected", _expression, _parsedExpression);
		}
Exemplo n.º 7
0
        private void CreateTree(ExpressionLexeme[] lexemes)
        {
            _root         = null;
            _lexemesQueue = new Queue(lexemes);
            NextLexeme();

            _root = ExprLevel1();
            if (CurrentLexeme.Lexeme != ExpressionLexeme.EndLexeme.Lexeme)
            {
                throw new ExpressionSyntaxException("operator expected", _expression, _parsedExpression);
            }
        }
Exemplo n.º 8
0
		// * / operators
		private ExpressionTreeNode ExprLevel2()
		{
			ExpressionTreeNode temp = ExprLevel3();
			ExpressionLexeme lex;

			while (true)
				switch (CurrentLexeme.Lexeme)
				{
					case Lexeme.Multiply:
					case Lexeme.Divide:		
						if (temp == null) throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);						
						lex = CurrentLexeme;
						NextLexeme();
						ExpressionTreeNode right = ExprLevel2();
						if (right == null) throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);						
						temp = new ExpressionTreeNode(lex,temp,right);
						break;
					default:
						return temp;
				}
		}
Exemplo n.º 9
0
        private static string df(ExpressionTreeNode node, string diffVar)
        {
            if (!node.HasVariable(diffVar))
            {
                return("0");
            }

            bool leftDiffVar  = (node.Left != null) && (node.Left.HasVariable(diffVar));
            bool rightDiffVar = (node.Right != null) && (node.Right.HasVariable(diffVar));

            switch (node.ExpressionLexeme.LexemeType)
            {
            case LexemeType.ltConstant:
                switch (node.ExpressionLexeme.Lexeme)
                {
                case Lexeme.Var: return("1");

                default: return("0");
                }

            case LexemeType.ltOperator:
                switch (node.ExpressionLexeme.Lexeme)
                {
                case Lexeme.Plus:
                case Lexeme.Minus:
                    return(String.Format("({0}{1}{2})",
                                         df(node.Left, diffVar),
                                         node.ExpressionLexeme.LexemeName,
                                         df(node.Right, diffVar)));

                case Lexeme.UMinus:
                    return(String.Format("(-({0}))", df(node.Left, diffVar)));

                case Lexeme.Multiply:
                    return(String.Format("(({0})*({1})+({2})*({3}))",
                                         df(node.Left, diffVar),
                                         node.Right,
                                         df(node.Right, diffVar),
                                         node.Left));

                case Lexeme.Divide:
                    return(String.Format("(({0})*({1})-({2})*({3}))/({4})^2",
                                         df(node.Left, diffVar),
                                         node.Right,
                                         df(node.Right, diffVar),
                                         node.Left,
                                         node.Right));

                case Lexeme.Power:
                    // diff f(x)^g(y)
                    if (leftDiffVar && !rightDiffVar)
                    {
                        return(String.Format("((({0})^({1})*({1})*({2}))/({0}))",
                                             node.Left, node.Right, df(node.Left, diffVar)));
                    }
                    // diff f(y)^g(x)
                    if (!leftDiffVar && rightDiffVar)
                    {
                        return(String.Format("((({0})^({1}))*({2})*log({0})))",
                                             node.Left, node.Right, df(node.Right, diffVar)));
                    }
                    // diff f(x)^g(x)
                    return(String.Format("((({0})^({1}))*(({3})*log({0})+({1})*({2})/({0})))",
                                         node.Left, node.Right,
                                         df(node.Left, diffVar), df(node.Right, diffVar)));

                default: throw new Exception();
                }

            case LexemeType.ltFunction:
                return(String.Format("((" + node.ExpressionLexeme.LexemeDiffMask + ")*({1}))",
                                     node.Left, df(node.Left, diffVar)));

            default:
                throw new Exception();
            }
        }
Exemplo n.º 10
0
 public ExpressionTreeNode(ExpressionLexeme lexeme, ExpressionTreeNode leftNode, ExpressionTreeNode rightNode)
 {
     ExpressionLexeme = lexeme;
     Left             = leftNode;
     Right            = rightNode;
 }
Exemplo n.º 11
0
 public ExpressionTreeNode(ExpressionLexeme lexeme, ExpressionTreeNode leftNode) :
     this(lexeme, leftNode, null)
 {
 }
Exemplo n.º 12
0
		// math functions
		private ExpressionTreeNode ExprLevel5()
		{
			ExpressionTreeNode temp = null;
			
			switch (CurrentLexeme.LexemeType)
			{									
				case LexemeType.ltFunction:
					temp = new ExpressionTreeNode(CurrentLexeme,null,null);
					NextLexeme();
					if (CurrentLexeme.Lexeme != Lexeme.LeftBracket)
						throw new ExpressionSyntaxException("( excepted", _expression, _parsedExpression);
					NextLexeme();

					ExpressionTreeNode left = ExprLevel1();						
					if (left == null) throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);																							
					temp.Left = left;
					if (CurrentLexeme.Lexeme != Lexeme.RightBracket)
						throw new ExpressionSyntaxException(") excepted", _expression, _parsedExpression);
					NextLexeme();
					break;
				default:
					temp = ExprLevel6();
					break;
			}

			return temp;

		}
Exemplo n.º 13
0
        private ExpressionTreeNode SimplifyNode(ExpressionTreeNode node)
        {
            if (node.Left != null)
            {
                node.Left = SimplifyNode(node.Left);
            }
            if (node.Right != null)
            {
                node.Right = SimplifyNode(node.Right);
            }

            bool l_null = node.Left == null;
            bool r_null = node.Right == null;

            bool l_number = !l_null && (node.Left.ExpressionLexeme.Lexeme == Lexeme.Number);
            bool r_number = !r_null && (node.Right.ExpressionLexeme.Lexeme == Lexeme.Number);

            double l_value = l_number ? double.Parse(node.Left.ExpressionLexeme.LexemeName) : 0;
            double r_value = r_number ? double.Parse(node.Right.ExpressionLexeme.LexemeName) : 0;

            bool l_zero = l_number && (l_value == 0);
            bool r_zero = r_number && (r_value == 0);

            bool l_one = l_number && (l_value == 1);
            bool r_one = r_number && (r_value == 1);

            switch (node.ExpressionLexeme.LexemeType)
            {
            case LexemeType.ltOperator:

                bool equal = (!l_null && !r_null) &&
                             (0 == String.Compare(node.Left.ToString(), node.Right.ToString()));

                switch (node.ExpressionLexeme.Lexeme)
                {
                case Lexeme.Multiply:
                    if (l_zero || r_zero)
                    {
                        return(new ExpressionTreeNode(
                                   new ExpressionLexeme("0", Lexeme.Number)));
                    }
                    if (l_one)
                    {
                        return(node.Right);
                    }
                    if (r_one)
                    {
                        return(node.Left);
                    }
                    if (l_number && r_number)
                    {
                        return(new ExpressionTreeNode(
                                   new ExpressionLexeme(Convert.ToString(l_value * r_value), Lexeme.Number)));
                    }
                    break;

                case Lexeme.Divide:
                    if (l_zero && !r_zero)
                    {
                        return(new ExpressionTreeNode(new ExpressionLexeme("0", Lexeme.Number)));
                    }
                    if (equal)
                    {
                        return(new ExpressionTreeNode(new ExpressionLexeme("1", Lexeme.Number)));
                    }
                    if (r_one)
                    {
                        return(node.Left);
                    }
                    if (l_number && r_number)
                    {
                        return(new ExpressionTreeNode(
                                   new ExpressionLexeme(Convert.ToString(l_value / r_value), Lexeme.Number)));
                    }
                    break;

                case Lexeme.Plus:
                    if (l_zero)
                    {
                        return(node.Right);
                    }
                    if (r_zero)
                    {
                        return(node.Left);
                    }
                    if (l_number && r_number)
                    {
                        return(new ExpressionTreeNode(
                                   new ExpressionLexeme(Convert.ToString(l_value + r_value), Lexeme.Number)));
                    }

                    if (node.Right.ExpressionLexeme.Lexeme == Lexeme.UMinus)
                    {
                        return(new ExpressionTreeNode(
                                   new ExpressionLexeme("-", Lexeme.Minus), node.Left, node.Right.Left));
                    }
                    break;

                case Lexeme.Minus:
                    if (r_zero)
                    {
                        return(node.Left);
                    }
                    if (l_zero)
                    {
                        return(new ExpressionTreeNode(
                                   new ExpressionLexeme("-", Lexeme.UMinus), node.Right));
                    }

                    if (equal)
                    {
                        return(new ExpressionTreeNode(new ExpressionLexeme("0", Lexeme.Number)));
                    }
                    if (l_number && r_number)
                    {
                        return(new ExpressionTreeNode(
                                   new ExpressionLexeme(Convert.ToString(l_value - r_value), Lexeme.Number)));
                    }
                    break;

                case Lexeme.UMinus:
                    if (node.Left.ExpressionLexeme.Lexeme == Lexeme.UMinus)
                    {
                        return(node.Left.Left);
                    }
                    break;
                }
                break;
            }

            return(node);
        }
Exemplo n.º 14
0
 /// <summary>Simplifies binary tree of expression.</summary>
 /// <remarks>
 ///     <para><strong>Simplify</strong> method removes all unneccessary nodes that can be
 /// simplifided. For example, expression "1+(x*0)" will be simplifided to "1" because (x*0)
 /// will always return zero.</para>
 ///     <para>Other samples of simplification</para>
 ///     <para>"0/x + x" -&gt; "x"</para>
 ///     <para>"x/x" -&gt; "1"</para>
 /// </remarks>
 public void Simplify()
 {
     _root = SimplifyNode(_root);
 }
Exemplo n.º 15
0
		// numbes, constants, variables and brakets
		private ExpressionTreeNode ExprLevel6()
		{
			ExpressionTreeNode temp = null;

			switch (CurrentLexeme.Lexeme)
			{
				case Lexeme.Number:
				case Lexeme.Pi:
				case Lexeme.E:
				case Lexeme.Var:
					temp = new ExpressionTreeNode(CurrentLexeme,null,null);
					NextLexeme();
					break;
				case Lexeme.LeftBracket:
					NextLexeme();
					temp = ExprLevel1();
					if (CurrentLexeme.Lexeme != Lexeme.RightBracket)
						throw new ExpressionSyntaxException(") excepted", _expression, _parsedExpression);
					NextLexeme();
					break;
			}

			return temp;
		}
Exemplo n.º 16
0
		private SortedList FindVariables(ExpressionTreeNode node)
		{
			SortedList list = new SortedList();

			if (node.ExpressionLexeme.Lexeme == Lexeme.Var)
			{
				list.Add(node.ExpressionLexeme.LexemeName.ToLower(),"");
			}

			if (node.Left != null)
				foreach(DictionaryEntry e in FindVariables(node.Left))
				{
					if (!list.ContainsKey(e.Key))
						list.Add(e.Key,e.Value);
				}
		
			if (node.Right != null)
				foreach(DictionaryEntry e in FindVariables(node.Right))
				{
					if (!list.ContainsKey(e.Key))
						list.Add(e.Key,e.Value);
				}

			return list;
		}
Exemplo n.º 17
0
		// unary + - operators
		private ExpressionTreeNode ExprLevel4()
		{
			ExpressionTreeNode temp = null;
			ExpressionLexeme lex;

			switch (CurrentLexeme.Lexeme)
			{
				case Lexeme.Plus:
					NextLexeme();
					temp = ExprLevel5();
					break;
				case Lexeme.Minus:
					lex = new ExpressionLexeme("",Lexeme.UMinus);
					NextLexeme();
					ExpressionTreeNode right = ExprLevel5();						
					if (right == null) throw new ExpressionSyntaxException("expression expected", _expression, _parsedExpression);																		
					temp = new ExpressionTreeNode(lex,right);
					break;

				default:
					temp = ExprLevel5();
					break;
			}

			return temp;
		}
Exemplo n.º 18
0
		public ExpressionTreeNode(ExpressionLexeme lexeme,ExpressionTreeNode leftNode) : 
			this (lexeme, leftNode,null)
		{
		}
Exemplo n.º 19
0
		/// <summary>Simplifies binary tree of expression.</summary>
		/// <remarks>
		/// 	<para><strong>Simplify</strong> method removes all unneccessary nodes that can be
		/// simplifided. For example, expression "1+(x*0)" will be simplifided to "1" because (x*0)
		/// will always return zero.</para>
		/// 	<para>Other samples of simplification</para>
		/// 	<para>"0/x + x" -&gt; "x"</para>
		/// 	<para>"x/x" -&gt; "1"</para>
		/// </remarks>
		public void Simplify()
		{
			_root = SimplifyNode(_root);		
		}
Exemplo n.º 20
0
		private ExpressionTreeNode SimplifyNode(ExpressionTreeNode node)
		{
			if (node.Left != null) node.Left = SimplifyNode(node.Left);
			if (node.Right != null)	node.Right = SimplifyNode(node.Right);

			bool l_null = node.Left == null;
			bool r_null = node.Right == null;

			bool l_number = !l_null && (node.Left.ExpressionLexeme.Lexeme == Lexeme.Number);
			bool r_number = !r_null && (node.Right.ExpressionLexeme.Lexeme == Lexeme.Number);

			double l_value = l_number ? double.Parse(node.Left.ExpressionLexeme.LexemeName) : 0;
			double r_value = r_number ? double.Parse(node.Right.ExpressionLexeme.LexemeName) : 0;

			bool l_zero = l_number && (l_value == 0);
			bool r_zero = r_number && (r_value == 0);
			
			bool l_one = l_number && (l_value == 1);
			bool r_one = r_number && (r_value == 1);

			switch (node.ExpressionLexeme.LexemeType)
			{
				case LexemeType.ltOperator:
						
					bool equal = (!l_null && !r_null) 
						&& (0==String.Compare(node.Left.ToString(),node.Right.ToString()));
					
				switch(node.ExpressionLexeme.Lexeme)
				{
					case Lexeme.Multiply:
						if (l_zero || r_zero) return new ExpressionTreeNode(
												  new ExpressionLexeme("0",Lexeme.Number));
						if (l_one) return node.Right;
						if (r_one) return node.Left;
						if (l_number && r_number) return new ExpressionTreeNode(
													  new ExpressionLexeme(Convert.ToString(l_value*r_value),Lexeme.Number));					
						break;

					case Lexeme.Divide:
						if (l_zero && !r_zero) return new ExpressionTreeNode(new ExpressionLexeme("0",Lexeme.Number));
						if (equal) return new ExpressionTreeNode(new ExpressionLexeme("1",Lexeme.Number));
						if (r_one) return node.Left;
						if (l_number && r_number) 
							return new ExpressionTreeNode(
								new ExpressionLexeme(Convert.ToString(l_value/r_value),Lexeme.Number));					
						break;
					
					case Lexeme.Plus:
						if (l_zero) return node.Right;
						if (r_zero) return node.Left;
						if (l_number && r_number) 
							return new ExpressionTreeNode(
								new ExpressionLexeme(Convert.ToString(l_value+r_value),Lexeme.Number));						
						
						if (node.Right.ExpressionLexeme.Lexeme == Lexeme.UMinus)
							return new ExpressionTreeNode(
								new ExpressionLexeme("-",Lexeme.Minus),node.Left,node.Right.Left);
						break;

					case Lexeme.Minus:
						if (r_zero) return node.Left;
						if (l_zero) return new ExpressionTreeNode(
										new ExpressionLexeme("-",Lexeme.UMinus),node.Right);

						if (equal) return new ExpressionTreeNode(new ExpressionLexeme("0",Lexeme.Number));
						if (l_number && r_number) 
							return new ExpressionTreeNode(
								new ExpressionLexeme(Convert.ToString(l_value-r_value),Lexeme.Number));						
						break;

					case Lexeme.UMinus:
						if (node.Left.ExpressionLexeme.Lexeme == Lexeme.UMinus)
							return node.Left.Left;
						break;
				}
					break;

			}
			
			return node;
		}
Exemplo n.º 21
0
		public ExpressionTreeNode(ExpressionLexeme lexeme,ExpressionTreeNode leftNode,ExpressionTreeNode rightNode )
		{
			ExpressionLexeme = lexeme;
			Left = leftNode;
			Right = rightNode;
		}
		private static string df(ExpressionTreeNode node, string diffVar)
		{
			if (!node.HasVariable(diffVar)) return "0";

			bool leftDiffVar = (node.Left != null) && (node.Left.HasVariable(diffVar));
			bool rightDiffVar = (node.Right != null) && (node.Right.HasVariable(diffVar));

			switch (node.ExpressionLexeme.LexemeType)
			{
				case LexemeType.ltConstant:					
				switch (node.ExpressionLexeme.Lexeme)
				{
					case Lexeme.Var: return "1";
					default: return "0";
				}
					
				case LexemeType.ltOperator:					
				switch (node.ExpressionLexeme.Lexeme)
				{
					case Lexeme.Plus:
					case Lexeme.Minus:
						return String.Format("({0}{1}{2})",
							df(node.Left,diffVar),
							node.ExpressionLexeme.LexemeName,
							df(node.Right,diffVar));
				
					case Lexeme.UMinus:
						return String.Format("(-({0}))",df(node.Left,diffVar));

					case Lexeme.Multiply:
						return String.Format("(({0})*({1})+({2})*({3}))", 
							df(node.Left, diffVar), 
							node.Right, 
							df(node.Right, diffVar), 
							node.Left);

					case Lexeme.Divide:
						return String.Format("(({0})*({1})-({2})*({3}))/({4})^2", 
							df(node.Left, diffVar), 
							node.Right, 
							df(node.Right, diffVar), 
							node.Left, 
							node.Right);
				
					case Lexeme.Power:
						// diff f(x)^g(y)
						if (leftDiffVar && !rightDiffVar)
						{
							return String.Format("((({0})^({1})*({1})*({2}))/({0}))",
								node.Left, node.Right, df(node.Left, diffVar));
						}
						// diff f(y)^g(x)
						if (!leftDiffVar && rightDiffVar)                                     
						{
							return String.Format("((({0})^({1}))*({2})*log({0})))", 
								node.Left, node.Right, df(node.Right, diffVar));           
						}
						// diff f(x)^g(x)
						return String.Format("((({0})^({1}))*(({3})*log({0})+({1})*({2})/({0})))", 
							node.Left, node.Right, 
							df(node.Left, diffVar), df(node.Right, diffVar));
					default: throw new Exception();
				}

				case LexemeType.ltFunction:										
					return String.Format("(("+node.ExpressionLexeme.LexemeDiffMask+")*({1}))",
						node.Left, df(node.Left, diffVar));

				default:
					throw new Exception();
			}
		}