#include <stdio.h>
#include <math.h>
#define N 4
void pivot(double x[][N+1], int i) {
double c[N+1] = {0};
for (int j = i + 1; j < N; j++) {
for (int n = 0; n < N + 1; n++) {
c[n] = x[j][n];
x[j][n] = x[i][n];
x[i][n] = c[n];
}
}
}
}
void lu(double x[][N+1], double l[][N], double u[][N], double y[N]) {
for (int i=0;i<N;i++) {
pivot(x, i);
l[i][i] = x[i][i];
for (int j=i;j<N;j++) {
u[i][j] = x[i][j];
}
for (int a=i+1;a<N;a++) {
l[a][i] = x[a][i];
double det = x[a][i] / x[i][i];
for (int b=i;b<N+1;b++) {
x[a][b] -= det * x[i][b];
}
}
}
for (int i=0;i<N;i++) {
y[i] = x[i][N];
}
}
void sol(double x[N], double u[][N], double y[N]) {
for (int i=N-1;i>=0;i--) {
x[i] = y[i];
for (int j=i+1;j<N;j++) {
x[i] -= u[i][j] * x[j];
}
x[i] /= u[i][i];
}
for (int i=0;i<N;i++) {
printf("x%d = %f\n", i
+1, x
[i
]); }
}
void con(double a[][N], double x[N]){
double result[N] = {0};
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
result[i] += a[i][j] * x[j];
}
}
for (int i = 0; i < N; i++) {
}
}
int main(void) {
double x[][N+1] = {
{3, 1.5, -6, 4.8, 1.2},
{1, 1.5, -2, -2.4, 0.6},
{0, -1.5, -2, -1, -2.4},
{2, 4, -1.8, -0.6, 0}
};
double l[N][N] = {0};
double u[N][N] = {0};
double y[N] = {0};
double s[N] = {0};
double a[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
a[i][j]=x[i][j];
}
}
lu(x, l, u, y);
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
}
}
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
}
}
for (int i=0;i<N;i++){
printf("y%d = %f\n", i
+1, y
[i
]); }
sol(s, u, y);
con(a, s);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+CgojZGVmaW5lIE4gNAoKdm9pZCBwaXZvdChkb3VibGUgeFtdW04rMV0sIGludCBpKSB7CiAgICBkb3VibGUgY1tOKzFdID0gezB9OwogICAgZm9yIChpbnQgaiA9IGkgKyAxOyBqIDwgTjsgaisrKSB7CiAgICAgICAgaWYgKGZhYnMoeFtpXVtpXSkgPCBmYWJzKHhbal1baV0pKSB7CiAgICAgICAgICAgIGZvciAoaW50IG4gPSAwOyBuIDwgTiArIDE7IG4rKykgewogICAgICAgICAgICAgICAgY1tuXSA9IHhbal1bbl07CiAgICAgICAgICAgICAgICB4W2pdW25dID0geFtpXVtuXTsKICAgICAgICAgICAgICAgIHhbaV1bbl0gPSBjW25dOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIGx1KGRvdWJsZSB4W11bTisxXSwgZG91YmxlIGxbXVtOXSwgZG91YmxlIHVbXVtOXSwgZG91YmxlIHlbTl0pIHsKICAgIGZvciAoaW50IGk9MDtpPE47aSsrKSB7CiAgICAgICAgcGl2b3QoeCwgaSk7CiAgICAgICAgbFtpXVtpXSA9IHhbaV1baV07CiAgICAgICAgZm9yIChpbnQgaj1pO2o8TjtqKyspIHsKICAgICAgICAgICAgdVtpXVtqXSA9IHhbaV1bal07CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGE9aSsxO2E8TjthKyspIHsKICAgICAgICAgICAgbFthXVtpXSA9IHhbYV1baV07CiAgICAgICAgICAgIGRvdWJsZSBkZXQgPSB4W2FdW2ldIC8geFtpXVtpXTsKICAgICAgICAgICAgZm9yIChpbnQgYj1pO2I8TisxO2IrKykgewogICAgICAgICAgICAgICAgeFthXVtiXSAtPSBkZXQgKiB4W2ldW2JdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgZm9yIChpbnQgaT0wO2k8TjtpKyspIHsKICAgICAgICB5W2ldID0geFtpXVtOXTsKICAgIH0KfQoKdm9pZCBzb2woZG91YmxlIHhbTl0sIGRvdWJsZSB1W11bTl0sIGRvdWJsZSB5W05dKSB7CiAgICBmb3IgKGludCBpPU4tMTtpPj0wO2ktLSkgewogICAgICAgIHhbaV0gPSB5W2ldOwogICAgICAgIGZvciAoaW50IGo9aSsxO2o8TjtqKyspIHsKICAgICAgICAgICAgeFtpXSAtPSB1W2ldW2pdICogeFtqXTsKICAgICAgICB9CiAgICAgICAgeFtpXSAvPSB1W2ldW2ldOwogICAgfQogICAgcHJpbnRmKCJ4OlxuIik7CiAgICBmb3IgKGludCBpPTA7aTxOO2krKykgewogICAgICAgIHByaW50ZigieCVkID0gJWZcbiIsIGkrMSwgeFtpXSk7CiAgICB9CiAgICBwcmludGYoIlxuIik7Cn0KCnZvaWQgY29uKGRvdWJsZSBhW11bTl0sIGRvdWJsZSB4W05dKXsKZG91YmxlIHJlc3VsdFtOXSA9IHswfTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBOOyBqKyspIHsKICAgICAgICAgICAgcmVzdWx0W2ldICs9IGFbaV1bal0gKiB4W2pdOwogICAgICAgIH0KICAgIH0KICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgaSsrKSB7IAogICAgCXByaW50ZigiJWZcbiIsIHJlc3VsdFtpXSk7IAogICAgfSAKCXByaW50ZigiXG4iKTsKfQoKaW50IG1haW4odm9pZCkgewogICAgZG91YmxlIHhbXVtOKzFdID0gewogICAgICAgIHszLCAxLjUsIC02LCA0LjgsIDEuMn0sCiAgICAgICAgezEsIDEuNSwgLTIsIC0yLjQsIDAuNn0sCiAgICAgICAgezAsIC0xLjUsIC0yLCAtMSwgLTIuNH0sCiAgICAgICAgezIsIDQsIC0xLjgsIC0wLjYsIDB9CiAgICB9OwoKICAgIGRvdWJsZSBsW05dW05dID0gezB9OwogICAgZG91YmxlIHVbTl1bTl0gPSB7MH07CiAgICBkb3VibGUgeVtOXSA9IHswfTsKICAgIGRvdWJsZSBzW05dID0gezB9OwogICAgCiAgICBkb3VibGUgYVs0XVs0XTsKCWZvcihpbnQgaT0wO2k8NDtpKyspewoJCWZvcihpbnQgaj0wO2o8NDtqKyspewoJCQlhW2ldW2pdPXhbaV1bal07CgkJfQoJfQoKICAgIGx1KHgsIGwsIHUsIHkpOwogICAgcHJpbnRmKCJV6KGM5YiXOlxuIik7CiAgICBmb3IgKGludCBpPTA7aTxOO2krKykgewogICAgICAgIGZvciAoaW50IGo9MDtqPE47aisrKSB7CiAgICAgICAgICAgIHByaW50ZigiJWYgIiwgdVtpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KICAgIHByaW50ZigiXG4iKTsKICAgIAogICAgcHJpbnRmKCJM6KGM5YiXOlxuIik7CiAgICBmb3IgKGludCBpPTA7aTxOO2krKykgewogICAgICAgIGZvciAoaW50IGo9MDtqPE47aisrKSB7CiAgICAgICAgICAgIHByaW50ZigiJWYgIiwgbFtpXVtqXSk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KICAgIHByaW50ZigiXG4iKTsKICAgIAogICAgcHJpbnRmKCJ5OlxuIik7CiAgICBmb3IgKGludCBpPTA7aTxOO2krKyl7CiAgICAgICAgcHJpbnRmKCJ5JWQgPSAlZlxuIiwgaSsxLCB5W2ldKTsKICAgIH0KICAgIHByaW50ZigiXG4iKTsKICAgIAogICAgc29sKHMsIHUsIHkpOwogICAgY29uKGEsIHMpOwogICAgcmV0dXJuIDA7Cn0=