//using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using System.Text; using System.Threading.Tasks; namespace Yw.WinFrmUI.Q3d { public class MathSolver { static double ErrNum = -1; public static List Solve(double a, double b, double c) { // 求解二次方程 ax^2 + bx + c = 0 double delta = b * b - 4 * a * c; if (delta >= 0) { double x1 = (-b + Math.Sqrt(delta)) / (2 * a); double x2 = (-b - Math.Sqrt(delta)) / (2 * a); return new List { x1, x2 }; } else { return null; } } public static List Solve(double a, double b, double c, double d) { double[] roots = null; roots = new double[3]; double f = ((3 * c / a) - ((b * b) / (a * a))) / 3; double g = ((2 * (b * b * b) / (a * a * a)) - (9 * b * c / (a * a)) + (27 * d / a)) / 27; double h = ((g * g) / 4) + ((f * f * f) / 27); if (h > 0) { double r = -(g / 2) + Math.Sqrt(h); double s = Math.Sign(r) * Math.Pow(Math.Abs(r), (1 / 3.0)); double t = -(g / 2) - Math.Sqrt(h); double u = Math.Sign(t) * Math.Pow(Math.Abs(t), (1 / 3.0)); roots[0] = (s + u) - (b / (3 * a)); roots[1] = -1; roots[2] = -1; } else if (f == 0 && g == 0 && h == 0) { roots[0] = Math.Pow((d / a), 1 / 3.0) * -1; roots[1] = roots[0]; roots[2] = roots[0]; } else { double i = Math.Sqrt(((g * g) / 4) - h); double j = Math.Pow(i, (1 / 3.0)); double k = Math.Acos(-(g / (2 * i))); double l = j * -1; double m = Math.Cos(k / 3); double n = Math.Sqrt(3) * Math.Sin(k / 3); double p = (b / (3 * a)) * -1; roots[0] = 2 * j * Math.Cos(k / 3) - (b / (3 * a)); roots[1] = l * (m + n) + p; roots[2] = l * (m - n) + p; } return roots?.ToList(); } public static List Solve(double a, double b, double c, double d, double e) { double x = 0; // Initial guess double epsilon = 1e-7; // Precision double diff = 1; double maxRoot = -1; List roots = new List(); while (diff > epsilon) { double x2 = x * x; double x3 = x2 * x; double x4 = x2 * x2; double f = a * x4 + b * x3 + c * x2 + d * x + e; double df = 4 * a * x3 + 3 * b * x2 + 2 * c * x + d; double x1 = x - f / df; diff = Math.Abs(x1 - x); x = x1; if (x > maxRoot) { maxRoot = x; } } if (maxRoot < 0) roots = null; else roots.Add(maxRoot); return roots; } } }