private static List <MatrixInputData> ReadFileInput(string fileName) { List <MatrixInputData> dataSets = new List <MatrixInputData>(); StreamReader stream = new StreamReader(fileName); string line; while ((line = stream.ReadLine()) != null) { if (line[0] == '#') { // convert to numbers ... example: "#4x4" -> "4x4" -> ["4", "4"] string[] size = line.Substring(1).Split('x'); int rows = int.Parse(size[0]); int cols = int.Parse(size[0]); MatrixInputData data = new MatrixInputData(); data.matrix = new float[rows, cols]; data.rhs = new float[rows, 1]; // read data for (int i = 0; i < rows; i++) { line = stream.ReadLine(); string[] values = line.Split(','); // read entire row matrix values for (int j = 0; j < cols; j++) { data.matrix[i, j] = float.Parse(values[j]); } // read the last column as rhs data.rhs[i, 0] = float.Parse(values[cols]); } dataSets.Add(data); } } return(dataSets); }
private static void SolveMatrix(MatrixInputData input) { Console.WriteLine("Processing {0}x{1}...", input.matrix.GetLength(0), input.matrix.GetLength(1)); Stopwatch watch = Stopwatch.StartNew(); float[,] inverse = new PerformanceMatrix().Inverse(input.matrix); float[,] answer = Matrix.Multiply(inverse, input.rhs); watch.Stop(); Console.WriteLine(); Console.WriteLine("Answer=\n" + FormatAsQuizAnswer(answer)); Console.WriteLine("Finished in " + watch.ElapsedMilliseconds + "ms"); }