ningshuxia
2025-04-21 018bfb9c78088d9cd7b9371edcd2102abd594b4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//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<double> 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<double> { x1, x2 };
            }
            else
            {
                return null;
            }
        }
        public static List<double> 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<double> 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<double> roots = new List<double>();
            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;
        }
 
    }
}