-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrendSurfaceInterpolation.java
More file actions
176 lines (150 loc) · 4.92 KB
/
Copy pathTrendSurfaceInterpolation.java
File metadata and controls
176 lines (150 loc) · 4.92 KB
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
package heyingzhe;
import java.util.ArrayList;
import java.util.List;
public class TrendSurfaceInterpolation {
/**
* 根据趋势面插值填充二维数组。
*
* @param inputArray 包含观测值和0的二维数组
* @return 插值后的二维数组
*/
public static double[][] trendSurfaceInterpolation(double[][] inputArray) {
int height = inputArray.length;
int width = inputArray[0].length;
// 收集所有非零点作为观测值
List<Point> points = new ArrayList<>();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (inputArray[i][j] != 0) {
points.add(new Point(j, i, inputArray[i][j]));
}
}
}
// 如果没有足够的点进行拟合,直接返回
if (points.size() < 3) {
throw new IllegalArgumentException("至少需要三个非零点进行插值计算!");
}
// 根据点拟合趋势面: z = a + b*x + c*y
double[] coefficients = fitTrendSurface(points);
// 使用拟合模型填充所有零点
double[][] resultArray = new double[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (inputArray[i][j] == 0) {
resultArray[i][j] = coefficients[0] + coefficients[1] * j + coefficients[2] * i;
} else {
resultArray[i][j] = inputArray[i][j];
}
}
}
return resultArray;
}
/**
* 拟合趋势面 z = a + b*x + c*y。
*
* @param points 包含所有观测值的点集合
* @return 拟合的趋势面系数 [a, b, c]
*/
private static double[] fitTrendSurface(List<Point> points) {
int n = points.size();
double sumX = 0, sumY = 0, sumZ = 0;
double sumX2 = 0, sumY2 = 0, sumXY = 0, sumXZ = 0, sumYZ = 0;
for (Point p : points) {
double x = p.x;
double y = p.y;
double z = p.z;
sumX += x;
sumY += y;
sumZ += z;
sumX2 += x * x;
sumY2 += y * y;
sumXY += x * y;
sumXZ += x * z;
sumYZ += y * z;
}
// 构造矩阵和向量
double[][] A = {
{n, sumX, sumY},
{sumX, sumX2, sumXY},
{sumY, sumXY, sumY2}
};
double[] B = {sumZ, sumXZ, sumYZ};
// 解线性方程组 A * [a, b, c] = B
return solveLinearSystem(A, B);
}
/**
* 解线性方程组 A * X = B。
*
* @param A 系数矩阵
* @param B 常数向量
* @return 解向量 X
*/
private static double[] solveLinearSystem(double[][] A, double[] B) {
int n = B.length;
double[] X = new double[n];
// 高斯消元法
for (int i = 0; i < n; i++) {
// 寻找主元
int max = i;
for (int j = i + 1; j < n; j++) {
if (Math.abs(A[j][i]) > Math.abs(A[max][i])) {
max = j;
}
}
// 交换行
double[] temp = A[i];
A[i] = A[max];
A[max] = temp;
double t = B[i];
B[i] = B[max];
B[max] = t;
// 归一化当前行
for (int j = i + 1; j < n; j++) {
double factor = A[j][i] / A[i][i];
B[j] -= factor * B[i];
for (int k = i; k < n; k++) {
A[j][k] -= factor * A[i][k];
}
}
}
// 回代求解
for (int i = n - 1; i >= 0; i--) {
double sum = 0;
for (int j = i + 1; j < n; j++) {
sum += A[i][j] * X[j];
}
X[i] = (B[i] - sum) / A[i][i];
}
return X;
}
/**
* 点类,存储 x, y, z 坐标。
*/
private static class Point {
double x, y, z;
Point(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
}
public static void main(String[] args) {
// 示例输入数组
double[][] inputArray = {
{0, 0, 0, 0, 0},
{0, 10, 0, 20, 0},
{0, 0, 0, 0, 0},
{0, 30, 0, 40, 0},
{0, 0, 0, 0, 0}
};
// 调用插值函数
double[][] resultArray = trendSurfaceInterpolation(inputArray);
// 输出结果
for (double[] row : resultArray) {
for (double val : row) {
System.out.printf("%.2f ", val);
}
System.out.println();
}
}
}