Advertisement
Guest User

LU factorization and inverse matrix

a guest
Jan 22nd, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.65 KB | None | 0 0
  1. // Rozkład macierzy LU = PA
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Globalization;
  6. namespace LUdecomposition
  7. {
  8.     class LUdecomposition
  9.     {
  10.         public const double ZERO = 1.0e-15;
  11.         public static void Main()
  12.         {
  13.             char esc;
  14.             string str,path;
  15.             int i,j,n;
  16.             double[,] A;
  17.             double[] b;
  18.             int[] p;
  19.             double d;
  20.             NumberFormatInfo nfi = new NumberFormatInfo();
  21.             nfi.NumberDecimalSeparator = ".";
  22.             Console.WriteLine("Podaj ścieżkę do pliku w którym chcesz zapisać wynik");
  23.             path = Console.ReadLine();
  24.             using(StreamWriter sw = new StreamWriter(path,true))
  25.             {
  26.                 do
  27.                 {
  28.                     try
  29.                     {
  30.                         Console.Clear();
  31.                         Console.WriteLine("Rozwiązywanie układów równań liniowych metodą rozkładu macierzy LU = PA");
  32.                         Console.WriteLine();
  33.                         Console.WriteLine("Podaj liczbę równań n=");
  34.                         str = Console.ReadLine();
  35.                         int.TryParse(str,out n);
  36.                         A = new double[n,n];
  37.                         b = new double[n];
  38.                         p = new int[n];
  39.                         for(i = 0;i < n;i++)
  40.                         {
  41.                             Console.WriteLine("Wprowadz " + (i+1).ToString(nfi) + ". wiersz macierzy");
  42.                             for(j = 0;j < n;j++)
  43.                             {
  44.                                 str = Console.ReadLine();
  45.                                 double.TryParse(str,out A[i,j]);
  46.                             }
  47.                             Console.WriteLine("Wprowadz " + (i+1).ToString(nfi) + ". wyraz wolny");
  48.                             str = Console.ReadLine();
  49.                             double.TryParse(str,out b[i]);
  50.                         }
  51.                         FactorLU(n,A,p,out d);
  52.                         sw.WriteLine(d.ToString(nfi));
  53.                         for(i = 0;i < n && Math.Abs(d) > ZERO  ;i++)
  54.                         {
  55.                             for(j = 0;j < n; j++)
  56.                             {
  57.                                 Console.Write(A[i,j].ToString(nfi)+" , ");
  58.                                 sw.Write(A[i,j].ToString(nfi) + " , ");
  59.                             }
  60.                             Console.WriteLine();
  61.                             sw.WriteLine();
  62.                         }
  63.                         if(Math.Abs(d) > ZERO)
  64.                         {
  65.                             SolveLU(n,A,p,b);
  66.                             for(i = 0;i < n && Math.Abs(d) > ZERO ;i++)
  67.                             {
  68.                                 Console.WriteLine(b[i].ToString(nfi));
  69.                                 sw.WriteLine(b[i].ToString(nfi));
  70.                             }
  71.                             Console.WriteLine();
  72.                             sw.WriteLine();
  73.                         }
  74.                     }
  75.                     catch(System.IndexOutOfRangeException e)
  76.                     {
  77.                        
  78.                     }
  79.                     esc = (char)Console.ReadKey().Key;
  80.                 }
  81.                 while(esc != (char)ConsoleKey.Escape);
  82.             }
  83.         }
  84.         public static void FactorLU(int n,double[,] A,int[] p,out double d)
  85.         {
  86.             int k,i,j,l;
  87.             double pmax,tmp;
  88.             d = 1.0;
  89.             for(k = 0;k < n ;k++)
  90.             {
  91.                 pmax = Math.Abs(A[k,k]);
  92.                 l = k;
  93.                 for(j = k + 1;j < n; j++)
  94.                 {
  95.                     if(Math.Abs(A[j,k]) > pmax)
  96.                     {
  97.                         pmax = Math.Abs(A[j,k]);
  98.                         l = j;
  99.                     }
  100.                 }
  101.                 if(l != k)
  102.                 {
  103.                     for(j = 0;j < n; j++)
  104.                     {
  105.                         tmp = A[k,j];
  106.                         A[k,j] = A[l,j];
  107.                         A[l,j] = tmp;
  108.                     }
  109.                     d = -d;
  110.                 }
  111.                 d *= A[k,k];
  112.                 if(pmax <= ZERO)
  113.                     return ;   
  114.                 p[k] = l;
  115.                 for(i = k + 1; i < n; i++)
  116.                 {
  117.                     A[i,k]/=A[k,k];
  118.                     for(j = k + 1;j < n; j++)
  119.                         A[i,j] -= A[i,k]*A[k,j];
  120.                 }
  121.             }
  122.         }
  123.         public static void SolveLU(int n,double[,] LU,int[] p,double[] b)
  124.         {
  125.             int i,k;
  126.             double tmp;
  127.             for(k = 0;k < n - 1; k++)
  128.             {
  129.                 tmp = b[k];
  130.                 b[k] = b[p[k]];
  131.                 b[p[k]] = tmp;
  132.             }
  133.             for(k = 1;k < n; k++)
  134.                 for(i = 0; i < k; i++)
  135.                     b[k] -= b[i]*LU[k,i];
  136.             for(k = n - 1; k >= 0; k--)
  137.             {
  138.                 for(i = k + 1;i < n; i++)
  139.                     b[k] -= LU[k,i]*b[i];
  140.                 b[k] /= LU[k,k];
  141.             }
  142.         }
  143.     }
  144. }
  145.  
  146. // Macierz odwrotna
  147.  
  148. using System;
  149. using System.IO;
  150. using System.Globalization;
  151. namespace MatrixInverse
  152. {
  153.     class MatrixInverse
  154.     {
  155.         public static void Main()
  156.         {
  157.             char esc;
  158.             string str,path;
  159.             int i,j,n;
  160.             double[,] A;
  161.             double d;
  162.             NumberFormatInfo nfi = new NumberFormatInfo();
  163.             nfi.NumberDecimalSeparator = ".";
  164.             Console.WriteLine("Podaj ścieżkę do pliku w którym chcesz zapisać wynik");
  165.             path = Console.ReadLine();
  166.             using(StreamWriter sw = new StreamWriter(path,true))
  167.             {
  168.                 do
  169.                 {
  170.                     Console.Clear();
  171.                     Console.WriteLine("Odwracanie macierzy");
  172.                     Console.WriteLine();
  173.                     Console.WriteLine("Podaj rozmiar macierzy n=");
  174.                     str = Console.ReadLine();
  175.                     int.TryParse(str,out n);
  176.                     A = new double[n+1,2*n+1];
  177.                     for(i = 1;i <= n;i++)
  178.                     {
  179.                         Console.WriteLine("Wprowadz " + i.ToString(nfi) + ". wiersz macierzy");
  180.                         for(j = 1;j <= n;j++)
  181.                         {
  182.                             str = Console.ReadLine();
  183.                             double.TryParse(str,out A[i,j]);
  184.                         }
  185.                     }
  186.                     Inverse(A,n,out d);
  187.                     sw.WriteLine(d.ToString(nfi));
  188.                     for(i = 1;i <= n && d != 0.0 ;i++)
  189.                     {
  190.                         for(j = n + 1;j <= 2*n;j++)
  191.                         {
  192.                             Console.Write(A[i,j].ToString(nfi)+" , ");
  193.                             sw.Write(A[i,j].ToString(nfi) + " , ");
  194.                         }
  195.                         Console.WriteLine();
  196.                         sw.WriteLine();
  197.                     }
  198.                    
  199.                     esc = (char)Console.ReadKey().Key;
  200.                 }
  201.                 while(esc != (char)ConsoleKey.Escape);
  202.             }
  203.         }
  204.         public static void Inverse(double[,] A,int n,out double d)
  205.         {
  206.             int i,j,k,m;
  207.             double s,t,temp;
  208.             d = 1.0;
  209.             for(i = 1;i <= n;i++)
  210.                 for(j = n+1;j <= 2*n;j++)
  211.                     if(i == j - n)
  212.                         A[i,j] = 1.0;
  213.                     else
  214.                         A[i,j] = 0.0;
  215.             for(i = 1;i <= n;i++)
  216.             {
  217.                 m = i;
  218.                 for(j = i;j <= n;j++)
  219.                     if(Math.Abs(A[j,i]) > Math.Abs(A[m,i]))
  220.                         m = j;
  221.                 for(j = 1;j <= 2*n;j++)
  222.                 {
  223.                     temp = A[i,j];
  224.                     A[i,j] = A[m,j];
  225.                     A[m,j] = temp;
  226.                 }
  227.                 if(m != i)
  228.                     d = -d;
  229.                 if(A[i,i] == 0.0)
  230.                 {
  231.                     d = 0.0;
  232.                     return;
  233.                 }
  234.                 s = A[i,i];
  235.                 for(j = 1;j <= 2*n;j++)
  236.                     A[i,j] /= s;
  237.                 for(j = i + 1;j <= n;j++)
  238.                 {
  239.                     t = A[j,i];
  240.                     for(k = i;k <= 2*n;k++)
  241.                         A[j,k]-=A[i,k]*t;
  242.                 }                  
  243.             }
  244.             for(k = n + 1;k <= 2*n;k++)
  245.             {
  246.                 for(i = n;i >= 1;i--)
  247.                 {
  248.                     s = A[i,k];
  249.                     for(j = i + 1;j <= n;j++)
  250.                         s -= A[i,j]*A[j,k];
  251.                     A[i,k] = s;
  252.                 }
  253.             }
  254.         }
  255.     }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement