Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Rozkład macierzy LU = PA
- using System;
- using System.IO;
- using System.Globalization;
- namespace LUdecomposition
- {
- class LUdecomposition
- {
- public const double ZERO = 1.0e-15;
- public static void Main()
- {
- char esc;
- string str,path;
- int i,j,n;
- double[,] A;
- double[] b;
- int[] p;
- double d;
- 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("Rozwiązywanie układów równań liniowych metodą rozkładu macierzy LU = PA");
- Console.WriteLine();
- Console.WriteLine("Podaj liczbę równań n=");
- str = Console.ReadLine();
- int.TryParse(str,out n);
- A = new double[n,n];
- b = new double[n];
- p = new int[n];
- for(i = 0;i < n;i++)
- {
- Console.WriteLine("Wprowadz " + (i+1).ToString(nfi) + ". wiersz macierzy");
- for(j = 0;j < n;j++)
- {
- str = Console.ReadLine();
- double.TryParse(str,out A[i,j]);
- }
- Console.WriteLine("Wprowadz " + (i+1).ToString(nfi) + ". wyraz wolny");
- str = Console.ReadLine();
- double.TryParse(str,out b[i]);
- }
- FactorLU(n,A,p,out d);
- sw.WriteLine(d.ToString(nfi));
- for(i = 0;i < n && Math.Abs(d) > ZERO ;i++)
- {
- for(j = 0;j < n; j++)
- {
- Console.Write(A[i,j].ToString(nfi)+" , ");
- sw.Write(A[i,j].ToString(nfi) + " , ");
- }
- Console.WriteLine();
- sw.WriteLine();
- }
- if(Math.Abs(d) > ZERO)
- {
- SolveLU(n,A,p,b);
- for(i = 0;i < n && Math.Abs(d) > ZERO ;i++)
- {
- Console.WriteLine(b[i].ToString(nfi));
- sw.WriteLine(b[i].ToString(nfi));
- }
- Console.WriteLine();
- sw.WriteLine();
- }
- }
- catch(System.IndexOutOfRangeException e)
- {
- }
- esc = (char)Console.ReadKey().Key;
- }
- while(esc != (char)ConsoleKey.Escape);
- }
- }
- public static void FactorLU(int n,double[,] A,int[] p,out double d)
- {
- int k,i,j,l;
- double pmax,tmp;
- d = 1.0;
- for(k = 0;k < n ;k++)
- {
- pmax = Math.Abs(A[k,k]);
- l = k;
- for(j = k + 1;j < n; j++)
- {
- if(Math.Abs(A[j,k]) > pmax)
- {
- pmax = Math.Abs(A[j,k]);
- l = j;
- }
- }
- if(l != k)
- {
- for(j = 0;j < n; j++)
- {
- tmp = A[k,j];
- A[k,j] = A[l,j];
- A[l,j] = tmp;
- }
- d = -d;
- }
- d *= A[k,k];
- if(pmax <= ZERO)
- return ;
- p[k] = l;
- for(i = k + 1; i < n; i++)
- {
- A[i,k]/=A[k,k];
- for(j = k + 1;j < n; j++)
- A[i,j] -= A[i,k]*A[k,j];
- }
- }
- }
- public static void SolveLU(int n,double[,] LU,int[] p,double[] b)
- {
- int i,k;
- double tmp;
- for(k = 0;k < n - 1; k++)
- {
- tmp = b[k];
- b[k] = b[p[k]];
- b[p[k]] = tmp;
- }
- for(k = 1;k < n; k++)
- for(i = 0; i < k; i++)
- b[k] -= b[i]*LU[k,i];
- for(k = n - 1; k >= 0; k--)
- {
- for(i = k + 1;i < n; i++)
- b[k] -= LU[k,i]*b[i];
- b[k] /= LU[k,k];
- }
- }
- }
- }
- // Macierz odwrotna
- using System;
- using System.IO;
- using System.Globalization;
- namespace MatrixInverse
- {
- class MatrixInverse
- {
- public static void Main()
- {
- char esc;
- string str,path;
- int i,j,n;
- double[,] A;
- double d;
- 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
- {
- Console.Clear();
- Console.WriteLine("Odwracanie macierzy");
- Console.WriteLine();
- Console.WriteLine("Podaj rozmiar macierzy n=");
- str = Console.ReadLine();
- int.TryParse(str,out n);
- A = new double[n+1,2*n+1];
- for(i = 1;i <= n;i++)
- {
- Console.WriteLine("Wprowadz " + i.ToString(nfi) + ". wiersz macierzy");
- for(j = 1;j <= n;j++)
- {
- str = Console.ReadLine();
- double.TryParse(str,out A[i,j]);
- }
- }
- Inverse(A,n,out d);
- sw.WriteLine(d.ToString(nfi));
- for(i = 1;i <= n && d != 0.0 ;i++)
- {
- for(j = n + 1;j <= 2*n;j++)
- {
- Console.Write(A[i,j].ToString(nfi)+" , ");
- sw.Write(A[i,j].ToString(nfi) + " , ");
- }
- Console.WriteLine();
- sw.WriteLine();
- }
- esc = (char)Console.ReadKey().Key;
- }
- while(esc != (char)ConsoleKey.Escape);
- }
- }
- public static void Inverse(double[,] A,int n,out double d)
- {
- int i,j,k,m;
- double s,t,temp;
- d = 1.0;
- for(i = 1;i <= n;i++)
- for(j = n+1;j <= 2*n;j++)
- if(i == j - n)
- A[i,j] = 1.0;
- else
- A[i,j] = 0.0;
- for(i = 1;i <= n;i++)
- {
- m = i;
- for(j = i;j <= n;j++)
- if(Math.Abs(A[j,i]) > Math.Abs(A[m,i]))
- m = j;
- for(j = 1;j <= 2*n;j++)
- {
- temp = A[i,j];
- A[i,j] = A[m,j];
- A[m,j] = temp;
- }
- if(m != i)
- d = -d;
- if(A[i,i] == 0.0)
- {
- d = 0.0;
- return;
- }
- s = A[i,i];
- for(j = 1;j <= 2*n;j++)
- A[i,j] /= s;
- for(j = i + 1;j <= n;j++)
- {
- t = A[j,i];
- for(k = i;k <= 2*n;k++)
- A[j,k]-=A[i,k]*t;
- }
- }
- for(k = n + 1;k <= 2*n;k++)
- {
- for(i = n;i >= 1;i--)
- {
- s = A[i,k];
- for(j = i + 1;j <= n;j++)
- s -= A[i,j]*A[j,k];
- A[i,k] = s;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement