#include <stdio.h>
#include <math.h>
#define N 4
void print_matrix(double A[][N+1]);
void swap_rows(double A[][N+1], int row1, int row2);
void diagonal_dominance(double A[][N+1]);
// 行列を表示する関数
void print_matrix(double A[][N+1]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N+1; j++) {
}
}
}
// 行列の2行を交換する関数
void swap_rows(double A[][N+1], int row1, int row2) {
for (int i = 0; i < N+1; i++) {
double temp = A[row1][i];
A[row1][i] = A[row2][i];
A[row2][i] = temp;
}
}
// 対角優位化を行う関数
void diagonal_dominance(double A[][N+1]) {
for (int i = 0; i < N; i++) {
double sum = 0;
for (int j = 0; j < N; j++) {
if (i != j) {
}
}
// 対角成分が非対角成分の和より小さい場合、行交換を試みる
if (fabs(A
[i
][i
]) < sum
) { int max_row = i;
double max_sum = 0;
// 交換可能な最大の対角優位行を探す
for (int k = i+1; k < N; k++) {
double row_sum = 0;
for (int j = 0; j < N; j++) {
if (k != j) {
row_sum
+= fabs(A
[k
][j
]); }
}
if (fabs(A
[k
][k
]) > row_sum
&& fabs(A
[k
][k
]) > max_sum
) { max_row = k;
max_sum = row_sum;
}
}
// 行の交換
if (max_row != i) {
swap_rows(A, i, max_row);
}
}
}
}
int main() {
double A[N][N+1] = {
{0, 8, 1, 7, 14.41},
{-2, 1, -3, 7, 15.11},
{1, 6, 0, -1, 10.8},
{5, -1, -2, 0, -1.32}
};
print_matrix(A);
diagonal_dominance(A);
printf("\nMatrix after diagonal dominance:\n"); print_matrix(A);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CgojZGVmaW5lIE4gNAoKdm9pZCBwcmludF9tYXRyaXgoZG91YmxlIEFbXVtOKzFdKTsKdm9pZCBzd2FwX3Jvd3MoZG91YmxlIEFbXVtOKzFdLCBpbnQgcm93MSwgaW50IHJvdzIpOwp2b2lkIGRpYWdvbmFsX2RvbWluYW5jZShkb3VibGUgQVtdW04rMV0pOwoKLy8g6KGM5YiX44KS6KGo56S644GZ44KL6Zai5pWwCnZvaWQgcHJpbnRfbWF0cml4KGRvdWJsZSBBW11bTisxXSkgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBOOyBpKyspIHsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IE4rMTsgaisrKSB7CiAgICAgICAgICAgIHByaW50ZigiJWxmICIsIEFbaV1bal0pOwogICAgICAgIH0KICAgICAgICBwcmludGYoIlxuIik7CiAgICB9Cn0KCi8vIOihjOWIl+OBrjLooYzjgpLkuqTmj5vjgZnjgovplqLmlbAKdm9pZCBzd2FwX3Jvd3MoZG91YmxlIEFbXVtOKzFdLCBpbnQgcm93MSwgaW50IHJvdzIpIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTisxOyBpKyspIHsKICAgICAgICBkb3VibGUgdGVtcCA9IEFbcm93MV1baV07CiAgICAgICAgQVtyb3cxXVtpXSA9IEFbcm93Ml1baV07CiAgICAgICAgQVtyb3cyXVtpXSA9IHRlbXA7CiAgICB9Cn0KCi8vIOWvvuinkuWEquS9jeWMluOCkuihjOOBhumWouaVsAp2b2lkIGRpYWdvbmFsX2RvbWluYW5jZShkb3VibGUgQVtdW04rMV0pIHsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgaSsrKSB7CiAgICAgICAgZG91YmxlIHN1bSA9IDA7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBOOyBqKyspIHsKICAgICAgICAgICAgaWYgKGkgIT0gaikgewogICAgICAgICAgICAgICAgc3VtICs9IGZhYnMoQVtpXVtqXSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CgogICAgICAgIC8vIOWvvuinkuaIkOWIhuOBjOmdnuWvvuinkuaIkOWIhuOBruWSjOOCiOOCiuWwj+OBleOBhOWgtOWQiOOAgeihjOS6pOaPm+OCkuippuOBv+OCiwogICAgICAgIGlmIChmYWJzKEFbaV1baV0pIDwgc3VtKSB7CiAgICAgICAgICAgIGludCBtYXhfcm93ID0gaTsKICAgICAgICAgICAgZG91YmxlIG1heF9zdW0gPSAwOwoKICAgICAgICAgICAgLy8g5Lqk5o+b5Y+v6IO944Gq5pyA5aSn44Gu5a++6KeS5YSq5L2N6KGM44KS5o6i44GZCiAgICAgICAgICAgIGZvciAoaW50IGsgPSBpKzE7IGsgPCBOOyBrKyspIHsKICAgICAgICAgICAgICAgIGRvdWJsZSByb3dfc3VtID0gMDsKICAgICAgICAgICAgICAgIGZvciAoaW50IGogPSAwOyBqIDwgTjsgaisrKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKGsgIT0gaikgewogICAgICAgICAgICAgICAgICAgICAgICByb3dfc3VtICs9IGZhYnMoQVtrXVtqXSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQoKICAgICAgICAgICAgICAgIGlmIChmYWJzKEFba11ba10pID4gcm93X3N1bSAmJiBmYWJzKEFba11ba10pID4gbWF4X3N1bSkgewogICAgICAgICAgICAgICAgICAgIG1heF9yb3cgPSBrOwogICAgICAgICAgICAgICAgICAgIG1heF9zdW0gPSByb3dfc3VtOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICAvLyDooYzjga7kuqTmj5sKICAgICAgICAgICAgaWYgKG1heF9yb3cgIT0gaSkgewogICAgICAgICAgICAgICAgc3dhcF9yb3dzKEEsIGksIG1heF9yb3cpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGRvdWJsZSBBW05dW04rMV0gPSB7CiAgICAgICAgezAsIDgsIDEsIDcsIDE0LjQxfSwKICAgICAgICB7LTIsIDEsIC0zLCA3LCAxNS4xMX0sCiAgICAgICAgezEsIDYsIDAsIC0xLCAxMC44fSwKICAgICAgICB7NSwgLTEsIC0yLCAwLCAtMS4zMn0KICAgIH07CgogICAgcHJpbnRmKCJPcmlnaW5hbCBtYXRyaXg6XG4iKTsKICAgIHByaW50X21hdHJpeChBKTsKCiAgICBkaWFnb25hbF9kb21pbmFuY2UoQSk7CgogICAgcHJpbnRmKCJcbk1hdHJpeCBhZnRlciBkaWFnb25hbCBkb21pbmFuY2U6XG4iKTsKICAgIHByaW50X21hdHJpeChBKTsKCiAgICByZXR1cm4gMDsKfQoK