MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / gauss

Method gauss

src/class133/ShowDetails.java:19–47  ·  view source on GitHub ↗
(int n)

Source from the content-addressed store, hash-verified

17 // 高斯消元解决加法方程组模版
18 // 需要保证变量有n个,表达式也有n个
19 public static void gauss(int n) {
20 for (int i = 1; i <= n; i++) {
21 // 如果想严格区分矛盾、多解、唯一解,一定要这么写
22 int max = i;
23 for (int j = 1; j <= n; j++) {
24 if (j < i && Math.abs(mat[j][j]) >= sml) {
25 continue;
26 }
27 if (Math.abs(mat[j][i]) > Math.abs(mat[max][i])) {
28 max = j;
29 }
30 }
31 swap(i, max);
32 if (Math.abs(mat[i][i]) >= sml) {
33 double tmp = mat[i][i];
34 for (int j = i; j <= n + 1; j++) {
35 mat[i][j] /= tmp;
36 }
37 for (int j = 1; j <= n; j++) {
38 if (i != j) {
39 double rate = mat[j][i] / mat[i][i];
40 for (int k = i; k <= n + 1; k++) {
41 mat[j][k] -= mat[i][k] * rate;
42 }
43 }
44 }
45 }
46 }
47 }
48
49 public static void swap(int a, int b) {
50 double[] tmp = mat[a];

Callers 1

mainMethod · 0.95

Calls 1

swapMethod · 0.95

Tested by

no test coverage detected