Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Globalization;
- namespace MatrixRREF
- {
- class MatrixRREF
- {
- public static void Main()
- {
- char esc;
- string str, path;
- int i, j, n, m;
- double[,] A;
- NumberFormatInfo nfi = new NumberFormatInfo();
- nfi.NumberDecimalSeparator = ".";
- Console.WriteLine("Podaj ścieżkę do pliku w którym chcesz zapisać wynik");
- path = Console.ReadLine();
- using(StreamWriter sw = new StreamWriter(path, true)) {
- do {
- try {
- Console.Clear();
- Console.WriteLine("Sprowadzanie macierzy do postaci schodkowej zredukowanej");
- Console.WriteLine();
- Console.WriteLine("Podaj liczbe wierszy n=");
- str = Console.ReadLine();
- int.TryParse(str, out n);
- Console.WriteLine("Podaj liczbe kolumn m=");
- str = Console.ReadLine();
- int.TryParse(str, out m);
- A = new double[n, m];
- for(i = 0; i < n; i++) {
- Console.WriteLine("Wprowadz " + (i + 1).ToString(nfi) + ". wiersz macierzy");
- for(j = 0; j < m; j++) {
- str = Console.ReadLine();
- double.TryParse(str, out A[i, j]);
- }
- }
- A = rref(A);
- for(i = 0; i < n; i++) {
- for(j = 0; j < m; j++) {
- Console.Write(A[i, j].ToString(nfi) + " , ");
- sw.Write(A[i, j].ToString(nfi) + " , ");
- }
- Console.WriteLine();
- sw.WriteLine();
- }
- } catch(System.IndexOutOfRangeException e) {
- Console.WriteLine("Błędnie podany indeks");
- Console.WriteLine(e.Message);
- }
- esc = (char)Console.ReadKey().Key;
- } while(esc != (char)ConsoleKey.Escape);
- }
- }
- private static double[,] rref(double[,] matrix)
- {
- int lead = 0, rowCount = matrix.GetLength(0), columnCount = matrix.GetLength(1);
- for (int r = 0; r < rowCount; r++) {
- if (columnCount <= lead) break;
- int i = r;
- while (matrix[i, lead] == 0) {
- i++;
- if (i == rowCount) {
- i = r;
- lead++;
- if (columnCount == lead) {
- lead--;
- break;
- }
- }
- }
- for (int j = 0; j < columnCount; j++) {
- double temp = matrix[r, j];
- matrix[r, j] = matrix[i, j];
- matrix[i, j] = temp;
- }
- double div = matrix[r, lead];
- if(div != 0)
- for (int j = 0; j < columnCount; j++) matrix[r, j] /= div;
- for (int j = 0; j < rowCount; j++) {
- if (j != r) {
- double sub = matrix[j, lead];
- for (int k = 0; k < columnCount; k++) matrix[j, k] -= (sub * matrix[r, k]);
- }
- }
- lead++;
- }
- return matrix;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment