MariuszM

Untitled

Aug 21st, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.02 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Globalization;
  4. namespace MatrixInverse
  5. {
  6.     static class ExtensionClass
  7.     {
  8.         public static void PrintMatrix(this StreamWriter sw,double[,] A)
  9.         {
  10.             int m = A.GetLength(0);
  11.             int n = A.GetLength(1);
  12.             NumberFormatInfo nfi = new NumberFormatInfo();
  13.             for(int i = 0;i < m;i++)
  14.             {
  15.                 for(int j = 0;j < n;j++)
  16.                     sw.Write("{0} ",A[i,j].ToString(nfi));
  17.                 sw.WriteLine();
  18.             }
  19.             sw.WriteLine();
  20.         }
  21.     }
  22.     class MatrixInverse
  23.     {
  24.         public const double EPS = 1e-12;
  25.         public static void Main()
  26.         {
  27.             char esc;
  28.             string str,path;
  29.             int i,j,n;
  30.             double[,] A;
  31.             uint error;
  32.             NumberFormatInfo nfi = new NumberFormatInfo();
  33.             nfi.NumberDecimalSeparator = ".";
  34.             Console.WriteLine("Podaj ścieżkę do pliku w którym chcesz zapisać wynik");
  35.             path = Console.ReadLine();
  36.             using(StreamWriter sw = new StreamWriter(path,true))
  37.             {
  38.                 do
  39.                 {
  40.                     Console.Clear();
  41.                     Console.WriteLine("Odwracanie macierzy");
  42.                     Console.WriteLine();
  43.                     Console.WriteLine("Podaj rozmiar macierzy n=");
  44.                     str = Console.ReadLine();
  45.                     int.TryParse(str,out n);
  46.                     A = new double[n,n];
  47.                     for(i = 0;i < n;i++)
  48.                     {
  49.                         Console.WriteLine("Wprowadz " + i.ToString(nfi) + ". wiersz macierzy");
  50.                         for(j = 0;j < n;j++)
  51.                         {
  52.                             str = Console.ReadLine();
  53.                             double.TryParse(str,out A[i,j]);
  54.                         }
  55.                     }
  56.                     PrintMatrix(A);
  57.                     Inverse(A,EPS,out error);
  58.                     sw.PrintMatrix(A);
  59.                     PrintMatrix(A);
  60.                     sw.WriteLine(error.ToString(nfi));
  61.                    
  62.                     esc = (char)Console.ReadKey().Key;
  63.                 }
  64.                 while(esc != (char)ConsoleKey.Escape);
  65.             }
  66.         }
  67.         public static void PrintMatrix(double[,] A)
  68.         {
  69.             int m = A.GetLength(0);
  70.             int n = A.GetLength(1);
  71.             NumberFormatInfo nfi = new NumberFormatInfo();
  72.             for(int i = 0;i < m;i++)
  73.             {
  74.                 for(int j = 0;j < n;j++)
  75.                     Console.Write("{0} ",A[i,j].ToString(nfi));
  76.                 Console.WriteLine();
  77.             }
  78.             Console.WriteLine();
  79.         }
  80.         public static void Inverse(double[,] A,double eps,out uint error)
  81.         {
  82.             int n;
  83.             if(A.GetLength(0) != A.GetLength(1))
  84.             {
  85.                 error = 2;
  86.                 return;
  87.             }
  88.             n = A.GetLength(0);
  89.             int[] M = new int[n];
  90.             double maxA,d,e;
  91.             int k;
  92.             error = 0;
  93.             for(int i = 0; i < n;i++)
  94.             {
  95.                 maxA = 0;
  96.                 k = i;
  97.                 for(int j = i; j < n;j++)
  98.                 {
  99.                     d = A[j,i];
  100.                     if(Math.Abs(maxA) < Math.Abs(d))
  101.                     {
  102.                         maxA = d;
  103.                         k = j;
  104.                     }
  105.                 }
  106.                 if(Math.Abs(maxA) < eps)
  107.                 {
  108.                     error = 1;
  109.                     return;
  110.                 }
  111.                 M[i] = k;
  112.                 A[k,i] = 1;
  113.                 for(int j = 0;j < n;j++)
  114.                 {
  115.                     d = A[k,j]/maxA;
  116.                     A[k,j] = A[i,j];
  117.                     A[i,j] = d;
  118.                 }
  119.                 for(int j = 0;j < n;j++)
  120.                     if(j != i)
  121.                     {
  122.                         d = A[j,i];
  123.                         A[j,i] = 0;
  124.                         for(int l = 0;l < n;l++)
  125.                         {
  126.                             e = d*A[i,l];
  127.                             A[j,l] -= e;
  128.                         }
  129.                     }
  130.             }
  131.             for(int i = n-1;i >= 0;i--)
  132.             {
  133.                 k = M[i];
  134.                 if(k != i)
  135.                     for(int j=0;j < n;j++)
  136.                     {
  137.                         d = A[j,i];
  138.                         A[j,i] = A[j,k];
  139.                         A[j,k] = d;
  140.                     }
  141.             }
  142.         }
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment