lx
2025-08-15 c97331d8eca8f9ef6a0c18ffce3518578b5638ff
ErrorAnalysis.Service/Utility.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -24,12 +25,128 @@
            return y;
        }
        public static (double, double) InterpolateBySw(double c, double o, double sw)
        public static (double slope, double intercept) FitLine(PointF[] points)
        {
            var cRes = (100 - sw) / 100 * c;
            var oRes = sw / 100 * o;
            return (cRes, oRes);
            // 计算各求和项
            int n = points.Length;
            double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
            foreach (var p in points)
            {
                sumX += p.X;
                sumY += p.Y;
                sumXY += p.X * p.Y;
                sumX2 += p.X * p.X;
            }
            // 计算斜率和截距
            double denominator = n * sumX2 - sumX * sumX;
            if (Math.Abs(denominator) < 1e-10) // 避免除以0
                throw new InvalidOperationException("点集过于集中,无法拟合直线");
            double slope = (n * sumXY - sumX * sumY) / denominator;
            double intercept = (sumY - slope * sumX) / n;
            return (slope, intercept);
        }
        public static double GetLineValue(double x, double slope, double intercept) => slope * x + intercept;
        public static (double a, double b, double c) FitParabola(PointF[] points)
        {
            if (points.Length < 3)
                throw new ArgumentException("至少需要3个点进行抛物线拟合");
            // 1. 计算各项求和值[1,6](@ref)
            int n = points.Length;
            double sx = 0, sy = 0, sx2 = 0, sx3 = 0, sx4 = 0, sxy = 0, sx2y = 0;
            foreach (var p in points)
            {
                double x = p.X;
                double y = p.Y;
                double x2 = x * x;
                double x3 = x2 * x;
                double x4 = x2 * x2;
                sx += x;
                sy += y;
                sx2 += x2;
                sx3 += x3;
                sx4 += x4;
                sxy += x * y;
                sx2y += x2 * y;
            }
            // 2. 构建正规方程矩阵[1,6](@ref)
            double[,] matrix = {
            { sx4, sx3, sx2, sx2y },
            { sx3, sx2, sx,  sxy  },
            { sx2, sx,  n,   sy   }
        };
            // 3. 使用高斯消元法求解
            double[] coefficients = GaussElimination(matrix);
            return (coefficients[0], coefficients[1], coefficients[2]);
        }
        // 高斯消元法实现(带部分主元选择)
        private static double[] GaussElimination(double[,] matrix)
        {
            int n = matrix.GetLength(0);
            double[] result = new double[n];
            // 前向消元
            for (int i = 0; i < n - 1; i++)
            {
                // 部分主元选择
                int maxRow = i;
                for (int k = i + 1; k < n; k++)
                {
                    if (Math.Abs(matrix[k, i]) > Math.Abs(matrix[maxRow, i]))
                        maxRow = k;
                }
                // 行交换
                if (maxRow != i)
                {
                    for (int j = 0; j <= n; j++)
                    {
                        double temp = matrix[i, j];
                        matrix[i, j] = matrix[maxRow, j];
                        matrix[maxRow, j] = temp;
                    }
                }
                // 消元过程
                for (int k = i + 1; k < n; k++)
                {
                    double factor = matrix[k, i] / matrix[i, i];
                    for (int j = i; j <= n; j++)
                    {
                        matrix[k, j] -= factor * matrix[i, j];
                    }
                }
            }
            // 回代求解
            for (int i = n - 1; i >= 0; i--)
            {
                result[i] = matrix[i, n];
                for (int j = i + 1; j < n; j++)
                {
                    result[i] -= matrix[i, j] * result[j];
                }
                result[i] /= matrix[i, i];
            }
            return result;
        }
        public static double GetParabolaValue(double x, double a, double b, double c)
        {
            return a * x * x + b * x + c;
        }
    }
}