// ガウスの消去法を行う関数を作成せよ。引数として拡大係数行列を受け取ることとする。
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define MN_MAX 100
#define EPS 1.0E-3 //許容誤差
struct matrix_data{
int row; //行数
int col; //列数
double dat[MN_MAX];
};
struct matrix_data AugmentedMatrix = {
3,
4,
{
2.0, 3.0, -1.0, 1.0,
1.0, 1.0, 2.0, 0.0,
3.0, -1.0, 1.0, 2.0,
}
};
struct matrix_data init_matrix(int m, int n)
{
struct matrix_data mat;
mat.row = m;
mat.col = n;
return mat;
}
void set_matrix(struct matrix_data* mat, int i, int j, double val)
{
mat->dat[i*mat->col + j] = val;
}
double get_matrix(struct matrix_data mat, int i, int j)
{
return mat.dat[i*mat.col + j];
}
//p行とq行を入れ替える
struct matrix_data exchange_matrix(struct matrix_data mat,int p, int q)
{
int i;
double dp,dq;
for(i = 0; i < mat.col; ++i){
dp = get_matrix(mat, p, i);
dq = get_matrix(mat, q, i);
set_matrix(&mat, q, i, dp);
set_matrix(&mat, p, i, dq);
}
return mat;
}
//行列の表示
void show_matrix(struct matrix_data mat)
{
int i, j;
for (i = 0; i < mat.row; i++){
for(j = 0; j < mat.col; j++){
printf("%f ",get_matrix
(mat
, i
, j
)); }
}
}
//ガウスの消去法
struct matrix_data Gaussian_Elimination(struct matrix_data mat)
{
int i,j,k;
int n;
double p,q;
struct matrix_data x;
x = init_matrix(mat.col-1,1);
n = mat.col;
// 前進消去
for(k = 0; k < n-1; k++){
for(i = k+1; i < n-1; i++){
if(fabs(get_matrix
(mat
, k
, k
)) < fabs(get_matrix
(mat
, i
, k
))){ mat = exchange_matrix(mat, i, k);
}
}
if(fabs(get_matrix
(mat
, k
, k
)) < EPS
){ }
p = get_matrix(mat, k, k);
for(j = k; j < n; j++){
set_matrix(&mat, k, j, get_matrix(mat, k, j) / p);
}
for(i = k+1; i < n-1; i++){
q = get_matrix(mat, i, k);
for(j = k; j< n; j++){
set_matrix(&mat, i, j, get_matrix(mat, i ,j) - get_matrix(mat, k, j)*q);
}
}
}
show_matrix(mat);
// 後退代入
for(k = n-2; k >= 0; k--){
q = get_matrix(mat, k, n-1);
for(j = k+1; j < n-1; j++){
q = q - get_matrix(mat, k ,j) * get_matrix(x, j, 0);
}
set_matrix(&x, k, 0, q);
}
return x;
}
int main(void)
{
struct matrix_data x;
show_matrix(AugmentedMatrix);
x = Gaussian_Elimination(AugmentedMatrix);
show_matrix(x);
return 0;
}
Ly8g44Ks44Km44K544Gu5raI5Y675rOV44KS6KGM44GG6Zai5pWw44KS5L2c5oiQ44Gb44KI44CC5byV5pWw44Go44GX44Gm5ouh5aSn5L+C5pWw6KGM5YiX44KS5Y+X44GR5Y+W44KL44GT44Go44Go44GZ44KL44CCCgojaW5jbHVkZTxzdGRpby5oPgojaW5jbHVkZTxzdGRsaWIuaD4KI2luY2x1ZGU8bWF0aC5oPgoKI2RlZmluZSBNTl9NQVggMTAwCiNkZWZpbmUgRVBTIDEuMEUtMyAvL+ioseWuueiqpOW3rgoKc3RydWN0IG1hdHJpeF9kYXRhewogIGludCByb3c7IC8v6KGM5pWwCiAgaW50IGNvbDsgLy/liJfmlbAKICBkb3VibGUgZGF0W01OX01BWF07CiAgCn07CgpzdHJ1Y3QgbWF0cml4X2RhdGEgQXVnbWVudGVkTWF0cml4ID0gewogIDMsCiAgNCwKICB7CiAgICAyLjAsICAzLjAsIC0xLjAsICAxLjAsCiAgICAxLjAsICAxLjAsICAyLjAsICAwLjAsCiAgICAzLjAsIC0xLjAsICAxLjAsICAyLjAsCiAgfQp9OwoKc3RydWN0IG1hdHJpeF9kYXRhIGluaXRfbWF0cml4KGludCBtLCBpbnQgbikKewogIHN0cnVjdCBtYXRyaXhfZGF0YSBtYXQ7CgogIG1hdC5yb3cgPSBtOwogIG1hdC5jb2wgPSBuOwoKICByZXR1cm4gbWF0Owp9CgoKdm9pZCBzZXRfbWF0cml4KHN0cnVjdCBtYXRyaXhfZGF0YSogbWF0LCBpbnQgaSwgaW50IGosIGRvdWJsZSB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IHNldF9tYXRyaXgoJngsIGssIDAsIHEpOwogIH0KICByZXR1cm4geDsKfQoKCmludCBtYWluKHZvaWQpCnsKICBzdHJ1Y3QgbWF0cml4X2RhdGEgeDsKCiAgcHJpbnRmKCLjgoLjgajjga7ooYzliJfjgpLooajnpLrjgZfjgb7jgZlcbiIpOwogIHNob3dfbWF0cml4KEF1Z21lbnRlZE1hdHJpeCk7CgogIHggPSBHYXVzc2lhbl9FbGltaW5hdGlvbihBdWdtZW50ZWRNYXRyaXgpOwogIHByaW50Zigi6Kej44KS6KGo56S644GX44G+44GZ44CCXG4iKTsKICBzaG93X21hdHJpeCh4KTsKCiAgcmV0dXJuIDA7Cn0K