Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Assignment_2_Poly_Roots
- {
- class Program
- {
- static double ArcSinh(double x)
- {
- double h = (x * x) + 1;
- double ArcSinh = Math.Log(x + Math.Sqrt(h));
- return ArcSinh;
- }
- static double ArcCosh(double x)
- {
- double l = (x * x) - 1;
- double ArcCosh = Math.Log(x + l);
- return ArcCosh;
- }
- static double[] PolyRoot(double a, double b)
- {
- double x1 = (-b / a);
- double[] root = new double[] { x1 };
- return root;
- }
- static double[] PolyRoot(double a, double b, double c)
- {
- double delta = b * b - 4 * a * c;
- if (delta > 1e-10)
- {
- double x1 = (-b - Math.Sqrt(delta)) / 2 * a;
- double x2 = (-b + Math.Sqrt(delta)) / 2 * a;
- double[] root = new double[] { x1, x2 };
- return root;
- }
- else if (delta == 1e-10)
- {
- double x1 = (-b) / 2 * a;
- double[] root = new double[] { x1 };
- return root;
- }
- else
- {
- double x = 0;
- double[] root = new double[] { x };
- return root;
- }
- }
- static double []PolyRoot(double a, double b, double c, double d)
- {
- double p = (3 * a * c - b * b) / 3 * a * a;
- double q = 2 * b * b * b - 9 * a * b * c + 27 * a * a * d / 27 * a * a * a;
- double delta = (-4 * p * p * p - 27 * q * q) * a * a * a * a;
- double B = -b/3*a;
- if (delta > 1e-10)
- {
- double A = 2 * Math.Sqrt(-p / 3);
- double φ = Math.Acos(3 * q / p * A);
- double x1 = A * Math.Cos(φ / 3) + B;
- double x2 = A * Math.Cos(φ + 2 * Math.PI / 3) + B;
- double x3 = A * Math.Cos(φ + 4 * Math.PI / 3) + B;
- double[] root = new double[] { x1,x2,x3 };
- return root;
- }
- else if (delta < 1e-10)
- {
- if (p < 1e-10)
- {
- double A = 2 * Math.Sqrt(-p / 3);
- double φ = ArcCosh(-3 * Math.Abs(q) / p * A);
- double x1 = A * Math.Cosh(φ / 3) + B;
- double[] root = new double[] { x1 };
- return root;
- }
- else if (p > 1e-10)
- {
- double A = 2 * Math.Sqrt(-p / 3);
- double φ = ArcSinh(3 * q / p * A);
- double x1 = -A * Math.Sinh(φ / 3) + B;
- double[] root = new double[] { x1 };
- return root;
- }
- else
- {
- double x1 = Math.Pow(-q + B, 1 / 3);
- double[] root = new double[] { x1 };
- return root;
- }
- }
- else
- { double deltaZero = b * b - 3 * a * c;
- if (deltaZero != 1e-10)
- {
- double x1 = 9 * a * d - b * c / 2 * deltaZero;
- double x2 = 4 * a * b * c - 9 * a * a * d - b * b * b / a * deltaZero;
- double[] root = new double[] { x1,x2 };
- return root;
- }
- else
- {
- double x1 = B;
- double[] root = new double[] { x1 };
- return root;
- }
- }
- }
- static void Main(string[] args)
- {
- double[][] TestPolys = new double[8][];
- TestPolys[0] = new double[] { 5, -2, -5 };
- TestPolys[1] = new double[] { 4, -4, 1 };
- TestPolys[2] = new double[] { 4, -4, 3 };
- TestPolys[3] = new double[] { 1, -6, 11, -6 };
- TestPolys[4] = new double[] { 8, -52, 110, -75 };
- TestPolys[5] = new double[] { 8, -12, 6, -1 };
- TestPolys[6] = new double[] { 2, 5, 8, 3 };
- TestPolys[7] = new double[] { 3, 2 };
- for (int i = 0; i < TestPolys.Length; i++)
- {
- double l = TestPolys[i].GetLength(0);
- if (l == 2)
- {
- roots[i] = PolyRoot(TestPolys[i][0]), TestPolys[i][1]);
- }
- else if (l == 3)
- {
- roots[i] = PolyRoot(TestPolys[i][0], TestPolys[i][1], TestPolys[i][2]);
- }
- else if (l == 4)
- {
- roots [i] = Assignment_2_Poly_Roots(TestPolys[i][0], TestPolys[i][1], TestPolys[i][2], TestPolys[i][3]);
- }
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement