Guest User

rref

a guest
Jun 1st, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Globalization;
  4. namespace MatrixRREF
  5. {
  6.     class MatrixRREF
  7.     {
  8.         public static void Main()
  9.         {
  10.             char esc;
  11.             string str, path;
  12.             int i, j, n, m;
  13.             double[,] A;
  14.             NumberFormatInfo nfi = new NumberFormatInfo();
  15.             nfi.NumberDecimalSeparator = ".";
  16.             Console.WriteLine("Podaj ścieżkę do pliku w którym chcesz zapisać wynik");
  17.             path = Console.ReadLine();
  18.             using(StreamWriter sw = new StreamWriter(path, true)) {
  19.                 do {
  20.                     try {
  21.                         Console.Clear();
  22.                         Console.WriteLine("Sprowadzanie macierzy do postaci schodkowej zredukowanej");
  23.                         Console.WriteLine();
  24.                         Console.WriteLine("Podaj liczbe wierszy n=");
  25.                         str = Console.ReadLine();
  26.                         int.TryParse(str, out n);
  27.                         Console.WriteLine("Podaj liczbe kolumn m=");
  28.                         str = Console.ReadLine();
  29.                         int.TryParse(str, out m);
  30.                         A = new double[n, m];
  31.                         for(i = 0; i < n; i++) {
  32.                             Console.WriteLine("Wprowadz " + (i + 1).ToString(nfi) + ". wiersz macierzy");
  33.                             for(j = 0; j < m; j++) {
  34.                                 str = Console.ReadLine();
  35.                                 double.TryParse(str, out A[i, j]);
  36.                             }
  37.                         }
  38.                         A = rref(A);
  39.                         for(i = 0; i < n; i++) {
  40.                             for(j = 0; j < m; j++) {
  41.                                 Console.Write(A[i, j].ToString(nfi) + " , ");
  42.                                 sw.Write(A[i, j].ToString(nfi) + " , ");
  43.                             }
  44.                             Console.WriteLine();
  45.                             sw.WriteLine();
  46.                         }
  47.                     } catch(System.IndexOutOfRangeException e) {
  48.                         Console.WriteLine("Błędnie podany indeks");
  49.                         Console.WriteLine(e.Message);
  50.                     }
  51.                     esc = (char)Console.ReadKey().Key;
  52.                 } while(esc != (char)ConsoleKey.Escape);
  53.             }
  54.         }
  55.         private static double[,] rref(double[,] matrix)
  56.         {
  57.             int lead = 0, rowCount = matrix.GetLength(0), columnCount = matrix.GetLength(1);
  58.             for (int r = 0; r < rowCount; r++) {
  59.                 if (columnCount <= lead) break;
  60.                 int i = r;
  61.                 while (matrix[i, lead] == 0) {
  62.                     i++;
  63.                     if (i == rowCount) {
  64.                         i = r;
  65.                         lead++;
  66.                         if (columnCount == lead) {
  67.                             lead--;
  68.                             break;
  69.                         }
  70.                     }
  71.                 }
  72.                 for (int j = 0; j < columnCount; j++) {
  73.                     double temp = matrix[r, j];
  74.                     matrix[r, j] = matrix[i, j];
  75.                     matrix[i, j] = temp;
  76.                 }
  77.                 double div = matrix[r, lead];
  78.                 if(div != 0)
  79.                     for (int j = 0; j < columnCount; j++) matrix[r, j] /= div;
  80.                 for (int j = 0; j < rowCount; j++) {
  81.                     if (j != r) {
  82.                         double sub = matrix[j, lead];
  83.                         for (int k = 0; k < columnCount; k++) matrix[j, k] -= (sub * matrix[r, k]);
  84.                     }
  85.                 }
  86.                 lead++;
  87.             }
  88.             return matrix;
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment