using System;
using System.Globalization;
namespace NamespaceName
{
public class ClassName
{
const int maxiter = 1000;
const double eps = 1e-12;
public static void PrintMatrix(int m,int n,double[,] A)
{
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalDigits = 12;
double p = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j < n; j++)
if(Math.Abs(A[i,j]) > eps)
Console.Write("{0} , ",A[i,j].ToString("N",nfi));
else
Console.Write("{0} , ",p.ToString("N",nfi));
Console.WriteLine();
}
Console.WriteLine();
}
public static double Hypot(double a,double b)
{
double absa,absb;
absa = Math.Abs(a);
absb = Math.Abs(b);
if(absa > absb) return absa * Math.Sqrt(1 + ((double) absb / absa) * ((double) absb / absa));
else return (absb == 0 ? 0 : absb * Math.Sqrt(1 + ((double) absa / absb) * ((double) absa / absb)));
}
public static void QRdecomp(int m,int n,double[,] A,double[,] Q)
{
for(int i = 0; i < m; i++)
for(int j = 0;j < m;j++)
Q[i,j] = (i == j ? 1 : 0);
int min = (m < n ? m : n);
for(int i = 0; i < min; i++)
for(int j = i + 1; j < m; j++)
if(A[j,i] != 0)
{
double r = Hypot(A[i,i],A[j,i]);
double c = (double) A[i,i] / r;
double s = (double) A[j,i] / r;
for(int k = 0; k < n; k++)
{
double temp = A[i,k];
A[i,k] = c * A[i,k] + s * A[j,k];
A[j,k] = -s * temp + c * A[j,k];
}
for(int k = 0;k < m;k++)
{
double temp = Q[k,i];
Q[k,i] = c * Q[k,i] + s * Q[k,j];
Q[k,j] = -s * temp + c * Q[k,j];
}
}
}
public static void MultiplyMatrix(int m,int n,int p,double[,] A,double[,] B,double[,] C)
{
for(int i = 0;i < m;i++)
for(int k=0;k < p;k++)
{
C[i,k] = 0;
for(int j=0;j < n;j++)
C[i,k] += A[i,j] * B[j,k];
}
}
public static void CopyMatrix(int m,int n,double[,] A,double[,] B)
{
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
B[i,j] = A[i,j];
}
public static void Main(string[] args)
{
int deg;
double[,] A;
double[,] Q;
double[,] R;
double[] c;
Console.WriteLine("Obliczanie przyblizonych pierwiastkow rownania wielomianowego za pomoca wartosci wlasnych macierzy stowarzyszonej");
Console.WriteLine("Podaj stopien wielomianu ktorego pierwiastki chcesz policzyc");
int.TryParse(Console.ReadLine(),out deg);
A = new double[deg,deg];
Q = new double[deg,deg];
R = new double[deg,deg];
c = new double[deg+1];
for(int i = deg; i >= 0;i--)
{
Console.Write("c[{0}]=",i);
double.TryParse(Console.ReadLine(),out c[i]);
}
for(int i=0;i<deg;i++)
A[0,i] = (double)(-c[deg-1-i])/c[deg];
for(int i=1;i<deg;i++)
for(int j = 0;j < deg;j++)
A[i,j] = (j+1 == i ? 1: 0);
Console.WriteLine("Macierz stowarzyszona");
PrintMatrix(deg,deg,A);
for(int i = 0;i<maxiter;i++)
{
QRdecomp(deg,deg,A,Q);
CopyMatrix(deg,deg,A,R);
MultiplyMatrix(deg,deg,deg,R,Q,A);
}
Console.WriteLine("Macierz stowarzyszona po iteracyjnym sprowadzeniu do postaci Schura");
PrintMatrix(deg,deg,A);
int k = 0;
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalDigits = 12;
while(k < deg)
{
if(k + 1 < deg && Math.Abs(A[k+1,k]) > eps)
{
/* Tutaj zalozylem ze w postaci Schura klatka 2x2 pojawia sie
na diagonali gdy macierz posiada zespolone wartosci wlasne
*/
double p = 0.5 * (A[k,k] + A[k + 1,k + 1]);
double q = A[k,k] * A[k+1,k+1] - A[k,k+1] * A[k+1,k];
double d = Math.Sqrt(Math.Abs(q - p * p));
Console.WriteLine("x[{0}] = {1} - {2}*i",k,p.ToString("N",nfi),d.ToString("N",nfi));
Console.WriteLine("x[{0}] = {1} + {2}*i",k+1,p.ToString("N",nfi),d.ToString("N",nfi));
k += 2;
}
else
{
Console.WriteLine("x[{0}] = {1}",k,A[k,k]);
k++;
}
}
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uR2xvYmFsaXphdGlvbjsKbmF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CQkJd2hpbGUoayA8IGRlZykKCQkJCXsKCQkJCQlpZihrICsgMSA8IGRlZyAmJiBNYXRoLkFicyhBW2srMSxrXSkgPiBlcHMpCgkJCQkJewoJCQkJCQkvKiBUdXRhaiB6YWxvenlsZW0gemUgdyBwb3N0YWNpIFNjaHVyYSBrbGF0a2EgMngyIHBvamF3aWEgc2llCgkJCQkJCSBuYSBkaWFnb25hbGkgZ2R5IG1hY2llcnogcG9zaWFkYSB6ZXNwb2xvbmUgd2FydG9zY2kgd2xhc25lCgkJCQkJCSAqLwoJCQkJCQlkb3VibGUgcCA9IDAuNSAqIChBW2ssa10gKyBBW2sgKyAxLGsgKyAxXSk7CgkJCQkJCWRvdWJsZSBxID0gQVtrLGtdICogQVtrKzEsaysxXSAtIEFbayxrKzFdICogQVtrKzEsa107CgkJCQkJCWRvdWJsZSBkID0gTWF0aC5TcXJ0KE1hdGguQWJzKHEgLSBwICogcCkpOwoJCQkJCQlDb25zb2xlLldyaXRlTGluZSgieFt7MH1dID0gezF9IC0gezJ9KmkiLGsscC5Ub1N0cmluZygiTiIsbmZpKSxkLlRvU3RyaW5nKCJOIixuZmkpKTsKCQkJCQkJQ29uc29sZS5Xcml0ZUxpbmUoInhbezB9XSA9IHsxfSArIHsyfSppIixrKzEscC5Ub1N0cmluZygiTiIsbmZpKSxkLlRvU3RyaW5nKCJOIixuZmkpKTsKCQkJCQkJayArPSAyOwoJCQkJCX0KCQkJCQllbHNlCgkJCQkJewoJCQkJCQlDb25zb2xlLldyaXRlTGluZSgieFt7MH1dID0gezF9IixrLEFbayxrXSk7CgkJCQkJCWsrKzsKCQkJCQl9CgkJCQl9CQkKCQl9Cgl9Cn0K
Obliczanie przyblizonych pierwiastkow rownania wielomianowego za pomoca wartosci wlasnych macierzy stowarzyszonej
Podaj stopien wielomianu ktorego pierwiastki chcesz policzyc
c[6]=c[5]=c[4]=c[3]=c[2]=c[1]=c[0]=Macierz stowarzyszona
0.000000000000 , -5.000000000000 , 0.000000000000 , -7.000000000000 , 0.000000000000 , -6.000000000000 ,
1.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 ,
0.000000000000 , 1.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 ,
0.000000000000 , 0.000000000000 , 1.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , 1.000000000000 , 0.000000000000 , 0.000000000000 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , 1.000000000000 , 0.000000000000 ,
Macierz stowarzyszona po iteracyjnym sprowadzeniu do postaci Schura
0.000000000000 , -3.485583997689 , 0.000000000000 , -6.781350173503 , 0.000000000000 , -6.672653481939 ,
1.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , -1.894700247594 , 0.000000000000 , -2.044006628719 ,
0.000000000000 , 0.000000000000 , 1.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , 1.194663674708 , 0.000000000000 , 0.380284245283 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , 1.000000000000 , 0.000000000000 ,
x[0] = 0.000000000000 - 1.866971879191*i
x[1] = 0.000000000000 + 1.866971879191*i
x[2] = 0.000000000000 - 1.376481110511*i
x[3] = 0.000000000000 + 1.376481110511*i
x[4] = 0.000000000000 - 0.616671910567*i
x[5] = 0.000000000000 + 0.616671910567*i