zadanie
mat:
1 0 5
2 1 3
−1 4 2
Oblicz A−1
7 lut 09:05
Mariusz:
Jaką metodą ?
Wyznacznikową
Złożoność O(n5)
Rozwiązujesz metodą Cramera
n układów równań liniowych gdzie
kolumny wyrazów wolnych są kolumnami macierzy jednostkowej
Z twierdzenia Cayleya−Hamiltona
Znajdujesz wielomian charakterystyczny
Stosujesz twierdzenie Cayleya−Hamiltona
I przekształcasz równanie macierzowe
Złożoność O(n4)
Metodą eliminacji
Złożoność O(n3)
Do odwracanej macierzy dołączasz macierz jednostkową i wykonujesz
na niej operacje elementarne aż wyjściowa macierz przyjmie postać
macierzy jednostkowej wtedy ta dołączona macierz stanie się macierzą odwrotną
7 lut 11:37
Mariusz:
Przeanalizuj sobie kod następującego programu
using System;
using System.IO;
using System.Globalization;
namespace MatrixInverse
{
static class ExtensionClass
{
public static void PrintMatrix(this StreamWriter sw,double[,] A)
{
int m = A.GetLength(0);
int n = A.GetLength(1);
NumberFormatInfo nfi = new NumberFormatInfo();
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
sw.Write("{0} ",A[i,j].ToString(nfi));
sw.WriteLine();
}
sw.WriteLine();
}
}
class MatrixInverse
{
public const double EPS = 1e−12;
public static void Main()
{
char esc;
string str,path;
int i,j,n;
double[,] A;
uint error;
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,n];
for(i = 0;i < n;i++)
{
Console.WriteLine("Wprowadz " + i.ToString(nfi) + ". wiersz macierzy");
for(j = 0;j < n;j++)
{
str = Console.ReadLine();
double.TryParse(str,out A[i,j]);
}
}
PrintMatrix(A);
Inverse(A,EPS,out error);
sw.PrintMatrix(A);
PrintMatrix(A);
sw.WriteLine(error.ToString(nfi));
esc = (char)Console.ReadKey().Key;
}
while(esc != (char)ConsoleKey.Escape);
}
}
public static void PrintMatrix(double[,] A)
{
int m = A.GetLength(0);
int n = A.GetLength(1);
NumberFormatInfo nfi = new NumberFormatInfo();
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
Console.Write("{0} ",A[i,j].ToString(nfi));
Console.WriteLine();
}
Console.WriteLine();
}
public static void Inverse(double[,] A,double eps,out uint error)
{
int n;
if(A.GetLength(0) != A.GetLength(1))
{
error = 2;
return;
}
n = A.GetLength(0);
int[] M = new int[n];
double maxA,d,e;
int k;
error = 0;
for(int i = 0; i < n;i++)
{
maxA = 0;
k = i;
for(int j = i; j < n;j++)
{
d = A[j,i];
if(Math.Abs(maxA) < Math.Abs(d))
{
maxA = d;
k = j;
}
}
if(Math.Abs(maxA) < eps)
{
error = 1;
return;
}
M[i] = k;
A[k,i] = 1;
for(int j = 0;j < n;j++)
{
d = A[k,j]/maxA;
A[k,j] = A[i,j];
A[i,j] = d;
}
for(int j = 0;j < n;j++)
if(j != i)
{
d = A[j,i];
A[j,i] = 0;
for(int l = 0;l < n;l++)
{
e = d*A[i,l];
A[j,l] −= e;
}
}
}
for(int i = n−1;i >= 0;i−−)
{
k = M[i];
if(k != i)
for(int j=0;j < n;j++)
{
d = A[j,i];
A[j,i] = A[j,k];
A[j,k] = d;
}
}
}
}
}
9 lut 06:15