fork download
  1. using System;
  2. using System.Globalization;
  3. namespace NamespaceName
  4. {
  5. public class ClassName
  6. {
  7. const int maxiter = 1000;
  8. const double eps = 1e-12;
  9. public static void PrintMatrix(int m,int n,double[,] A)
  10. {
  11. NumberFormatInfo nfi = new NumberFormatInfo();
  12. nfi.NumberDecimalDigits = 12;
  13. double p = 0;
  14. for(int i=0;i<m;i++)
  15. {
  16. for(int j=0;j < n; j++)
  17. if(Math.Abs(A[i,j]) > eps)
  18. Console.Write("{0} , ",A[i,j].ToString("N",nfi));
  19. else
  20. Console.Write("{0} , ",p.ToString("N",nfi));
  21. Console.WriteLine();
  22. }
  23. Console.WriteLine();
  24. }
  25. public static double Hypot(double a,double b)
  26. {
  27. double absa,absb;
  28. absa = Math.Abs(a);
  29. absb = Math.Abs(b);
  30. if(absa > absb) return absa * Math.Sqrt(1 + ((double) absb / absa) * ((double) absb / absa));
  31. else return (absb == 0 ? 0 : absb * Math.Sqrt(1 + ((double) absa / absb) * ((double) absa / absb)));
  32. }
  33. public static void QRdecomp(int m,int n,double[,] A,double[,] Q)
  34. {
  35. for(int i = 0; i < m; i++)
  36. for(int j = 0;j < m;j++)
  37. Q[i,j] = (i == j ? 1 : 0);
  38. int min = (m < n ? m : n);
  39. for(int i = 0; i < min; i++)
  40. for(int j = i + 1; j < m; j++)
  41. if(A[j,i] != 0)
  42. {
  43. double r = Hypot(A[i,i],A[j,i]);
  44. double c = (double) A[i,i] / r;
  45. double s = (double) A[j,i] / r;
  46. for(int k = 0; k < n; k++)
  47. {
  48. double temp = A[i,k];
  49. A[i,k] = c * A[i,k] + s * A[j,k];
  50. A[j,k] = -s * temp + c * A[j,k];
  51. }
  52. for(int k = 0;k < m;k++)
  53. {
  54. double temp = Q[k,i];
  55. Q[k,i] = c * Q[k,i] + s * Q[k,j];
  56. Q[k,j] = -s * temp + c * Q[k,j];
  57. }
  58. }
  59.  
  60. }
  61.  
  62. public static void MultiplyMatrix(int m,int n,int p,double[,] A,double[,] B,double[,] C)
  63. {
  64. for(int i = 0;i < m;i++)
  65. for(int k=0;k < p;k++)
  66. {
  67. C[i,k] = 0;
  68. for(int j=0;j < n;j++)
  69. C[i,k] += A[i,j] * B[j,k];
  70. }
  71. }
  72. public static void CopyMatrix(int m,int n,double[,] A,double[,] B)
  73. {
  74. for(int i=0;i<m;i++)
  75. for(int j=0;j<n;j++)
  76. B[i,j] = A[i,j];
  77. }
  78. public static void Main(string[] args)
  79. {
  80. int deg;
  81. double[,] A;
  82. double[,] Q;
  83. double[,] R;
  84. double[] c;
  85. Console.WriteLine("Obliczanie przyblizonych pierwiastkow rownania wielomianowego za pomoca wartosci wlasnych macierzy stowarzyszonej");
  86. Console.WriteLine("Podaj stopien wielomianu ktorego pierwiastki chcesz policzyc");
  87. int.TryParse(Console.ReadLine(),out deg);
  88. A = new double[deg,deg];
  89. Q = new double[deg,deg];
  90. R = new double[deg,deg];
  91. c = new double[deg+1];
  92. for(int i = deg; i >= 0;i--)
  93. {
  94. Console.Write("c[{0}]=",i);
  95. double.TryParse(Console.ReadLine(),out c[i]);
  96. }
  97. for(int i=0;i<deg;i++)
  98. A[0,i] = (double)(-c[deg-1-i])/c[deg];
  99.  
  100. for(int i=1;i<deg;i++)
  101. for(int j = 0;j < deg;j++)
  102. A[i,j] = (j+1 == i ? 1: 0);
  103. Console.WriteLine("Macierz stowarzyszona");
  104. PrintMatrix(deg,deg,A);
  105. for(int i = 0;i<maxiter;i++)
  106. {
  107. QRdecomp(deg,deg,A,Q);
  108. CopyMatrix(deg,deg,A,R);
  109. MultiplyMatrix(deg,deg,deg,R,Q,A);
  110. }
  111. Console.WriteLine("Macierz stowarzyszona po iteracyjnym sprowadzeniu do postaci Schura");
  112. PrintMatrix(deg,deg,A);
  113. int k = 0;
  114. NumberFormatInfo nfi = new NumberFormatInfo();
  115. nfi.NumberDecimalDigits = 12;
  116. while(k < deg)
  117. {
  118. if(k + 1 < deg && Math.Abs(A[k+1,k]) > eps)
  119. {
  120. /* Tutaj zalozylem ze w postaci Schura klatka 2x2 pojawia sie
  121. na diagonali gdy macierz posiada zespolone wartosci wlasne
  122. */
  123. double p = 0.5 * (A[k,k] + A[k + 1,k + 1]);
  124. double q = A[k,k] * A[k+1,k+1] - A[k,k+1] * A[k+1,k];
  125. double d = Math.Sqrt(Math.Abs(q - p * p));
  126. Console.WriteLine("x[{0}] = {1} - {2}*i",k,p.ToString("N",nfi),d.ToString("N",nfi));
  127. Console.WriteLine("x[{0}] = {1} + {2}*i",k+1,p.ToString("N",nfi),d.ToString("N",nfi));
  128. k += 2;
  129. }
  130. else
  131. {
  132. Console.WriteLine("x[{0}] = {1}",k,A[k,k]);
  133. k++;
  134. }
  135. }
  136. }
  137. }
  138. }
  139.  
Success #stdin #stdout 0.03s 23240KB
stdin
6
1
0
5
0
7
0
6
stdout
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