From 0b108f56b2d0c35d01ee361dc593119ac52a14e1 Mon Sep 17 00:00:00 2001
From: lx <ex_lixiang17@cosl.com.cn>
Date: 星期五, 05 九月 2025 16:39:13 +0800
Subject: [PATCH] update

---
 ErrorAnalysis.Service/Utility.cs |  318 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 318 insertions(+), 0 deletions(-)

diff --git a/ErrorAnalysis.Service/Utility.cs b/ErrorAnalysis.Service/Utility.cs
index 6c10062..66b5738 100644
--- a/ErrorAnalysis.Service/Utility.cs
+++ b/ErrorAnalysis.Service/Utility.cs
@@ -1,5 +1,6 @@
 锘縰sing System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -23,5 +24,322 @@
             double y = y0 + (value - x0) * (y1 - y0) / (x1 - x0);
             return y;
         }
+
+        public static (double slope, double intercept) FitLine(PointF[] points)
+        {
+            // 璁$畻鍚勬眰鍜岄」
+            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. 鏋勫缓姝h鏂圭▼鐭╅樀[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 a, double b, double c) quadraticLine(double[] arrX, double[] arrY)
+        {
+            double[] coef = new double[3];
+            double[,] AA = new double[3, 3];
+            double[,] TAA = new double[3, 3];
+            double[,] BAA = new double[3, 1];
+            double[] B = new double[3];
+            int m = arrX.Length;
+
+            double[] arrXp = new double[m];
+            double[] arrXc = new double[m];
+            double[] arrXv = new double[m];
+            double[] y = new double[m];
+
+            for (int i = 0; i < m; i++)
+            {
+                arrXp[i] = arrX[i] * arrX[i];
+            }
+            for (int i = 0; i < m; i++)
+            {
+                arrXc[i] = arrX[i] * arrX[i] * arrX[i];
+            }
+            for (int i = 0; i < m; i++)
+            {
+                arrXv[i] = arrX[i] * arrX[i] * arrX[i] * arrX[i];
+            }
+            double a1 = mysum(arrX);
+            double a2 = mysum(arrXp);
+            double a3 = mysum(arrXc);
+            double a4 = mysum(arrXv);
+
+            AA[0, 0] = m;
+            AA[0, 1] = a1;
+            AA[0, 2] = a2;
+            AA[1, 0] = a1;
+            AA[1, 1] = a2;
+            AA[1, 2] = a3;
+            AA[2, 0] = a2;
+            AA[2, 1] = a3;
+            AA[2, 2] = a4;
+            double[] xy = new double[m];
+            double[] xxy = new double[m];
+            for (int i = 0; i < m; i++)
+            {
+                xy[i] = arrX[i] * arrY[i];
+            }
+            for (int i = 0; i < m; i++)
+            {
+                xxy[i] = arrX[i] * arrX[i] * arrY[i];
+            }
+            double b1 = mysum(arrY);
+            double b2 = mysum(xy);
+            double b3 = mysum(xxy);
+            B[0] = b1;
+            B[1] = b2;
+            B[2] = b3;
+            TAA = ReverseMatrix(AA, 3);
+            for (int i = 0; i < 3; i++)
+            {
+                for (int j = 0; j < 3; j++)
+                {
+                    coef[i] = coef[i] + TAA[i, j] * B[j];
+                }
+            }
+            return (coef[2], coef[1], coef[0]);
+
+        }
+
+        public static double[,] ReverseMatrix(double[,] dMatrix, int Level)
+        {
+            double dMatrixValue = MatrixValue(dMatrix, Level);
+            if (dMatrixValue == 0) return null;
+            double[,] dReverseMatrix = new double[Level, 2 * Level];
+            double x, c;
+            // Init Reverse matrix
+            for (int i = 0; i < Level; i++)
+            {
+                for (int j = 0; j < 2 * Level; j++)
+                {
+                    if (j < Level)
+                        dReverseMatrix[i, j] = dMatrix[i, j];
+                    else
+                        dReverseMatrix[i, j] = 0;
+                }
+                dReverseMatrix[i, Level + i] = 1;
+            }
+            for (int i = 0, j = 0; i < Level && j < Level; i++, j++)
+            {
+                if (dReverseMatrix[i, j] == 0)
+                {
+                    int m = i;
+                    for (; dMatrix[m, j] == 0; m++) ;
+                    if (m == Level)
+                        return null;
+                    else
+                    {
+                        // Add i-row with m-row
+                        for (int n = j; n < 2 * Level; n++)
+                            dReverseMatrix[i, n] += dReverseMatrix[m, n];
+                    }
+                }
+                // Format the i-row with "1" start
+                x = dReverseMatrix[i, j];
+                if (x != 1)
+                {
+                    for (int n = j; n < 2 * Level; n++)
+                        if (dReverseMatrix[i, n] != 0)
+                            dReverseMatrix[i, n] /= x;
+                }
+                // Set 0 to the current column in the rows after current row
+                for (int s = Level - 1; s > i; s--)
+                {
+                    x = dReverseMatrix[s, j];
+                    for (int t = j; t < 2 * Level; t++)
+                        dReverseMatrix[s, t] -= (dReverseMatrix[i, t] * x);
+                }
+            }
+            // Format the first matrix into unit-matrix
+            for (int i = Level - 2; i >= 0; i--)
+            {
+                for (int j = i + 1; j < Level; j++)
+                    if (dReverseMatrix[i, j] != 0)
+                    {
+                        c = dReverseMatrix[i, j];
+                        for (int n = j; n < 2 * Level; n++)
+                            dReverseMatrix[i, n] -= (c * dReverseMatrix[j, n]);
+                    }
+            }
+            double[,] dReturn = new double[Level, Level];
+            for (int i = 0; i < Level; i++)
+                for (int j = 0; j < Level; j++)
+                    dReturn[i, j] = dReverseMatrix[i, j + Level];
+            return dReturn;
+        }
+        private static double MatrixValue(double[,] MatrixList, int Level)
+        {
+            double[,] dMatrix = new double[Level, Level];
+            for (int i = 0; i < Level; i++)
+                for (int j = 0; j < Level; j++)
+                    dMatrix[i, j] = MatrixList[i, j];
+            double c, x;
+            int k = 1;
+            for (int i = 0, j = 0; i < Level && j < Level; i++, j++)
+            {
+                if (dMatrix[i, j] == 0)
+                {
+                    int m = i;
+                    for (; dMatrix[m, j] == 0; m++) ;
+                    if (m == Level)
+                        return 0;
+                    else
+                    {
+                        // Row change between i-row and m-row
+                        for (int n = j; n < Level; n++)
+                        {
+                            c = dMatrix[i, n];
+                            dMatrix[i, n] = dMatrix[m, n];
+                            dMatrix[m, n] = c;
+                        }
+                        // Change value pre-value
+                        k *= (-1);
+                    }
+                }
+                // Set 0 to the current column in the rows after current row
+                for (int s = Level - 1; s > i; s--)
+                {
+                    x = dMatrix[s, j];
+                    for (int t = j; t < Level; t++)
+                        dMatrix[s, t] -= dMatrix[i, t] * (x / dMatrix[i, j]);
+                }
+            }
+            double sn = 1;
+            for (int i = 0; i < Level; i++)
+            {
+                if (dMatrix[i, i] != 0)
+                    sn *= dMatrix[i, i];
+                else
+                    return 0;
+            }
+            return k * sn;
+        }
+
+        public static double mysum(double[] XX)
+        {
+            double reout = 0;
+            int mm = XX.Length;
+            for (int i = 0; i < mm; i++)
+            {
+                reout = reout + XX[i];
+            }
+            return reout;
+        }
+
+        public static double GetParabolaValue(double x, double a, double b, double c)
+        {
+            return a * x * x + b * x + c;
+        }
     }
 }

--
Gitblit v1.9.3