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 ToHessenberg(int n,double[,] A)
{
for(int i = 0;i < n-2;i++)
{
int k = i + 1;
double maxA = Math.Abs(A[k,i]);
for(int j= i + 1; j < n; j++)
if(Math.Abs(A[j,i]) > maxA)
{
k = j;
maxA = Math.Abs(A[k,i]);
}
if(maxA > 0)
{
for(int j = 0; j < n; j++)
{
double temp = A[k,j];
A[k,j] = A[i + 1,j];
A[i + 1,j] = temp;
}
for(int j=0;j < n;j++)
{
double temp = A[j,k];
A[j,k] = A[j, i + 1];
A[j,i + 1] = temp;
}
for(int j = i + 2;j < n;j++)
{
double m = (double)A[j,i]/A[i + 1,i];
for(int l = 0; l < n;l++)
A[j,l] -= m * A[i + 1,l];
for(int l = 0;l < n;l++)
A[l,i + 1] += m * A[l,j];
}
}
}
}
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 n,m;
double[,] A;
double[,] Q;
double[,] R;
Console.WriteLine("Obliczanie przyblizonych wartosci wlasnych metoda rozkladu QR");
Console.WriteLine("Podaj wymiar macierzy ktorej wartosci wlasne chcesz policzyc");
int.TryParse(Console.ReadLine(),out n);
Console.WriteLine("Podaj gorny zakres przedzialu do ktorego beda nalezec elementy macierzy");
int.TryParse(Console.ReadLine(),out m);
A = new double[n,n];
Q = new double[n,n];
R = new double[n,n];
Random
rand = new Random
(); for(int i=0;i<n;i++)
for(int j = 0;j < n;j++)
A
[i
,j
] = (1 - 2 * rand.
Next(2)) * rand.
Next(m
); Console.WriteLine("Wylosowana macierz");
PrintMatrix(n,n,A);
ToHessenberg(n,A);
Console.WriteLine("Macierz po sprowadzeniu do postaci Hessenberga");
PrintMatrix(n,n,A);
for(int i = 0;i<maxiter;i++)
{
QRdecomp(n,n,A,Q);
CopyMatrix(n,n,A,R);
MultiplyMatrix(n,n,n,R,Q,A);
}
Console.WriteLine("Macierz po iteracyjnym sprowadzeniu do postaci Schura");
PrintMatrix(n,n,A);
int k = 0;
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalDigits = 12;
while(k < n)
{
if(k + 1 < n && 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].ToString("N",nfi));
k++;
}
}
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uR2xvYmFsaXphdGlvbjsKbmFtZXNwYWNlIE5hbWVzcGFjZU5hbWUKewoJcHVibGljIGNsYXNzIENsYXNzTmFtZQoJewoJCWNvbnN0IGludCBtYXhpdGVyID0gMTAwMDsKCQljb25zdCBkb3VibGUgZXBzID0gMWUtMTI7CgkJcHVibGljIHN0YXRpYyB2b2lkIFByaW50TWF0cml4KGludCBtLGludCBuLGRvdWJsZVssXSBBKQoJCXsKCQkJTnVtYmVyRm9ybWF0SW5mbyBuZmkgPSBuZXcgTnVtYmVyRm9ybWF0SW5mbygpOwoJCQluZmkuTnVtYmVyRGVjaW1hbERpZ2l0cyA9IDEyOwoJCQlkb3VibGUgcCA9IDA7CgkJCWZvcihpbnQgaT0wO2k8bTtpKyspCgkJCXsKCQkJCWZvcihpbnQgaj0wO2ogPCBuOyBqKyspCgkJCQkJaWYoTWF0aC5BYnMoQVtpLGpdKSA+IGVwcykKCQkJCQkJQ29uc29sZS5Xcml0ZSgiezB9ICwgIixBW2ksal0uVG9TdHJpbmcoIk4iLG5maSkpOwoJCQkJCWVsc2UKCQkJCQkJQ29uc29sZS5Xcml0ZSgiezB9ICwgIixwLlRvU3RyaW5nKCJOIixuZmkpKTsgCgkJCQlDb25zb2xlLldyaXRlTGluZSgpOwoJCQl9CgkJC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l9CgkJCQkJZWxzZQoJCQkJCXsKCQkJCQkJQ29uc29sZS5Xcml0ZUxpbmUoInhbezB9XSA9IHsxfSIsayxBW2ssa10uVG9TdHJpbmcoIk4iLG5maSkpOwoJCQkJCQlrKys7CgkJCQkJfQoJCQkJfQkJCgkJfQoJfQp9Cg==
Obliczanie przyblizonych wartosci wlasnych metoda rozkladu QR
Podaj wymiar macierzy ktorej wartosci wlasne chcesz policzyc
Podaj gorny zakres przedzialu do ktorego beda nalezec elementy macierzy
Wylosowana macierz
50.000000000000 , -4.000000000000 , 36.000000000000 , -62.000000000000 , 26.000000000000 , -55.000000000000 ,
-18.000000000000 , -19.000000000000 , 9.000000000000 , 87.000000000000 , 28.000000000000 , 62.000000000000 ,
60.000000000000 , 41.000000000000 , 94.000000000000 , 50.000000000000 , -38.000000000000 , -88.000000000000 ,
-70.000000000000 , -41.000000000000 , -52.000000000000 , 96.000000000000 , -62.000000000000 , 22.000000000000 ,
-8.000000000000 , 39.000000000000 , 30.000000000000 , -89.000000000000 , 91.000000000000 , -51.000000000000 ,
-37.000000000000 , -22.000000000000 , 0.000000000000 , -29.000000000000 , 73.000000000000 , -55.000000000000 ,
Macierz po sprowadzeniu do postaci Hessenberga
50.000000000000 , -119.985714285714 , -34.428814698633 , 45.988245403736 , -28.474520843633 , -55.000000000000 ,
-70.000000000000 , 134.571428571429 , -1.967376204347 , -30.866002055952 , -31.210191662547 , 22.000000000000 ,
0.000000000000 , -136.622448979592 , 12.606021574213 , 17.595722952219 , 19.872323355780 , -53.514285714286 ,
0.000000000000 , 0.000000000000 , -170.402570335754 , 61.290052786869 , -18.443749698397 , -86.558082007618 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , 65.457768486961 , 16.755848740297 , -24.394519807795 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , -58.394467123537 , -18.223351672808 ,
Macierz po iteracyjnym sprowadzeniu do postaci Schura
135.121802693143 , 122.544269149596 , -21.144163199209 , 11.909137803369 , -14.569156253241 , -59.969193765852 ,
-31.158058040203 , 149.267345057168 , 21.542275595957 , -86.602047931168 , 152.994725309717 , -4.435405604538 ,
0.000000000000 , 0.000000000000 , -36.618093965312 , 70.318210019517 , -94.022615149702 , -6.686945621790 ,
0.000000000000 , 0.000000000000 , -46.866188359712 , -53.549371565518 , 53.760852706482 , 15.178537351671 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , 16.962298883562 , 67.424836305601 , 29.199783169510 ,
0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , 0.000000000000 , -4.646518525077 ,
x[0] = 142.194573875155 - 61.385807467722*i
x[1] = 142.194573875155 + 61.385807467722*i
x[2] = -45.083732765415 - 56.779216581410*i
x[3] = -45.083732765415 + 56.779216581410*i
x[4] = 67.424836305601
x[5] = -4.646518525077