Node_M PartInv() { //inv([ 2 5 7 ; 6 3 4 ; 5 -2 -3]) splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); //skip space } if (splitter.current_sign == Sign.Open) { splitter.Move_sign(); if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); //skip space } //Parse sequentially all the expression after the ( Node_M inside = PlusMinus(); // Find the ) if (splitter.current_sign != Sign.Close) //if there's no ) { Console.WriteLine("Missing closing parenthesis"); Environment.Exit(0); } else if (splitter.current_sign == Sign.Close) { int r = inside.Calculate().GetLength(0); int c = inside.Calculate().GetLength(1); double[,] matr = new double[r, c]; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { matr[i, j] = inside.Calculate()[i, j]; } } if (r == c) { int n = r; int t = 0; double[,] A = new double[r, c * 2]; //Create a copy of array for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i, j] = matr[i, j]; } } double[,] inv = new double[n, n]; if (MatrInv(A, inv, n)) { Node_M part = new NodeMatr(inv); //create the node with value of matrix splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); } return(part); } else { Console.WriteLine("Impossible to MatrInv"); Environment.Exit(0); } } else { Console.WriteLine("Impossible to MatrInv a non-square matrix"); Environment.Exit(0); } } } else { Console.WriteLine("Missing opening parenthesis"); Environment.Exit(0); } return(null); }
Node_M PartMatrOpen() { splitter.Move_sign(); List <List <int> > matr = new List <List <int> >(); int c = 0; while (splitter.current_sign != Sign.MatrClose) { matr.Add(new List <int>()); if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); //skip space } else if (splitter.current_sign == Sign.DotComa) { splitter.Move_sign(); } while (splitter.current_sign != Sign.DotComa) { if (splitter.current_sign == Sign.Minus) { splitter.Move_sign(); if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); //skip space } if (splitter.current_sign == Sign.Number) { matr[c].Add(-splitter.parsed_value); splitter.Move_sign(); //move to the next element } else { Console.WriteLine("Matrix element error"); Environment.Exit(0); } } if (splitter.current_sign == Sign.Number) { matr[c].Add(splitter.parsed_value); splitter.Move_sign(); //move to the next element } else if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); //skip space } else if (splitter.current_sign == Sign.MatrClose) { break; } else { Console.WriteLine("Matrix element error"); Environment.Exit(0); } } c++; } //Check whether each row has an even number of elements int columns = matr.Count; List <int> curr_row = matr[0]; for (int i = 1; i < columns; i++) { if (curr_row.Count == matr[i].Count) { curr_row = matr[i]; } else { Console.WriteLine("The number of colums is uneven"); Environment.Exit(0); } } double[,] matr1 = new double[columns, matr[0].Count]; int c1 = 0; int c2 = 0; for (int i = 0; i < columns; i++) { for (int j = 0; j < matr[0].Count; j++) { matr1[i, j] = matr[i][j]; } } Node_M part = new NodeMatr(matr1); splitter.Move_sign(); //move to the next sign if (splitter.current_sign == Sign.Space) { splitter.Move_sign(); //skip space } return(part); }