示例#1
0
        public SRECoefficient[] Solve()
        {
            for (int i = 0; i < EquationCount; i++)
            {
                var iteration = Coefs[i, i].Iteration();
                for (int j = EquationCount; j >= i; j--)
                {
                    Coefs[i, j] = iteration * Coefs[i, j];                     // optimization of expression Coefs[i, j] /= Coefs[i, i];
                }
                for (int j = i + 1; j < EquationCount; j++)
                {
                    for (int k = EquationCount; k >= 0; k--)
                    {
                        Coefs[j, k] += Coefs[j, i] * Coefs[i, k];
                    }
                }
            }

            var result = new SRECoefficient[EquationCount];

            for (int i = EquationCount - 1; i >= 0; i--)
            {
                var Sum = new SRECoefficient(Helper.EmptySymbol);
                for (int j = i + 1; j < EquationCount; j++)
                {
                    Sum += Coefs[i, j] * result[j];
                    //Sum += SRECoefficient.SimpleMultiplication(Coefs[i, j], result[j]);
                }
                result[i] = Coefs[i, EquationCount] + Sum;
                //result[i] = SRECoefficient.SimpleAddition(Coefs[i, EquationCount], Sum);
            }

            return(result);
        }
示例#2
0
        /// <summary>
        /// Умножение без оптимизации.
        /// </summary>
        /// <param name="coefficient1"></param>
        /// <param name="coefficient2"></param>
        /// <returns></returns>
        public static SRECoefficient SimpleMultiplication(SRECoefficient coefficient1, SRECoefficient coefficient2)
        {
            var factor1 = coefficient1.Symbol.IndexOfAny(new char[] { Helper.Plus }) != -1 ?
                          Helper.LeftBracket + coefficient1.Symbol + Helper.RightBracket :
                          coefficient1.Symbol;
            var factor2 = coefficient2.Symbol.IndexOfAny(new char[] { Helper.Plus }) != -1 ?
                          Helper.LeftBracket + coefficient2.Symbol + Helper.RightBracket :
                          coefficient2.Symbol;

            return(new SRECoefficient(factor1 + Helper.Multiply + factor2));
        }
        public FiniteStateMachine(string expression)
        {
            var         summands = SRECoefficient.Split(expression);
            Stack <int> leftBracketNodeNumbers = new Stack <int>();
            int         multiplyPosition, oldPosition;

            Nodes = new List <FiniteStateMachineNode>();
            int currentNumber = 0;

            FirstNode = new FiniteStateMachineNode {
                Number = currentNumber++, Symbol = Helper.IdentitySymbol
            };
            Nodes.Add(FirstNode);
            LastNodes = new List <FiniteStateMachineNode>();

            foreach (var summand in summands)
            {
                var currentNode = new FiniteStateMachineNode {
                    Number = currentNumber++
                };
                Nodes.Add(currentNode);
                FirstNode.ChildrenNodes.Add(new KeyValuePair <string, FiniteStateMachineNode>(FirstNode.Symbol, currentNode));

                oldPosition = -1;

                bool endState = false;
                do
                {
                    multiplyPosition = summand.IndexOf(Helper.Multiply, oldPosition + 1);
                    if (multiplyPosition == -1)
                    {
                        multiplyPosition = summand.Length;                         // Нужно учесть последнее слагаемое.
                    }
                    if (multiplyPosition != -1)
                    {
                        if (summand[multiplyPosition - 1] == Helper.IterationSymbol)
                        {
                            if (summand[multiplyPosition - 2] == Helper.RightBracket)
                            {
                                if (summand[multiplyPosition - 3] == Helper.IterationSymbol)
                                {
                                    var symbol = summand.Substring(oldPosition + 1, multiplyPosition - oldPosition - 4);
                                    currentNode.ChildrenNodes.Add(new KeyValuePair <string, FiniteStateMachineNode>(symbol, currentNode));
                                }
                                else
                                {
                                    var newNode = new FiniteStateMachineNode {
                                        Number = currentNumber++
                                    };
                                    var symbol = summand.Substring(oldPosition + 1, multiplyPosition - oldPosition - 3);
                                    currentNode.ChildrenNodes.Add(new KeyValuePair <string, FiniteStateMachineNode>(symbol, newNode));
                                    Nodes.Add(newNode);
                                    currentNode = newNode;
                                }
                                Nodes[leftBracketNodeNumbers.Peek()].ChildrenNodes.Add(new KeyValuePair <string, FiniteStateMachineNode>(Helper.IdentitySymbol, currentNode));
                                currentNode.ChildrenNodes.Add(new KeyValuePair <string, FiniteStateMachineNode>(
                                                                  Nodes[leftBracketNodeNumbers.Peek() + 1].Symbol, Nodes[leftBracketNodeNumbers.Pop() + 1]));
                                if (multiplyPosition == summand.Length)
                                {
                                    endState = true;
                                }
                            }
                            else
                            {
                                // Итерация узла на самого себя.
                                if (multiplyPosition + 1 < summand.Length && summand[multiplyPosition + 1] == Helper.LeftBracket)
                                {
                                    // Если следующий символ является скобкой, то нужно ввести дополнительный узел.
                                    var newNode = new FiniteStateMachineNode {
                                        Number = currentNumber++, Symbol = Helper.IdentitySymbol
                                    };
                                    currentNode = newNode;
                                }
                                else
                                {
                                    var symbol = summand.Substring(oldPosition + 1, multiplyPosition - oldPosition - 2);
                                    currentNode.ChildrenNodes.Add(new KeyValuePair <string, FiniteStateMachineNode>(symbol, currentNode));
                                    if (multiplyPosition == summand.Length)
                                    {
                                        endState = true;
                                    }
                                }
                            }
                        }
                        else
                        {
                            // Не итерация.
                            currentNode.Symbol = summand.Substring(oldPosition + 1, multiplyPosition - oldPosition - 1);
                            if (multiplyPosition == summand.Length)
                            {
                                endState = true;
                            }
                        }

                        if (!endState)
                        {
                            if (summand[multiplyPosition + 1] == Helper.LeftBracket)
                            {
                                leftBracketNodeNumbers.Push(currentNumber);                                 // Запоминаем номер ноды.
                                oldPosition = multiplyPosition + 1;
                            }
                            else
                            {
                                oldPosition = multiplyPosition;
                            }

                            if (summand[multiplyPosition - 1] != Helper.IterationSymbol)
                            {
                                var newNode = new FiniteStateMachineNode {
                                    Number = currentNumber++
                                };
                                currentNode.ChildrenNodes.Add(new KeyValuePair <string, FiniteStateMachineNode>(currentNode.Symbol, newNode));
                                Nodes.Add(newNode);
                                currentNode = newNode;
                            }
                        }
                    }
                }while (!endState);

                LastNodes.Add(currentNode);
            }
        }
示例#4
0
 private static string[] Split(SRECoefficient coefficient)
 {
     return(Split(coefficient.Symbol));
 }
示例#5
0
 /// <summary>
 /// Сложение без оптимизации.
 /// </summary>
 /// <param name="coefficient1"></param>
 /// <param name="coefficient2"></param>
 /// <returns></returns>
 public static SRECoefficient SimpleAddition(SRECoefficient coefficient1, SRECoefficient coefficient2)
 {
     return(new SRECoefficient(coefficient1.Symbol + Helper.Plus + coefficient2.Symbol));
 }