欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#盛金公式求解一元三次方程

程序员文章站 2022-05-09 13:47:54
...
static float A;
        static float B;
        static float C;
        static float delta;
        static float x1, x2, x3;
        static float x2_real, x3_real, x2_virtual, x3_virtual;
        static float deltaQuadratic;

        /// <summary>
        /// 盛金公式求解一元三次方程
        /// </summary>
        /// <param name="a">三次系数</param>
        /// <param name="b">二次系数</param>
        /// <param name="c">一次系数</param>
        /// <param name="d">常系数</param>
        /// <param name="r1">结果1 为null则没有实根</param>
        /// <param name="r2">结果2</param>
        /// <param name="r3">结果3</param>
        private static void solveEquations(float a, float b, float c, float d, out float r1, out float r2, out float r3)
        {
            r1 = 0;
            r2 = 0;
            r3 = 0;

            A = b * b - 3 * a * c;
            B = b * c - 9 * a * d;
            C = c * c - 3 * b * d;
            delta = B * B - 4 * A * C;

            if (d == 0)
            {
                x3 = 0;
                solveQuadraticEquation(a, b, c, d);
            }
            else if (A == 0 && B == 0)
            {
                x1 = -b / (3 * a);
                x2 = -c / b;
                x3 = -3 * d / c;

            }
            else if (delta > 0)
            {
                float Y1 = float.Parse((A * b + 3 * a * (-B + Math.Sqrt(delta)) / 2).ToString());
                float Y2 = float.Parse((A * b + 3 * a * (-B - Math.Sqrt(delta)) / 2).ToString());

                x1 = (-b - (getCubeRoot(Y1) + getCubeRoot(Y2))) / (3 * a);
                x3_real = x2_real = (-b + getCubeRoot(Y1)) / (3 * a);
                x2_virtual = float.Parse((((Math.Sqrt(3) / 2) * (getCubeRoot(Y1) - getCubeRoot(Y2))) / (3 * a)).ToString());
                x3_virtual = -x2_virtual;
            }
            else if (delta == 0)
            {
                var K = B / A; //A != 0
                x1 = -b / a + K;
                x2 = x3 = -K / 2;
            }
            else
            {
                //delta < 0
                var T = (2 * A * b - 3 * a * B) / (2 * Math.Sqrt(A * A * A)); // A > 0, -1 < T < 1
                var angle = Math.Acos(T) / 3;
                x1 = float.Parse(((-b - 2 * Math.Sqrt(A) * Math.Cos(angle)) / (3 * a)).ToString());
                x2 = float.Parse(((-b + Math.Sqrt(A) * (Math.Cos(angle) + Math.Sqrt(3) * Math.Sin(angle))) / (3 * a)).ToString());
                x3 = float.Parse(((-b + Math.Sqrt(A) * (Math.Cos(angle) - Math.Sqrt(3) * Math.Sin(angle))) / (3 * a)).ToString());
            }

            if (A == 0 && B == 0)
            {
                //方程有一个三重实根
                r1 = x1;
                r2 = x1;
                r3 = x1;
                return;
            }
            else if (delta > 0)
            {
                //方程有一个实根和一对共轭虚根
                r1 = x1;
                r2 = x2_real + x2_virtual;
                r3 = x2_real + x2_virtual;
            }
            else if (delta == 0)
            {
                //方程有一个实根,其中有一个两重根
                r1 = x1;
                r2 = x2;
                r3 = x2;
            }
            else
            {
                //方程有三个不相等的实根
                r1 = x1;
                r2 = x2;
                r3 = x3;
            }
        }

        private static void solveQuadraticEquation(float a, float b, float c, float d)
        {
            deltaQuadratic = b * b - 4 * a * c;
            if (deltaQuadratic == 0)
            {
                x1 = x2 = -b / (2 * a);
            }
            else if (deltaQuadratic > 0)
            {
                x1 = -b + float.Parse((Math.Sqrt(deltaQuadratic) / (2 * a)).ToString());
                x2 = -b - float.Parse((Math.Sqrt(deltaQuadratic) / (2 * a)).ToString());
            }
            else
            {
                x2_real = -b / (2 * a);
                x2_virtual = float.Parse((Math.Sqrt(-deltaQuadratic) / (2 * a)).ToString());
            }
        }

        private static float getCubeRoot(float value)
        {
            if (value < 0)
            {
                return float.Parse((-Math.Pow(-value, 1 / 3)).ToString());
            }
            else if (value == 0)
            {
                return 0;
            }
            else
            {
                return float.Parse(Math.Pow(value, 1 / 3).ToString());
            }
        }

调用solveEquations方法即可。