-
Notifications
You must be signed in to change notification settings - Fork 9
/
6gjdn.c
88 lines (87 loc) · 1.61 KB
/
6gjdn.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
int gjdn(double *a, double *b, int n, int m)
{
int *js, l, k, i, j, is = 0, p, q;
double d, t;
js = malloc(n * sizeof(int));
l = 1;
for (k = 0; k <= n - 1; k++) {
d = 0.0;
for (i = k; i <= n - 1; i++)
for (j = k; j <= n - 1; j++) {
t = fabs(a[i * n + j]);
if (t > d) {
d = t;
js[k] = j;
is = i;
}
}
if (d + 1.0 == 1.0)
l = 0;
else {
if (js[k] != k)
for (i = 0; i <= n - 1; i++) {
p = i * n + k;
q = i * n + js[k];
t = a[p];
a[p] = a[q];
a[q] = t;
}
if (is != k) {
for (j = k; j <= n - 1; j++) {
p = k * n + j;
q = is * n + j;
t = a[p];
a[p] = a[q];
a[q] = t;
}
for (j = 0; j <= m - 1; j++) {
p = k * m + j;
q = is * m + j;
t = b[p];
b[p] = b[q];
b[q] = t;
}
}
}
if (l == 0) {
free(js);
fprintf(stderr, "ERROR: fail\n");
return -1;
}
d = a[k * n + k];
for (j = k + 1; j <= n - 1; j++) {
p = k * n + j;
a[p] = a[p] / d;
}
for (j = 0; j <= m - 1; j++) {
p = k * m + j;
b[p] = b[p] / d;
}
for (j = k + 1; j <= n - 1; j++)
for (i = 0; i <= n - 1; i++) {
p = i * n + j;
if (i != k)
a[p] = a[p] - a[i * n + k] * a[k * n + j];
}
for (j = 0; j <= m - 1; j++)
for (i = 0; i <= n - 1; i++) {
p = i * m + j;
if (i != k)
b[p] = b[p] - a[i * n + k] * b[k * m + j];
}
}
for (k = n - 1; k >= 0; k--)
if (js[k] != k)
for (j = 0; j <= m - 1; j++) {
p = k * m + j;
q = js[k] * m + j;
t = b[p];
b[p] = b[q];
b[q] = t;
}
free(js);
return 0;
}