Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. namespace Assignment_2_Poly_Roots
  2. {
  3. class Program
  4. {
  5.  
  6. static double ArcSinh(double x)
  7.  
  8. {
  9.  
  10. double h = (x * x) + 1;
  11.  
  12. double ArcSinh = Math.Log(x + Math.Sqrt(h));
  13.  
  14. return ArcSinh;
  15.  
  16. }
  17.  
  18. static double ArcCosh(double x)
  19.  
  20. {
  21.  
  22. double l = (x * x) - 1;
  23.  
  24. double ArcCosh = Math.Log(x + l);
  25.  
  26. return ArcCosh;
  27.  
  28. }
  29.  
  30. static double[] PolyRoot(double a, double b)
  31.  
  32. {
  33. double x1 = (-b / a);
  34. double[] root = new double[] { x1 };
  35.  
  36. return root;
  37. }
  38.  
  39. static double[] PolyRoot(double a, double b, double c)
  40.  
  41. {
  42. double delta = b * b - 4 * a * c;
  43. if (delta > 1e-10)
  44. {
  45. double x1 = (-b - Math.Sqrt(delta)) / 2 * a;
  46. double x2 = (-b + Math.Sqrt(delta)) / 2 * a;
  47.  
  48. double[] root = new double[] { x1, x2 };
  49.  
  50. return root;
  51. }
  52. else if (delta == 1e-10)
  53. {
  54. double x1 = (-b) / 2 * a;
  55. double[] root = new double[] { x1 };
  56.  
  57. return root;
  58. }
  59. else
  60. {
  61. double x = 0;
  62. double[] root = new double[] { x };
  63. return root;
  64.  
  65. }
  66. }
  67.  
  68. static double []PolyRoot(double a, double b, double c, double d)
  69.  
  70. {
  71.  
  72. double p = (3 * a * c - b * b) / 3 * a * a;
  73.  
  74. double q = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d / 27 * a * a * a;
  75.  
  76. double delta = (-4 * p * p * p - 27 * q * q) * a * a * a * a;
  77.  
  78. double B = -b/3*a;
  79.  
  80. if (delta > 1e-10)
  81. {
  82. double A = 2 * Math.Sqrt(-p / 3);
  83. double φ = Math.Acos(3 * q / p * A);
  84. double x1 = A * Math.Cos(φ / 3) + B;
  85. double x2 = A * Math.Cos(φ + 2 * Math.PI / 3) + B;
  86. double x3 = A * Math.Cos(φ + 4 * Math.PI / 3) + B;
  87.  
  88. double[] root = new double[] { x1,x2,x3 };
  89. return root;
  90. }
  91. else if (delta < 1e-10)
  92. {
  93. if (p < 1e-10)
  94. {
  95. double A = 2 * Math.Sqrt(-p / 3);
  96. double φ = ArcCosh(-3 * Math.Abs(q) / p * A);
  97. double x1 = A * Math.Cosh(φ / 3) + B;
  98. double[] root = new double[] { x1 };
  99. return root;
  100. }
  101. else if (p > 1e-10)
  102. {
  103. double A = 2 * Math.Sqrt(-p / 3);
  104. double φ = ArcSinh(3 * q / p * A);
  105. double x1 = -A * Math.Sinh(φ / 3) + B;
  106. double[] root = new double[] { x1 };
  107. return root;
  108. }
  109. else
  110. {
  111. double x1 = Math.Pow(-q + B, 1 / 3);
  112. double[] root = new double[] { x1 };
  113. return root;
  114.  
  115. }
  116. }
  117. else
  118. { double deltaZero = b * b - 3 * a * c;
  119. if (deltaZero != 1e-10)
  120. {
  121. double x1 = 9 * a * d - b * c / 2 * deltaZero;
  122. double x2 = 4 * a * b * c - 9 * a * a * d - b * b * b / a * deltaZero;
  123. double[] root = new double[] { x1,x2 };
  124. return root;
  125. }
  126. else
  127. {
  128. double x1 = B;
  129. double[] root = new double[] { x1 };
  130. return root;
  131. }
  132. }
  133. }
  134.  
  135. static void Main(string[] args)
  136. {
  137. double[][] TestPolys = new double[8][];
  138. TestPolys[0] = new double[] { 5, -2, -5 };
  139. TestPolys[1] = new double[] { 4, -4, 1 };
  140. TestPolys[2] = new double[] { 4, -4, 3 };
  141. TestPolys[3] = new double[] { 1, -6, 11, -6 };
  142. TestPolys[4] = new double[] { 8, -52, 110, -75 };
  143. TestPolys[5] = new double[] { 8, -12, 6, -1 };
  144. TestPolys[6] = new double[] { 2, 5, 8, 3 };
  145. TestPolys[7] = new double[] { 3, 2 };
  146.  
  147. for (int i = 0; i < TestPolys.Length; i++)
  148. {
  149. double l = TestPolys[i].GetLength(0);
  150. if (l == 2)
  151. {
  152. roots[i] = PolyRoot(TestPolys[i][0]), TestPolys[i][1]);
  153. }
  154.  
  155. else if (l == 3)
  156. {
  157. roots[i] = PolyRoot(TestPolys[i][0], TestPolys[i][1], TestPolys[i][2]);
  158. }
  159.  
  160. else if (l == 4)
  161. {
  162. roots [i] = Assignment_2_Poly_Roots(TestPolys[i][0], TestPolys[i][1], TestPolys[i][2], TestPolys[i][3]);
  163. }
  164.  
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171. Console.ReadKey();
  172.  
  173. }
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement