回答編集履歴
1
修正箇所の追加
test
CHANGED
@@ -11,3 +11,87 @@
|
|
11
11
|
|
12
12
|
|
13
13
|
`double t = b[k]; b[k] = b[m]; b[m] = t;`
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
**追記**
|
18
|
+
|
19
|
+
```C
|
20
|
+
|
21
|
+
/* 前進消去(ピボット選択)*/
|
22
|
+
|
23
|
+
for(k = 0; k < N-1; k++){ /* 第kステップ */
|
24
|
+
|
25
|
+
m = k;
|
26
|
+
|
27
|
+
mmax = fabs(*(ak+k));
|
28
|
+
|
29
|
+
ai = ak; // ★
|
30
|
+
|
31
|
+
for(i = k+1; i < N; i++){ /* ピボット選択 */
|
32
|
+
|
33
|
+
ai += N; // ★
|
34
|
+
|
35
|
+
if(fabs(*(ai+k)) > mmax){ // ★
|
36
|
+
|
37
|
+
m = i;
|
38
|
+
|
39
|
+
mmax = fabs(*(ai+k)); // ★
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
printf("column %d: row %d is max\n", k, m);
|
46
|
+
|
47
|
+
if(m != k){ /* 第k行と第m行を交換 */
|
48
|
+
|
49
|
+
double *am = A + m*N; // ★
|
50
|
+
|
51
|
+
for(i = k; i < N; i++){/* 係数行列 */
|
52
|
+
|
53
|
+
temp = *(ak+i);
|
54
|
+
|
55
|
+
*(ak+i) = *(am+i); // ★
|
56
|
+
|
57
|
+
*(am+i) = temp; // ★
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
/* 既知ベクトル */
|
62
|
+
|
63
|
+
temp = b[k];
|
64
|
+
|
65
|
+
b[k] = b[m];
|
66
|
+
|
67
|
+
b[m] = temp;
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
/* 第k列消去 */
|
72
|
+
|
73
|
+
ai = ak; // ★
|
74
|
+
|
75
|
+
for (i = k + 1; i < N; i++){
|
76
|
+
|
77
|
+
ai += N; // ★
|
78
|
+
|
79
|
+
r = *(ai+k) / *(ak+k); // ★
|
80
|
+
|
81
|
+
for (j = k; j < N; j++){
|
82
|
+
|
83
|
+
*(ai+j) -= *(ak+j) * r; // ★
|
84
|
+
|
85
|
+
}
|
86
|
+
|
87
|
+
b[i] -= b[k] * r;
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
PrintAb(A, b, N);
|
92
|
+
|
93
|
+
ak+=N;
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
```
|