lx
2025-11-18 e81cbb4c978bec283c58629156c07106aaa46943
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ErrorAnalysis.Service
{
    public class Utility
    {
        /// <summary> 
        /// 单线性插值法
        /// </summary>
        /// <param name="value">插值</param>
        /// <param name="x0">起始值</param>
        /// <param name="y0">起始值对应值</param>
        /// <param name="x1">结束值</param>
        /// <param name="y1">结束值对应值</param>
        /// <returns>插值对应结果</returns>
        public static double Interpolate(double value, double x0, double y0, double x1, double y1)
        {
            // Calculate the interpolated value using linear interpolation formula
            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. 构建正规方程矩阵[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;
        }
    }
}